Skip to content

Commit

Permalink
Add 2024, day 9, first part
Browse files Browse the repository at this point in the history
  • Loading branch information
bewuethr committed Dec 9, 2024
1 parent 0166946 commit be4b808
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 2024/day09/day09a
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby

disk_map = File.read(ARGV[0]).strip

expanded = []
id = 0

disk_map.chars.map(&:to_i).each_with_index do |n, idx|
if idx.even?
expanded << [id] * n
id += 1
else
expanded << ["."] * n
end
end

expanded.flatten!

loop do
empty_idx = expanded.index(".")
block_idx = expanded.rindex { _1 != "." }
break unless empty_idx < block_idx
expanded[empty_idx], expanded[block_idx] = expanded[block_idx], expanded[empty_idx]
end

checksum = 0

expanded.each_with_index do |n, idx|
checksum += n * idx unless n == "."
end

puts checksum

0 comments on commit be4b808

Please sign in to comment.