Skip to content

Commit d787bfd

Browse files
committed
Day 15
1 parent 3568e3c commit d787bfd

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

2022/15/beacon_exclusion_zone.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from utils import read_input, run
2+
from collections import Counter
3+
4+
5+
FNAME = "15/input.txt"
6+
7+
8+
def parse_line(line):
9+
return [tuple(int(coord.rstrip(',')[2:]) for coord in item.split(' ')[-2:]) for item in line.split(':')]
10+
11+
12+
##########
13+
# PART 1 #
14+
##########
15+
16+
def distance(c1, c2):
17+
x1, y1 = c1
18+
x2, y2 = c2
19+
return abs(x2 - x1) + abs(y2 - y1)
20+
21+
22+
def find_not_possible(data, target_y):
23+
not_possible = set()
24+
25+
for sensor, beacon in data:
26+
x, y = sensor
27+
dist = distance(sensor, beacon)
28+
remaining_dist = dist - abs(target_y - y)
29+
if remaining_dist >= 0:
30+
not_possible |= {x + i for i in range(-remaining_dist, remaining_dist)}
31+
32+
return not_possible
33+
34+
35+
def part_one(input_file):
36+
data = read_input(input_file, parse_chunk=parse_line)
37+
return len(find_not_possible(data, 2_000_000))
38+
39+
##########
40+
# PART 2 #
41+
##########
42+
43+
44+
def is_correct(data, coord):
45+
return all(distance(sensor, coord) > distance(sensor, beacon) for sensor, beacon in data)
46+
47+
48+
def find_outer_coords(counter: Counter, sensor, beacon):
49+
dist = distance(sensor, beacon) + 1
50+
x, y = sensor
51+
52+
# Possible optimisation: just store the lines for each square
53+
# only consider points that intersect
54+
for i in range(dist):
55+
for edge_x in (x + dist - i, x - dist + i):
56+
for edge_y in (y-i, y+i):
57+
if 0 <= edge_x <= 4_000_000 and 0 <= edge_y <= 4_000_000:
58+
counter[(edge_x, edge_y)] += 1
59+
60+
return counter
61+
62+
63+
def part_two(input_file):
64+
data = read_input(input_file, parse_chunk=parse_line)
65+
counts = Counter()
66+
67+
for sensor, beacon in data:
68+
find_outer_coords(counts, sensor, beacon)
69+
70+
if answer := next((p for p in counts if counts[p] >= 4 and is_correct(data, p)), None):
71+
x, y = answer
72+
return x * 4_000_000 + y
73+
74+
return None
75+
76+
77+
if __name__ == '__main__':
78+
run(part_one, part_two, FNAME)

2022/15/input.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Sensor at x=2389280, y=2368338: closest beacon is at x=2127703, y=2732666
2+
Sensor at x=1882900, y=3151610: closest beacon is at x=2127703, y=2732666
3+
Sensor at x=2480353, y=3555879: closest beacon is at x=2092670, y=3609041
4+
Sensor at x=93539, y=965767: closest beacon is at x=501559, y=361502
5+
Sensor at x=357769, y=2291291: closest beacon is at x=262473, y=2000000
6+
Sensor at x=2237908, y=1893142: closest beacon is at x=2127703, y=2732666
7+
Sensor at x=2331355, y=3906306: closest beacon is at x=2092670, y=3609041
8+
Sensor at x=3919787, y=2021847: closest beacon is at x=2795763, y=2589706
9+
Sensor at x=3501238, y=3327244: closest beacon is at x=3562181, y=3408594
10+
Sensor at x=1695968, y=2581703: closest beacon is at x=2127703, y=2732666
11+
Sensor at x=3545913, y=3356504: closest beacon is at x=3562181, y=3408594
12+
Sensor at x=1182450, y=1405295: closest beacon is at x=262473, y=2000000
13+
Sensor at x=3067566, y=3753120: closest beacon is at x=3562181, y=3408594
14+
Sensor at x=1835569, y=3983183: closest beacon is at x=2092670, y=3609041
15+
Sensor at x=127716, y=2464105: closest beacon is at x=262473, y=2000000
16+
Sensor at x=3065608, y=3010074: closest beacon is at x=2795763, y=2589706
17+
Sensor at x=2690430, y=2693094: closest beacon is at x=2795763, y=2589706
18+
Sensor at x=2051508, y=3785175: closest beacon is at x=2092670, y=3609041
19+
Sensor at x=2377394, y=3043562: closest beacon is at x=2127703, y=2732666
20+
Sensor at x=1377653, y=37024: closest beacon is at x=501559, y=361502
21+
Sensor at x=2758174, y=2627042: closest beacon is at x=2795763, y=2589706
22+
Sensor at x=1968468, y=2665146: closest beacon is at x=2127703, y=2732666
23+
Sensor at x=3993311, y=3779031: closest beacon is at x=3562181, y=3408594
24+
Sensor at x=159792, y=1923149: closest beacon is at x=262473, y=2000000
25+
Sensor at x=724679, y=3489022: closest beacon is at x=2092670, y=3609041
26+
Sensor at x=720259, y=121267: closest beacon is at x=501559, y=361502
27+
Sensor at x=6, y=46894: closest beacon is at x=501559, y=361502
28+
Sensor at x=21501, y=2098549: closest beacon is at x=262473, y=2000000
29+
Sensor at x=2974083, y=551886: closest beacon is at x=4271266, y=-98555

2022/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- [Day 11 - Monkey in the Middle](./11/monkey_in_the_middle.py)
1414
- [Day 12 - Hill Climbing Algorithm](./12/hill_climbing_algorithm.py)
1515
- [Day 13 - Distress Signal](./13/distress_signal.py)
16+
- [Day 14 - Regolith Reservoir](./14/regolith_reservoir.py)
17+
- [Day 15 - Beacon Exclusion Zone](./15/beacon_exclusion_zone.py)
1618

1719
```bash
1820
# Run solutions as a module from this directory

0 commit comments

Comments
 (0)