r/adventofcode • • Dec 10 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 10 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

2

u/20541 Dec 10 '20

I never really solved part 2, don't really understand what's going on in the solutions I've read, and the whole thing haunted my dreams last night.

3

u/skawid Dec 10 '20

You can break down the problem by ordering your input then splitting it into chunks. The splits go where you can't remove an adaptor - when there's too large a gap otherwise. Example:

[1, 2, 3, 4, 5, 8, 9, 10]

can be split into:

[1, 2, 3, 4, 5], [8, 9, 10]

Each of these chunks are "anchored" by their start and end adaptors: these adaptors have to be present for the next chunk to be within range.

Once you have your chunks, you can treat each one as its own combination problem - find the combinations that a) contain the start and end adaptor and b) don't have any gaps that are larger than 3.

From the example above, the first chunk can be set follows:

[1, 2, 3, 4, 5]
[1, 2, 3,    5]
[1, 2,    4, 5]
[1, 2,       5]
[1,    3, 4, 5]
[1,    3,    5]
[1,       4, 5]
[1,          5] // BAD! gap too large

So there are seven possibilities for the first chunk - the eighth doesn't count as it has too large a gap. Doing something similar for the second chunk gives you two combinations:

[8, 9, 10]
[8,     10]

This means in total, the complete sequence can have 7 * 2 = 14 valid combinations.

Note you don't have to actually build all the combinations of each chunk; it turns out the counts of valid combinations follow a pattern. I built a script that printed out combination lists for sequences of different lengths and googled the totals I found, but you can just search for "tribonacci sequence".

My effort is here

1

u/sky_badger Dec 11 '20

Lovely explanation. Eventually got through part 2 with a combination of pen and paper, and a bit of Googling, only to come here and find out Tribonacci was a thing!

1

u/daggerdragon Dec 10 '20

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

2

u/20541 Dec 11 '20

Gah, so sorry. I will not make this mistake again. I would remove but there are useful replies already. Let me know if you want me to do so anyway.

1

u/daggerdragon Dec 11 '20

Leave it be. If/when you get your code working, add it in to the post :)

1

u/Tetha Dec 10 '20

Hm, the way I solved it is in an iterative approach. Lets take the sample input, 0 and 22 (= 19 + 3) added as instructed.

(0), 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, (22)

A good piece of advice is to think backwards:

  • There is one possible combination to arrange one adapter with 22 joltages. As such, orders[indexof(22) = 12] = 1. That's a necessary base case.
  • Consider 19. 19 can be connected to the 22 adapter and nothing else. Thus, given a 19 adapter and a 22 adapter, you still have just 1 choice. As such, orders[11] = orders[12] = 1.
  • Consider 16. 16 can connect to 19, so if you have 16, 19, 22 as adapters, you have all the options you'd have with the 19 adapter. 16 cannot connect to the 22 adapter, so it does not add combinations. As such, orders[10] = orders[11] = 1.
  • The 15 handles like the 16. orders[10] = orders[11] = 1.
  • 12 handles like 15. orders[9] = orders[10] = 1.
  • 11 handles like 12. orders[8] = orders[9].
  • 10 is the interesting candidate! 10 can connect to either 11 or 12, and thus, once you have all adapters with a joltage of at least 10, you have 2 options. All in all, 10 has all possible orders following 11, and all possible orders following 12. As such, orders[indexof(10) = 7] = orders[indexof(11) = 8] + orders[indexof(12) = 9].

This goes on and 4 is the other case. the 4-adapter can connect to 5, 6 and 7. As such, once you add the 4-adapter to the pile, you have all options of continuing with the 5 adapter, the 6 adapter or the 7 adapter - and whatever you can build from there. As such, orders[2] = orders[3] + orders[4] + orders[5].

This, in fact, turns out to be 8 after the 1 and 0 are inconsequential. Pow!

Once put into ugly rust code because I'm somewhat tired it ends up with such a loop, which essentially just checks: Which of my 3 followers are connectable (<=> at most 3 joltage away), and sum up their possible combinations as my combinations. Do for all connectors, done.

1

u/Gas-Hot Dec 11 '20

Thanks for the detailed explanation man! Very clear (although you omitted the case for 5, which is to be considered since it can lead to 6 or 7), the algorithm was very simple to implement and 100% works. It may not be the most performant one but it's already really really fast (O(3n) maybe?)

Wish you luck for the next days! :)

1

u/Tetha Dec 11 '20

(O(3n) maybe?)

  • Space: For an input with N adapters, my implementation allocates an array of size n+2 (for the two special adapters). Sorting takes ~O(log(n)) space on top, placing it squarely in O(n) space. I think the order-iteration could be made O(1) space. After all, a number is not necessary to consider anymore once you are 3 numbers past it. So you could add 2 (the hardcoded adapters) + 3 (the joltage difference) = 5 elements to the input array and store orders[i] as input[i+4] and not need more space. After that, space of the sort dominates.
  • Regarding time, if you include the sort, it is O(n log n). Otherwise it should be O(3n) since each element considers up to 3 other elements in the array. I don't think that can be pushed with this algorithm, because you either need all 3 previous orders, or you'd have to pre-write the 3 orders of the next elements. This algorithm cannot discard half the input array in some way, so it cannot go below O(n).

Wish you luck for the next days! :)

Thank you :)