Solve day 9 till 11 and part 1 day 12 2018

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

54
2018/d10.py Normal file
View File

@ -0,0 +1,54 @@
from lib import str_to_ints
def print_points(objs):
x_min, x_max = 10**9, 0
y_min, y_max = 10**9, 0
exist = set()
for x, y, _, _ in objs:
x_min = min(x_min, x)
y_min = min(y_min, y)
x_max = max(x_max, x)
y_max = max(y_max, y)
exist.add((x, y))
if x_max - x_min > 67:
return False
for y in range(y_min, y_max + 1):
s = ""
for x in range(x_min, x_max + 1):
if (x, y) in exist:
s += "X"
else:
s += " "
print(s)
return True
def part_1(data):
objs = []
for line in data.splitlines():
x, y, dx, dy = str_to_ints(line)
objs.append((x,y, dx, dy))
for t in range(1_000_000):
for i in range(len(objs)):
x, y, dx, dy = objs[i]
on = (x + dx, y + dy, dx, dy)
objs[i] = on
if print_points(objs):
print(t + 1)
break
def main():
input_file = __file__.replace(".py", ".txt")
with open(input_file) as f:
data = f.read()
part_1(data)
if __name__ == "__main__":
main()

80
2018/d11.py Normal file
View File

@ -0,0 +1,80 @@
from functools import lru_cache
@lru_cache
def score(serial, x, y):
# Find the fuel cell's rack ID, which is its X coordinate plus 10.
# Begin with a power level of the rack ID times the Y coordinate.
# Increase the power level by the value of the grid serial number (your puzzle input).
# Set the power level to itself multiplied by the rack ID.
# Keep only the hundreds digit of the power level (so 12345 becomes 3; numbers with no hundreds digit become 0).
# Subtract 5 from the power level.
rack_id = x + 10
power_level = rack_id * y
power_level += serial
power_level *= rack_id
try:
return int(str(power_level)[-3]) - 5
except IndexError:
return -5
def part_1(data):
v = int(data.strip())
max_val = 0
max_coord = None
assert score(8, 3, 5) == 4
assert score(57, 122, 79) == -5
assert score(39, 217, 196) == 0
for x in range(1, 299):
for y in range(1, 299):
s = 0
for x2 in range(x, x + 3):
for y2 in range(y, y + 3):
s += score(v, x2, y2)
if x2 > 300 or y2 > 300:
assert False
if s > max_val:
max_val = s
max_coord = (x, y)
assert max_coord is not None
print(",".join(list(map(str, max_coord))))
def part_2(data):
v = int(data.strip())
max_val = 0
max_coord = None
# brute force, just tried if 15 is small enough
for size in range(2, 15):
for x in range(1, 299):
for y in range(1, 299):
s = 0
if x + size > 300 or y + size > 300:
break
for x2 in range(x, x + size):
for y2 in range(y, y + size):
s += score(v, x2, y2)
if x2 > 300 or y2 > 300:
assert False
if s > max_val:
max_val = s
max_coord = (x, y, size)
assert max_coord is not None
print(",".join(list(map(str, max_coord))))
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()

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()

73
2018/d9.py Normal file
View File

@ -0,0 +1,73 @@
from lib import str_to_ints
from collections import defaultdict
def part_1(data):
scores = defaultdict(int)
n_players, worth = str_to_ints(data)
current_player = 0
circle = [0]
current = 1
for n in range(1, worth + 1):
if n % 23 == 0:
scores[current_player] += n
current = (current - 7) % len(circle)
scores[current_player] += circle.pop(current)
else:
current = (current + 2) % len(circle)
circle.insert(current, n)
current_player = (current_player + 1) % n_players
print(max(scores.values()))
class Node:
def __init__(self, value, prev=None, next=None):
self.value = value
if prev is None:
self.prev = self
else:
self.prev = prev
if next is None:
self.next = self
else:
self.next = next
def part_2(data):
scores = defaultdict(int)
n_players, worth = str_to_ints(data)
current_player = 0
first = Node(0)
current = first
current.next = current
current.prev = current
for n in range(1, (worth * 100) + 1):
if n % 23 == 0:
scores[current_player] += n
for _ in range(7):
current = current.prev
scores[current_player] += current.value
current.prev.next = current.next
current.next.prev = current.prev
current = current.next
else:
new_current = Node(n, current.next, current.next.next)
current.next.next.prev = new_current
current.next.next = new_current
current = new_current
current_player = (current_player + 1) % n_players
print(max(scores.values()))
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()

1
2018/monitor.py Symbolic link
View File

@ -0,0 +1 @@
../monitor.py

View File

@ -97,7 +97,11 @@ written in Python.
- Day 6: 1:10:29
- Day 7: 20:15
- Day 8: 18:35
- Day 9:
- Day 9: 1:17:52
- Day 10: 19:14
- Day 11: 22:52
- Day 12:
# 2022