-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday.rb
162 lines (131 loc) · 3.31 KB
/
day.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
require "debug"
require "benchmark"
class Day
attr_accessor :mode, :day, :matrix, :nr_of_rows, :nr_of_columns
def initialize(day)
self.day = day
self.mode = :test
reset
end
def reset
end
def parse_input
raise NotImplementedError
end
def solve
{ 1 => :solution_one, 2 => :solution_two }.each do |idx, selector|
solutions = %i[test real].collect do |mode|
self.mode = mode
reset
parse_input
send(selector)
end
puts "Dag #{@day}, deel #{idx}: #{solutions.join(", ")}"
end
end
def file_name
fn = ("#{__dir__}/day_%02d/input_#{mode}.txt" % self.day)
return fn if File.exist?(fn)
"./input/day_%02d_#{mode == :test ? 1 : 2}.txt" % self.day
end
def read_lines
File.readlines(file_name).collect(&:strip)
end
def read_matrix
rows = read_lines
@matrix = rows.collect { |row| row.strip.chars }
@nr_of_rows = @matrix.size
@nr_of_columns = @matrix.first.size
@matrix
end
def matrix_elements
return [] unless @matrix
(0..nr_of_columns - 1).flat_map do |x|
(0..nr_of_rows - 1).collect do |y|
MatrixElement.new(x, y, @matrix[x][y])
end
end
end
def matrix_contains?(x, y)
x.between?(0, nr_of_columns - 1) && y.between?(0, nr_of_rows - 1)
end
def matrix_each
(0..nr_of_columns - 1).each do |x|
(0..nr_of_rows - 1).each do |y|
yield x, y
end
end
end
end
class MatrixElement
attr_accessor :x, :y, :value
def initialize(x, y, value)
@x, @y, @value = x, y, value
end
end
class MatrixElement
attr_accessor :x, :y, :value
def initialize(x, y, value)
@x, @y, @value = x, y, value
end
end
class Map
attr_accessor :locations, :nr_of_rows, :nr_of_columns
def self.new_from_input_lines(lines, &value_block)
arrays = lines.collect { |l| l.strip.chars.collect { |input| value_block ? value_block.call(input) : input } }
self.new(arrays)
end
def self.new_from_dimensions(columns, rows, initial_value: nil)
self.new(Array.new(rows).collect { |_| Array.new(columns, initial_value.dup) })
end
def initialize(arrays)
@locations = []
@nr_of_rows = arrays.size
@nr_of_columns = arrays.first.size
(0..nr_of_rows - 1).each do |y|
@locations << []
(0..nr_of_columns - 1).each do |x|
@locations.last << Location.new(x, y, arrays[y][x], self)
end
end
end
def locations_flattened
@locations.flatten
end
def location_at(x, y)
return nil unless x.between?(0, @nr_of_columns - 1)
return nil unless y.between?(0, @nr_of_rows - 1)
self.locations[y][x]
end
def print
(0..nr_of_rows - 1).each do |y|
line = ""
(0..nr_of_columns - 1).each do |x|
line += location_at(x, y).value
end
puts line
end
end
end
class Location
attr_accessor :x, :y, :value, :map
def initialize(x, y, value, map)
@x, @y, @value, @map = x, y, value, map
end
def neighbours(diagonals: false)
possible = [[-1, 0], [1, 0], [0, -1], [0, 1]]
possible = possible + [[-1, -1], [1, 1], [1, -1], [-1, 1]] if diagonals
possible
.collect { |a| @map.location_at(x + a.first, y + a.last) }
.compact
end
def ==(other)
x == other.x && y == other.y
end
def to_s
"(#{x},#{y}) : #{value}"
end
def inspect
to_s
end
end