2019 day 03 part 2

This commit is contained in:
Tristan 2019-12-05 23:02:43 +00:00
parent d25c3b26d9
commit dd9f13cb0d
5 changed files with 84 additions and 6 deletions

29
2019/03/part2.py Normal file
View file

@ -0,0 +1,29 @@
with open('input.txt', 'r') as f:
data = f.read().split('\n')
# i wish i thought of this from the start
grid = {}
crossings = []
lineNo = 0
for line in data:
steps = 0
pos = [0, 0]
line = line.split(',')
for segment in line:
dirChar = segment[0]
dirV = [0, 1] if dirChar == "U" else [0, -1] if dirChar == "D" else [1, 0] if dirChar == "R" else [-1, 0]
for i in range(1, int(segment[1:])+1):
pos[0] += dirV[0]
pos[1] += dirV[1]
steps += 1
if lineNo == 0:
grid[str(pos)] = steps
continue
try:
crossings.append(grid[str(pos)] + steps)
print(pos, grid[str(pos)], steps)
except:
continue
lineNo += 1
print(min(crossings))