Skip to content

Instantly share code, notes, and snippets.

@wconrad
Created December 16, 2015 13:31
Show Gist options
  • Save wconrad/f1d5c686a129eaa4626a to your computer and use it in GitHub Desktop.
Save wconrad/f1d5c686a129eaa4626a to your computer and use it in GitHub Desktop.
Advent of Code, day 16
#!/usr/bin/env ruby
# http://adventofcode.com/day/16
Sue = Struct.new(:id, :attrs)
sues = File.read("input").each_line.map do |line|
id = Integer(line[/(\d+)/])
attrs = Hash[
line.scan(/(\w+): (\d+)/).map do |key, value|
[key.to_sym, Integer(value)]
end
]
Sue.new(id, attrs)
end
KNOWN_ATTRS = {
children: 3,
cats: 7,
samoyeds: 2,
pomeranians: 3,
akitas: 0,
vizslas: 0,
goldfish: 5,
trees: 3,
cars: 2,
perfumes: 1,
}
def matches_known_attrs(sue)
sue.all? do |key, value|
KNOWN_ATTRS[key] === value
end
end
gifter_number = sues.find do |sue|
matches_known_attrs(sue.attrs)
end.id
puts gifter_number
#!/usr/bin/env ruby
# http://adventofcode.com/day/16
Sue = Struct.new(:id, :attrs)
sues = File.read("input").each_line.map do |line|
id = Integer(line[/(\d+)/])
attrs = Hash[
line.scan(/(\w+): (\d+)/).map do |key, value|
[key.to_sym, Integer(value)]
end
]
Sue.new(id, attrs)
end
def eq(target)
->(n) { n == target }
end
def gt(target)
->(n) { n > target }
end
def lt(target)
->(n) { n < target }
end
KNOWN_ATTRS = {
children: eq(3),
cats: gt(7),
samoyeds: eq(2),
pomeranians: lt(3),
akitas: eq(0),
vizslas: eq(0),
goldfish: lt(5),
trees: gt(3),
cars: eq(2),
perfumes: eq(1),
}
def matches_known_attrs(sue)
sue.all? do |key, value|
KNOWN_ATTRS[key].(value)
end
end
gifter_number = sues.find do |sue|
matches_known_attrs(sue.attrs)
end.id
puts gifter_number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment