Skip to content

Commit f94c70d

Browse files
committed
day 7, part 1
1 parent 59e2ac0 commit f94c70d

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

2024/ruby/day07.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require_relative 'day'
2+
3+
class Day07 < Day # >
4+
5+
# @example
6+
# day.part1 #=> 3749
7+
def part1
8+
input
9+
.split("\n")
10+
.map { |line| line.scan(/\d+/).map(&:to_i) }
11+
.map { |nums| [ nums[0] , nums[1..-1] ] }
12+
.select { |test_value, operands|
13+
operator_combos = [:+, :*].repeated_permutation(operands.length - 1)
14+
operator_combos.map { |combo|
15+
combo
16+
.each_with_index
17+
.reduce(operands[0]) { |result, (op, idx)|
18+
result = [ result, operands[idx+1] ].reduce(op)
19+
}
20+
}
21+
.any? { |result| result == test_value }
22+
}
23+
.map { |test_value, _| test_value }
24+
.reduce(&:+)
25+
end
26+
27+
# @example
28+
# day.part2 #=> 'how are you'
29+
def part2
30+
end
31+
32+
EXAMPLE_INPUT = File.read("../inputs/day07-example-input.txt")
33+
end

0 commit comments

Comments
 (0)