Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 924c906

Browse files
committed
Day 15
1 parent a6fc113 commit 924c906

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

day15/input.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Sensor at x=2557568, y=3759110: closest beacon is at x=2594124, y=3746832
2+
Sensor at x=2684200, y=1861612: closest beacon is at x=2816974, y=2000000
3+
Sensor at x=1003362, y=1946094: closest beacon is at x=1972523, y=2563441
4+
Sensor at x=2142655, y=1481541: closest beacon is at x=1932524, y=967542
5+
Sensor at x=2796219, y=1955744: closest beacon is at x=2816974, y=2000000
6+
Sensor at x=3890832, y=1818644: closest beacon is at x=3454717, y=2547103
7+
Sensor at x=2828842, y=1921726: closest beacon is at x=2816974, y=2000000
8+
Sensor at x=2065227, y=583957: closest beacon is at x=1932524, y=967542
9+
Sensor at x=2725784, y=2088998: closest beacon is at x=2816974, y=2000000
10+
Sensor at x=3574347, y=927734: closest beacon is at x=1932524, y=967542
11+
Sensor at x=2939312, y=2652370: closest beacon is at x=3454717, y=2547103
12+
Sensor at x=2495187, y=3681541: closest beacon is at x=2431306, y=3703654
13+
Sensor at x=2878002, y=2054681: closest beacon is at x=2816974, y=2000000
14+
Sensor at x=1539310, y=3235516: closest beacon is at x=1972523, y=2563441
15+
Sensor at x=545413, y=533006: closest beacon is at x=-538654, y=69689
16+
Sensor at x=1828899, y=3980292: closest beacon is at x=2431306, y=3703654
17+
Sensor at x=3275729, y=2937931: closest beacon is at x=3454717, y=2547103
18+
Sensor at x=600131, y=3861189: closest beacon is at x=2431306, y=3703654
19+
Sensor at x=2089895, y=28975: closest beacon is at x=1932524, y=967542
20+
Sensor at x=2960402, y=3942666: closest beacon is at x=2594124, y=3746832
21+
Sensor at x=3785083, y=3905392: closest beacon is at x=2594124, y=3746832
22+
Sensor at x=1721938, y=1077173: closest beacon is at x=1932524, y=967542
23+
Sensor at x=2515156, y=3751221: closest beacon is at x=2594124, y=3746832
24+
Sensor at x=2469423, y=2109095: closest beacon is at x=2816974, y=2000000
25+
Sensor at x=1776986, y=904092: closest beacon is at x=1932524, y=967542
26+
Sensor at x=2789294, y=3316115: closest beacon is at x=2594124, y=3746832
27+
Sensor at x=3538757, y=2695066: closest beacon is at x=3454717, y=2547103
28+
Sensor at x=2299738, y=2708004: closest beacon is at x=1972523, y=2563441
29+
Sensor at x=2388366, y=3234346: closest beacon is at x=2431306, y=3703654

day15/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import re
3+
4+
def parse(filename):
5+
with open(filename) as f:
6+
return [[int(d) for d in re.findall("(\d+)", line)] for line in f.readlines()]
7+
8+
def dist_l1(pt1, pt2):
9+
return abs(pt2[0] - pt1[0]) + abs(pt2[1] - pt1[1])
10+
11+
def coverage(sensors, row):
12+
ranges = []
13+
for (sx, sy), r in sensors.items():
14+
dx = r - abs(sy - row) # horizontal distance to edge on a certain row
15+
if dx >= 0:
16+
ranges.append((sx - dx, sx + dx))
17+
return ranges
18+
19+
def find_hole(ranges):
20+
highest = 0
21+
for a, b in sorted(ranges):
22+
if a <= highest + 1:
23+
highest = max(highest, b)
24+
else:
25+
return a
26+
return -1
27+
28+
def part1(sensors, row):
29+
ranges = coverage(sensors, row)
30+
# assuming no hole on the row
31+
a = min([a for a, b in ranges])
32+
b = max([b for a, b in ranges])
33+
return b - a
34+
35+
def part2(sensors, limit):
36+
for row in reversed(range(limit + 1)):
37+
ranges = coverage(sensors, row)
38+
col = find_hole(ranges)
39+
if col > 0:
40+
return col * 4_000_000 + row
41+
42+
43+
if __name__ == "__main__":
44+
input_file = os.path.join(os.path.dirname(__file__), "input.txt")
45+
signal = parse(input_file)
46+
sensors = {(sx, sy): dist_l1((sx, sy), (bx, by)) for sx, sy, bx, by in signal}
47+
48+
solution1 = part1(sensors, row=2_000_000)
49+
print(f"part 1 - solution : {solution1}")
50+
51+
solution2 = part2(sensors, limit=4_000_000)
52+
print(f"part 2 - solution : {solution2}")

0 commit comments

Comments
 (0)