Skip to content

Commit

Permalink
Add 2024, day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
bewuethr committed Dec 7, 2024
1 parent d560041 commit a3cf2ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 2024/day07/day07a
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby

lines = File.readlines(ARGV[0]).map(&:chomp)

equations = lines.map do |line|
test_val = line.split(":").first.to_i
numbers = line.split(":").last.strip.split.map(&:to_i)
[test_val, numbers]
end

total = equations.select do |test_val, numbers|
ops = [:+, :*].repeated_permutation(numbers.length - 1).to_a
ops.any? do |perm|
numbers.each_with_index.reduce do |(acc, _), (n, idx)|
acc.send(perm[idx - 1], n)
end.eql?(test_val)
end
end.sum { _1.first }

puts total
24 changes: 24 additions & 0 deletions 2024/day07/day07b
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby

class Integer
def concat(n) = [self, n].join.to_i
end

lines = File.readlines(ARGV[0]).map(&:chomp)

equations = lines.map do |line|
test_val = line.split(":").first.to_i
numbers = line.split(":").last.strip.split.map(&:to_i)
[test_val, numbers]
end

total = equations.select do |test_val, numbers|
ops = [:+, :*, :concat].repeated_permutation(numbers.length - 1).to_a
ops.any? do |perm|
numbers.each_with_index.reduce do |(acc, _), (n, idx)|
acc.send(perm[idx - 1], n)
end.eql?(test_val)
end
end.sum { _1.first }

puts total

0 comments on commit a3cf2ba

Please sign in to comment.