8000 Part 2 test passes · dumbledad/adventofcode@5ffef7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ffef7b

Browse files
author
Tim Regan
committed
Part 2 test passes
1 parent b8d641a commit 5ffef7b

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

2022/day15.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,15 @@ def impossible_count(self, y):
3838
return len(impossible_x)
3939

4040
def distress_tuning(self, min=0, max=4_000_000):
41-
possible = {}
4241
for y in range(min, max + 1):
43-
possible[y] = set()
4442
for x in range(min, max + 1):
45-
possible[y].add(x)
46-
for sensor in self.sensors:
47-
if (sensor['coords'][1] in possible) and (sensor['coords'][0] in possible[sensor['coords'][1]]):
48-
possible[sensor['coords'][1]].pop(sensor['coords'][0])
49-
elif (sensor['closest_beacon'][1] in possible) and (sensor['closest_beacon'][0] in possible[sensor['closest_beacon'][1]]):
50-
possible[sensor['closest_beacon'][1]].pop(sensor['closest_beacon'][0])
51-
while True:
52-
y = next(iter(possible.keys()))
53-
x = next(iter(possible[y]))
54-
if len(possible) == 1 and len(possible[y]) == 1:
55-
return (x * 4_000_000) + y
56-
for sensor in self.sensors:
57-
if ((abs(sensor['coords'][0] - x) + abs(sensor['coords'][1] - y)) <= sensor['distance']
58-
and ((x, y) not in [s['coords'] for s in self.sensors])
59-
and ((x, y) not in [s['closest_beacon'] for s in self.sensors])):
60-
possible[y].pop(x, None)
61-
43+
possible = True
44+
for sensor in self.sensors:
45+
if (abs(sensor['coords'][0] - x) + abs(sensor['coords'][1] - y)) <= sensor['distance']:
46+
possible = False
47+
break
48+
if possible:
49+
return (x * 4_000_000) + y
6250

6351

6452
def main():

2022/test_day15.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def test_impossible_count():
1717

1818
def test_distress_tuning():
1919
tunnels = Tunnels(filename)
20-
assert tunnels.distress_tuning() == 56_000_011
20+
assert tunnels.distress_tuning(max=20) == 56_000_011

0 commit comments

Comments
 (0)
0