AOC day 11 (part 1 works, part 2 broken)

This commit is contained in:
2022-12-11 23:25:29 +00:00
parent 2a7d8e927d
commit d232576b64
8 changed files with 178 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
-lib kiss
--macro kiss.KissFrontend.dsl("monkeys", "src/year2022/MonkeyDSL.kiss")
-cp src
--main Main
--interp

View File

@@ -10,3 +10,6 @@
(defMacro dayTodo [num]
`(day ,num (print "TODO")))
(defMacro assertEquals [expected actual]
`(assert (= ,expected ,actual)))

View File

@@ -0,0 +1,31 @@
(importAs year2022.inputs.Day11Example MonkeysExample)
(importAs year2022.inputs.Day11 Monkeys)
(MonkeysExample.main)
(Monkeys.main)
(doFor _ (range 20)
(MonkeysExample.round true)
(Monkeys.round true))
(doFor [id monkey] (enumerate MonkeysExample.monkeys)
(print "${id}: $monkey.items (${monkey.inspections} inspections)"))
(function :Float monkeyBusiness [:Array<Monkey> monkeys]
(apply * ~(.slice (sort (for monkey monkeys monkey.inspections)) -2)))
(assertEquals 10605 (monkeyBusiness MonkeysExample.monkeys))
(assertEquals 118674 (monkeyBusiness Monkeys.monkeys))
(MonkeysExample.main)
(MonkeysExample.round false)
(doFor [id monkey] (enumerate MonkeysExample.monkeys)
(print "${id}: $monkey.items (${monkey.inspections} inspections)"))
(MonkeysExample.main)
(Monkeys.main)
(doFor _ (range 10000)
(MonkeysExample.round false)
(Monkeys.round false))
(assertEquals 2713310158 ~(monkeyBusiness MonkeysExample.monkeys))

View File

@@ -0,0 +1,9 @@
package year2022;
typedef Monkey = {
items:Array<Float>,
operation: Float->Float,
testAndThrow: Float->Int,
inspections:Int,
testDenominator:Int
};

View File

@@ -0,0 +1,51 @@
(import year2022.Monkey)
(var :Array<Monkey> monkeys [])
(defReaderMacro "Monkey " [stream]
(let [id (Std.parseInt (expect stream "monkey id" takeUntilAndDrop ":"))
items {
(stream.takeUntilAndDrop "Starting items: ")
(.map (.split (expect stream "item list" takeLine) ", ") ->i (Prelude.symbol "${i}.0"))
}
operationBody {
(stream.takeUntilAndDrop "Operation: new = ")
(expect stream "operation" takeLine)
}
testDenominator {
(stream.takeUntilAndDrop "Test: divisible by ")
(Std.parseInt (expect stream "divisibility specifier" takeLine))
}
trueMonkey {
(stream.takeUntilAndDrop "If true: throw to monkey ")
(Std.parseInt (expect stream "true monkey target" takeLine))
}
falseMonkey {
(stream.takeUntilAndDrop "If false: throw to monkey ")
(Std.parseInt (expect stream "false monkey target" takeLine))
}]
`(setNth monkeys ,id
(object
items
,items
operation
->old ,(ReaderExp.RawHaxe operationBody)
testDenominator
,testDenominator
testAndThrow
->item (if (= 0 (% item ,testDenominator))
,trueMonkey
,falseMonkey)
inspections 0))))
(function turn [:Monkey monkey :Bool part1]
(set monkey.items (monkey.items.map monkey.operation))
(set monkey.items (for item monkey.items (if part1
(Std.int (/ item 3))
item)))
(doFor item (monkey.items.copy)
(+= monkey.inspections 1)
(monkey.items.shift)
(.push .items (nth monkeys (monkey.testAndThrow item)) item)))
(function round [:Bool part1]
(doFor monkey monkeys (turn monkey part1)))

View File

@@ -13,7 +13,7 @@
(dayTodo 8)//(day 8 (load "Day8.kiss"))
(dayTodo 9)//(day 9 (load "Day9.kiss"))
(dayTodo 10)//(day 10 (load "Day10.kiss"))
(dayTodo 11)//(day 11 (load "Day11.kiss"))
(day 11 (load "Day11.kiss"))
(dayTodo 12)//(day 12 (load "Day12.kiss"))
(dayTodo 13)//(day 13 (load "Day13.kiss"))
(dayTodo 14)//(day 14 (load "Day14.kiss"))

View File

@@ -0,0 +1,55 @@
Monkey 0:
Starting items: 85, 79, 63, 72
Operation: new = old * 17
Test: divisible by 2
If true: throw to monkey 2
If false: throw to monkey 6
Monkey 1:
Starting items: 53, 94, 65, 81, 93, 73, 57, 92
Operation: new = old * old
Test: divisible by 7
If true: throw to monkey 0
If false: throw to monkey 2
Monkey 2:
Starting items: 62, 63
Operation: new = old + 7
Test: divisible by 13
If true: throw to monkey 7
If false: throw to monkey 6
Monkey 3:
Starting items: 57, 92, 56
Operation: new = old + 4
Test: divisible by 5
If true: throw to monkey 4
If false: throw to monkey 5
Monkey 4:
Starting items: 67
Operation: new = old + 5
Test: divisible by 3
If true: throw to monkey 1
If false: throw to monkey 5
Monkey 5:
Starting items: 85, 56, 66, 72, 57, 99
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 1
If false: throw to monkey 0
Monkey 6:
Starting items: 86, 65, 98, 97, 69
Operation: new = old * 13
Test: divisible by 11
If true: throw to monkey 3
If false: throw to monkey 7
Monkey 7:
Starting items: 87, 68, 92, 66, 91, 50, 68
Operation: new = old + 2
Test: divisible by 17
If true: throw to monkey 4
If false: throw to monkey 3

View File

@@ -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