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

154 lines
3.4 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)
directions = {
# e,w 101
# ne, sw 101
# 101
"e": [1,0,-1],
"w": [-1,0,1],
"nw": [0,-1,1],
"se": [0,1,-1],
"ne": [1,-1,0],
"sw": [-1,1,0],
}
i2 = []
for sample in inp:
cnt = 0
steps = []
while cnt < len(sample):
if sample[cnt] in directions:
steps.append(directions[sample[cnt]])
cnt += 1
elif sample[cnt:cnt+2] in directions:
steps.append(directions[sample[cnt:cnt+2]])
cnt += 2
i2.append(steps)
b = collections.defaultdict(lambda: 0)
for n in i2:
c = [0,0,0]
for s in n:
c[0] += s[0]
c[1] += s[1]
c[2] += s[2]
print(c)
b[tuple(c)] += 1
count = 0
for x in b.values():
if x%2 == 1:
count += 1
print(count)
return inp
def p2():
inp = get_input(pw)
directions = {
# e,w 101
# ne, sw 101
# 101
"e": tuple([1,0,-1]),
"w": tuple([-1,0,1]),
"nw": tuple([0,-1,1]),
"se": tuple([0,1,-1]),
"ne": tuple([1,-1,0]),
"sw": tuple([-1,1,0]),
}
i2 = []
for sample in inp:
cnt = 0
steps = []
while cnt < len(sample):
if sample[cnt] in directions:
steps.append(directions[sample[cnt]])
cnt += 1
elif sample[cnt:cnt+2] in directions:
steps.append(directions[sample[cnt:cnt+2]])
cnt += 2
i2.append(steps)
b = collections.defaultdict(lambda: 0)
for n in i2:
c = [0,0,0]
for s in n:
c[0] += s[0]
c[1] += s[1]
c[2] += s[2]
b[tuple(c)] = (b[tuple(c)]+1)%2
count = 0
for x in b.values():
if x%2 == 1:
count += 1
print(count)
minx = min(map(lambda x: x[0], b.keys()))
maxx = max(map(lambda x: x[0], b.keys()))
miny = min(map(lambda x: x[1], b.keys()))
maxy = max(map(lambda x: x[1], b.keys()))
minz = min(map(lambda x: x[2], b.keys()))
maxz = max(map(lambda x: x[2], b.keys()))
def iterate2():
b2 = collections.defaultdict(lambda: 0)
for oc,v in b.items():
x,y,z = oc
if v == 1:
for s in directions.values():
c = tuple([x+s[0], y+s[1], z+s[2]])
b2[c] += 1
for c, x in b2.items():
if x == 2 and b[c] == 0:
b[c] = 1
elif (x == 0 or x > 2) and b[c] == 1:
b[c] = 0
for i in range(100):
iterate2()
count = 0
for x in b.values():
if x%2 == 1:
count += 1
print(i+1, count)
break
count = 0
for x in b.values():
if x%2 == 1:
count += 1
print(count)
return 0
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')