AdventOfCode/2020/elixir/day10/README

68 lines
2.1 KiB
Text
Raw Normal View History

2020-12-10 08:42:58 +00:00
Day 10 Notes
+--------+
| Part 1 |
+--------+
$ elixir day10part1.exs
2020-12-10 15:28:41 +00:00
2450
2020-12-10 08:42:58 +00:00
Thoughts:
Noticed that the questions rely on you finding some insight into the data, rather than just
implementing exactly what the instructions say.
In this case, we just need to sort the data, and compute the difference between all the pairs.
2020-12-10 15:28:41 +00:00
+--------+
| Part 2 |
+--------+
2020-12-10 08:42:58 +00:00
$ elixir day10part2.exs
2020-12-10 15:28:41 +00:00
32396521357312
2020-12-10 08:42:58 +00:00
Thoughts:
2020-12-10 15:28:41 +00:00
Fudge me this took me a long time (hours, including a walk in the park...)
I spent far too long trying to find a mathematical answer based on combinations, and went down
another rabbit hole identifying the "removable" items and then trying to calculate the answer
based on that information.
Turns out the trick I was missing is graph theory.
Finally, after drawing out the example on paper, I realised that the number of combinations
forms a directed graph, where the weight of each node inherits the weight of all its parents.
For the example case (excuse my ascii drawing skills):
0(1)-1(1)-4(1)-5(1)-|
2020-12-10 17:33:17 +00:00
\ \ | |
\ 6(2) |
\ | |
7(4)-|
2020-12-10 15:28:41 +00:00
|
10(4)-11(4)
2020-12-10 16:04:13 +00:00
\ |
2020-12-10 15:28:41 +00:00
12(8)-18(8)-19(8)
2020-12-10 08:42:58 +00:00
2020-12-10 15:28:41 +00:00
So just need to make an algorithm that builds this graph, by iterating the sorted list
and checking the next 3 elements, keeping track of the weights as we go. It's actually simpler
than that, because we don't need to keep the connections around, just the weightings for each node.
2020-12-10 08:42:58 +00:00
2020-12-10 15:28:41 +00:00
Finally, the weight of the max joltage adapter is the answer.
2020-12-10 08:42:58 +00:00
** Update **
I added day10part2-refactored.exs, which contains a commented and arguably easier to understand
version that uses Enum.reduce instead of so much recursion. However, in benchmarks it was
negligibly (~1 microsecond) slower, so I decided to leave the original version in place.
2020-12-10 08:42:58 +00:00
+------------------+
| Overall Thoughts |
+------------------+
2020-12-10 15:28:41 +00:00
Part 1 was very easy, part 2 was fiendish. Glad this wasn't an interview question. Sometimes my
brain just doesn't process certain types of problem. This was one of them. Once I made the
connection to a weighted graph it was fine.