-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-2.rb
executable file
·99 lines (78 loc) · 2.02 KB
/
15-2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env ruby
class Intervals
attr_accessor :intervals
def initialize
@intervals = []
end
def size
@intervals.size
end
def add(interval)
new = []
@intervals.append interval
@intervals.sort_by! { |i| i[0] }
new.append @intervals[0]
@intervals[1..].each do |i|
if new.last[0] <= i[0] and i[0] <= new.last[1]
new.last[1] = [new.last[1], i[1]].max
elsif new.last[1] + 1 == i[0]
new.last[1] = [new.last[1], i[1]].max
else
new.append i
end
end
@intervals = new
end
def to_s
@intervals.inspect
end
alias inspect to_s
end
class Map
def initialize(input)
sensor_regexp = /^Sensor at x=(?<sx>[[:digit:]-]+), y=(?<sy>[[:digit:]-]+): closest beacon is at x=(?<bx>[[:digit:]-]+), y=(?<by>[[:digit:]-]+)$/
@dim = 4_000_001
sensors = []
beacons = []
input.each do |sensor|
m = sensor_regexp.match(sensor)
sensors.append [m['sx'].to_i, m['sy'].to_i]
beacons.append [m['bx'].to_i, m['by'].to_i]
end
@rows = []
@dim.times do |row|
int = Intervals.new
sensors.size.times do |index|
sx = sensors[index][0]
sy = sensors[index][1]
bx = beacons[index][0]
by = beacons[index][1]
manhattan_distance = (sx - bx).abs + (sy - by).abs
next unless (sy - row).abs < manhattan_distance
left = sx - (manhattan_distance - (sy - row).abs)
right = sx + (manhattan_distance - (sy - row).abs)
int.add [left, right]
end
@rows.append int
end
end
def find_beacon
@dim.times do |row|
next unless @rows[row].size == 2
return [@rows[row].intervals[0][1] + 1, row]
end
end
def to_s
s = "<#{self.class}:\n"
@rows.each_with_index do |row, index|
s += index.to_s + ': ' + row.to_s + "\n"
end
s += '>'
s
end
alias inspect to_s
end
input = File.read('15.input').lines.map(&:strip)
map = Map.new(input)
beacon = map.find_beacon
print beacon[0] * 4_000_000 + beacon[1], "\n"