parent
ecd8ac4d7b
commit
9cd7cba1c2
@ -1,11 +1,55 @@ |
|||||||
import unittest |
import unittest |
||||||
import os |
import os |
||||||
|
import sys |
||||||
|
|
||||||
|
|
||||||
class Day3(unittest.TestCase): |
class Day3(unittest.TestCase): |
||||||
inputfile = os.path.join(os.path.dirname(__file__), "input/day3_input") |
inputfile = os.path.join(os.path.dirname(__file__), "input/day3_input") |
||||||
|
|
||||||
|
def draw_path(self, inst): |
||||||
|
path = set() |
||||||
|
dists = {} |
||||||
|
cnt = 0 |
||||||
|
ptr = (0, 0) |
||||||
|
for d, l in ((x[0], x[1:]) for x in inst.split(',')): |
||||||
|
for _ in range(int(l)): |
||||||
|
if d == 'D': |
||||||
|
ptr = (ptr[0], ptr[1] - 1) |
||||||
|
elif d == 'R': |
||||||
|
ptr = (ptr[0] + 1, ptr[1]) |
||||||
|
elif d == 'U': |
||||||
|
ptr = (ptr[0], ptr[1] + 1) |
||||||
|
else: |
||||||
|
ptr = (ptr[0] - 1, ptr[1]) |
||||||
|
cnt += 1 |
||||||
|
if ptr not in dists or dists[ptr] > cnt: |
||||||
|
dists[ptr] = cnt |
||||||
|
path.add(ptr) |
||||||
|
return (path, dists) |
||||||
|
|
||||||
def test_day3a(self): |
def test_day3a(self): |
||||||
|
paths = [] |
||||||
|
result = sys.maxsize |
||||||
|
|
||||||
|
with open(self.inputfile) as fp: |
||||||
|
for line in fp.readlines(): |
||||||
|
paths.append(self.draw_path(line)[0]) |
||||||
|
for x, y in paths[0].intersection(paths[1]): |
||||||
|
dist = abs(x) + abs(y) |
||||||
|
result = dist if dist < result else result |
||||||
|
self.assertEqual(result, 399) |
||||||
|
|
||||||
|
def test_day3b(self): |
||||||
|
paths = [] |
||||||
|
distances = [] |
||||||
|
result = sys.maxsize |
||||||
|
|
||||||
with open(self.inputfile) as fp: |
with open(self.inputfile) as fp: |
||||||
steps = fp.readline().split(',') |
for line in fp.readlines(): |
||||||
pass |
computed = self.draw_path(line) |
||||||
|
paths.append(computed[0]) |
||||||
|
distances.append(computed[1]) |
||||||
|
for point in paths[0].intersection(paths[1]): |
||||||
|
dist = distances[0][point] + distances[1][point] |
||||||
|
result = dist if dist < result else result |
||||||
|
self.assertEqual(result, 15678) |
||||||
|
|||||||
Loading…
Reference in new issue