From fc5821b2770083d01eeec8e6c3095ddcb68173a3 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 3 Dec 2023 14:36:49 -0500 Subject: [PATCH] [2023][ruby][3.2] --- 2023/ruby/day_03.rb | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/2023/ruby/day_03.rb b/2023/ruby/day_03.rb index 7812d42..374772c 100644 --- a/2023/ruby/day_03.rb +++ b/2023/ruby/day_03.rb @@ -1,3 +1,5 @@ +CurrentNumber = Data.define(:coords, :acc) + cur_num = nil nums = {} syms = {} @@ -6,13 +8,14 @@ ARGF.readlines(chomp: true).each.with_index do |row, y| case c when /\d/ if cur_num - cur_num << c + cur_num.coords << [y,x] + cur_num.acc << c else - cur_num = c + cur_num = CurrentNumber.new([[y,x]], c) end else if cur_num - nums[[y,x-cur_num.length]] = cur_num.to_i + nums.merge!(cur_num.coords.to_h { [_1, cur_num.acc.to_i] }) cur_num = nil end @@ -23,21 +26,37 @@ ARGF.readlines(chomp: true).each.with_index do |row, y| end if cur_num - nums[[y, row.length-cur_num.length]] = cur_num.to_i + nums.merge!(cur_num.coords.to_h { [_1, cur_num.acc.to_i] }) cur_num = nil end end -p nums - .filter_map {|(y,x), num| +# part one +p syms + .flat_map {|(y,x), _| dy = (-1..1).to_a - dx = (-1..num.digits.length).to_a - if dy.product(dx) - .map {|dy,dx| [y+dy,x+dx] } - .any? { syms.has_key?(_1) } - num + dx = (-1..1).to_a + dy.product(dx) + .map {|dy,dx| [y+dy,x+dx] } + .filter_map { nums.fetch(_1, nil) } + .uniq # lol, hack + } + .sum + +p syms + .select { _2 == ?* } + .filter_map {|(y,x), _| + dy = (-1..1).to_a + dx = (-1..1).to_a + parts = dy.product(dx) + .map {|dy,dx| [y+dy,x+dx] } + .filter_map { nums.fetch(_1, nil) } + .uniq # lol, hack + + if parts.length == 2 + parts else nil end } - .sum + .sum { _1.inject(:*) }