Created
December 11, 2022 16:16
-
-
Save cjavdev/8334e7dfef0a47076b90f1af8382d6f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Monkey | |
attr_reader :inspection_count, :test | |
@monkeys = {} | |
def self.parse(monkey_data) | |
n, i, o, t, tr, fa = monkey_data.split("\n") | |
name = n.split(/:| /)[1].to_i | |
items = i.split(': ').last.split(', ').map(&:to_i) | |
operation = o.split('= ').last | |
test = t.split(' ').last.to_i | |
success_monkey = tr.split(' ').last.to_i | |
failure_monkey = fa.split(' ').last.to_i | |
@monkeys[name] = Monkey.new( | |
items, | |
operation, | |
test, | |
success_monkey, | |
failure_monkey | |
) | |
end | |
def self.all | |
@monkeys | |
end | |
def self.round(n) | |
n.times do | |
@monkeys.values.each(&:inspect_all) | |
end | |
end | |
def self.active_monkeys | |
@monkeys.values.sort_by{ -_1.inspection_count } | |
end | |
def self.monkey_business | |
active_monkeys.take(2).map(&:inspection_count).inject(:*) | |
end | |
def self.relief | |
@monkeys.values.map(&:test).inject(:*) | |
end | |
def initialize(items, operation, test, success_monkey, failure_monkey) | |
@items = items | |
@operation = operation | |
@test = test | |
@success_monkey = success_monkey | |
@failure_monkey = failure_monkey | |
@inspection_count = 0 | |
end | |
def inspect_all | |
while @items.any? | |
insp | |
end | |
end | |
protected | |
def catch_item(item) | |
@items << item | |
end | |
private | |
def insp | |
@inspection_count += 1 | |
item = @items.shift | |
item = apply_operation(item) | |
# item /= 3 | |
item %= Monkey.relief | |
to_monkey = apply_test(item) | |
throw_item(to_monkey, item) | |
end | |
def throw_item(monkey, item) | |
Monkey.all[monkey].catch_item(item) | |
end | |
def apply_test(item) | |
item % @test == 0 ? @success_monkey : @failure_monkey | |
end | |
def apply_operation(old) | |
eval(@operation) | |
end | |
end | |
if ARGV.empty? | |
data = DATA.read | |
else | |
data = File.read(ARGV[0]) | |
end | |
data.split("\n\n").each do |monkey_data| | |
Monkey.parse(monkey_data) | |
end | |
Monkey.round(10000) | |
p Monkey.all | |
p Monkey.monkey_business | |
# p Monkey.relief | |
__END__ | |
Monkey 0: | |
Starting items: 79, 98 | |
Operation: new = old * 19 | |
Test: divisible by 23 | |
If true: throw to monkey 2 | |
If false: throw to monkey 3 | |
Monkey 1: | |
Starting items: 54, 65, 75, 74 | |
Operation: new = old + 6 | |
Test: divisible by 19 | |
If true: throw to monkey 2 | |
If false: throw to monkey 0 | |
Monkey 2: | |
Starting items: 79, 60, 97 | |
Operation: new = old * old | |
Test: divisible by 13 | |
If true: throw to monkey 1 | |
If false: throw to monkey 3 | |
Monkey 3: | |
Starting items: 74 | |
Operation: new = old + 3 | |
Test: divisible by 17 | |
If true: throw to monkey 0 | |
If false: throw to monkey 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://youtu.be/GbUmGhF-BGo