Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created December 3, 2019 08:13
Show Gist options
  • Save sotolf2/5b9e8bee3f64102bbbb91ea85a8e2f66 to your computer and use it in GitHub Desktop.
Save sotolf2/5b9e8bee3f64102bbbb91ea85a8e2f66 to your computer and use it in GitHub Desktop.
inlines = []
with open("day3.txt") as f:
inlines = f.readlines() \
|> map$(-> _.strip()) \
|> list
data Wire(covering,):
def find(self, point) = self.covering.index(point)
data Point(x,y)
def create_wire(direction_list):
covering = []
cur_p = Point(0,0)
covering.append(cur_p)
for line in direction_list:
match ('R', x) in line:
for i in range(x):
cur_p = Point(cur_p.x + 1, cur_p.y)
covering.append(cur_p)
else: match ('L', x) in line:
for i in range(x):
cur_p = Point(cur_p.x - 1, cur_p.y)
covering.append(cur_p)
else: match ('U', x) in line:
for i in range(x):
cur_p = Point(cur_p.x, cur_p.y + 1)
covering.append(cur_p)
else: match ('D', x) in line:
for i in range(x):
cur_p = Point(cur_p.x, cur_p.y - 1)
covering.append(cur_p)
else:
raise TypeError("Wrongly formatted Direction")
return Wire(covering)
def parse_line(line):
directions = line \
|> .split(",") \
|> map$(-> (_[0], int(_[1:]))) \
|> create_wire$() \
return directions
wire_a, wire_b = map(-> parse_line(_), inlines) |> tuple
part1 = set(wire_b.covering).intersection(set(wire_a.covering)) |> list \
|> map$(-> abs(_.x) + abs(_.y)) \
|> filter$(-> _ != 0) \
|> reduce$(min)
print(part1)
part2 = set(wire_b.covering).intersection(set(wire_a.covering)) |> list \
|> map$(-> wire_a.find(_) + wire_b.find(_)) \
|> filter$(-> _ != 0) \
|> reduce$(min)
print(part2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment