Skip to content

Commit cb59587

Browse files
author
Simon Hessner
committed
Add solution for day 12
1 parent 67a0850 commit cb59587

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

day12/example

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
initial state: #..#.#..##......###...###
2+
3+
...## => #
4+
..#.. => #
5+
.#... => #
6+
.#.#. => #
7+
.#.## => #
8+
.##.. => #
9+
.#### => #
10+
#.#.# => #
11+
#.### => #
12+
##.#. => #
13+
##.## => #
14+
###.. => #
15+
###.# => #
16+
####. => #

day12/input

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
initial state: ##.#..#.#..#.####.#########.#...#.#.#......##.#.#...##.....#...#...#.##.#...##...#.####.##..#.#..#.
2+
3+
..#.. => .
4+
..#.# => .
5+
#.#.. => .
6+
.#..# => .
7+
#.... => .
8+
....# => .
9+
.#.#. => #
10+
#.### => .
11+
####. => .
12+
..... => .
13+
.#... => #
14+
##### => #
15+
.#### => .
16+
#..#. => #
17+
#...# => #
18+
.###. => .
19+
###.# => #
20+
...## => #
21+
#.##. => #
22+
.#.## => #
23+
##.#. => #
24+
...#. => .
25+
..### => #
26+
###.. => #
27+
##... => .
28+
..##. => .
29+
.##.# => .
30+
##.## => .
31+
.##.. => .
32+
##..# => #
33+
#.#.# => .
34+
#..## => #

day12/solution.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def find_match(rule, pots):
2+
for i in range(len(pots)-5):
3+
if pots[i:i+5] == rule:
4+
return i+2
5+
return None
6+
7+
from collections import defaultdict
8+
9+
def getsum(pots, first):
10+
s = 0
11+
for i,c in enumerate(pots):
12+
if c == "#":
13+
s += i - first
14+
return s
15+
16+
with open('input') as f:
17+
lines = f.read().splitlines()
18+
pots = lines[0].split(": ")[1]
19+
pots = "."*20000 + pots + "."*20000
20+
first = 0
21+
for i,c in enumerate(pots):
22+
if c == "#":
23+
first = i
24+
break
25+
26+
rules = defaultdict(lambda : ".")
27+
for rule in lines[2:]:
28+
condition, replacement = rule.split(" => ")
29+
rules[condition] = replacement
30+
31+
for g in range(200):
32+
newpots = list(pots)
33+
for i in range(2, len(pots)-2):
34+
newpots[i] = rules[pots[i-2:i+3]]
35+
pots = "".join(newpots)
36+
s = getsum(pots, first)
37+
38+
print((50000000000-200)*87+s)

0 commit comments

Comments
 (0)