2017-12-19 - A Series of Tubes
(original .ipynb)
puzzle_input_lines = open("puzzle_input/day19.txt").readlines()
test_input_lines = """ |
| +--+
A | C
F---|----E|--+
| | | D
+B-+ +--+ """.split("\n")
def left(dr, dc):
return (-dc, dr)
def right(dr, dc):
return (dc, -dr)
def can_move_to(grid, r, c):
row_ok = r >= 0 and r < len(grid)
col_ok = c >= 0 and c < len(grid[0])
if row_ok and col_ok and not grid[r][c].isspace():
return True
return False
def solve(input_lines):
r, c = (0, input_lines[0].index("|"))
dr, dc = (1, 0)
path = []
steps = 0
while True:
tile = input_lines[r][c]
if tile == "+":
lr, lc = left(dr, dc)
rr, rc = right(dr, dc)
if can_move_to(input_lines, r+lr, c+lc):
dr, dc = lr, lc
else:
dr, dc = rr, rc
elif tile.isalpha():
path.append(tile)
elif tile == " ":
break
r += dr
c += dc
steps += 1
return "".join(path), steps
assert "ABCDEF", 38 == solve(test_input_lines)
part_one, part_two = solve(puzzle_input_lines)
print("part one:", part_one)
print("part two:", part_two)
part one: EPYDUXANIT
part two: 17544