Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 17, 2015 10:54
Show Gist options
  • Save wconrad/6860f05d13effecb1187 to your computer and use it in GitHub Desktop.
Save wconrad/6860f05d13effecb1187 to your computer and use it in GitHub Desktop.
Advent of Code, day 17
#!/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