aboutsummaryrefslogtreecommitdiff
path: root/day15/part1.rb
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2022-12-15 14:39:13 +0000
committerNat Lasseter <user@4574.co.uk>2022-12-15 14:39:13 +0000
commit9f1b752bbd96c53f974e0cca4af6c1412102e62d (patch)
treee9529bb5effa3e9ed474afe63ecd4b4b3abf8adc /day15/part1.rb
parent78dcf1d32fe821c9043148df2de888c415314e17 (diff)
Day 15, part 2 broken.
Diffstat (limited to 'day15/part1.rb')
-rw-r--r--day15/part1.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/day15/part1.rb b/day15/part1.rb
new file mode 100644
index 0000000..7dd3531
--- /dev/null
+++ b/day15/part1.rb
@@ -0,0 +1,36 @@
+class Sensor
+ def initialize(sx, sy, bx, by)
+ @x = sx
+ @y = sy
+ @range = (sx - bx).abs + (sy - by).abs
+ end
+
+ attr_reader :x, :y, :range
+end
+
+sensors = []
+beacons = []
+$stdin.readlines.each do |line|
+ c = line.scan(/x=(-?\d+), y=(-?\d+)/).flatten.map(&:to_i)
+ sensors << Sensor.new(*c)
+ beacons << c[2..3]
+end
+
+row = 2_000_000
+
+xranges = []
+sensors.each do |s|
+ dx = s.range - (s.y - row).abs
+ xranges << ((s.x - dx)..(s.x + dx)) if dx > 0
+end
+
+from = xranges.map(&:begin).min
+to = xranges.map(&:end).max
+xs = to - from + 1
+
+(from..to).each do |x|
+ xs += 1 unless xranges.any? { |xrange| xrange.include?(x) }
+end
+xs -= beacons.select { |_, y| y == row }.map { |x, _| x }.uniq.count
+
+pp xs