Skip to content

Instantly share code, notes, and snippets.

@crashtech
Created October 22, 2020 19:21
Show Gist options
  • Save crashtech/5684225b009e5f0d241017fc6dfb9bd0 to your computer and use it in GitHub Desktop.
Save crashtech/5684225b009e5f0d241017fc6dfb9bd0 to your computer and use it in GitHub Desktop.
Able code interview
CHECKERS = {
'cats' => :>,
'trees' => :>,
'pomeranians' => :<,
'goldfish' => :<,
}
def find_aunt(criteria, input)
input.scan(/Sue (\d+): (.*)\n/).find do |(number, data)|
break number if data.scan(/(\w+): (\d+)/).all? do |(item, value)|
value.to_i.public_send(CHECKERS[item] || :eql?, criteria[item])
end
end
end
criteria = {
'children' => 3,
'cats' => 7,
'samoyeds' => 2,
'pomeranians' => 3,
'akitas' => 0,
'vizslas' => 0,
'goldfish' => 5,
'trees' => 3,
'cars' => 2,
'perfumes' => 1,
}
puts find_aunt(criteria, <<-INPUT)
INPUT
require 'set'
MOVEMENTS = {
'^' => [ 0, -1],
'v' => [ 0, 1],
'>' => [ 1, 0],
'<' => [-1, 0],
}.freeze
def count_places(input)
operators = [[0, 0], [0, 0]].cycle
input.split('').each_with_object(Set.new([[0, 0]])) do |step, items|
items << move_operator(operators.next, step).dup
end.size
end
def move_operator(input, direction)
x, y = MOVEMENTS[direction]
input.replace([input[0] + x, input[1] + y])
end
puts count_places(<<-INPUT.gsub(/\s/, ''))
INPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment