Skip to content

Commit bccdd3c

Browse files
committed
10 halfway done. Part 2 will require some serious
thought. Honestly, I have no idea at the moment...
1 parent 9e3bc64 commit bccdd3c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

solutions/solution_10.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import sys
2+
sys.path.append("..")
3+
4+
from aoclib import util
5+
6+
# Find the single giant loop starting at S. How many steps along the loop
7+
# does it take to get from the starting position to the point farthest
8+
# from the starting position?
9+
def solve_10(data):
10+
# N, E, S, W mappings (clockwise!)
11+
pipes = {'|': [True, False, True, False],
12+
'-': [False, True, False, True],
13+
'L': [True, True, False, False],
14+
'J': [True, False, False, True],
15+
'7': [False, False, True, True],
16+
'F': [False, True, True, False],
17+
'.': [False, False, False, False], # ground; there is no pipe in this tile.
18+
'S': [True, True, True, True] # Start, should be able to enter from all directions?
19+
}
20+
# vectors for N, E, S, W
21+
nswe = [(0,-1), (1,0), (0,1), (-1,0) ]
22+
23+
# S is the starting position of the animal; there is a pipe on this tile,
24+
# but your sketch doesn't show what shape the pipe has.
25+
for y0 in range(0,len(data)):
26+
if -1 != (x0 := data[y0].find('S')):
27+
break
28+
print(x0,y0)
29+
30+
x = x0
31+
y = y0
32+
path = [] # stores the path
33+
34+
loop = True
35+
while loop:
36+
for direction in range(0, len(nswe)):
37+
if not pipes[data[y][x]][direction]:
38+
continue
39+
# Get candidate position, I don't even pretend to 100% understand this
40+
(cx,cy) = tuple(sum(i) for i in zip ((x,y), nswe[direction]) )
41+
42+
# Edge cases
43+
if cx < 0 or cx >=len(data[0]) or cy < 0 or cy >=len(data):
44+
continue
45+
46+
comefrom = (direction + 2) % len(nswe)
47+
pipe = data[cy][cx]
48+
if pipes[pipe][comefrom]: # We could go that way
49+
print(f"{x} {y} {cx} {cy} {comefrom} {pipe}")
50+
if not path or direction != path[-1]:
51+
# Let's not go if we're doing a 180
52+
path.append(comefrom)
53+
(x,y) = (cx,cy)
54+
print(f"OK {x} {y}")
55+
56+
if (x,y) == (x0,y0) and path:
57+
loop = False
58+
break
59+
60+
return len(path)/2
61+
62+
# Oh, but how many tiles are inside the loop?
63+
def solve_10b(data):
64+
# Great. How do I define "outside" and "inside"?
65+
return None
66+
67+
if __name__ == '__main__':
68+
data = util.getInput("../inputs/10.txt")
69+
print( solve_10(data) )
70+

tests/test_10.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
sys.path.append("..")
3+
4+
from solutions import solution_10
5+
from aoclib import util
6+
7+
data1 = [".....",".S-7.",".|.|.",".L-J.","....."]
8+
data2 = ["..F7.",".FJ|.","SJ.L7","|F--J","LJ..."]
9+
10+
data3 = ["...........",
11+
".S-------7.",
12+
".|F-----7|.",
13+
".||OOOOO||.",
14+
".||OOOOO||.",
15+
".|L-7OF-J|.",
16+
".|II|O|II|.",
17+
".L--JOL--J.",
18+
".....O....."]
19+
20+
data4 = [".F----7F7F7F7F-7....",
21+
".|F--7||||||||FJ....",
22+
".||.FJ||||||||L7....",
23+
"FJL7L7LJLJ||LJ.L-7..",
24+
"L--J.L7...LJS7F-7L7.",
25+
"....F-J..F7FJ|L7L7L7",
26+
"....L7.F7||L7|.L7L7|",
27+
".....|FJLJ|FJ|F7|.LJ",
28+
"....FJL-7.||.||||...",
29+
"....L---J.LJ.LJLJ..."]
30+
31+
def test_10():
32+
# First part
33+
assert solution_10.solve_10(data1) == 4
34+
assert solution_10.solve_10(data2) == 8
35+
36+
def test_10b():
37+
assert solution_10.solve_10b(data3) == 4
38+
assert solution_10.solve_10b(data4) == 8

0 commit comments

Comments
 (0)