diff options
author | Nat Lasseter <user@4574.co.uk> | 2022-12-15 14:39:13 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2022-12-15 14:39:13 +0000 |
commit | 9f1b752bbd96c53f974e0cca4af6c1412102e62d (patch) | |
tree | e9529bb5effa3e9ed474afe63ecd4b4b3abf8adc /day15/part2.rb | |
parent | 78dcf1d32fe821c9043148df2de888c415314e17 (diff) |
Day 15, part 2 broken.
Diffstat (limited to 'day15/part2.rb')
-rw-r--r-- | day15/part2.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/day15/part2.rb b/day15/part2.rb new file mode 100644 index 0000000..cb3219d --- /dev/null +++ b/day15/part2.rb @@ -0,0 +1,61 @@ +class Sensor + def initialize(sx, sy, bx, by) + @x = sx + @y = sy + @range = dist(bx, by) + end + + attr_reader :x, :y, :range + + def dist(x, y) + (@x - x).abs + (@y - y).abs + end + + def cover?(x, y) + dist(x, y) <= @range + end + + def edge + edges = [] + at = [@x, @y - (@range + 1)] + until at[1] == @y + edges << at + at[0] += 1 + at[1] += 1 + end + until at[0] == @x + edges << at + at[0] -= 1 + at[1] += 1 + end + until at[1] == @y + edges << at + at[0] -= 1 + at[1] -= 1 + end + until at[0] == @x + edges << at + at[0] += 1 + at[1] -= 1 + end + edges + end +end + +sensors = [] +$stdin.readlines.each do |line| + c = line.scan(/x=(-?\d+), y=(-?\d+)/).flatten.map(&:to_i) + sensors << Sensor.new(*c) +end + +max = 4_000_000 + +coord = sensors.map { |s| + s.edge.reject { |e| + e[0] < 0 || e[0] > max || + e[1] < 0 || e[1] > max || + sensors.any? { |s2| s2.cover?(*e) } + } +}.flatten(1).uniq[0] + +puts coord[0] * 4_000_000 + coord[1] |