diff --git a/day11/.gitignore b/day11/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/day11/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/day11/.idea/.gitignore b/day11/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/day11/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/day11/.idea/inspectionProfiles/Project_Default.xml b/day11/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/day11/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/day11/.idea/kotlinc.xml b/day11/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/day11/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/day11/.idea/libraries/KotlinJavaRuntime.xml b/day11/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/day11/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day11/.idea/libraries/Permanent_Script_Dependencies.xml b/day11/.idea/libraries/Permanent_Script_Dependencies.xml new file mode 100644 index 0000000..0013169 --- /dev/null +++ b/day11/.idea/libraries/Permanent_Script_Dependencies.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day11/.idea/misc.xml b/day11/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/day11/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day11/.idea/modules.xml b/day11/.idea/modules.xml new file mode 100644 index 0000000..c5a1a71 --- /dev/null +++ b/day11/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/day11/.idea/vcs.xml b/day11/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/day11/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day11/day11.iml b/day11/day11.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/day11/day11.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day11/day11.ipynb b/day11/day11.ipynb new file mode 100644 index 0000000..826ada1 --- /dev/null +++ b/day11/day11.ipynb @@ -0,0 +1,314 @@ +{ + "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 +} diff --git a/day11/src/Main.kt b/day11/src/Main.kt new file mode 100644 index 0000000..a7a5e00 --- /dev/null +++ b/day11/src/Main.kt @@ -0,0 +1,3 @@ +fun main() { + println("Hello World!") +} \ No newline at end of file diff --git a/day12/.gitignore b/day12/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/day12/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/day12/.idea/.gitignore b/day12/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/day12/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/day12/.idea/inspectionProfiles/Project_Default.xml b/day12/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/day12/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/day12/.idea/kotlinc.xml b/day12/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/day12/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/day12/.idea/libraries/KotlinJavaRuntime.xml b/day12/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/day12/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day12/.idea/misc.xml b/day12/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/day12/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day12/.idea/modules.xml b/day12/.idea/modules.xml new file mode 100644 index 0000000..0499bf8 --- /dev/null +++ b/day12/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/day12/.idea/vcs.xml b/day12/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/day12/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day12/day12.iml b/day12/day12.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/day12/day12.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day12/src/Main.kt b/day12/src/Main.kt new file mode 100644 index 0000000..10632c9 --- /dev/null +++ b/day12/src/Main.kt @@ -0,0 +1,100 @@ +import java.io.File +import kotlin.math.min + +data class Coordinate ( val x: Int, val y: Int ) + +fun main() { + var y = 0 + var x = 0 + var start = Coordinate(0,0) + var end = Coordinate(0,0) + val grid: HashMap = hashMapOf() + for (line in File("b.input").readLines()) { + x = 0 + line.chars().forEach { + val c = when(Char(it)) { + 'S' -> { start = Coordinate(x,y); 'a' } + 'E' -> { end = Coordinate(x,y); 'z' } + else -> Char(it) + } + grid.put(Coordinate(x,y), c) + x++ + } + y++ + } + + println("start point is $start, end point is $end") + val neighbors: Map> = + grid.keys.map {s -> + Pair(s, + neighbors(s).filter {e -> + grid.containsKey(e) && close(grid.get(s)!!, grid.get(e)!!) + }.toSet()) + }.toMap() + + val distances: MutableMap = mutableMapOf(start to 0) + var nodesToVisit: MutableSet = mutableSetOf(start) + val visited: MutableSet = mutableSetOf() + while (nodesToVisit.isNotEmpty()) { + for (currentNode in nodesToVisit.toMutableSet()) { + println("visiting $currentNode and examining itss neighbors, ${neighbors[currentNode]}") + nodesToVisit.remove(currentNode) + + for (neighbor in neighbors[currentNode]!!) { + distances.put(neighbor, min( + distances[neighbor] ?: Int.MAX_VALUE, + (distances[currentNode] ?: (Int.MAX_VALUE - 100)) + 1 + ) + ) + if (!(neighbor in visited)) { + nodesToVisit.add(neighbor) + } + } + + visited.add(currentNode) + } + } + println("distance from S to end point is ${distances[end]}") + + var minimum = Int.MAX_VALUE + for (startPoint in grid.keys.filter {grid[it] == 'a'}) { + + val distances: MutableMap = mutableMapOf(startPoint to 0) + var nodesToVisit: MutableSet = mutableSetOf(startPoint) + val visited: MutableSet = mutableSetOf() + while (nodesToVisit.isNotEmpty()) { + for (currentNode in nodesToVisit.toMutableSet()) { + //println("visiting $currentNode and examining itss neighbors, ${neighbors[currentNode]}") + nodesToVisit.remove(currentNode) + + for (neighbor in neighbors[currentNode]!!) { + distances.put( + neighbor, min( + distances[neighbor] ?: Int.MAX_VALUE, + (distances[currentNode] ?: (Int.MAX_VALUE - 100)) + 1 + ) + ) + if (!(neighbor in visited)) { + nodesToVisit.add(neighbor) + } + } + + visited.add(currentNode) + } + } + minimum = min(distances[end]?: Int.MAX_VALUE, minimum) + } + println("smallest distance across any starting point was $minimum") +} + +fun neighbors(r: Coordinate): Set{ + val left = Coordinate(r.x-1, r.y) + val right = Coordinate(r.x+1, r.y) + val up = Coordinate(r.x, r.y-1) + val down = Coordinate(r.x, r.y+1) + return setOf(left, right, up, down) +} + +fun close(a: Char, b: Char): Boolean { + return b <= a.inc() +} \ No newline at end of file diff --git a/day13/.gitignore b/day13/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/day13/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/day13/.idea/.gitignore b/day13/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/day13/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/day13/.idea/inspectionProfiles/Project_Default.xml b/day13/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/day13/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/day13/.idea/kotlinc.xml b/day13/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/day13/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/day13/.idea/libraries/KotlinJavaRuntime.xml b/day13/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/day13/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day13/.idea/misc.xml b/day13/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/day13/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day13/.idea/modules.xml b/day13/.idea/modules.xml new file mode 100644 index 0000000..5480e81 --- /dev/null +++ b/day13/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/day13/.idea/vcs.xml b/day13/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/day13/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day13/day13.iml b/day13/day13.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/day13/day13.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day13/src/Main.kt b/day13/src/Main.kt new file mode 100644 index 0000000..69523ab --- /dev/null +++ b/day13/src/Main.kt @@ -0,0 +1,126 @@ +import java.io.BufferedReader +import java.io.File +import kotlin.math.max + +open class Packet +data class IntPacket(val value: Int) : Packet() { + override fun toString(): String = value.toString() +} +data class ListPacket(val contents: MutableList): Packet() { + override fun toString(): String = contents.joinToString(",","[","]") +} +fun parsePacket(s: String): Packet { + val stack: MutableList = mutableListOf() + var running = "" + for (c in s) { + if (c == '[') { + val thisOne = ListPacket(mutableListOf()) + + if (stack.isNotEmpty()) { + val last = stack.last() + + last.contents.add(thisOne) + } + if (stack.isEmpty()) { + stack.add(thisOne) + } + stack.add(thisOne) + } else if (c == ']') { + if (running.isNotEmpty()) { + stack.last().contents.add(IntPacket(running.trim().toInt())) + running = "" + + } + stack.removeLast() + } else if (c == ',') { + if (running.isNotEmpty()) { + stack.last().contents.add(IntPacket(running.trim().toInt())) + running = "" + } + } else { + running += c + } + } + + return stack.last() +} +fun main() { + println("Hello, AoC 2022 day 13!") + + val file = File("b.input") + var reader = file.bufferedReader() + val pairs = reader.lineSequence().chunked(3) { it.take(2)}; + println(parsePacket("[[[]]]").toString()) + + println("part 1!") + var sum = 0 + pairs.forEachIndexed { index, strings -> + val c = compare(parsePacket(strings[0]), parsePacket(strings[1])) + if (c > 0) { + println("index ${index+1} are in the right order!") + sum += index + 1 + } else if (c < 0) { + println("index ${index+1} are in the wrong order!") + } else { + println("they're the same??") + } + } + println("the sum of correctly ordered pair indices is $sum") + reader.close() + + reader = file.bufferedReader() + println("part 2!") + val rows = sequence { + yieldAll(reader.lineSequence()) + yield("[[6]]") + yield("[[2]]") + } + .filter { it.isNotEmpty() } + .map { parsePacket(it) } + .sortedWith { a: Packet, b: Packet -> compare(a,b) } + .toList() + .asReversed() + + var decoderKey = 1 + rows.forEachIndexed { index, packet: Packet -> + if (compare(packet, parsePacket("[[6]]"))==0) { + decoderKey *= index+1 + } else if (compare(packet, parsePacket("[[2]]"))==0) { + decoderKey *= index+1 + } + //println(packet) + } + println("decoder key is $decoderKey") + reader.close() + +} + +fun compare(left: Packet, right: Packet): Int { + if (left is IntPacket && right is IntPacket) { + //println("$left and $right are both ints!") + + return right.value - left.value + } else if (left is ListPacket && right is ListPacket) { + //println("$left and $right are both listpackets!") + for (i in 0..= left.contents.count()) { + return 1 + } else if (i >= right.contents.count()) { + return -1 + } + val comparison = compare(left.contents[i], right.contents[i]) + if (comparison != 0) { + return comparison + } + } + return 0; + } else if (left is ListPacket && right is IntPacket) { + //println("converting $right to a listpacket!") + return compare(left, ListPacket(contents = mutableListOf(right))) + } else if (left is IntPacket && right is ListPacket) { + //println("converting $left to a listpacket!") + return compare(ListPacket(contents = mutableListOf(left)), right) + } + + return 0 +}