aboutsummaryrefslogtreecommitdiffstats
path: root/src/12
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/12
downloadaoc2018-python-61c19fd58a9c32919b9b06d38dd9ba202cf43bfb.tar.gz
aoc2018-python-61c19fd58a9c32919b9b06d38dd9ba202cf43bfb.zip
Solve 25 days
Diffstat (limited to 'src/12')
-rw-r--r--src/12/input.txt34
-rw-r--r--src/12/solve.py61
2 files changed, 95 insertions, 0 deletions
diff --git a/src/12/input.txt b/src/12/input.txt
new file mode 100644
index 0000000..6dcf208
--- /dev/null
+++ b/src/12/input.txt
@@ -0,0 +1,34 @@
+initial state: ##..#.#.#..##..#..##..##..#.#....#.....##.#########...#.#..#..#....#.###.###....#..........###.#.#..
+
+..##. => .
+..... => .
+##..# => .
+...#. => .
+#.... => .
+...## => #
+.#.#. => .
+#..#. => #
+##.#. => .
+#..## => .
+..#.. => .
+#.#.# => .
+###.# => .
+###.. => .
+.#... => #
+.##.# => .
+##... => #
+..### => .
+####. => .
+#...# => #
+.#..# => #
+##### => #
+..#.# => #
+.#.## => #
+#.### => .
+....# => .
+.###. => .
+.#### => #
+.##.. => .
+##.## => #
+#.##. => #
+#.#.. => #
diff --git a/src/12/solve.py b/src/12/solve.py
new file mode 100644
index 0000000..8c4784c
--- /dev/null
+++ b/src/12/solve.py
@@ -0,0 +1,61 @@
+from sys import argv as args
+
+lines = open("input.txt").read().split("\n")
+istate = lines[0].split(": ")[1]
+rules = [l.split(" => ") for l in lines[2:] if l != ""]
+rules = [r[0] for r in rules if r[1] == "#"]
+
+def nextGen(pots):
+ pmin = min(pots)
+ pmax = max(pots)
+ npots = list()
+ for i in range(pmin-4, pmax+1):
+ for r in rules:
+ match = True
+ for j in range(5):
+ if (r[j] == "#") != ((i+j) in pots):
+ match = False
+ break
+ if match:
+ npots.append(i+2)
+ break
+ return npots
+
+def getPots():
+ pots = list()
+ for i in range(len(istate)):
+ if istate[i] == "#":
+ pots.append(i)
+ return pots
+
+def solve1():
+ pots = getPots()
+ for i in range(20):
+ pots = nextGen(pots)
+ print(sum(pots))
+ return
+
+def solve2():
+ pots = getPots()
+ psum = sum(pots)
+ pdif = None
+ i = 0
+ while (True):
+ pots = nextGen(pots)
+ i += 1
+ csum = sum(pots)
+ if pdif == csum - psum:
+ print(csum + pdif * (50000000000 - 164))
+ break
+ pdif = csum - psum
+ psum = csum
+ return
+
+def main():
+ if len(args) > 1:
+ if args[1] == "1":
+ solve1()
+ elif args[1] == "2":
+ solve2()
+
+main()