{ "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,\n", " var operation: Operation,\n", " var divisor: Int,\n", " var trueTarget: Int,\n", " var falseTarget: Int)\n", "\n", "fun read(file: String): MutableList {\n", " val monkeys: MutableList = mutableListOf()\n", " var items: MutableList = 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, idx: Int): Int {\n", " val me = monkeys[idx]\n", " for (i in 0.., monkeyBusiness: MutableList) {\n", " for (i in 0..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 }