-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
74 lines (62 loc) · 2.09 KB
/
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
#AOC 2019 Brianna Frye
#Day 3
import re
def parse_to_array(base_file):
#Just going to hardcode it for 2 lines because why not?
wire_one = []
wire_two = []
try:
file = open(base_file)
temp = file.readline()
if temp == '':
print('-------ERROR--------')
wire_one = temp.split(',')
temp = file.readline()
if temp == '':
print('-------ERROR--------')
wire_two = temp.split(',')
file.close()
return wire_one, wire_two
except IOError:
print('File does not exist!')
finally:
print('Parsing complete!')
def list_points(wire):
#Converts wire into list of coordinates
wire_coords = []
pointer = [0,0]
step_reg = re.compile(r'([UDLR])(\d+)')
for step in wire:
raw = step_reg.search(step)
direction = raw.group(1)
amount = int(raw.group(2))
if direction == 'U':
for i in range(amount + 1):
wire_coords.append((pointer[0], (pointer[1] + i)))
pointer = [pointer[0], pointer[1] + amount]
elif direction == 'D':
for i in range(amount + 1):
wire_coords.append((pointer[0], (pointer[1] - i)))
pointer = [pointer[0], pointer[1] - amount]
elif direction == 'L':
for i in range(amount + 1):
wire_coords.append((pointer[0] - i, pointer[1]))
pointer = [pointer[0] - amount, pointer[1]]
elif direction == 'R':
for i in range(amount + 1):
wire_coords.append((pointer[0] + i, pointer[1]))
pointer = [pointer[0] + amount, pointer[1]]
return wire_coords
wire_one, wire_two = parse_to_array('day3input.txt')
wire_one_points = list_points(wire_one)
wire_two_points = list_points(wire_two)
intersections = list(set(wire_one_points) & set(wire_two_points))
intersections.remove((0,0))
min_dist = 1000000
min_point = 0
for i in intersections:
dist = abs(i[0]) + abs(i[1])
if dist < min_dist:
min_dist = dist
min_point = i
print(min_dist, min_point)