Update solution for 2015 day 17
Remove redundant recursion step in solution for part 1 This speeds up the solution and makes the marshalling unnecessary
This commit is contained in:
parent
59cdbf724b
commit
5382b4b0f1
1
2015/day-17/.gitignore
vendored
1
2015/day-17/.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
*.rbdump
|
|
@ -6,39 +6,18 @@ require "set"
|
|||||||
|
|
||||||
Container = Struct.new(:id, :cap)
|
Container = Struct.new(:id, :cap)
|
||||||
|
|
||||||
# dumps result of previous solution to avoid recomputing
|
|
||||||
def memo(file)
|
|
||||||
dump_path = "#{file.basename}.rbdump"
|
|
||||||
|
|
||||||
if File.exist?(dump_path)
|
|
||||||
File.open(dump_path, "rb") { |df| Marshal.load(df) }
|
|
||||||
else
|
|
||||||
File.open(dump_path, "wb") do |dumpfile|
|
|
||||||
result = yield
|
|
||||||
dumpfile.write(Marshal.dump(result))
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# quite inefficient, probably needs a DP solution
|
|
||||||
def combinations(amount, containers, stack = [], results = Set[])
|
def combinations(amount, containers, stack = [], results = Set[])
|
||||||
stack_sum = stack.sum { |x| x.cap }
|
next_sum = stack.sum(&:cap)
|
||||||
|
|
||||||
if stack_sum > amount
|
|
||||||
return results
|
|
||||||
elsif stack_sum == amount
|
|
||||||
results.add(stack.dup)
|
|
||||||
return results
|
|
||||||
end
|
|
||||||
|
|
||||||
|
return results if next_sum > amount
|
||||||
|
return results.add(stack.dup) if next_sum == amount
|
||||||
return results if containers.empty?
|
return results if containers.empty?
|
||||||
|
|
||||||
containers.each_with_index do |container, idx|
|
containers.each_with_index do |container, idx|
|
||||||
combinations(amount, containers[(idx+1)..], [*stack, container], results)
|
combinations(amount, containers[(idx+1)..], [*stack, container], results)
|
||||||
end
|
end
|
||||||
|
|
||||||
combinations(amount, containers[1..], [], results)
|
results
|
||||||
end
|
end
|
||||||
|
|
||||||
def main(amount, file_paths)
|
def main(amount, file_paths)
|
||||||
@ -49,13 +28,15 @@ def main(amount, file_paths)
|
|||||||
|
|
||||||
containers = file_path.each_line.with_index.map { |x, i| Container.new(i, x.to_i) }.freeze
|
containers = file_path.each_line.with_index.map { |x, i| Container.new(i, x.to_i) }.freeze
|
||||||
|
|
||||||
result = memo(file_path) { combinations(amount, containers) }
|
result = combinations(amount, containers)
|
||||||
|
|
||||||
solution1 = result.size
|
solution1 = result.size
|
||||||
puts " Solution 1: #{solution1}"
|
puts " Solution 1: #{solution1}"
|
||||||
|
|
||||||
solution2 = result.min_by { |x| x.size }.size
|
solution2 = result
|
||||||
.then { |mincomb| result.select { |x| x.size == mincomb } }
|
.group_by { |x| x.size }
|
||||||
|
.min { |(a, _), (b, _)| a <=> b }
|
||||||
|
.last
|
||||||
.size
|
.size
|
||||||
puts " Solution 2: #{solution2}"
|
puts " Solution 2: #{solution2}"
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user