It's a mistake coding in C. Nevertheless, I managed to scrape through the whole thing. Admittedly, I overcomplicate, overengineer, and overefficientize everything. So it's mostly my fault.
In theory, this whole day's AoC could have been done using vim
or sed
+ bc
The theory behind 1-1's code is simple: in that it's not.
- Read the file line by line (max 64 char per line).
- Turn all encountered digits to their int value ('0' -> 0, done by subtracting '0' or ascii val 30).
- Bit shift to the left all encountered digits by 4*
k
wherek
is the amount of digits encountered starting from 0 (the logic being that the value 9 occupies only 4 bits). - Modulo
z
by1 << 4
to get the first digit (and * 10 to make it the tenths). - Divide
z
by1 << (4 * (k - 1))
to get the last digit. - Add the previous two together, add all the lines, and you get the total result.
1-2's code is blatant madness. Here's how the "logic" (if any) goes:
- Read the file line by line (max 64 char per line).
- Iterate through the line char by char.
- If a digit is encountered:
- Turn it into a number (by subtracting '0').
- Set
xc
oryc
to 1 (essentially locking it).
- If any character in "otfsen" is found, then
numericmagify
:- Check
len * 5
and prevent it from exceedingsizeof(uint_fast32_t) * 8
, if exceeding, usesizeof(uint_fast32_t) * 3 / 2
instead. - Then, for every char in
str
, subtract by '`' so that 'a' = 1, then bit shift left by 5*i
(the logic being'z' - '`'
is 5 bits long). - Now check if the encountered chars really constitutes a number by checking the "magic"/"hash" against the number-word you want.
- If yes, set the number to
x
ory
and setxc
oryc
to 1 (essentially locking it).
- Check
Note: the encounter is done from the front and behind of the buf
even if it's 1 loop.
Note2: yes, numericmagify
is a shabby, knockoff hash function.