-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.rb
executable file
·205 lines (164 loc) · 4.82 KB
/
day05.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
198
199
200
201
202
203
204
205
#!/usr/bin/env ruby
require 'test/unit'
InputFile = Struct.new(:stacks, :instructions)
Instruction = Struct.new(:quantity, :source, :destination)
def parse_file(filename)
stacks = []
instructions = []
read_stacks = true
File.readlines(filename).map(&:chomp).each do |line|
if line == ''
read_stacks = false
next
end
stacks << line if read_stacks
instructions << line unless read_stacks
end
InputFile.new(stacks, instructions)
end
def initialize_stacks(lines)
number_of_stacks = lines[-1].strip.split(/ +/).size
stacks = Array.new(number_of_stacks + 1) { [] }
lines[0..-2].reverse.each do |line|
entries = parse_stack_line(line, number_of_stacks)
entries.each_with_index do |entry, index|
stacks[index + 1] << entry if entry != ' '
end
end
stacks
end
def parse_stack_line(line, number_of_stacks)
# input: ' [N] [C] ', 4 (leading/trailing spaces make everything save)
# output: [' ','N','C',' ']
result = []
number_of_stacks.times.map do |i|
result << line[4 * i + 1]
end
result
end
def initialize_instructions(lines)
lines.map { |line| parse_instruction_line(line) }
end
def parse_instruction_line(line)
numbers = line.scan(/\d+/).map(&:to_i)
Instruction.new(numbers[0], numbers[1], numbers[2])
end
def apply_instructions(stacks, instructions)
instructions.each do |instruction|
instruction.quantity.times do
stacks[instruction.destination].push(stacks[instruction.source].pop)
end
end
end
def apply_instructions_part2(stacks, instructions)
instructions.each do |instruction|
on_crane = []
instruction.quantity.times do
on_crane << stacks[instruction.source].pop
end
on_crane.reverse.each do |crate|
stacks[instruction.destination].push(crate)
end
end
end
def get_stack_tops(stacks)
stacks[1..].map { |s| s.last }.join
end
def move_crates(filename, is_part_one)
parsed_input = parse_file(filename)
stacks = initialize_stacks(parsed_input.stacks)
instructions = initialize_instructions(parsed_input.instructions)
if is_part_one
apply_instructions(stacks, instructions)
else
apply_instructions_part2(stacks, instructions)
end
get_stack_tops(stacks)
end
def part1(filename: 'day05.in')
is_part_one = true
move_crates(filename, is_part_one)
end
def part2(filename: 'day05.in')
is_part_one = false
move_crates(filename, is_part_one)
end
print "Part 1: #{part1}\n"
print "Part 2: #{part2}\n"
print "\n"
# Tests
class TestDay05 < Test::Unit::TestCase
@@filename = 'day05.sample'
def test_parse_file_reads_stack_lines
stacks = parse_file(@@filename).stacks
assert_equal 4, stacks.length
3.times { |i| assert_match(/^[ \[\]A-Z]+$/, stacks[i]) }
assert_match(/^[ \d]+$/, stacks[3])
end
def test_parse_file_reads_instruction_lines
instructions = parse_file(@@filename).instructions
assert_equal 4, instructions.length
4.times { |i| assert_match(/^move \d+ from \d to \d$/, instructions[i]) }
end
def test_initialize_stacks
lines = [
' [D] ',
'[N] [C] ',
'[Z] [M] [P]',
' 1 2 3 '
]
assert_equal [[], %w[Z N], %w[M C D], ['P']],
initialize_stacks(lines)
end
def test_initialize_instructions
lines = [
'move 1 from 2 to 3',
'move 1001 from 9 to 5'
]
assert_equal [
Instruction.new(1, 2, 3),
Instruction.new(1001, 9, 5)
], initialize_instructions(lines)
end
def test_apply_instructions_single
instructions = [Instruction.new(1, 2, 1)]
stacks = [[], %w[V W], %w[X Y A]]
expected_stacks = [[], %w[V W A], %w[X Y]]
apply_instructions(stacks, instructions)
assert_equal expected_stacks, stacks
end
def test_apply_instructions_multiple
instructions = [
Instruction.new(1, 3, 2),
Instruction.new(2, 2, 1)
]
# 1 2 3
stacks = [[], %w[K], %w[V W], %w[X Y A]]
# after step 1: [[], %w[K], %w[V W A], %w[X Y]]
expected_stacks = [[], %w[K A W], %w[V], %w[X Y]]
apply_instructions(stacks, instructions)
assert_equal expected_stacks, stacks
end
def test_apply_instructions_part2
instructions = [
Instruction.new(1, 3, 2),
Instruction.new(2, 2, 1)
]
# 1 2 3
stacks = [[], %w[K], %w[V W], %w[X Y A]]
# after step 1: [[], %w[K], %w[V W A], %w[X Y]]
expected_stacks = [[], %w[K W A], %w[V], %w[X Y]]
apply_instructions_part2(stacks, instructions)
assert_equal expected_stacks, stacks
end
def test_get_stack_tops
stacks = [[], %w[K A W], %w[V], %w[X Y]]
assert_equal 'WVY', get_stack_tops(stacks)
end
def test_part1
assert_equal 'CMZ', part1(filename: @@filename)
end
def test_part2
assert_equal 'MCD', part2(filename: @@filename)
end
end