You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
314 lines
8.6 KiB
314 lines
8.6 KiB
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"First of all, we set up a function to parse all the input, and a data class."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 50,
|
|
"outputs": [],
|
|
"source": [
|
|
"import java.io.File\n",
|
|
"\n",
|
|
"data class Operation(var amount: Int, var action: Action) {\n",
|
|
" enum class Action { ADD, MULTIPLY, SQUARE }\n",
|
|
"}\n",
|
|
"data class Monkey(var items:MutableList<Int>,\n",
|
|
" var operation: Operation,\n",
|
|
" var divisor: Int,\n",
|
|
" var trueTarget: Int,\n",
|
|
" var falseTarget: Int)\n",
|
|
"\n",
|
|
"fun read(file: String): MutableList<Monkey> {\n",
|
|
" val monkeys: MutableList<Monkey> = mutableListOf()\n",
|
|
" var items: MutableList<Int> = mutableListOf()\n",
|
|
" var operation: Operation = Operation(0, Operation.Action.ADD)\n",
|
|
" var divisor = 0\n",
|
|
" var trueTarget = 0\n",
|
|
" var falseTarget = 0\n",
|
|
"\n",
|
|
" for (line in File(file).readLines()) {\n",
|
|
" if (line.contains(\"Starting items:\")) {\n",
|
|
" items = line.substringAfter(\":\").split(\",\").map { Integer.parseInt(it.trim()) }.toMutableList();\n",
|
|
" }\n",
|
|
"\n",
|
|
" if (line.contains(\"Operation\")) {\n",
|
|
" val op = when (line.substringAfter(\"old \")[0]) {\n",
|
|
" '*' -> Operation.Action.MULTIPLY\n",
|
|
" '+' -> Operation.Action.ADD\n",
|
|
" else -> Operation.Action.ADD\n",
|
|
" }\n",
|
|
" if (line.substringAfter(\"old\").contains(\"old\")) {\n",
|
|
" operation = Operation(0, Operation.Action.SQUARE)\n",
|
|
" } else {\n",
|
|
" val amt = Integer.parseInt(line.substringAfter(\"old \").substring(1).trim())\n",
|
|
" operation = Operation(amt, op)\n",
|
|
" }\n",
|
|
" }\n",
|
|
"\n",
|
|
" if (line.contains(\"Test:\")) {\n",
|
|
" divisor = Integer.parseInt(line.substringAfter(\"divisible by \"))\n",
|
|
" }\n",
|
|
" if (line.contains(\"If true:\")) {\n",
|
|
" trueTarget = Integer.parseInt(line.substringAfter(\"monkey \"))\n",
|
|
" }\n",
|
|
" if (line.contains(\"If false:\")) {\n",
|
|
" falseTarget = Integer.parseInt(line.substringAfter(\"monkey \"))\n",
|
|
" }\n",
|
|
"\n",
|
|
" if (line.isBlank()) {\n",
|
|
" monkeys.add(Monkey(items, operation, divisor, trueTarget, falseTarget))\n",
|
|
" }\n",
|
|
" }\n",
|
|
"\n",
|
|
" \n",
|
|
" return monkeys\n",
|
|
"}"
|
|
],
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2023-12-17T03:36:28.544624Z",
|
|
"start_time": "2023-12-17T03:36:28.272541Z"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"Next up we need a function for executing one step of all the monkeys' actions."
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 51,
|
|
"outputs": [],
|
|
"source": [
|
|
"fun operate(op: Operation, x: Int): Int {\n",
|
|
" return when (op.action) {\n",
|
|
" Operation.Action.ADD -> x + op.amount\n",
|
|
" Operation.Action.MULTIPLY -> x * op.amount\n",
|
|
" Operation.Action.SQUARE -> x * x\n",
|
|
" } / 3\n",
|
|
"}\n",
|
|
"fun execute(monkeys: MutableList<Monkey>, idx: Int): Int {\n",
|
|
" val me = monkeys[idx]\n",
|
|
" for (i in 0..<me.items.count()) {\n",
|
|
" val worry = operate(me.operation, me.items[i])\n",
|
|
" if (worry % me.divisor == 0) monkeys[me.trueTarget].items.add(worry)\n",
|
|
" else monkeys[me.falseTarget].items.add(worry)\n",
|
|
" }\n",
|
|
" val output = me.items.count()\n",
|
|
" me.items.clear()\n",
|
|
" return output\n",
|
|
"}\n",
|
|
"\n",
|
|
"fun runRound(monkeys: MutableList<Monkey>, monkeyBusiness: MutableList<Int>) {\n",
|
|
" for (i in 0..<monkeys.count()) {\n",
|
|
" monkeyBusiness[i] += execute(monkeys, i);\n",
|
|
" }\n",
|
|
"}"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2023-12-17T03:36:31.292654Z",
|
|
"start_time": "2023-12-17T03:36:31.108619Z"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"source": [
|
|
"Now that we've set everything up, it's time to fly!"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 58,
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[Monkey(items=[71, 86], operation=Operation(amount=13, action=MULTIPLY), divisor=19, trueTarget=6, falseTarget=7), Monkey(items=[66, 50, 90, 53, 88, 85], operation=Operation(amount=3, action=ADD), divisor=2, trueTarget=5, falseTarget=4), Monkey(items=[97, 54, 89, 62, 84, 80, 63], operation=Operation(amount=6, action=ADD), divisor=13, trueTarget=4, falseTarget=1), Monkey(items=[82, 97, 56, 92], operation=Operation(amount=2, action=ADD), divisor=5, trueTarget=6, falseTarget=0), Monkey(items=[50, 99, 67, 61, 86], operation=Operation(amount=0, action=SQUARE), divisor=7, trueTarget=5, falseTarget=3), Monkey(items=[61, 66, 72, 55, 64, 53, 72, 63], operation=Operation(amount=4, action=ADD), divisor=11, trueTarget=3, falseTarget=0), Monkey(items=[59, 79, 63], operation=Operation(amount=7, action=MULTIPLY), divisor=17, trueTarget=2, falseTarget=7), Monkey(items=[55], operation=Operation(amount=7, action=ADD), divisor=3, trueTarget=2, falseTarget=1)]\n",
|
|
"108\n",
|
|
"304\n",
|
|
"957\n",
|
|
"1974\n",
|
|
"3074\n",
|
|
"4891\n",
|
|
"6880\n",
|
|
"8930\n",
|
|
"12760\n",
|
|
"15872\n",
|
|
"19734\n",
|
|
"25110\n",
|
|
"30788\n",
|
|
"36672\n",
|
|
"43878\n",
|
|
"51300\n",
|
|
"60258\n",
|
|
"68382\n",
|
|
"79230\n",
|
|
"88208\n",
|
|
"100160\n",
|
|
"110888\n",
|
|
"124244\n",
|
|
"136160\n",
|
|
"150920\n",
|
|
"164024\n",
|
|
"179760\n",
|
|
"194040\n",
|
|
"211584\n",
|
|
"226575\n",
|
|
"245009\n",
|
|
"261632\n",
|
|
"281945\n",
|
|
"299756\n",
|
|
"320340\n",
|
|
"339888\n",
|
|
"362388\n",
|
|
"383160\n",
|
|
"407028\n",
|
|
"428370\n",
|
|
"454260\n",
|
|
"476790\n",
|
|
"502665\n",
|
|
"527075\n",
|
|
"555009\n",
|
|
"580643\n",
|
|
"609160\n",
|
|
"636006\n",
|
|
"666652\n",
|
|
"693888\n",
|
|
"725032\n",
|
|
"755160\n",
|
|
"786744\n",
|
|
"818120\n",
|
|
"851904\n",
|
|
"884540\n",
|
|
"917739\n",
|
|
"952575\n",
|
|
"988011\n",
|
|
"1023132\n",
|
|
"1060875\n",
|
|
"1097256\n",
|
|
"1136331\n",
|
|
"1172888\n",
|
|
"1212176\n",
|
|
"1252160\n",
|
|
"1292744\n",
|
|
"1332870\n",
|
|
"1373559\n",
|
|
"1416099\n",
|
|
"1459239\n",
|
|
"1501850\n",
|
|
"1547511\n",
|
|
"1591382\n",
|
|
"1638375\n",
|
|
"1683506\n",
|
|
"1731831\n",
|
|
"1776888\n",
|
|
"1825176\n",
|
|
"1874160\n",
|
|
"1923744\n",
|
|
"1972620\n",
|
|
"2024904\n",
|
|
"2075040\n",
|
|
"2125739\n",
|
|
"2178575\n",
|
|
"2232011\n",
|
|
"2286143\n",
|
|
"2340875\n",
|
|
"2396303\n",
|
|
"2452331\n",
|
|
"2509055\n",
|
|
"2564772\n",
|
|
"2622780\n",
|
|
"2681376\n",
|
|
"2740680\n",
|
|
"2800572\n",
|
|
"2859480\n",
|
|
"2920656\n",
|
|
"2980802\n",
|
|
"3043250\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"val monkeys = read(\"b.input\")\n",
|
|
"var monkeyBusiness = monkeys.map { 0 } .toMutableList()\n",
|
|
"\n",
|
|
"println(\"$monkeys\")\n",
|
|
"\n",
|
|
"for (i in 0..100) {\n",
|
|
" //println(\"After round ${i+1}:\")\n",
|
|
" runRound(monkeys, monkeyBusiness)\n",
|
|
" println(\"${monkeyBusiness.sortedDescending().take(2).reduce{a,b->a*b}}\")\n",
|
|
"}\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2023-12-17T03:43:00.545746Z",
|
|
"start_time": "2023-12-17T03:43:00.401417Z"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 43,
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": "88208"
|
|
},
|
|
"execution_count": 43,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"296*298"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2023-12-17T03:29:13.658590Z",
|
|
"start_time": "2023-12-17T03:29:13.543931Z"
|
|
}
|
|
}
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Kotlin",
|
|
"language": "kotlin",
|
|
"name": "kotlin"
|
|
},
|
|
"language_info": {
|
|
"name": "kotlin",
|
|
"version": "1.9.0",
|
|
"mimetype": "text/x-kotlin",
|
|
"file_extension": ".kt",
|
|
"pygments_lexer": "kotlin",
|
|
"codemirror_mode": "text/x-kotlin",
|
|
"nbconvert_exporter": ""
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|
|
|