Solve 2018 day 12

This commit is contained in:
2024-07-13 10:08:16 -04:00
parent 26f523047e
commit f04f8ed426
2 changed files with 35 additions and 106 deletions

View File

@@ -1,53 +1,12 @@
from lib import * def score(state, offset):
r = 0
data = """initial state: #..#.#..##......###...### for i in range(len(state)):
if state[i] == "#":
...## => # r += i + offset
..#.. => # return r
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #"""
all = """ def solve(data, target_i):
1 2 3
0 0 0 0
0: ...#..#.#..##......###...###...........
1: ...#...#....#.....#..#..#..#...........
2: ...##..##...##....#..#..#..##..........
3: ..#.#...#..#.#....#..#..#...#..........
4: ...#.#..#...#.#...#..#..##..##.........
5: ....#...##...#.#..#..#...#...#.........
6: ....##.#.#....#...#..##..##..##........
7: ...#..###.#...##..#...#...#...#........
8: ...#....##.#.#.#..##..##..##..##.......
9: ...##..#..#####....#...#...#...#.......
10: ..#.#..#...#.##....##..##..##..##......
11: ...#...##...#.#...#.#...#...#...#......
12: ...##.#.#....#.#...#.#..##..##..##.....
13: ..#..###.#....#.#...#....#...#...#.....
14: ..#....##.#....#.#..##...##..##..##....
15: ..##..#..#.#....#....#..#.#...#...#....
16: .#.#..#...#.#...##...#...#.#..##..##...
17: ..#...##...#.#.#.#...##...#....#...#...
18: ..##.#.#....#####.#.#.#...##...##..##..
19: .#..###.#..#.#.#######.#.#.#..#.#...#..
20: .#....##....#####...#######....#.#..##.
"""
def part_1(data):
state = None state = None
map = {} map = {}
for line in data.splitlines(): for line in data.splitlines():
@@ -60,72 +19,41 @@ def part_1(data):
map[lhs] = rhs map[lhs] = rhs
assert state is not None assert state is not None
offset = 50 seen = {}
state = "." * offset + state + "." * offset offset = 0
for i in range(20): for i in range(target_i):
ns = ".." while not state.startswith("...."):
state = "." + state
offset -= 1
while not state.endswith("...."):
state += "."
if state in seen:
prev_i, prev_score = seen[state]
assert (i - prev_i) == 1
current_score = score(state, offset)
delta_score = current_score - prev_score
final_score = current_score + (target_i - i) * delta_score
print(final_score)
return
else:
seen[state] = (i, score(state, offset))
ns = ""
for i in range(2, len(state) - 2): for i in range(2, len(state) - 2):
s = state[i - 2:i + 3] s = state[i - 2 : i + 3]
ns += map[s] ns += map[s]
ns += ".."
state = ns state = ns
offset += 2
r = 0 print(score(state, offset))
for i in range(2, len(state) - 2):
if state[i] == "#":
r += (i - offset)
print(r)
def part_2(data):
state = None
map = {}
for line in data.splitlines():
if "initial state" in line:
_, rest = line.split(":")
state = rest.strip()
if "=>" in line:
lhs, rhs = line.split(" => ")
assert lhs not in map
map[lhs] = rhs
assert state is not None
offset = 55
state = "." * offset + state + offset * ".."
for i in range(100):
# if state.startswith("..."):
# assert False
# elif state.startswith("..#"):
# pass
# elif state.startswith(".#"):
# state = "." + state
# offset += 1
# elif state.startswith("#"):
# state = ".." + state
# offset += 2
print(state)
ns = ".."
for i in range(2, len(state) - 2):
s = state[i - 2:i + 3]
ns += map[s]
ns += ".."
state = ns
# print(state)
r = 0
for i in range(2, len(state) - 2):
if state[i] == "#":
r += (i - offset)
print(r)
def main(): def main():
input_file = __file__.replace(".py", ".txt") input_file = __file__.replace(".py", ".txt")
with open(input_file) as f: with open(input_file) as f:
data = f.read() data = f.read()
part_1(data) solve(data, 20)
part_2(data) solve(data, 50 * 10**9)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -100,7 +100,8 @@ written in Python.
- Day 9: 1:17:52 - Day 9: 1:17:52
- Day 10: 19:14 - Day 10: 19:14
- Day 11: 22:52 - Day 11: 22:52
- Day 12: - Day 12: 180:00
- Day 13:
# 2022 # 2022