-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2019-Day3.py
110 lines (98 loc) · 5.42 KB
/
AoC2019-Day3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Code written by @gerty
# Input from Day 3, Problem 1 (and 2) of Advent of Code 2019
# Input files in same directory
# My input: in file Day03-INPUT.txt
with open('Day03-INPUT.txt', 'r') as f:
filedata = f.readlines()
data = []
gridsize = 20000
origin = int(gridsize / 2);
grid = [[0 for i in range(gridsize)] for j in range(gridsize)] # bits in grid locations will correspond to crossing
wirecode = 0 # keep track of which wire/bit we're tracking
minDistance = gridsize * 2
wire = filedata[0]
print(wire)
wirecode += 1
print(wirecode)
x = origin # put the cursor at the origin (defined above as center-ish of grid) for each wire
y = origin
nav = wire.split(',') # split commands by commas
print(nav)
movecount = 0
for navcom in nav:
if navcom[0] == 'U': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
y += 1 # go up
movecount += 1
if not (grid[x][y]): # if the point has not been passed (only record first pass)
grid[x][y] = movecount # leave a trail of movecounts
if navcom[0] == 'D': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
y -= 1 # go down
movecount += 1
if not (grid[x][y]): # if the point has not been passed
grid[x][y] = movecount # leave a trail of movecounts
if navcom[0] == 'R': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
x += 1 # go right
movecount += 1
if not (grid[x][y]): # if the point has not been passed
grid[x][y] = movecount # leave a trail of movecounts
if navcom[0] == 'L': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
x -= 1 # go left
movecount += 1
if not (grid[x][y]): # if the point has not been passed
grid[x][y] = movecount # leave a trail of movecounts
wire = filedata[1]
print(wire)
wirecode += 1
print(wirecode)
x = origin # put the cursor at the origin (defined above as center-ish of grid) for each wire
y = origin
nav = wire.split(',') # split commands by commas
print(nav)
movecount = 0
for navcom in nav:
if navcom[0] == 'U': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
y += 1
movecount += 1 # if previous pass did not contact point just count and move along
if (grid[x][y]): # if record of a crossing at x,y then count it as a possible winner
print('CROSSED at x:' + str(x) + " y:" + str(y) + " with moves: " + str(movecount + grid[x][y]))
if movecount + grid[x][y] < minDistance: # calculate Manhattan distance
minDistance = movecount + grid[x][y]
print(movecount + grid[x][y])
if navcom[0] == 'D': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
y -= 1
movecount += 1 # if previous pass did not contact point just count and move along
if (grid[x][y]): # if record of a crossing at x,y then count it as a possible winner
print('CROSSED at x:' + str(x) + " y:" + str(y) + " with moves: " + str(movecount + grid[x][y]))
if movecount + grid[x][y] < minDistance: # calculate Manhattan distance
minDistance = movecount + grid[x][y]
print(movecount + grid[x][y])
if navcom[0] == 'R': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
x += 1
movecount += 1 # if previous pass did not contact point just count and move along
if (grid[x][y]): # if record of a crossing at x,y then count it as a possible winner
print('CROSSED at x:' + str(x) + " y:" + str(y) + " with moves: " + str(movecount + grid[x][y]))
if movecount + grid[x][y] < minDistance: # calculate Manhattan distance
minDistance = movecount + grid[x][y]
print(movecount + grid[x][y])
if navcom[0] == 'L': # if the first letter of the navcom is an "UP" command
for i in range(int(navcom[1:])): # the remainder of the navcom is a distance
x -= 1
movecount += 1 # if previous pass did not contact point just count and move along
if (grid[x][y]): # if record of a crossing at x,y then count it as a possible winner
print('CROSSED at x:' + str(x) + " y:" + str(y) + " with moves: " + str(movecount + grid[x][y]))
if movecount + grid[x][y] < minDistance: # calculate Manhattan distance
minDistance = movecount + grid[x][y]
print(movecount + grid[x][y])
# At this point we should have two (or more) wires overlayed onto our grid.
# Now we just need to find the closest intersecting point (all bits flipped) to our origin.
# Or not, if the above integrated code works.
print('DONE!')
print(minDistance)
# Not 12930 even when adding 2 to 12928 it is too low.