Skip to content

Commit 98cd226

Browse files
committed
Update day-11
1 parent 67b1b6f commit 98cd226

6 files changed

Lines changed: 150 additions & 1 deletion

File tree

2015/day-06/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# --- Day 6: Probably a Fire Hazard ---
2+
3+
4+
def parse_instruction(line):
5+
parts = line.strip().split()
6+
if parts[0] == "turn":
7+
action = parts[1]
8+
start = tuple(map(int, parts[2].split(",")))
9+
end = tuple(map(int, parts[4].split(",")))
10+
else: # toggle
11+
action = "toggle"
12+
start = tuple(map(int, parts[1].split(",")))
13+
end = tuple(map(int, parts[3].split(",")))
14+
return action, start, end
15+
16+
17+
def part1():
18+
data = open("../inputs/day-06.txt", "r").readlines()
19+
grid = [[0 for _ in range(1000)] for _ in range(1000)]
20+
21+
for line in data:
22+
action, start, end = parse_instruction(line)
23+
24+
for x in range(start[0], end[0] + 1):
25+
for y in range(start[1], end[1] + 1):
26+
if action == "on":
27+
grid[x][y] = 1
28+
elif action == "off":
29+
grid[x][y] = 0
30+
else: # toggle
31+
grid[x][y] = 1 - grid[x][y]
32+
33+
return sum(sum(row) for row in grid)
34+
35+
36+
def part2():
37+
data = open("../inputs/day-06.txt", "r").readlines()
38+
grid = [[0 for _ in range(1000)] for _ in range(1000)]
39+
40+
for line in data:
41+
action, start, end = parse_instruction(line)
42+
43+
for x in range(start[0], end[0] + 1):
44+
for y in range(start[1], end[1] + 1):
45+
if action == "on":
46+
grid[x][y] += 1
47+
elif action == "off":
48+
grid[x][y] = max(0, grid[x][y] - 1)
49+
else: # toggle
50+
grid[x][y] += 2
51+
52+
return sum(sum(row) for row in grid)
53+
54+
55+
if __name__ == "__main__":
56+
print("Part 1:", part1())
57+
print("Part 2:", part2())

2024/python/utils/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11

2-

2025/day-11/example.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
aaa: you hhh
2+
you: bbb ccc
3+
bbb: ddd eee
4+
ccc: ddd eee fff
5+
ddd: ggg
6+
eee: out
7+
fff: out
8+
ggg: out
9+
hhh: ccc fff iii
10+
iii: out

2025/day-11/example2.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
svr: aaa bbb
2+
aaa: fft
3+
fft: ccc
4+
bbb: tty
5+
tty: ccc
6+
ccc: ddd eee
7+
ddd: hub
8+
hub: fff
9+
eee: dac
10+
dac: fff
11+
fff: ggg hhh
12+
ggg: out
13+
hhh: out

2025/day-11/main.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# --- Day 11: Reactor ---
2+
from functools import cache
3+
4+
5+
def part1():
6+
# data = open('example.txt').readlines()
7+
data = open("../inputs/day-11.txt").readlines()
8+
graph = {}
9+
10+
for line in data:
11+
device, connections = line.strip().split(":")
12+
device = device.strip()
13+
# Parse the connections - they are space-separated
14+
connections = connections.strip().split()
15+
graph[device] = connections
16+
17+
@cache
18+
def paths(start, stop):
19+
return (
20+
1
21+
if start == stop
22+
else sum(paths(step, stop) for step in graph.get(start, []))
23+
)
24+
25+
return paths("you", "out")
26+
27+
28+
def part2():
29+
# data = open('example2.txt').readlines()
30+
data = open("../inputs/day-11.txt").readlines()
31+
graph = {}
32+
33+
for line in data:
34+
device, connections = line.strip().split(":")
35+
device = device.strip()
36+
# Parse the connections - they are space-separated
37+
connections = connections.strip().split()
38+
graph[device] = connections
39+
40+
@cache
41+
def paths(start, stop):
42+
return (
43+
1
44+
if start == stop
45+
else sum(paths(step, stop) for step in graph.get(start, []))
46+
)
47+
48+
tot = paths("svr", "dac") * paths("dac", "fft") * paths("fft", "out") + paths(
49+
"svr", "fft"
50+
) * paths("fft", "dac") * paths("dac", "out")
51+
return tot
52+
53+
54+
if __name__ == "__main__":
55+
print("Part 1:", part1())
56+
print("Part 2:", part2())

2025/day-12/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
3+
4+
def part1():
5+
pass
6+
7+
8+
def part2():
9+
pass
10+
11+
12+
if __name__ == "__main__":
13+
print("Part 1:", part1())
14+
print("Part 2:", part2())

0 commit comments

Comments
 (0)