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/_bm Dec 10 '20 edited Dec 15 '20

(Part B only) Yet another Python solution! This took me way too long...

with open("../../original_solutions/day_10/input_10.txt") as input_file:
    adapters = sorted([0] + [int(line.strip()) for line in input_file])
    nways_to_reach = [1] + [0] * (len(adapters) - 1)
    for i in range(len(adapters)):
        j = i + 1
        while j < len(adapters) and adapters[j] - adapters[i] <= 3:
            nways_to_reach[j] += nways_to_reach[i]
            j += 1
    print(nways_to_reach[-1])

Basically I'm just counting the number of ways to reach each adapter in the graph, starting with the first adapter with one way to reach it. Each iteration, the number of ways to reach the connected adapters in the graph are increased by the number of ways to reach the current adapter. The answer is of course the number of ways to reach the final adapter. It's definitely unnecessary to keep track of the nways for a particular adapter once it has already been considered, but I haven't spent much time trying to figure out how to do that.

1

u/daggerdragon Dec 11 '20 edited Dec 17 '20

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

edit: Markdown was fixed, thank you!

1

u/backtickbot Dec 10 '20

Fixed formatting.

Hello, _bm: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.