-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.rb
95 lines (76 loc) · 2.03 KB
/
day11.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Monkey
attr_reader :inspections, :items, :test
attr_accessor :magic
def initialize(worry_level)
@worry_level = worry_level
@items = []
@operation = nil
@op_arg = nil
@test = nil
@test_true_target = nil
@test_false_target = nil
@inspections = 0
@magic = nil
end
def perform_turn(monkeys)
items = @items.dup
items.each do |item|
item = inspect(item)
item /= @worry_level unless @worry_level == 1
target = if (item % @test).zero?
@test_true_target
else
@test_false_target
end
throw_item(monkeys[target], item)
end
@items = []
end
def inspect(item)
@inspections += 1
arg = if @op_arg == 'old'
item
else
@op_arg.to_i
end
if @operation == '+'
item += arg
elsif @operation == '*'
item *= arg
end
item % @magic
end
def throw_item(monkey, item)
monkey.catch_item(item)
end
def catch_item(item)
@items << item
end
def load(lines)
lines.shift
_, items = lines.shift.split(':').map(&:strip)
@items = items.split(',').map(&:to_i)
_, op = lines.shift.strip.split('old ')
@operation, @op_arg = op.split(' ')
@test = lines.shift.split('by ')[1].to_i
@test_true_target = lines.shift.split('monkey ')[1].strip.to_i
@test_false_target = lines.shift.split('monkey ')[1].strip.to_i
end
end
def rounds(nb, monkeys_data, worry_level)
monkeys = Array.new(monkeys_data.length) { Monkey.new(worry_level) }
monkeys.each_index { |i| monkeys[i].load(monkeys_data[i].split("\n")) }
magic = monkeys.reduce(1) { |prev, monkey| prev * monkey.test }
monkeys.each { |m| m.magic = magic }
nb.times do
monkeys.each do |m|
m.perform_turn(monkeys)
end
end
mb = monkeys.map(&:inspections).max(2)
mb[0] * mb[1]
end
file = File.open('input11.txt')
monkeys_data = file.read.split("\n\n")
puts rounds(20, monkeys_data, 3) # 78678
puts rounds(10_000, monkeys_data, 1) # 15333249714