Skip to content

Commit

Permalink
feat: day 15 (a + b) in ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
Animeshz committed Dec 19, 2023
1 parent d7026e3 commit c52abaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions day15_a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
line = gets.chomp
seqs = line.split(',')

ans = seqs.map { |seq| seq.chars.reduce(0) { |acc, e| (17 * (acc + e.ord)) % 256 } }.sum
puts ans
20 changes: 20 additions & 0 deletions day15_b.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
line = gets.chomp

ops = line.split(',').map { |seq| /(\w+)([=-])(\d?)/.match(seq)&.captures }.map { |lab, cmd, mp|
dst = lab.chars.reduce(0) { |acc, e| (17 * (acc + e.ord)) % 256 }
[lab, cmd, mp.to_i, dst]
}

boxes = Array.new(256) { {} }
ops.each { |l, c, m, d|
case c
when '=' then boxes[d][l] = m
when '-' then boxes[d].delete(l)
end
}

ans = boxes.each_with_index.map { |box, idx|
box.to_a.each_with_index.map { |bx, i| (idx+1) * (i+1) * bx[1] }.sum
}.sum

puts ans

0 comments on commit c52abaa

Please sign in to comment.