r/adventofcode • • Dec 13 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 13 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

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

--- Day 13: Shuttle Search ---


Post your code solution in this megathread.

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:16:14, megathread unlocked!

46 Upvotes

659 comments sorted by

View all comments

2

u/bobtom8482 Dec 13 '20

Doing a few CRTs out by hand was fun

Got some weird behaviour with the full input vs test input. My answers for the test input were all perfect but the full input was one too low. Anyone know what that might have been?

if __name__ == '__main__':
with open("input.txt") as f:
    lines = f.read().splitlines()
splits = lines[1].split(',')

buses = []
a_i = []
count = 0
for i in splits:
    if i != 'x':
        buses.append(int(i))
        a_i.append(count)
    count -= 1

for i in range(len(a_i)):
    a_i[i] = a_i[i] % buses[i]

M = 1
for i in buses:
    M *= i

b_i = []
for i in buses:
    b_i.append(M / int(i))

b_inv = []
for i in range(len(buses)):
    b_inv.append(pow(int(b_i[i]), -1, buses[i]))

ans = 0
for i in range(len(a_i)):
    ans += a_i[i] * b_i[i] * b_inv[i]
ans = int(ans % M) + 1 # why would I need to add 1?
print(ans)

1

u/daggerdragon Dec 13 '20 edited Dec 14 '20

My answers for the test input were all perfect but the full input was one too low. Anyone know what that might have been?

If you don't get an answer here, you can always create your own Help thread. :)

1

u/obserwator00 Dec 13 '20

bump, same situation