From 7591a5dc19e1e105521b81a8d5eb76a569838d2d Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 3 Dec 2024 07:40:47 -0800 Subject: [PATCH] [2024][ruby][3.x] trolling with flip-flops --- 2024/ruby/day_03.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/2024/ruby/day_03.rb b/2024/ruby/day_03.rb index d307b67..76c9227 100644 --- a/2024/ruby/day_03.rb +++ b/2024/ruby/day_03.rb @@ -1,9 +1,22 @@ -input = DATA.read - .gsub(/don't\(\)(?~do\(\))do\(\)/, "") # part two - .scan(/mul\((\d+),(\d+)\)/) - .map { _1.map(&:to_i) } +# input = DATA.read +# .gsub(/don't\(\)(?~do\(\))do\(\)/, "") # part two +# .scan(/mul\((\d+),(\d+)\)/) +# .map { _1.map(&:to_i) } +# +# pp input.sum { _1 * _2 } -pp input.sum { _1 * _2 } +# because I felt like using a flip-flop... +input = DATA.read.scan(/(do|don't)\(\)|mul\((\d+),(\d+)\)/) +input.unshift(["do"]) + +sum = 0 +input.each do |dodont, *mul| + next unless (dodont == "do")..(dodont == "don't") + next unless dodont.nil? + a, b = mul.map(&:to_i) + sum += a * b +end +pp sum __END__ xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))