aboutsummaryrefslogtreecommitdiffstats
path: root/src/18/solve.py
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-04-07 17:18:18 -0400
committerLouis Burda <quent.burda@gmail.com>2023-04-07 17:19:39 -0400
commit87ab487d59fa85dbe2afa55cc841b02805ae42ca (patch)
treecd90ab715e1b5b5803674045dbafd6d51d27ac90 /src/18/solve.py
parent1bcc82c5bfbde87edd03c01ffdf9ee5934681592 (diff)
downloadaoc2018-python-87ab487d59fa85dbe2afa55cc841b02805ae42ca.tar.gz
aoc2018-python-87ab487d59fa85dbe2afa55cc841b02805ae42ca.zip
Reorder days into src
Diffstat (limited to 'src/18/solve.py')
-rw-r--r--src/18/solve.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/18/solve.py b/src/18/solve.py
new file mode 100644
index 0000000..f209429
--- /dev/null
+++ b/src/18/solve.py
@@ -0,0 +1,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])