From 9f1b752bbd96c53f974e0cca4af6c1412102e62d Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Thu, 15 Dec 2022 14:39:13 +0000 Subject: Day 15, part 2 broken. --- day15/part1.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 day15/part1.rb (limited to 'day15/part1.rb') 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 -- cgit v1.2.1