Skip to content

Commit acaff37

Browse files
committed
2018 Day 12
1 parent b94ad7a commit acaff37

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

2018/12a.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
state = "###....#..#..#......####.#..##..#..###......##.##..#...#.##.###.##.###.....#.###..#.#.##.#..#.#"
2+
# state = '#..#.#..##......###...###'
3+
4+
rules = {
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+
"#..##": ".",
35+
"#.#..": "#",
36+
"..#.#": ".",
37+
}
38+
39+
# rules = {
40+
# '...##': '#',
41+
# '..#..': '#',
42+
# '.#...': '#',
43+
# '.#.#.': '#',
44+
# '.#.##': '#',
45+
# '.##..': '#',
46+
# '.####': '#',
47+
# '#.#.#': '#',
48+
# '#.###': '#',
49+
# '##.#.': '#',
50+
# '##.##': '#',
51+
# '###..': '#',
52+
# '###.#': '#',
53+
# '####.': '#',
54+
# }
55+
56+
57+
def grow(state, left_index):
58+
"""grows the plants
59+
60+
:param state: state of plants to grow
61+
:param lef_index: index of the lefmost plant
62+
63+
:returns: (new_state, new index of the leftmost plant)
64+
65+
"""
66+
if state[0] == "#":
67+
prefix = "...."
68+
left_index -= 4
69+
elif state[:2] == ".#":
70+
prefix = "..."
71+
left_index -= 3
72+
elif state[:3] == "..#":
73+
prefix = ".."
74+
left_index -= 2
75+
elif state[:4] == "...#":
76+
prefix = "."
77+
left_index -= 1
78+
else:
79+
prefix = ""
80+
81+
if state[-1] == "#":
82+
postfix = "...."
83+
elif state[-2:] == "#.":
84+
postfix = "..."
85+
elif state[-3:] == "#..":
86+
postfix = ".."
87+
elif state[-4:] == "#...":
88+
postfix = "."
89+
else:
90+
postfix = ""
91+
92+
state = prefix + state + postfix
93+
94+
new_plant_positions = set()
95+
96+
for plant_index, i in enumerate(range(len(state) - 4), left_index + 2):
97+
plant_cut = state[i : i + 5]
98+
try:
99+
if rules[plant_cut] == "#":
100+
new_plant_positions.add(plant_index)
101+
except KeyError:
102+
pass
103+
104+
new_state = "".join(
105+
"#" if i in new_plant_positions else "."
106+
for i in range(left_index, len(state) + left_index)
107+
)
108+
109+
return new_state, left_index
110+
111+
112+
def main():
113+
global state
114+
left_index = 0
115+
116+
for i in range(20):
117+
state, left_index = grow(state, left_index)
118+
119+
print(sum(i for (i, plant) in enumerate(state, left_index) if plant == "#"))
120+
121+
122+
main()
123+
124+
125+
# 3128 too high
126+
# 2688 too high -- 30 generations instead of 20 :facepalm:

2018/12b.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
state = "###....#..#..#......####.#..##..#..###......##.##..#...#.##.###.##.###.....#.###..#.#.##.#..#.#"
2+
# state = '#..#.#..##......###...###'
3+
4+
rules = {
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+
"#..##": ".",
35+
"#.#..": "#",
36+
"..#.#": ".",
37+
}
38+
39+
# rules = {
40+
# '...##': '#',
41+
# '..#..': '#',
42+
# '.#...': '#',
43+
# '.#.#.': '#',
44+
# '.#.##': '#',
45+
# '.##..': '#',
46+
# '.####': '#',
47+
# '#.#.#': '#',
48+
# '#.###': '#',
49+
# '##.#.': '#',
50+
# '##.##': '#',
51+
# '###..': '#',
52+
# '###.#': '#',
53+
# '####.': '#',
54+
# }
55+
56+
57+
def grow(state, left_index):
58+
"""grows the plants
59+
60+
:param state: state of plants to grow
61+
:param lef_index: index of the lefmost plant
62+
63+
:returns: (new_state, new index of the leftmost plant)
64+
65+
"""
66+
if state[0] == "#":
67+
prefix = "...."
68+
left_index -= 4
69+
elif state[:2] == ".#":
70+
prefix = "..."
71+
left_index -= 3
72+
elif state[:3] == "..#":
73+
prefix = ".."
74+
left_index -= 2
75+
elif state[:4] == "...#":
76+
prefix = "."
77+
left_index -= 1
78+
else:
79+
prefix = ""
80+
81+
if state[-1] == "#":
82+
postfix = "...."
83+
elif state[-2:] == "#.":
84+
postfix = "..."
85+
elif state[-3:] == "#..":
86+
postfix = ".."
87+
elif state[-4:] == "#...":
88+
postfix = "."
89+
else:
90+
postfix = ""
91+
92+
state = prefix + state + postfix
93+
94+
new_plant_positions = set()
95+
96+
for plant_index, i in enumerate(range(len(state) - 4), left_index + 2):
97+
plant_cut = state[i : i + 5]
98+
try:
99+
if rules[plant_cut] == "#":
100+
new_plant_positions.add(plant_index)
101+
except KeyError:
102+
pass
103+
104+
new_state = "".join(
105+
"#" if i in new_plant_positions else "."
106+
for i in range(left_index, len(state) + left_index)
107+
)
108+
109+
return new_state, left_index
110+
111+
112+
def main():
113+
global state
114+
left_index = 0
115+
116+
for i in range(50000000000):
117+
if i % 10000 == 0:
118+
print(i)
119+
print(sum(i for (i, plant) in enumerate(state, left_index) if plant == "#"))
120+
121+
state, left_index = grow(state, left_index)
122+
123+
print(sum(i for (i, plant) in enumerate(state, left_index) if plant == "#"))
124+
125+
126+
main()
127+
128+
129+
# 12b
130+
# 1700000000045 too high

0 commit comments

Comments
 (0)