Skip to content

Instantly share code, notes, and snippets.

@rvedotrc
Last active August 25, 2020 20:39
Show Gist options
  • Save rvedotrc/f5e1c912b67ab53b3e0657a3dff28667 to your computer and use it in GitHub Desktop.
Save rvedotrc/f5e1c912b67ab53b3e0657a3dff28667 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# "1 rabbit saw 3 elephants while going to the river"
# - rabbit was going to the river
# - elephants were going to the river
# - rabbit and elephants were going to the river
[
{ rabbits: { count: 1, going_to_river: true }, elephants: { count: 3, going_to_river: false } },
{ rabbits: { count: 1, going_to_river: true }, elephants: { count: 3, going_to_river: true } },
{ rabbits: { count: 1, going_to_river: false }, elephants: { count: 3, going_to_river: true } },
].each do |state|
# "every elephant saw 3 monkeys going to the river"
# - monkeys were going to the river
# - monkeys were not going to the river
# and, anywhere from 3 to 9 monkeys
[false, true].each do |monkeys_going_to_the_river|
(3..9).each do |monkeys|
state = state.merge(monkeys: { count: monkeys, going_to_river: monkeys_going_to_the_river })
# "each monkey had 1 tortoise in each hand"
# Let's assume that each monkey has at most 2 hands
# (no extra limbs, and the lower limb extremeties are "feet")
# - anywhere from 0 to 2 hands, for each monkey independently
# therefore anywhere from 0 to 2n hands
(0..2 * monkeys).each do |monkey_hands|
# Anywhere from 1 (assuming at least 1 hand) tortoise, to N tortoises.
# They could all be holding the same tortoise.
tortoises_range = if monkey_hands > 0
(1..monkey_hands)
else
[0]
end
tortoises_range.each do |tortoises|
# - tortoises were going to the river [if the monkeys were going the river]
# - tortoises were being taken to the river against their will, so were not "going" of their own accord
[false, true].each do |tortoises_going_to_the_river|
next if tortoises_going_to_the_river && !monkeys_going_to_the_river
state = state.merge(tortoises: { count: tortoises, going_to_river: tortoises_going_to_the_river })
# "How many animals were going to the river?"
answer = state.values.select {|animal| animal[:going_to_river]}.map {|animal| animal[:count]}.sum
require 'json'
puts JSON.generate(state: state, answer: answer)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment