diff --git a/projects/aoc/build.hxml b/projects/aoc/build.hxml index c56fd45b..1a1e1fcd 100644 --- a/projects/aoc/build.hxml +++ b/projects/aoc/build.hxml @@ -1,4 +1,5 @@ -lib kiss +--macro kiss.KissFrontend.dsl("monkeys", "src/year2022/MonkeyDSL.kiss") -cp src --main Main --interp \ No newline at end of file diff --git a/projects/aoc/src/UtilMacros.kiss b/projects/aoc/src/UtilMacros.kiss index 69cddd07..9f2beac2 100644 --- a/projects/aoc/src/UtilMacros.kiss +++ b/projects/aoc/src/UtilMacros.kiss @@ -10,3 +10,6 @@ (defMacro dayTodo [num] `(day ,num (print "TODO"))) + +(defMacro assertEquals [expected actual] + `(assert (= ,expected ,actual))) \ No newline at end of file diff --git a/projects/aoc/src/year2022/Day11.kiss b/projects/aoc/src/year2022/Day11.kiss new file mode 100644 index 00000000..77d88ab8 --- /dev/null +++ b/projects/aoc/src/year2022/Day11.kiss @@ -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 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)) \ No newline at end of file diff --git a/projects/aoc/src/year2022/Monkey.hx b/projects/aoc/src/year2022/Monkey.hx new file mode 100644 index 00000000..589df5cd --- /dev/null +++ b/projects/aoc/src/year2022/Monkey.hx @@ -0,0 +1,9 @@ +package year2022; + +typedef Monkey = { + items:Array, + operation: Float->Float, + testAndThrow: Float->Int, + inspections:Int, + testDenominator:Int +}; diff --git a/projects/aoc/src/year2022/MonkeyDSL.kiss b/projects/aoc/src/year2022/MonkeyDSL.kiss new file mode 100644 index 00000000..6db68e39 --- /dev/null +++ b/projects/aoc/src/year2022/MonkeyDSL.kiss @@ -0,0 +1,51 @@ +(import year2022.Monkey) +(var :Array 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))) \ No newline at end of file diff --git a/projects/aoc/src/year2022/Solutions2022.kiss b/projects/aoc/src/year2022/Solutions2022.kiss index 136f40e8..e175889c 100644 --- a/projects/aoc/src/year2022/Solutions2022.kiss +++ b/projects/aoc/src/year2022/Solutions2022.kiss @@ -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")) diff --git a/projects/aoc/src/year2022/inputs/Day11.monkeys b/projects/aoc/src/year2022/inputs/Day11.monkeys new file mode 100644 index 00000000..676617f4 --- /dev/null +++ b/projects/aoc/src/year2022/inputs/Day11.monkeys @@ -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 diff --git a/projects/aoc/src/year2022/inputs/Day11Example.monkeys b/projects/aoc/src/year2022/inputs/Day11Example.monkeys new file mode 100644 index 00000000..c04eddb7 --- /dev/null +++ b/projects/aoc/src/year2022/inputs/Day11Example.monkeys @@ -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 \ No newline at end of file