-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.rb
197 lines (159 loc) · 3.47 KB
/
6.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'pry'
input = File.read('./6.input')
def next_id
if @id
@id = @id.succ
else
@id = "😀"
end
end
class Point
attr_reader :x, :y, :id, :closest_danger
def self.parse(line)
x, y = line.split(',')
new(Integer(x), Integer(y))
end
def initialize(x, y)
@x = x
@y = y
@danger = false
end
def to_s
return id || '❔'
prefix = @danger ? "D" : "P"
"#{prefix}<#{x}, #{y}>"
end
def to_str
to_s
end
def danger!(id)
@id = id
@danger = true
end
def danger?
@danger
end
def distance_to(other)
(self.x - other.x).abs + (self.y - other.y).abs
end
# Given a list of points, which is the closest? In the case of a tie, there is no closest.
def closest(points)
closest = points.sort_by {|point| self.distance_to(point) }
if distance_to(closest[0]) != distance_to(closest[1])
@id = closest[0].id
@closest_danger = closest[0]
return closest[0]
end
end
def closest_danger=(point)
return if @disqualified
if @closest_danger
disqualify!
return
end
@closest_danger = point
end
def disqualify!
@closest_danger = nil
@id = nil
@disqualified = true
end
end
class Grid
def self.construct(matrix)
grid = Grid.new(matrix[0].length, matrix.length)
grid.instance_variable_set :@matrix, matrix
grid
end
def initialize(x, y)
@x = x
@y = y
@matrix = Array.new(y) { Array.new(x) }
end
def set(x, y, value)
@matrix[y][x] = value
end
def get(x, y)
@matrix[y][x]
end
def add(point)
set(point.x, point.y, point)
point
end
def view(x, y)
Grid.construct(@matrix[0, x].map {|row| row[0, y] })
end
def edges
cells = []
first, *middle, last = @matrix
cells = cells + first + last
middle.each do |row|
cells << row.first
cells << row.last
end
cells
end
def each
@matrix.each_with_index do |row, y|
row.each_with_index do |cell, x|
if cell
yield cell
else
set(x, y, Point.new(x, y))
yield get(x, y)
end
end
end
end
def to_s
s = ''
@matrix.each do |row|
row.each do |cell|
if cell
s << cell.to_s
else
s << '.'
end
end
s << "\n"
end
return s
end
end
g = Grid.new(10,20)
g.set(0,0, '1')
g.set(1,1, '2')
g.set(2,2, '3')
g.set(3,0, '4')
if g.get(3,0) != '4'
raise 'wrong value gotten'
end
puts g
puts
subgrid = g.view(3,2)
puts subgrid
puts
grid = Grid.new(400, 400)
danger_points = []
input.each_line do |line|
danger_points << point = Point.parse(line)
grid.add(point).danger!(next_id)
end
results = Hash.new { 0 }
# Iterate over each cell in the grid and calculate its distance to all danger points, recording the closest one.
grid.each do |cell|
next if cell.danger?
if closest = cell.closest(danger_points)
#puts "Closest for #{cell.x},#{cell.y} is #{closest}"
results[closest] += 1
else
#puts "Disqualifying #{cell} as it is equidistant to multiple danger points"
cell.disqualify!
end
end
# Scan the edges and accumulate which appear there, and disqualify them.
disqualified = grid.edges.map {|cell| cell.closest_danger}.uniq.compact
sorted = results.sort_by {|point, count| count}.reverse
largest_non_infinite_area = sorted.find{|item| !disqualified.include?(item[0])}.last
puts largest_non_infinite_area + 1 # Plus one for the danger space itself
#puts grid.view(400, 200)