Skip to content

Instantly share code, notes, and snippets.

@erikaderstedt
Created December 5, 2019 08:41
Show Gist options
  • Save erikaderstedt/396dd270103189f37e7477c48181ceb0 to your computer and use it in GitHub Desktop.
Save erikaderstedt/396dd270103189f37e7477c48181ceb0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import sys
wires = []
for line in open(sys.argv[1],'r').readlines():
parts = line.split(',')
wire = {}
r = 0
c = 0
steps = 1
for part in parts:
direction = part[0]
length = int(part[1:])
for i in range(length):
if direction == "D":
r = r + 1
elif direction == "U":
r = r - 1
elif direction == "L":
c = c - 1
elif direction == "R":
c = c + 1
wire[(r,c)] = steps + i
steps = steps + length
wires.append(wire)
w1k = set(wires[0].keys())
w2k = set(wires[1].keys())
intersections = w1k & w2k
print("pt 1:", min(abs(i[0]) + abs(i[1]) for i in intersections))
print("pt 2:", min(wires[0][i] + wires[1][i] for i in intersections))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment