days 11, 12, 13

master
Shoofle 10 months ago
parent b2cabd079b
commit 632dcec217
  1. 29
      day11/.gitignore
  2. 8
      day11/.idea/.gitignore
  3. 6
      day11/.idea/inspectionProfiles/Project_Default.xml
  4. 10
      day11/.idea/kotlinc.xml
  5. 26
      day11/.idea/libraries/KotlinJavaRuntime.xml
  6. 16
      day11/.idea/libraries/Permanent_Script_Dependencies.xml
  7. 6
      day11/.idea/misc.xml
  8. 8
      day11/.idea/modules.xml
  9. 6
      day11/.idea/vcs.xml
  10. 15
      day11/day11.iml
  11. 314
      day11/day11.ipynb
  12. 3
      day11/src/Main.kt
  13. 29
      day12/.gitignore
  14. 8
      day12/.idea/.gitignore
  15. 6
      day12/.idea/inspectionProfiles/Project_Default.xml
  16. 10
      day12/.idea/kotlinc.xml
  17. 26
      day12/.idea/libraries/KotlinJavaRuntime.xml
  18. 6
      day12/.idea/misc.xml
  19. 8
      day12/.idea/modules.xml
  20. 6
      day12/.idea/vcs.xml
  21. 15
      day12/day12.iml
  22. 100
      day12/src/Main.kt
  23. 29
      day13/.gitignore
  24. 8
      day13/.idea/.gitignore
  25. 6
      day13/.idea/inspectionProfiles/Project_Default.xml
  26. 10
      day13/.idea/kotlinc.xml
  27. 26
      day13/.idea/libraries/KotlinJavaRuntime.xml
  28. 6
      day13/.idea/misc.xml
  29. 8
      day13/.idea/modules.xml
  30. 6
      day13/.idea/vcs.xml
  31. 15
      day13/day13.iml
  32. 126
      day13/src/Main.kt

29
day11/.gitignore vendored

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

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

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
</profile>
</component>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.9" />
<option name="languageVersion" value="1.9" />
</component>
</project>

@ -0,0 +1,26 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.0" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-sources.jar!/" />
</SOURCES>
</library>
</component>

@ -0,0 +1,16 @@
<component name="libraryTable">
<library name="Permanent Script Dependencies">
<CLASSES>
<root url="file://$USER_HOME$/Library/Caches/JetBrains/IntelliJIdea2023.3/kotlinNotebook/day11.ac435c4f/kernels/0.12.0-93/kotlin-jupyter-ide-classpath-shadowed-jar/kotlin-jupyter-ide-classpath-shadowed-0.12.0-93.jar" />
<root url="file://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0.jar" />
<root url="file://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0.jar" />
<root url="file://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0.jar" />
<root url="file://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar" />
<root url="file://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0.jar" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="file://$USER_HOME$/Library/Caches/JetBrains/IntelliJIdea2023.3/kotlinNotebook/day11.ac435c4f/kernels/0.12.0-93/kotlin-jupyter-script-classpath-shadowed-sources-jar/kotlin-jupyter-script-classpath-shadowed-0.12.0-93-sources.jar" />
</SOURCES>
</library>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/day11.iml" filepath="$PROJECT_DIR$/day11.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

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

@ -0,0 +1,3 @@
fun main() {
println("Hello World!")
}

29
day12/.gitignore vendored

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

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

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
</profile>
</component>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.9" />
<option name="languageVersion" value="1.9" />
</component>
</project>

@ -0,0 +1,26 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.0" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-sources.jar!/" />
</SOURCES>
</library>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/day12.iml" filepath="$PROJECT_DIR$/day12.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

@ -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<Coordinate, Char> = 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<Coordinate, Set<Coordinate>> =
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<Coordinate, Int> = mutableMapOf(start to 0)
var nodesToVisit: MutableSet<Coordinate> = mutableSetOf(start)
val visited: MutableSet<Coordinate> = 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<Coordinate, Int> = mutableMapOf(startPoint to 0)
var nodesToVisit: MutableSet<Coordinate> = mutableSetOf(startPoint)
val visited: MutableSet<Coordinate> = 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<Coordinate>{
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()
}

29
day13/.gitignore vendored

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

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

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
</profile>
</component>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.9" />
<option name="languageVersion" value="1.9" />
</component>
</project>

@ -0,0 +1,26 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.0" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.0/kotlin-stdlib-jdk8-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.0/kotlin-stdlib-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.9.0/kotlin-stdlib-common-1.9.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.0/kotlin-stdlib-jdk7-1.9.0-sources.jar!/" />
</SOURCES>
</library>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/day13.iml" filepath="$PROJECT_DIR$/day13.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

@ -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>): Packet() {
override fun toString(): String = contents.joinToString(",","[","]")
}
fun parsePacket(s: String): Packet {
val stack: MutableList<ListPacket> = 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..<max(left.contents.count(), right.contents.count())) {
if (i >= 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
}
Loading…
Cancel
Save