changeset 16:a255c0d4996b

Day 11 part 1
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 13 Dec 2022 22:05:35 +0100
parents 6fd4445cbd22
children fc0bae3dddac
files 11/11.jl 11/input.txt 11/test_input.txt
diffstat 3 files changed, 144 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/11/11.jl	Tue Dec 13 22:05:35 2022 +0100
@@ -0,0 +1,61 @@
+
+const test_input = "11/test_input.txt";
+
+struct Monkey
+    items::Vector{Int}
+    operation::Function
+    test::Function
+    iftrue::Int
+    iffalse::Int
+end
+
+function test_init_monkeys()::Vector{Monkey}
+    # Manual parsing is easier for small numbers of monkeys.
+    [Monkey([79, 98], o -> o*19, i -> i%23 == 0, 2, 3),
+    Monkey([54, 65, 75, 74], o -> o+6, i -> i%19 == 0, 2, 0),
+    Monkey([79, 60, 97], o -> o*o, i -> i%13 == 0, 1, 3),
+    Monkey([74], o -> o+3, i -> i%17 == 0, 0, 1)]
+end
+
+function test_monkeys()::Vector{Monkey}
+    [Monkey([72,97], o->o*13, i->i%19==0, 5, 6),
+     Monkey([55, 70, 90, 74, 95], o->o*o, i->i%7==0, 5, 0),
+     Monkey([74, 97, 66, 57], o->o+6, i->i%17==0, 1, 0),
+     Monkey([86, 54, 53], o->o+2, i->i%13==0, 1, 2),
+     Monkey([50, 65, 78, 50, 62, 99], o->o+3, i->i%11==0, 3, 7),
+     Monkey([90], o->o+4, i->i%2==0, 4, 6),
+     Monkey([88, 92, 63, 94, 96, 82, 53, 53], o->o+8, i->i%5==0, 4, 7),
+     Monkey([70, 60, 71, 69, 77, 70, 98], o->o*7, i->i%3==0, 2, 3)]
+end
+
+function engine(init::Vector{Monkey}; rounds=20)::Vector{Monkey}
+    activity = zeros(Int, length(init));
+    for r = 1:rounds
+        #@show r
+        for (i,m) in enumerate(init)
+            #@show i
+            while length(m.items) > 0
+                activity[i] += 1;
+                it = pop!(m.items);
+                it_ = m.operation(it);
+                it__ = round(Int, floor(it_));
+                t = m.test(it__);
+                #println("Inspect item of worry level $it\nNew worry level is $it_\nWorry level div. by 3 is $it__\nThrow item to $(t ? m.iftrue+1 : m.iffalse+1)");
+                if t
+                    @assert m.iftrue+1 != i
+                    push!(init[m.iftrue+1].items, it__);
+                else
+                    @assert m.iffalse+1 != i
+                    push!(init[m.iffalse+1].items, it__);
+                end
+            end
+        end
+        if r % 1000 == 0 || r == 20
+            println(r, " ", activity);
+        end
+    end
+    println(activity, " ", prod(sort(activity)[end-1:end]));
+    init
+end
+
+engine(test_init_monkeys(); rounds=10000);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/11/input.txt	Tue Dec 13 22:05:35 2022 +0100
@@ -0,0 +1,56 @@
+Monkey 0:
+  Starting items: 72, 97
+  Operation: new = old * 13
+  Test: divisible by 19
+    If true: throw to monkey 5
+    If false: throw to monkey 6
+
+Monkey 1:
+  Starting items: 55, 70, 90, 74, 95
+  Operation: new = old * old
+  Test: divisible by 7
+    If true: throw to monkey 5
+    If false: throw to monkey 0
+
+Monkey 2:
+  Starting items: 74, 97, 66, 57
+  Operation: new = old + 6
+  Test: divisible by 17
+    If true: throw to monkey 1
+    If false: throw to monkey 0
+
+Monkey 3:
+  Starting items: 86, 54, 53
+  Operation: new = old + 2
+  Test: divisible by 13
+    If true: throw to monkey 1
+    If false: throw to monkey 2
+
+Monkey 4:
+  Starting items: 50, 65, 78, 50, 62, 99
+  Operation: new = old + 3
+  Test: divisible by 11
+    If true: throw to monkey 3
+    If false: throw to monkey 7
+
+Monkey 5:
+  Starting items: 90
+  Operation: new = old + 4
+  Test: divisible by 2
+    If true: throw to monkey 4
+    If false: throw to monkey 6
+
+Monkey 6:
+  Starting items: 88, 92, 63, 94, 96, 82, 53, 53
+  Operation: new = old + 8
+  Test: divisible by 5
+    If true: throw to monkey 4
+    If false: throw to monkey 7
+
+Monkey 7:
+  Starting items: 70, 60, 71, 69, 77, 70, 98
+  Operation: new = old * 7
+  Test: divisible by 3
+    If true: throw to monkey 2
+    If false: throw to monkey 3
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/11/test_input.txt	Tue Dec 13 22:05:35 2022 +0100
@@ -0,0 +1,27 @@
+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