-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-2.rb
executable file
·113 lines (94 loc) · 2.25 KB
/
13-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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env ruby
require 'numo/narray'
class Map
attr_accessor :map
attr_accessor :height
attr_accessor :width
def initialize
@height = 1
@width = 1
@map = Numo::UInt8.zeros(@width, @height)
end
def expand(x, y)
expand_height = 0
if x >= @height
expand_height = x - @height + 1
end
expand_width = 0
if y >= @width
expand_width = y - @width + 1
end
if expand_width > 0
@map = @map.append(Numo::UInt8.zeros(expand_width, @height), axis: 0)
end
if expand_height > 0
@map = @map.append(Numo::UInt8.zeros(@width + expand_width, expand_height), axis: 1)
end
@height += expand_height
@width += expand_width
end
def [](x, y)
# expand(x, y)
@map[y, x]
end
def []=(x, y, val)
expand(x, y)
@map[y, x] = val
end
def fold(axis, coord)
splits = [coord, coord + 1]
if axis == :y
up, *down = @map.split(splits, axis: 0)
down = down[-1].flipud
zeros = Numo::UInt8.zeros(up.shape[0] - down.shape[0], @height)
@map = up + zeros.append(down, axis: 0)
@width /= 2
else
left, *right = @map.split(splits, axis: 1)
right = right[-1].fliplr
zeros = Numo::UInt8.zeros(@width, left.shape[1] - right.shape[1])
@map = left + zeros.append(right, axis: 1)
@height /= 2
end
end
def count_marks
count = 0
@width.times do |y|
@height.times do |x|
count += 1 if @map[y, x].positive?
end
end
count
end
def to_s
s = "<#{self.class}:\n"
s += "Width: #{self.width}\n"
s += "Height: #{self.height}\n"
@width.times do |y|
@height.times do |x|
s += @map[y, x].zero? ? '.' : '#'
end
s += "\n"
end
s += '>'
s
end
def inspect
to_s
end
end
coord_format = /^(?<x>[[:digit:]]+),(?<y>[[:digit:]]+)$/
fold_format = /^fold along (?<axis>[xy])=(?<coord>[[:digit:]]+)$/
input = File.read('13.input').lines.map(&:strip)
map = Map.new
input.each do |line|
if (coords = coord_format.match line)
map[coords['x'].to_i, coords['y'].to_i] = 1
elsif (fold = fold_format.match line)
map.fold(fold['axis'].to_sym, fold['coord'].to_i)
elsif line == ''
else
print "Unknown instruction #{line}\n"
end
end
print map, "\n"