aboutsummaryrefslogtreecommitdiffstats
path: root/src/18
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2020-12-08 21:53:34 +0100
committerLouis Burda <quent.burda@gmail.com>2020-12-08 21:53:34 +0100
commit61c19fd58a9c32919b9b06d38dd9ba202cf43bfb (patch)
tree716d58ee8979d3df123acfedcd5cb8472722d422 /src/18
downloadaoc2018-python-61c19fd58a9c32919b9b06d38dd9ba202cf43bfb.tar.gz
aoc2018-python-61c19fd58a9c32919b9b06d38dd9ba202cf43bfb.zip
Solve 25 days
Diffstat (limited to 'src/18')
-rw-r--r--src/18/input.txt50
-rw-r--r--src/18/solve.py106
2 files changed, 156 insertions, 0 deletions
diff --git a/src/18/input.txt b/src/18/input.txt
new file mode 100644
index 0000000..0f62ca5
--- /dev/null
+++ b/src/18/input.txt
@@ -0,0 +1,50 @@
+|...||..|....|..#..|.|..#.|......#|.||.||.|..|....
+.#.....#....#|||#|#..||..|.#..|....#...||.#.#|.|.|
+.|...|#..|||..|.#....#..|.|....|...||...#.#|...|#.
+.........|.|.#||.##.#|.||.|#..||..||...##.##......
+..|.||.#.|.|.|....||#.....|.|.||##.#|##.|....|.#..
+#..#|#..|#...|...#...#....#|##......#..|||#..#.#..
+...#.#.#|...||...|.#...||#.....|##|||.....#.#.#.|.
+.#..|.###|#.|.##......||..#...#.#..|#.|.##.#.#.|..
+..||.|.|.....###...#|...###..##|.#.#....#|..##.|.|
+.##..#.|.#....##...||.|#..#.####..|#..##.|...#....
+..#...#|..|..|.|.#|#.|.|..|..||......#|.|...##..|.
+..||...##|#.....#|..|..|.|.##.|.##.|#|.##|.||...#.
+..|.||......#...#......|......|.##...|..|.|.#...#.
+||#.#|...#.......#......|..|#.#.|.....#...#.|##..|
+.....||.|..|##.#|..#..#|..#|.##....##|.##.||...###
+#|..#..#....|...|.|....#.##...#..|.|..#........|#|
+....#.|.....##.|..##.....##..#.....|#.|.#.|.#.###.
+...###........|.....|..|#..#..|##..###.....#||..||
+#.#...#|..##...|..|||#.....|#|.......#..#.......#|
+#.....||||.#..##.|..|#|.#..|.......|##.|#|#....#.|
+#..##|...|....#.#.#.||.|..|.....#|...|....#|..##|#
+#..|||#..#..|..#|....#.........#...#...#...|.|...#
+.........|.|...#...|...#|##..#.....#.|.#..||.#...#
+.|#...#|...#.###.|#.||.|..#..#.####..#..||.#.#|..|
+.|#....||#|#|.|.|#....##..||.##|....#....#||...|.|
+...|....|...|.||#...#...........#..#....##|.#....|
+....#|#..#....|##...#..|#..#.|......|#........|#..
+.|..#...|..#.|.#....#....#.|...#..|........|..|...
+...##|....||...##...|......|#..|..#..|.........##.
+..##.|.#..........##.|....|##|.....|.#.#.|.....|.#
+#|.|.......#|..#..#.#.|..|.|.#.||#.#...|.#..|.....
+||.#............#...|...#..|#|..||##...#.|#|##|.#|
+..|..||####.|....|##.#.|...|||#..|.|.|.........##.
+...|....|..||.#..##||.|||#..|..#...#.##...||.|..||
+....|...|##...#|.|.|..#|..|#|..##.|....||..|.|.||.
+#..#.......|.#..#..||...#|..#.....#|#..|..|...##..
+#...#..##..|.#...#.....|......###|..|...||.....|..
+#.|..#..|#..|#|..#..||#......|#.......|.#....###..
+#...#||...##|.#.#|####|...|......|.#.#..|#.##...|.
+|.#..#....#..#.....|#..|#.|..##.#..|#|##........||
+#.........##..|....|#.........||..||##...||.|..|.|
+.#....#.......#..|#.|...|....#.#.#........##.##.|.
+.#.#..|.|#..|..#..###...|.#......|.|#.|.##..|.|##.
+..||..|.##.##|...|....#||##....#..#.|#|.|#||#..|..
+.......#|#||.....|.||.|##..|.|....|....|.#...#..|.
+#||##|.||..|##.|..|#.#.|.|....#|.#|.....|||..#.||.
+.||#.|#...||#.#..#..|.|#.....#.#.|#.|...#..#...#..
+#........|..#...|#..###.|.#|...#..#...........#.|#
+.|...#|....||.|...#.|..|.||##.|..#|.#|..|.#....#..
+||....###...#...|||.||..#.|.....||.#|..###..|....#
diff --git a/src/18/solve.py b/src/18/solve.py
new file mode 100644
index 0000000..270ace1
--- /dev/null
+++ b/src/18/solve.py
@@ -0,0 +1,106 @@
+from sys import argv as args
+from copy import deepcopy
+from collections import deque
+
+states = (".", "|", "#")
+
+ivals = dict()
+ivals["#"] = 0
+ivals["."] = 0
+ivals["|"] = 0
+
+def parseLine(l):
+ return tuple([states.index(c) for c in l])
+
+inputstr = open("input.txt").read()
+
+sinputstr = """.#.#...|#.
+.....#|##|
+.|..|...#.
+..|#.....#
+#.#|||#|#|
+...#.||...
+.|....|...
+||...#|.#|
+|.||||..|.
+...#.|..|."""
+
+vmap = [parseLine(l) for l in inputstr.split("\n") if len(l) != 0]
+ylen = len(vmap)
+xlen = len(vmap[0])
+
+def getAt(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(getAt(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 getVals():
+ 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 drawMap(cmap):
+ for y in range(ylen):
+ print("".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 getRes():
+ vals = getVals()
+ return (vals[1] * vals[2])
+
+def solve1():
+ #drawMap(vmap)
+ iterate(10)
+ vals = getVals()
+ #drawMap(vmap)
+ print(vals[1] * vals[2])
+ return
+
+def solve2():
+ iterate(1000)
+ omap = deepcopy(vmap)
+ counter = 0
+ while True:
+ iterate(1)
+ counter += 1
+ if vmap == omap:
+ break
+
+ #print(counter)
+ print(getRes())
+ #drawMap(vmap)
+ return
+
+def main():
+ if len(args) > 1:
+ if args[1] == "1":
+ solve1()
+ elif args[1] == "2":
+ solve2()
+
+main()