Finished 2017/18
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
## \-\-- Day 19: A Series of Tubes \-\--
|
||||
|
||||
Somehow, a network packet got
|
||||
lost
|
||||
and ended up here. It\'s trying to follow a routing diagram (your puzzle
|
||||
input), but it\'s confused about where to go.
|
||||
|
||||
Its starting point is just off the top of the diagram. Lines (drawn with
|
||||
`|`, `-`, and `+`) show the path it needs to take, starting by going
|
||||
down onto the only line connected to the top of the diagram. It needs to
|
||||
follow this path until it reaches the end (located somewhere within the
|
||||
diagram) and stop there.
|
||||
|
||||
Sometimes, the lines cross over each other; in these cases, it needs to
|
||||
continue going the same direction, and only turn left or right when
|
||||
there\'s no other option. In addition, someone has left *letters* on the
|
||||
line; these also don\'t change its direction, but it can use them to
|
||||
keep track of where it\'s been. For example:
|
||||
|
||||
|
|
||||
| +--+
|
||||
A | C
|
||||
F---|----E|--+
|
||||
| | | D
|
||||
+B-+ +--+
|
||||
|
||||
Given this diagram, the packet needs to take the following path:
|
||||
|
||||
- Starting at the only line touching the top of the diagram, it must
|
||||
go down, pass through `A`, and continue onward to the first `+`.
|
||||
- Travel right, up, and right, passing through `B` in the process.
|
||||
- Continue down (collecting `C`), right, and up (collecting `D`).
|
||||
- Finally, go all the way left through `E` and stopping at `F`.
|
||||
|
||||
Following the path to the end, the letters it sees on its path are
|
||||
`ABCDEF`.
|
||||
|
||||
The little packet looks up at you, hoping you can help it find the way.
|
||||
*What letters will it see* (in the order it would see them) if it
|
||||
follows the path? (The routing diagram is very wide; make sure you view
|
||||
it without line wrapping.)
|
||||
|
||||
To begin, [get your puzzle input](19/input).
|
||||
|
||||
Answer:
|
||||
@@ -0,0 +1,130 @@
|
||||
#!/bin/python3
|
||||
import sys,re
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int
|
||||
|
||||
input_f = 'test'
|
||||
|
||||
part = 1
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
grid = []
|
||||
|
||||
start = ()
|
||||
prev = ()
|
||||
|
||||
def valid_n(grid,cur):
|
||||
r,c = cur
|
||||
count = 0
|
||||
if grid[r-1][c] != ' ':
|
||||
count += 1
|
||||
if grid[r+1][c] != ' ':
|
||||
count += 1
|
||||
if grid[r][c+1] != ' ':
|
||||
count += 1
|
||||
if grid[r][c-1] != ' ':
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
if part == 1:
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
grid.append(list(line.rstrip()))
|
||||
|
||||
for r, row in enumerate(grid):
|
||||
for c, col in enumerate(row):
|
||||
print(grid[r][c],end='')
|
||||
print()
|
||||
|
||||
for r, row in enumerate(grid):
|
||||
for c, col in enumerate(row):
|
||||
if r == 0:
|
||||
if grid[r][c] == '|':
|
||||
start = (r,c)
|
||||
|
||||
end = False
|
||||
cur = start
|
||||
prev = (-1,-1)
|
||||
prev_dir = {
|
||||
'up': False,
|
||||
'down': False,
|
||||
'left': False,
|
||||
'right': False
|
||||
}
|
||||
|
||||
while not end:
|
||||
r,c = cur
|
||||
try:
|
||||
if grid[r-1][c] != ' ' and prev != (r-1,c):
|
||||
print('up',grid[r-1][c])
|
||||
|
||||
prev = cur
|
||||
prev_dir = {
|
||||
'up': True,
|
||||
'down': False,
|
||||
'left': False,
|
||||
'right': False
|
||||
}
|
||||
cur = (r-1,c)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
if grid[r+1][c] != ' ' and prev != (r+1,c):
|
||||
print('down',grid[r+1][c])
|
||||
if valid_n(grid,cur) >= 1 and prev_dir['down']:
|
||||
prev = cur
|
||||
prev_dir = {
|
||||
'up': False,
|
||||
'down': True,
|
||||
'left': False,
|
||||
'right': False
|
||||
}
|
||||
cur = (r+1,c)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
if grid[r][c-1] != ' ' and prev != (r,c-1) and not prev_dir['left']:
|
||||
print('left',grid[r][c-1])
|
||||
prev = cur
|
||||
prev_dir = {
|
||||
'up': False,
|
||||
'down': False,
|
||||
'left': True,
|
||||
'right': False
|
||||
}
|
||||
cur = (r,c-1)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
try:
|
||||
if grid[r][c+1] != ' ' and prev != (r,c+1) and not prev_dir['right']:
|
||||
print('right',grid[r][c+1])
|
||||
prev = cur
|
||||
prev_dir = {
|
||||
'up': False,
|
||||
'down': False,
|
||||
'left': False,
|
||||
'right': True
|
||||
}
|
||||
cur = (r,c+1)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
input()
|
||||
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
if part == 2:
|
||||
exit()
|
||||
Reference in New Issue
Block a user