/
day10.rb
63 lines (54 loc) · 1.16 KB
/
day10.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
# frozen_string_literal: true
require './lib/runner'
Noop = Data.define
Addx = Data.define(:val)
class Day10 < Runner
def do_puzzle1
cycles = generate_cycles(@input)
output_for(cycles, [20, 60, 100, 140, 180, 220])
end
def do_puzzle2
cycles = generate_cycles(@input)
"\n" + draw(cycles)
end
def output_for(cycles, indexes)
indexes.sum do |idx|
cycles[idx - 2] * idx
end
end
def generate_cycles(input)
x = 1
buff = []
input.each_with_object([]) do |inst, memo|
curr_register = buff.shift
if curr_register
x += curr_register
memo << x
end
buff << inst.val if inst.is_a? Addx
memo << x
end
end
def draw(cycles)
((0..39).to_a * 6).zip(cycles).map do |idx, pos|
# spr = [pos - 1, pos, pos + 1]
spr = [pos - 2, pos - 1, pos]
if spr.include?(idx)
'#'
else
'.'
end
end.each_slice(40).map(&:join).join("\n")
end
def parse(raw_input)
raw_input.split("\n").map do
inst, val = _1.split(' ')
case inst
when 'addx'
Addx.new(val.to_i)
when 'noop'
Noop.new
end
end
end
end