AdventOfCode/2020/elixir/day14/README

69 lines
2.2 KiB
Text
Raw Normal View History

2020-12-14 11:57:38 +00:00
Day 14 Notes
+--------+
| Part 1 |
+--------+
$ elixir day14part1.exs
2020-12-14 16:31:10 +00:00
12135523360904
2020-12-14 11:57:38 +00:00
Thoughts:
2020-12-14 16:31:10 +00:00
Bitmasking.
Not sure if I missed something here. Since the mask bits overwrite the value, we could
just copy the bits. But I decided to implement it with real bitmasking, so I *think*
it is necessary to split the mask into two seperate masks - one containing the 1s which
will be ORed to set the bits, and another containing the 0s will will be ANDed to unset
the bits.
When parsing the file, convert the mask into the and and or masks, then when
executing the parsed instructions, apply the masks as necessary.
2020-12-14 11:57:38 +00:00
+--------+
| Part 2 |
+--------+
$ elixir day14part2.exs
2020-12-14 16:31:10 +00:00
2741969047858
2020-12-14 11:57:38 +00:00
Thoughts:
2020-12-14 16:31:10 +00:00
OK. This requires quite a re-write of part 1. Now the Xs in the mask represent
floating values. We need to permute all the possible floating values, then substitute
those into the result.
I wasted a lot of time misreading the question, by replacing the values in the mask
*before* applying the mask to the memory address. But the question requires the mask
to be applied first, and then replace the values in the masked result. Reworking ensues.
This got a bit long so decided to split the file parsing logic into its own module.
Parser Module
Parses the file, and produces the "mask" along with the indices that contained the X bits.
Can't perform the substitution at this stage, because it has to be applied to the
masked value.
Day14Part2 Module
Runs the main logic. For each mask, applies it to the value, permutes the options
nd subsitutes the relevant bits.
I was unsure whether it was better to compute all the permuted bits ahead of time in the
parsing stage, or do it on demand each time we apply a mask. Decided to do it on demand,
because it keeps the data that is being passed around simpler - the mask can just
store the X indices, rather than all the permuted replacement values.
2020-12-14 11:57:38 +00:00
+------------------+
| Overall Thoughts |
+------------------+
2020-12-14 16:31:10 +00:00
Enjoyed this one. Was great to get the experience of working with binaries as integer
values rather than strings. Getting used to working with the <<>> syntax, although it
took me a long time to work it all out.
2020-12-14 11:57:38 +00:00
2020-12-14 16:31:10 +00:00
Especially for Part 2 - generating permutations by counting in binary was pretty
fun, but completely unnecessary. :-)