mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-22 06:28:11 +01:00
Solve exercise 3 part 2
This commit is contained in:
parent
86cc38af54
commit
04fab8ad4e
1 changed files with 11 additions and 8 deletions
|
@ -5,11 +5,11 @@ import math
|
||||||
def already_seen_on_others(seen, wire, position):
|
def already_seen_on_others(seen, wire, position):
|
||||||
for key, value in seen.items():
|
for key, value in seen.items():
|
||||||
if key != wire and position in value:
|
if key != wire and position in value:
|
||||||
return True
|
return value[position]
|
||||||
return False
|
return None
|
||||||
|
|
||||||
|
|
||||||
def distance(position):
|
def distance(seen, position, wire_length):
|
||||||
return int(math.fabs(position[0]) + math.fabs(position[1]))
|
return int(math.fabs(position[0]) + math.fabs(position[1]))
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,9 +18,10 @@ def main():
|
||||||
raw = f.read()
|
raw = f.read()
|
||||||
|
|
||||||
distances = set()
|
distances = set()
|
||||||
seen_positions = collections.defaultdict(set)
|
seen_positions = collections.defaultdict(dict)
|
||||||
for wire, line in enumerate(raw.split()):
|
for wire, line in enumerate(raw.split()):
|
||||||
x, y = 0, 0
|
x, y = 0, 0
|
||||||
|
wire_length = 0
|
||||||
for segment in line.split(","):
|
for segment in line.split(","):
|
||||||
direction = segment[0]
|
direction = segment[0]
|
||||||
length = int(segment[1:])
|
length = int(segment[1:])
|
||||||
|
@ -35,11 +36,13 @@ def main():
|
||||||
y -= 1
|
y -= 1
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unknown direction: {direction}")
|
raise ValueError(f"Unknown direction: {direction}")
|
||||||
|
wire_length += 1
|
||||||
position = (x, y)
|
position = (x, y)
|
||||||
seen_positions[wire].add(position)
|
if position not in seen_positions[wire]:
|
||||||
if already_seen_on_others(seen_positions, wire, position):
|
seen_positions[wire][position] = wire_length
|
||||||
d = distance(position)
|
steps = already_seen_on_others(seen_positions, wire, position)
|
||||||
distances.add(distance(position))
|
if steps:
|
||||||
|
distances.add(steps + seen_positions[wire][position])
|
||||||
|
|
||||||
print(min(distances))
|
print(min(distances))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue