[2022][ruby][10.x]

pull/1/head
Alpha Chen 1 year ago
parent 88c3c73d2f
commit f871f28d27
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -0,0 +1,25 @@
instructions = ARGF.read.lines(chomp: true)
adds = {}
cycle = 1
instructions.each do |instruction|
cycle += case instruction
when /noop/
1
when /addx (-?\d+)/
adds[cycle+1] = $1.to_i
2
else fail instruction
end
end
xs = (0..adds.keys.max).inject([1]) { _1 << _1.last + adds.fetch(_2, 0) }
p 20.step(by: 40, to: 220).map { [_1, xs[_1]] }.sum { _1 * _2 }
puts 6.times.map {|y|
40.times.map {|x|
cycle = 40*y + x + 1
xx = xs.fetch(cycle, xs.last)
(-1..1).cover?(x - xx) ? ?# : ?.
}.join
}.join("\n")
Loading…
Cancel
Save