38 lines
852 B
Ruby
38 lines
852 B
Ruby
|
#!/usr/bin/env ruby
|
||
|
# frozen_string_literal: true
|
||
|
|
||
|
# input = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
|
||
|
|
||
|
input = File.read('./input')
|
||
|
# Part 1
|
||
|
solution = input.scan(/mul\(\d{1,3},\d{1,3}\)/).map do |instruction|
|
||
|
instruction.scan(/\d{1,3},\d{1,3}/)
|
||
|
end.flatten.map { |pair| pair.split(',').map(&:to_i) }.map { |x, y| x * y }.sum
|
||
|
|
||
|
puts solution
|
||
|
|
||
|
# Part 2
|
||
|
|
||
|
instructions = input.scan(/(mul\(\d{1,3},\d{1,3}\))|(don't)|(do)/).flatten.compact
|
||
|
|
||
|
delete = false
|
||
|
instructions.map! do |instruction|
|
||
|
case instruction
|
||
|
when 'don\'t'
|
||
|
delete = true
|
||
|
nil
|
||
|
when 'do'
|
||
|
delete = false
|
||
|
nil
|
||
|
else
|
||
|
if delete
|
||
|
nil
|
||
|
else
|
||
|
instruction.scan(/\d{1,3},\d{1,3}/).flatten.map { |pair| pair.split(',').map(&:to_i) }.map { |x, y| x * y }
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
solution = instructions.flatten.compact.sum
|
||
|
puts solution
|