45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
|
def read_file():
|
||
|
with open("input.txt","r") as f:
|
||
|
return list(map(int,list(f.read())))
|
||
|
|
||
|
def play(cups, rounds):
|
||
|
current = cups[0]
|
||
|
next_cup = {c:n for c, n in zip(cups, cups[1:] + [cups[0]])}
|
||
|
for _ in range(rounds):
|
||
|
# pickup next 3 cups
|
||
|
c1 = next_cup[current]
|
||
|
c2 = next_cup[c1]
|
||
|
c3 = next_cup[c2]
|
||
|
# select a destination cup (-1 until found, wraps around)
|
||
|
destination = current - 1 if current > 1 else len(cups)
|
||
|
while destination in [c1, c2, c3]:
|
||
|
destination = destination - 1 if destination > 1 else len(cups)
|
||
|
# add cups back
|
||
|
next_cup[current] = next_cup[c3]
|
||
|
next_cup[c3] = next_cup[destination]
|
||
|
next_cup[destination] = c1
|
||
|
# select a new current cup (next)
|
||
|
current = next_cup[current]
|
||
|
# rebuild cups from next_cup
|
||
|
cup = 1
|
||
|
new_cups = []
|
||
|
for _ in range(len(cups)):
|
||
|
cup = next_cup[cup]
|
||
|
new_cups.append(cup)
|
||
|
return new_cups
|
||
|
|
||
|
def part1(cups):
|
||
|
cups = play(cups, 100)
|
||
|
return "".join(str(c) for c in cups[:-1])
|
||
|
|
||
|
def part2(cups):
|
||
|
cups += list(range(10,1_000_001))
|
||
|
cups = play(cups,10_000_000)
|
||
|
return cups[0] * cups[1]
|
||
|
|
||
|
def main():
|
||
|
cups = read_file()
|
||
|
print(part1(cups), part2(cups))
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|