Skip to content

Commit 1c637bd

Browse files
committed
day15: add shitty solutions
1 parent ed49258 commit 1c637bd

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

day15/1.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env pypy3
2+
3+
import functools
4+
import re
5+
import sys
6+
7+
ROW_Y = 2_000_000
8+
9+
def taxidist(p1, p2):
10+
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
11+
12+
def main():
13+
xranges = set()
14+
for line in sys.stdin:
15+
sx, sy, bx, by = map(int, re.findall(r'-?[0-9]+', line.strip()))
16+
d = taxidist((sx, sy), (bx, by))
17+
rowd = taxidist((sx, sy), (sx, ROW_Y))
18+
if rowd <= d:
19+
xlower = sx - (d - rowd)
20+
xupper = sx + (d - rowd)
21+
if bx == xlower:
22+
xlower += 1
23+
elif bx == xupper:
24+
xupper -= 1
25+
xranges.add((xlower, xupper))
26+
27+
# XXX: wasteful!
28+
xs = functools.reduce(set.union, (set(range(l, u + 1))
29+
for l, u in xranges))
30+
print(len(xs))
31+
32+
if __name__ == '__main__':
33+
main()

day15/2.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env pypy3
2+
3+
import functools
4+
import re
5+
import sys
6+
7+
MIN = 0
8+
MAX = 4_000_000
9+
10+
def taxidist(p1, p2):
11+
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
12+
13+
def outer_perimeter(s, r):
14+
sx, sy = s
15+
x = sx - r - 1
16+
y = sy
17+
while y > sy - r - 1:
18+
yield (x, y)
19+
x += 1
20+
y -= 1
21+
while y < sy:
22+
yield (x, y)
23+
x += 1
24+
y += 1
25+
while y < sy + r + 1:
26+
yield (x, y)
27+
x -= 1
28+
y += 1
29+
while y > sy:
30+
yield (x, y)
31+
x -= 1
32+
y -= 1
33+
34+
def contained(p, signals, ignore):
35+
for s, r in signals:
36+
if s == ignore:
37+
continue
38+
d = taxidist(s, p)
39+
if d <= r:
40+
return True
41+
return False
42+
43+
def find_beacon(signals, minimum, maximum):
44+
for s1, r1 in signals:
45+
for p in outer_perimeter(s1, r1):
46+
if ((not contained(p, signals, s1)) and
47+
minimum <= p[0] <= maximum and
48+
minimum <= p[1] <= maximum):
49+
return p
50+
51+
raise AssertionError('bruh')
52+
53+
def main():
54+
signals = []
55+
for line in sys.stdin:
56+
sx, sy, bx, by = map(int, re.findall(r'-?[0-9]+', line.strip()))
57+
signals.append(((sx, sy), taxidist((sx, sy), (bx, by))))
58+
59+
p = find_beacon(signals, MIN, MAX)
60+
freq = p[0] * 4_000_000 + p[1]
61+
print(freq)
62+
63+
if __name__ == '__main__':
64+
main()

day15/input.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Sensor at x=2208586, y=2744871: closest beacon is at x=2094814, y=3380585
2+
Sensor at x=3937279, y=2452476: closest beacon is at x=3597034, y=2313095
3+
Sensor at x=3535638, y=3151860: closest beacon is at x=4098735, y=3373876
4+
Sensor at x=1867584, y=2125870: closest beacon is at x=2685670, y=2236965
5+
Sensor at x=2290971, y=1583182: closest beacon is at x=2685670, y=2236965
6+
Sensor at x=3137806, y=2216828: closest beacon is at x=3233556, y=2000000
7+
Sensor at x=3393352, y=331000: closest beacon is at x=3233556, y=2000000
8+
Sensor at x=1444302, y=821689: closest beacon is at x=1683873, y=-199301
9+
Sensor at x=1084667, y=3412239: closest beacon is at x=2094814, y=3380585
10+
Sensor at x=439341, y=3916996: closest beacon is at x=-290982, y=4102300
11+
Sensor at x=295460, y=2114590: closest beacon is at x=362644, y=370187
12+
Sensor at x=2212046, y=3819484: closest beacon is at x=2094814, y=3380585
13+
Sensor at x=3413280, y=3862465: closest beacon is at x=4098735, y=3373876
14+
Sensor at x=3744934, y=1572303: closest beacon is at x=3597034, y=2313095
15+
Sensor at x=3349047, y=2522469: closest beacon is at x=3597034, y=2313095
16+
Sensor at x=171415, y=591241: closest beacon is at x=362644, y=370187
17+
Sensor at x=3237499, y=2150414: closest beacon is at x=3233556, y=2000000
18+
Sensor at x=559077, y=454593: closest beacon is at x=362644, y=370187
19+
Sensor at x=3030733, y=2047512: closest beacon is at x=3233556, y=2000000
20+
Sensor at x=1667358, y=3956837: closest beacon is at x=2094814, y=3380585
21+
Sensor at x=1850337, y=98963: closest beacon is at x=1683873, y=-199301
22+
Sensor at x=2699546, y=3157824: closest beacon is at x=2094814, y=3380585
23+
Sensor at x=1113195, y=98130: closest beacon is at x=1683873, y=-199301
24+
Sensor at x=59337, y=246804: closest beacon is at x=362644, y=370187
25+
Sensor at x=566043, y=29068: closest beacon is at x=362644, y=370187
26+
Sensor at x=2831421, y=2581088: closest beacon is at x=2685670, y=2236965
27+
Sensor at x=597818, y=749461: closest beacon is at x=362644, y=370187

day15/sample.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
2+
Sensor at x=9, y=16: closest beacon is at x=10, y=16
3+
Sensor at x=13, y=2: closest beacon is at x=15, y=3
4+
Sensor at x=12, y=14: closest beacon is at x=10, y=16
5+
Sensor at x=10, y=20: closest beacon is at x=10, y=16
6+
Sensor at x=14, y=17: closest beacon is at x=10, y=16
7+
Sensor at x=8, y=7: closest beacon is at x=2, y=10
8+
Sensor at x=2, y=0: closest beacon is at x=2, y=10
9+
Sensor at x=0, y=11: closest beacon is at x=2, y=10
10+
Sensor at x=20, y=14: closest beacon is at x=25, y=17
11+
Sensor at x=17, y=20: closest beacon is at x=21, y=22
12+
Sensor at x=16, y=7: closest beacon is at x=15, y=3
13+
Sensor at x=14, y=3: closest beacon is at x=15, y=3
14+
Sensor at x=20, y=1: closest beacon is at x=15, y=3

0 commit comments

Comments
 (0)