Created
December 17, 2015 10:54
-
-
Save wconrad/6860f05d13effecb1187 to your computer and use it in GitHub Desktop.
Advent of Code, day 17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# http://adventofcode.com/day/17 | |
def containers_totalling(containers, target_sum) | |
Enumerator.new do |yielder| | |
(0...(2 ** containers.size)).map do |mask| | |
selected_containers = containers.select.with_index do |container, bitnum| | |
(mask & (1 << bitnum)) != 0 | |
end | |
sum = selected_containers.reduce(0, &:+) | |
yielder.yield(selected_containers) if sum == target_sum | |
end | |
end | |
end | |
def num_containers_totalling(containers, target_sum) | |
containers_totalling(containers, target_sum).count | |
end | |
def min_containers_totalling(containers, target_sum) | |
containers_totalling(containers, target_sum).map(&:size).min | |
end | |
containers = File.readlines("input").map(&:to_i) | |
target_sum = 150 | |
# part 1 | |
puts num_containers_totalling(containers, target_sum) | |
# part 2 | |
num_containers = min_containers_totalling(containers, target_sum) | |
count = containers_totalling(containers, target_sum).count do |containers| | |
containers.size == num_containers | |
end | |
puts count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment