aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

solve.py (1919B)


      1import sys
      2sys.path.append("../common")
      3import aoc
      4from copy import deepcopy
      5from collections import deque
      6
      7states = (".", "|", "#")
      8
      9ivals = dict()
     10ivals["#"] = 0
     11ivals["."] = 0
     12ivals["|"] = 0
     13
     14def pasrse_line(l):
     15    return tuple([states.index(c) for c in l])
     16
     17vmap = [pasrse_line(l) for l in aoc.data.split("\n")]
     18xlen, ylen = len(vmap[0]), len(vmap)
     19
     20def get_at(x, y):
     21    if y < 0 or y >= ylen or x < 0 or x >= xlen:
     22        return None
     23    return vmap[y][x]
     24
     25def next(x, y):
     26    v = vmap[y][x]
     27    around = list()
     28    [[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)]
     29    if v == 0:
     30        if len([v for v in around if v == 1]) >= 3:
     31            return 1
     32    elif v == 1:
     33        if len([v for v in around if v == 2]) >= 3:
     34            return 2
     35    elif v == 2:
     36        if len([v for v in around if v == 1]) < 1 or len([v for v in around if v == 2]) < 1:
     37            return 0
     38    return v
     39
     40def get_vals():
     41    vals = [0 for x in range(3)]
     42    for y in range(ylen):
     43        for x in range(xlen):
     44            vals[vmap[y][x]] += 1
     45    return vals
     46
     47def draw_map(cmap):
     48    for y in range(ylen):
     49        aoc.debug("".join([str(c) for c in cmap[y]]))
     50
     51def iterate(n):
     52    global vmap
     53    for i in range(n):
     54        omap = [deque() for y in range(ylen)]
     55        for y in range(ylen):
     56            for x in range(xlen):
     57                omap[y].append(next(x, y))
     58        vmap = omap
     59
     60def get_res():
     61    vals = get_vals()
     62    return (vals[1] * vals[2])
     63
     64def solve1(args):
     65    iterate(10)
     66    vals = get_vals()
     67    return vals[1] * vals[2]
     68
     69def solve2(args):
     70    iterate(1000)
     71    omap = deepcopy(vmap)
     72    counter = 0
     73    while True:
     74        iterate(1)
     75        counter += 1
     76        if vmap == omap:
     77            break
     78
     79    return get_res()
     80
     81aoc.run(solve1, solve2, sols=[574590, 183787])