aboutsummaryrefslogtreecommitdiffstats
path: root/src/12/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/12/solve.py
parent1bcc82c5bfbde87edd03c01ffdf9ee5934681592 (diff)
downloadaoc2018-python-87ab487d59fa85dbe2afa55cc841b02805ae42ca.tar.gz
aoc2018-python-87ab487d59fa85dbe2afa55cc841b02805ae42ca.zip
Reorder days into src
Diffstat (limited to 'src/12/solve.py')
-rw-r--r--src/12/solve.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/12/solve.py b/src/12/solve.py
new file mode 100644
index 0000000..a688376
--- /dev/null
+++ b/src/12/solve.py
@@ -0,0 +1,53 @@
+import sys
+sys.path.append("../common")
+import aoc
+
+lines = aoc.data.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 next_gen(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 get_pots():
+ pots = list()
+ for i in range(len(istate)):
+ if istate[i] == "#":
+ pots.append(i)
+ return pots
+
+def solve1(args):
+ pots = get_pots()
+ for i in range(20):
+ pots = next_gen(pots)
+ return sum(pots)
+
+def solve2(args):
+ pots = get_pots()
+ psum = sum(pots)
+ pdif = None
+ i = 0
+ while (True):
+ pots = next_gen(pots)
+ i += 1
+ csum = sum(pots)
+ if pdif == csum - psum:
+ return csum + pdif * (50000000000 - 164)
+ pdif = csum - psum
+ psum = csum
+
+aoc.run(solve1, solve2, sols=[1987, 1150000000358])