1
I am in desperate need of some ear defenders, so I can program in peace. Unfortunately, I don't have ear defenders. But I do have a pair of headphones, a microphone and a microcontroller, so I thought I'd make some noise-cancelling headphones.
However, there is one tiny problem. I can't program in noisy environments! So you'll need to write the code for me.
Task
Given a constant noisy input, output the input's "complement"; a value that will completely cancel out the input value (0x00
cancels 0xff
, 0x7f
cancels 0x80
). Because the headphones block some noise, also take a value as input that you will divide the "complement" by, using the following algorithm: if (c < 0x80) ceil ( 0x7f - ( (0x7f - c) / n ) ) else floor ( ( (c - 0x80) / n ) + 0x80 )
where c
is the "complement" and n
is the value to divide by. (If the value exceeds the byte range, it should be clipped to 0
or 255
.)
The input will always be a sequence of bytes, via stdin
or function parameter (see bonuses) but the modifier can be given via any input method. The output sequence should be output via stdout
or as a return value (see bonuses). The output should be a series of bytes (e.g. $.d<±3Ç/ó¢
), or a set of numbers representing those bytes (e.g. 217 135 21 43 126 219
).
Oh, and your code will have to be short, because there isn't much flash memory on the microcontroller (4096 bytes; and I need to fit on drivers too!).
Examples
In these examples, I have used hexadecimal codes to represent bytes, because DEL
and NULL
are somewhat hard to read. The actual input and output should be done as a string of bytes or as a byte-stream.
Modifier: 2
Input: 0x00 0x10 0xe0
...
Output: 0xbf 0xb7 0x4f
...
Modifier: 128
Input: 0x00 0x10 0xe0
...
Output: 0x80 0x80 0x7f
...
Modifier: 0.5
Input: 0x00 0x10 0xe0
...
Output: 0xff 0xff 0x00
...
Your code should cope with much longer inputs than this; these are just examples.
Bonuses
To add a bonus to a score, use the following formula: s * (100% - b)
where s
is the current score and b
is the bonus as a percentage.
- 1% If you output as raw bytes.
- 5% If there's no
stdin
pipe (or byte input), get the bytes straight from the microphone, such as withparec
. - 5% If you output to the speakers as well as returning / outputting to
stdout
.
5Why require STDIN/STDOUT? I don't see a reason why we shouldn't be allowed to use function arguments/returns values instead. – Denker – 2016-03-15T18:30:56.717
2Although, there is a good reason you shouldn't be allowed to; it would be a bit of a rubbish noise-canceller if there wasn't any cancelling until all the bytes had been received through the microphone! – wizzwizz4 – 2016-03-15T18:35:41.623
2What's a compliment? Is it like a complement? – CalculatorFeline – 2016-03-15T18:37:51.657
Does the output have to be given in hex? – xnor – 2016-03-15T18:49:49.780
@xnor It has to not be given in Hex. I just didn't think it would be easy to read
NULL
andDEL
characters. You need to output as a byte stream. (I'll update the question.) – wizzwizz4 – 2016-03-15T18:54:03.403@wizzwizz4 What's a byte stream? Some languages have different data types and names. Will a number do? – xnor – 2016-03-15T18:55:42.920
@xnor An arbitrary-sized number or a string of bytes is an acceptable replacement for a byte-stream. – wizzwizz4 – 2016-03-15T18:58:37.933
1I'm assuming we can use an algorithm equivalent to the provided one if it's shorter? – CalculatorFeline – 2016-03-15T19:13:03.843
@CatsAreFluffy Of course; this wouldn't be code-golf if you were just golfing my code! – wizzwizz4 – 2016-03-15T19:19:17.153
4Bonuses are generally discouraged in [tag:code-golf]. – lirtosiast – 2016-03-15T21:10:04.473
@lirtosiast That's why I didn't add bonuses for bonuses' sake. I only added bonuses that I thought would add to the challenge. – wizzwizz4 – 2016-03-15T21:11:58.307
"An arbitrary-sized number or a string of bytes is an acceptable replacement for a byte-stream." By "string of bytes", do you mean my language's native String type, or a bunch of
byte
s strung together in an array? – cat – 2016-03-16T01:12:27.113@tac The string type is what I had in mind, but there's a problem with
NULL
and "end of string" in some languages. – wizzwizz4 – 2016-03-16T16:42:48.070@wizzwizz4 can you clarify in the question how the output format should be? the current spec is quite vague – cat – 2016-03-16T17:23:23.317