Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ab842d

Browse files
authored
Day 12 (#10)
* Day 12 * Remove .swp * Add .gitignore
1 parent ecd7c7f commit 9ab842d

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.swp
2+
.DS_Store

2018/12/input-full.txt

Lines changed: 34 additions & 0 deletions
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+
##.## => #

2018/12/input-sample.txt

Lines changed: 16 additions & 0 deletions
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+
####. => #

2018/12/solution.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from __future__ import print_function
2+
import re
3+
4+
input_file = 'input-full.txt'
5+
6+
patterns = {}
7+
pots = ''
8+
with open(input_file, 'r') as f:
9+
raw_data = [i.rstrip() for i in f]
10+
11+
for line in raw_data:
12+
if 'initial state:' in line:
13+
pots = ('.' * 10) + line[15:] + ('.' * 120)
14+
elif '=>' in line:
15+
pattern, action = line.split(' => ')
16+
if action == '#':
17+
patterns[pattern] = action
18+
19+
# do large number of iterations, look for patterns
20+
print('{0:>2}: {1}'.format(0, pots))
21+
for i in range(1, 120):
22+
actions = set()
23+
for pattern in patterns.keys():
24+
action = patterns[pattern]
25+
for m in re.finditer('(?={})'.format(re.escape(pattern)), pots):
26+
actions.add((m.start() + 2, action))
27+
pots = '.' * len(pots)
28+
pot_idx_sum = 0
29+
for pos, char in actions:
30+
pots = pots[:pos] + char + pots[pos+1:]
31+
pot_idx_sum += pos - 10
32+
print('{0:>2}: {1} {2}'.format(i, pots, pot_idx_sum))
33+
34+
# after 100 generations, a pattern emerges:
35+
#
36+
# gen sum
37+
# --- ----
38+
# 100 6175
39+
# 101 6225
40+
# 102 6275
41+
# 103 6325
42+
# 104 6375
43+
# 105 6425
44+
# 106 6475
45+
# 107 6525
46+
# 108 6575
47+
# 109 6625
48+
# 110 6675
49+
#
50+
# sum = gen_nbr * 50 + 1175
51+
part2_sum = 50000000000 * 50 + 1175
52+
print('part 2: {}'.format(part2_sum))

0 commit comments

Comments
 (0)