Files
aoc2020/day23/module.py
2020-12-24 07:13:42 +01:00

123 lines
3.0 KiB
Python

from aoc.input import get_input
import copy
import itertools
import time
import collections
import re
from aoc.partselector import part_one, part_two
import functools
def pw(line):
return line.strip()
def p1():
inp = get_input(pw)
cups = []
for sample in inp:
print(sample)
cups.append(int(sample))
def domove(cups, current):
current = current % len(cups)
selected = (cups+cups)[current+1:current+4]
selected_index = sorted([(current+1)%len(cups), (current+2)%len(cups), (current+3)%len(cups)], reverse=True)
print(cups)
origcurrent = cups[current]
targetid = cups[current] - 1
print(current, cups[current], selected, targetid)
for c in selected_index:
cups.pop(c)
target = None
if targetid == 0:
targetid = 9
while target == None:
try:
target = cups.index(targetid)
except:
targetid = (targetid - 1)
print('-', targetid)
if targetid == 0:
targetid = 9
if targetid < -1:
break
print('-', targetid)
print('destination', targetid, target, cups)
cups.insert(target + 1, selected[-1])
cups.insert(target + 1, selected[-2])
cups.insert(target + 1, selected[-3])
while not cups[current] == origcurrent:
cups = cups[1:] + cups[0:1]
print(cups)
return cups
current = 0
for i in range(5):
cups = domove(cups, current)
current += 1
cups = cups
cupss = ''.join(map(str,cups))
print(cupss)
a, b = cupss.split('1')
print(b + a)
return inp
def p2():
inp = get_input(pw)
cups = collections.deque()
for sample in inp:
cups.append(int(sample))
total = 1_000_000
for cc in range(10, 1_000_000+1):
cups.append(cc)
backup = collections.defaultdict(list)
def fixnext():
next_ = cups.popleft()
# ok, now is the time to put these values back
if next_ in backup:
cups.extendleft(reversed(backup[next_]))
del backup[next_]
return next_
def domove():
current = fixnext()
selected = [fixnext(), fixnext(), fixnext()]
unavailable = set([0, current] + selected)
# find dest
target = current - 1
while target in set(unavailable):
target -= 1
if target < 1:
target = total
backup[target] += selected
cups.append(current)
for _ in range(10_000_000):
domove()
while (next_ := fixnext()) != 1:
cups.append(next_)
a, b = fixnext(), fixnext()
print(a*b)
return inp
result1 = None
if part_one():
start = time.time()
result1 = p1()
print(round(1000*(time.time() - start), 2), 'ms')
if part_two():
start = time.time()
p2()
print(round(1000*(time.time() - start), 2), 'ms')