Skip to content

Instantly share code, notes, and snippets.

@dougdroper
Last active December 8, 2020 12:17
Show Gist options
  • Save dougdroper/9b99847e5d1edecba24cb0bad81195f6 to your computer and use it in GitHub Desktop.
Save dougdroper/9b99847e5d1edecba24cb0bad81195f6 to your computer and use it in GitHub Desktop.
day 5 (1 and 2)
values = File.read('input2.txt')
def binary(range, rows, mLetter='F')
mid = (range.last - range.first) / 2.0
return rows.first.match(/F|L/) ? range.first : range.last if rows.count == 1
letter = rows.shift
letter == mLetter ? binary((range.first..(range.first + mid.floor)), rows, mLetter) : binary(((range.first + mid.ceil)..range.end), rows, mLetter)
end
a = values.split(/\n/).map do |line|
value = line.split(//)
(binary((0..127), value.first(7)) * 8) + binary((0..7), value.last(3), 'L')
end
puts (a.sort.first..a.sort.last).to_a - a
values = File.read('input2.txt')
a = values.split(/\n\n/).map do |all_groups|
group = all_groups.split(/\n/).map {|p| p.split(//) }
f = group.first
f.select { |a| group.all? { |per| per.include?(a) } }.count
end.sum
puts a
def bag_holder(type, bag_rule)
can_hold_gold = bag_rule.match(/^(\w+ \w+) .* \d+ #{type}/)&.captures
end
def get_bags(type, found=[], belongs_to='')
found << type
bags = values2.split(/\n/).map do |bag_rule|
bag_holder(type, bag_rule)
end.compact.flatten
(bags - found).flat_map do |bag|
get_bags(bag, found, type)
end
found
end
puts (get_bags('shiny gold') - ['shiny gold']).uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment