Add solution for 2015 day 17 part 1
This commit is contained in:
parent
5517d18918
commit
b16cc7dacb
20
2015/day-17/inputs/puzzle.txt
Normal file
20
2015/day-17/inputs/puzzle.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
11
|
||||||
|
30
|
||||||
|
47
|
||||||
|
31
|
||||||
|
32
|
||||||
|
36
|
||||||
|
3
|
||||||
|
1
|
||||||
|
5
|
||||||
|
3
|
||||||
|
32
|
||||||
|
36
|
||||||
|
15
|
||||||
|
11
|
||||||
|
46
|
||||||
|
26
|
||||||
|
28
|
||||||
|
1
|
||||||
|
19
|
||||||
|
3
|
5
2015/day-17/inputs/test.txt
Normal file
5
2015/day-17/inputs/test.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
20
|
||||||
|
5
|
||||||
|
15
|
||||||
|
10
|
||||||
|
5
|
44
2015/day-17/main.rb
Executable file
44
2015/day-17/main.rb
Executable file
@ -0,0 +1,44 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "pathname"
|
||||||
|
require "set"
|
||||||
|
|
||||||
|
Container = Struct.new(:id, :cap)
|
||||||
|
|
||||||
|
# quite inefficient, probably needs a DP solution
|
||||||
|
def combinations(amount, containers, stack = [], results = Set[])
|
||||||
|
stack_sum = stack.sum { |x| x.cap }
|
||||||
|
|
||||||
|
if stack_sum > amount
|
||||||
|
return results
|
||||||
|
elsif stack_sum == amount
|
||||||
|
results.add(stack.dup)
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
|
return results if containers.empty?
|
||||||
|
|
||||||
|
containers.each_with_index do |container, idx|
|
||||||
|
combinations(amount, containers[(idx+1)..], [*stack, container], results)
|
||||||
|
end
|
||||||
|
|
||||||
|
combinations(amount, containers[1..], [], results)
|
||||||
|
end
|
||||||
|
|
||||||
|
def main(amount, file_paths)
|
||||||
|
amount = amount.to_i
|
||||||
|
|
||||||
|
file_paths.each do |file_path|
|
||||||
|
puts "Files: #{file_path}"
|
||||||
|
|
||||||
|
containers = file_path.each_line.with_index.map { |x, i| Container.new(i, x.to_i) }.freeze
|
||||||
|
|
||||||
|
result = combinations(amount, containers)
|
||||||
|
|
||||||
|
solution1 = result.size
|
||||||
|
puts " Solution 1: #{solution1}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
main(ARGV[0], ARGV[1..].map { |x| Pathname(x) })
|
Loading…
Reference in New Issue
Block a user