-
Notifications
You must be signed in to change notification settings - Fork 1
/
14-1.rb
executable file
·81 lines (70 loc) · 1.36 KB
/
14-1.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
#!/usr/bin/env ruby
require 'numo/narray'
class Dish
attr_accessor :map
def initialize(input)
@height = input.size
@width = input[0].size
@map = Numo::UInt8.zeros(@height, @width)
@height.times do |y|
@width.times do |x|
@map[y, x] = 1 if input[y][x] == 'O'
@map[y, x] = 2 if input[y][x] == '#'
end
end
end
def load
load = 0
@height.times do |y|
@width.times do |x|
if @map[y, x] == 1
load += @height - y
end
end
end
load
end
def move_north!(start_y, x)
end_y = start_y
(start_y - 1).downto(0) do |check_y|
break unless map[check_y, x].zero?
end_y = check_y
end
map[start_y, x] = 0
map[end_y, x] = 1
end_y
end
def tilt_north!
@height.times do |y|
@width.times do |x|
if @map[y, x] == 1
move_north!(y, x)
end
end
end
end
def inspect
to_s
end
def to_s
s = "<#{self.class}:\n"
@height.times do |y|
@width.times do |x|
s += case @map[y, x]
when 0
'.'
when 1
'O'
when 2
'#'
end
end
s += "\n"
end
s += ">"
end
end
input = File.read('14.input').lines.map(&:strip).map(&:chars)
dish = Dish.new(input)
dish.tilt_north!
print dish.load, "\n"