Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 20, 2025. It is now read-only.

Commit 1ee19e2

Browse files
Day 15 part 1 solutions
1 parent 1ea9afa commit 1ee19e2

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

‎15-1.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
3+
import numpy
4+
import re
5+
6+
class Map:
7+
def __init__(self, input, row):
8+
sensor_regexp = re.compile("^Sensor at x=(?P<sx>[0-9-]+), y=(?P<sy>[0-9-]+): closest beacon is at x=(?P<bx>[0-9-]+), y=(?P<by>[0-9-]+)$")
9+
self.row = row
10+
11+
min_x = pow(2, 30) - 1
12+
max_x = 0
13+
max_dist = 0
14+
sensors = []
15+
beacons = []
16+
17+
for sensor in input:
18+
m = sensor_regexp.match(sensor)
19+
sx = int(m['sx'])
20+
sy = int(m['sy'])
21+
bx = int(m['bx'])
22+
by = int(m['by'])
23+
min_x = min(min_x, sx, bx)
24+
max_x = max(max_x, sx, bx)
25+
max_dist = max(max_dist, abs(sx - bx) + abs(sy - by) + 1)
26+
sensors.append((sx, sy))
27+
beacons.append((bx, by))
28+
29+
self.width = (max_x - min_x) + 2 * max_dist + 1
30+
self.x_offset = min_x - max_dist
31+
self.map = numpy.zeros((self.width), dtype=numpy.uint)
32+
33+
for index in range(len(sensors)):
34+
sx = sensors[index][0] - self.x_offset
35+
sy = sensors[index][1]
36+
bx = beacons[index][0] - self.x_offset
37+
by = beacons[index][1]
38+
if sy == self.row:
39+
self.map[sx] = 1
40+
if by == self.row:
41+
self.map[bx] = 2
42+
43+
manhattan_distance = abs(sx - bx) + abs(sy - by)
44+
if not abs(sy - self.row) < manhattan_distance:
45+
continue
46+
47+
num_cols = manhattan_distance - abs(sy - self.row) + 1
48+
for x_dist in range(num_cols):
49+
if sx - x_dist >= 0 and self.map[sx - x_dist] == 0:
50+
self.map[sx - x_dist] = 3
51+
if sx + x_dist < self.width and self.map[sx + x_dist] == 0:
52+
self.map[sx + x_dist] = 3
53+
54+
def count_impossibles(self):
55+
count = 0
56+
for cell in self.map:
57+
if cell == 1 or cell == 3:
58+
count += 1
59+
return count
60+
61+
def __str__(self):
62+
s = f"<{self.__class__.__name__}:\n"
63+
s += f"Row: #{self.row}, x offset: #{self.x_offset}, width: #{self.width}\n"
64+
for col in range(self.width):
65+
if self.map[col] == 0:
66+
s += "."
67+
elif self.map[col] == 1:
68+
s += "S"
69+
elif self.map[col] == 2:
70+
s += "B"
71+
elif self.map[col] == 3:
72+
s += "#"
73+
s += "\n>"
74+
return s
75+
76+
77+
lines = [line.strip() for line in open('15.input').readlines()]
78+
79+
obj = Map(lines, 2_000_000)
80+
print(obj.count_impossibles())

‎15-1.rb‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Map
6+
def initialize(input, row)
7+
sensor_regexp = /^Sensor at x=(?<sx>[[:digit:]-]+), y=(?<sy>[[:digit:]-]+): closest beacon is at x=(?<bx>[[:digit:]-]+), y=(?<by>[[:digit:]-]+)$/
8+
@row = row
9+
10+
min_x = 2**30 - 1
11+
max_x = 0
12+
max_dist = 0
13+
sensors = []
14+
beacons = []
15+
16+
input.each do |sensor|
17+
m = sensor_regexp.match(sensor)
18+
sx = m['sx'].to_i
19+
sy = m['sy'].to_i
20+
bx = m['bx'].to_i
21+
by = m['by'].to_i
22+
min_x = [min_x, sx, bx].min
23+
max_x = [max_x, sx, bx].max
24+
max_dist = [max_dist, (sx - bx).abs + (sy - by).abs + 1].max
25+
sensors.append [sx, sy]
26+
beacons.append [bx, by]
27+
end
28+
29+
@width = (max_x - min_x) + 2 * max_dist + 1
30+
@x_offset = min_x - max_dist
31+
@map = Numo::UInt8.zeros(@width)
32+
33+
sensors.size.times do |index|
34+
sx = sensors[index][0] - @x_offset
35+
sy = sensors[index][1]
36+
bx = beacons[index][0] - @x_offset
37+
by = beacons[index][1]
38+
@map[sx] = 1 if sy == @row
39+
@map[bx] = 2 if by == @row
40+
41+
manhattan_distance = (sx - bx).abs + (sy - by).abs
42+
next unless (sy - @row).abs < manhattan_distance
43+
44+
num_cols = manhattan_distance - (sy - @row).abs + 1
45+
num_cols.times do |x_dist|
46+
@map[sx - x_dist] = 3 if sx - x_dist >= 0 and @map[sx - x_dist].zero?
47+
@map[sx + x_dist] = 3 if sx + x_dist < @width and @map[sx + x_dist].zero?
48+
end
49+
end
50+
end
51+
52+
def count_impossibles
53+
count = 0
54+
@map.each { |cell| count += 1 if [1, 3].include? cell }
55+
count
56+
end
57+
58+
def to_s
59+
s = "<#{self.class}:\n"
60+
s += "Row: #{@row}, x offset: #{@x_offset}, width: #{@width}\n"
61+
@width.times do |col|
62+
s += case @map[col]
63+
when 0
64+
'.'
65+
when 1
66+
'S'
67+
when 2
68+
'B'
69+
when 3
70+
'#'
71+
end
72+
end
73+
s += "\n>"
74+
s
75+
end
76+
77+
alias inspect to_s
78+
end
79+
80+
input = File.read('15.input').lines.map(&:strip)
81+
82+
map = Map.new(input, 2_000_000)
83+
84+
print map.count_impossibles, "\n"

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /