Solve day 9 till 11 and part 1 day 12 2018

This commit is contained in:
2024-07-09 17:45:08 -04:00
parent e9ba9cee63
commit 26f523047e
6 changed files with 345 additions and 1 deletions

132
2018/d12.py Normal file
View File

@@ -0,0 +1,132 @@
from lib import *
data = """initial state: #..#.#..##......###...###
...## => #
..#.. => #
.#... => #
.#.#. => #
.#.## => #
.##.. => #
.#### => #
#.#.# => #
#.### => #
##.#. => #
##.## => #
###.. => #
###.# => #
####. => #"""
all = """
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
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 = 50
state = "." * offset + state + "." * offset
for i in range(20):
ns = ".."
for i in range(2, len(state) - 2):
s = state[i - 2:i + 3]
ns += map[s]
ns += ".."
state = ns
r = 0
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():
input_file = __file__.replace(".py", ".txt")
with open(input_file) as f:
data = f.read()
part_1(data)
part_2(data)
if __name__ == "__main__":
main()