mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-22 05:41:51 +01:00
Solve day 13 part 2
This commit is contained in:
parent
12e608f853
commit
cd02ef3595
1 changed files with 48 additions and 1 deletions
|
@ -36,6 +36,53 @@ def solve_part_1(notes) -> int:
|
|||
|
||||
|
||||
def solve_part_2(notes) -> int:
|
||||
split = notes[1].split(",")
|
||||
modulos = []
|
||||
remainders = []
|
||||
for position, bus_index in enumerate(split):
|
||||
if bus_index == "x":
|
||||
continue
|
||||
bus_index = int(bus_index)
|
||||
modulos.append(bus_index)
|
||||
remainders.append(bus_index - position)
|
||||
return chinese_remainder(modulos, remainders)
|
||||
|
||||
|
||||
def chinese_remainder(modulos, remainders):
|
||||
"""List of modulos, then list of remainders"""
|
||||
# https://rosettacode.org/wiki/Chinese_remainder_theorem#Python
|
||||
# License: https://www.gnu.org/licenses/old-licenses/fdl-1.2.html
|
||||
total = 0
|
||||
prod = functools.reduce(lambda a, b: a * b, modulos)
|
||||
for modulo, remainder in zip(modulos, remainders):
|
||||
p = prod // modulo
|
||||
total += remainder * mul_inv(p, modulo) * p
|
||||
return total % prod
|
||||
|
||||
|
||||
def mul_inv(a, b):
|
||||
# https://rosettacode.org/wiki/Chinese_remainder_theorem#Python
|
||||
# License: https://www.gnu.org/licenses/old-licenses/fdl-1.2.html
|
||||
b0 = b
|
||||
x0, x1 = 0, 1
|
||||
if b == 1:
|
||||
return 1
|
||||
while a > 1:
|
||||
q = a // b
|
||||
a, b = b, a % b
|
||||
x0, x1 = x1 - q * x0, x0
|
||||
if x1 < 0:
|
||||
x1 += b0
|
||||
return x1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
n = [3, 5, 7]
|
||||
a = [2, 3, 2]
|
||||
print(chinese_remainder(n, a))
|
||||
|
||||
|
||||
def solve_part_2_iterative(notes) -> int:
|
||||
"""Works but far too slow for the real deal."""
|
||||
split = notes[1].split(",")
|
||||
buses = list(map(int, filter(lambda bus: bus != "x", split)))
|
||||
|
@ -66,4 +113,4 @@ if __name__ == "__main__":
|
|||
main("inputs/day13-test4", None, 779210)
|
||||
main("inputs/day13-test5", None, 1261476)
|
||||
main("inputs/day13-test6", None, 1202161486)
|
||||
main("inputs/day13", 4315)
|
||||
main("inputs/day13", 4315, 556100168221141)
|
||||
|
|
Loading…
Reference in a new issue