aboutsummaryrefslogtreecommitdiffstats
path: root/src/18/solve.py
blob: f209429030b5cf0cf590ca8f99dd17655a987c5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys
sys.path.append("../common")
import aoc
from copy import deepcopy
from collections import deque

states = (".", "|", "#")

ivals = dict()
ivals["#"] = 0
ivals["."] = 0
ivals["|"] = 0

def pasrse_line(l):
    return tuple([states.index(c) for c in l])

vmap = [pasrse_line(l) for l in aoc.data.split("\n")]
xlen, ylen = len(vmap[0]), len(vmap)

def get_at(x, y):
    if y < 0 or y >= ylen or x < 0 or x >= xlen:
        return None
    return vmap[y][x]

def next(x, y):
    v = vmap[y][x]
    around = list()
    [[around.append(get_at(x+i-1, y+j-1)) for j in range(3) if not (i == 1 and j == 1)] for i in range(3)]
    if v == 0:
        if len([v for v in around if v == 1]) >= 3:
            return 1
    elif v == 1:
        if len([v for v in around if v == 2]) >= 3:
            return 2
    elif v == 2:
        if len([v for v in around if v == 1]) < 1 or len([v for v in around if v == 2]) < 1:
            return 0
    return v

def get_vals():
    vals = [0 for x in range(3)]
    for y in range(ylen):
        for x in range(xlen):
            vals[vmap[y][x]] += 1
    return vals

def draw_map(cmap):
    for y in range(ylen):
        aoc.debug("".join([str(c) for c in cmap[y]]))

def iterate(n):
    global vmap
    for i in range(n):
        omap = [deque() for y in range(ylen)]
        for y in range(ylen):
            for x in range(xlen):
                omap[y].append(next(x, y))
        vmap = omap

def get_res():
    vals = get_vals()
    return (vals[1] * vals[2])

def solve1(args):
    iterate(10)
    vals = get_vals()
    return vals[1] * vals[2]

def solve2(args):
    iterate(1000)
    omap = deepcopy(vmap)
    counter = 0
    while True:
        iterate(1)
        counter += 1
        if vmap == omap:
            break

    return get_res()

aoc.run(solve1, solve2, sols=[574590, 183787])