-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
executable file
·69 lines (48 loc) · 1.88 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
#!/usr/bin/env python3
# [Day 3: Crossed Wires](https://adventofcode.com/2019/day/3)
import sys
from pathlib import Path
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text()
lines = data.splitlines()
def draw(instr):
x, y = 0, 0
line = set()
# line.add((x, y)) # do not add the central port
for s in instr.split(","):
dir, n = s[0], int(s[1:])
dx, dy = {"R": (1, 0), "L": (-1, 0), "D": (0, -1), "U": (0, 1)}[dir]
for _ in range(n):
x, y = x + dx, y + dy
line.add((x, y))
return line
def steps(instr, target):
x, y = 0, 0
count = 0
for s in instr.split(","):
dir, n = s[0], int(s[1:])
dx, dy = {"R": (1, 0), "L": (-1, 0), "D": (0, -1), "U": (0, 1)}[dir]
for _ in range(n):
x, y = x + dx, y + dy
count += 1
if (x, y) == target:
return count
return 0
def manhattan(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def part1(line1, line2):
l1 = draw(line1)
l2 = draw(line2)
return min(manhattan(x, (0, 0)) for x in l1.intersection(l2))
def part2(line1, line2):
l1 = draw(line1)
l2 = draw(line2)
return min(steps(line1, x) + steps(line2, x) for x in l1.intersection(l2))
assert part1("R8,U5,L5,D3", "U7,R6,D4,L4") == 6
assert part1("R75,D30,R83,U83,L12,D49,R71,U7,L72", "U62,R66,U55,R34,D71,R55,D58,R83") == 159
assert part1("R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7") == 135
assert part2("R8,U5,L5,D3", "U7,R6,D4,L4") == 30
assert part2("R75,D30,R83,U83,L12,D49,R71,U7,L72", "U62,R66,U55,R34,D71,R55,D58,R83") == 610
assert part2("R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7") == 410
print(part1(lines[0], lines[1]))
print(part2(lines[0], lines[1]))