From a15dcf85cf88d58240756261493c3c2dccd57e8e Mon Sep 17 00:00:00 2001 From: shoofle Date: Mon, 11 Dec 2023 21:53:49 -0500 Subject: [PATCH] days 3-5 i guess! --- day03/src/Main.kt | 2 +- day04/.gitignore | 29 ++++++ day04/.idea/.gitignore | 8 ++ .../inspectionProfiles/Project_Default.xml | 6 ++ day04/.idea/kotlinc.xml | 10 +++ day04/.idea/libraries/KotlinJavaRuntime.xml | 26 ++++++ day04/.idea/misc.xml | 6 ++ day04/.idea/modules.xml | 8 ++ day04/.idea/vcs.xml | 6 ++ day04/day04.iml | 15 ++++ day04/src/Main.kt | 28 ++++++ day05/.gitignore | 29 ++++++ day05/.idea/.gitignore | 8 ++ .../inspectionProfiles/Project_Default.xml | 6 ++ day05/.idea/kotlinc.xml | 10 +++ day05/.idea/libraries/KotlinJavaRuntime.xml | 26 ++++++ day05/.idea/misc.xml | 6 ++ day05/.idea/modules.xml | 8 ++ day05/.idea/vcs.xml | 6 ++ day05/day05.iml | 15 ++++ day05/src/Main.kt | 89 +++++++++++++++++++ 21 files changed, 346 insertions(+), 1 deletion(-) create mode 100644 day04/.gitignore create mode 100644 day04/.idea/.gitignore create mode 100644 day04/.idea/inspectionProfiles/Project_Default.xml create mode 100644 day04/.idea/kotlinc.xml create mode 100644 day04/.idea/libraries/KotlinJavaRuntime.xml create mode 100644 day04/.idea/misc.xml create mode 100644 day04/.idea/modules.xml create mode 100644 day04/.idea/vcs.xml create mode 100644 day04/day04.iml create mode 100644 day04/src/Main.kt create mode 100644 day05/.gitignore create mode 100644 day05/.idea/.gitignore create mode 100644 day05/.idea/inspectionProfiles/Project_Default.xml create mode 100644 day05/.idea/kotlinc.xml create mode 100644 day05/.idea/libraries/KotlinJavaRuntime.xml create mode 100644 day05/.idea/misc.xml create mode 100644 day05/.idea/modules.xml create mode 100644 day05/.idea/vcs.xml create mode 100644 day05/day05.iml create mode 100644 day05/src/Main.kt diff --git a/day03/src/Main.kt b/day03/src/Main.kt index 16a9dca..1a953ea 100644 --- a/day03/src/Main.kt +++ b/day03/src/Main.kt @@ -3,7 +3,7 @@ import java.io.File //TIP Press twice to open the Search Everywhere dialog and type show whitespaces, // then press . You can now see whitespace characters in your code. fun main() { - val file = File("a.input").readLines(); + val file = File("b.input").readLines(); val sum = file.map { findOverlap(it) } .map { priority(it) }.sum(); println("the sum of item priorities is $sum"); diff --git a/day04/.gitignore b/day04/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/day04/.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/day04/.idea/.gitignore b/day04/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/day04/.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/day04/.idea/inspectionProfiles/Project_Default.xml b/day04/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/day04/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/day04/.idea/kotlinc.xml b/day04/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/day04/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/day04/.idea/libraries/KotlinJavaRuntime.xml b/day04/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/day04/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day04/.idea/misc.xml b/day04/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/day04/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day04/.idea/modules.xml b/day04/.idea/modules.xml new file mode 100644 index 0000000..395516b --- /dev/null +++ b/day04/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/day04/.idea/vcs.xml b/day04/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/day04/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day04/day04.iml b/day04/day04.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/day04/day04.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day04/src/Main.kt b/day04/src/Main.kt new file mode 100644 index 0000000..d533ce5 --- /dev/null +++ b/day04/src/Main.kt @@ -0,0 +1,28 @@ +import java.io.File + +//TIP Press twice to open the Search Everywhere dialog and type show whitespaces, +// then press . You can now see whitespace characters in your code. +fun main() { + val file = File("b.input").readLines(); + val pairsOfSets = file.map { + Pair(it.substringBefore(","), it.substringAfter(",")) + }; + + val number = pairsOfSets.filter { (a, b) -> cntains(parseRange(a), parseRange(b)) }.count(); + println("The number of contained sets is $number"); + + val number2 = pairsOfSets.filter { (a, b) -> overlaps(parseRange(a), parseRange(b)) }.count(); + println("The number of overlapping sets is $number2"); +} + +fun cntains(a:Pair, b:Pair): Boolean { + return ((a.first <= b.first) and (a.second >= b.second)) or + ((b.first <= a.first) and (b.second >= a.second)); +} +fun overlaps(a:Pair, b:Pair): Boolean { + return (a.first in b.first..b.second) or (a.second in b.first..b.second) or cntains(a,b); +} + +fun parseRange(a:String): Pair { + return Pair(a.substringBefore("-").toInt(),a.substringAfter("-").toInt()); +} \ No newline at end of file diff --git a/day05/.gitignore b/day05/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/day05/.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/day05/.idea/.gitignore b/day05/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/day05/.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/day05/.idea/inspectionProfiles/Project_Default.xml b/day05/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/day05/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/day05/.idea/kotlinc.xml b/day05/.idea/kotlinc.xml new file mode 100644 index 0000000..5cba058 --- /dev/null +++ b/day05/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/day05/.idea/libraries/KotlinJavaRuntime.xml b/day05/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..30d5a17 --- /dev/null +++ b/day05/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day05/.idea/misc.xml b/day05/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/day05/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day05/.idea/modules.xml b/day05/.idea/modules.xml new file mode 100644 index 0000000..ea8e5f1 --- /dev/null +++ b/day05/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/day05/.idea/vcs.xml b/day05/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/day05/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/day05/day05.iml b/day05/day05.iml new file mode 100644 index 0000000..43dd653 --- /dev/null +++ b/day05/day05.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/day05/src/Main.kt b/day05/src/Main.kt new file mode 100644 index 0000000..4b91b53 --- /dev/null +++ b/day05/src/Main.kt @@ -0,0 +1,89 @@ +import java.io.File +import java.lang.Integer.max + +data class Move(val count: Int, val from: Int, val to: Int) + +fun main() { + val startWorkspace = MutableList(0) {""}; + var addingToWorkspace = true; + val sInstructions = MutableList(0) {""} + + for (line in File("b.input").readLines()) { + if (line.isEmpty()) addingToWorkspace = false; + + if (addingToWorkspace) startWorkspace.add(line); + else if (line.isNotEmpty()) sInstructions.add(line); + } + val workspace = transpose(startWorkspace) + .filter { !(("[" in it) or ("]" in it) or it.isBlank()) } + .map { it.drop(1).trim { it == ' '} } + .toMutableList(); + + val instructions = sInstructions.map { parse(it) }; + + println("before doing anything, the workspace looks like this:"); + println(workspace.joinToString("\n")); + + for (m in instructions) { + println("after doing $m, the workspace looks like:"); + + for (x in 0..(m.count-1)) { + val source = workspace[m.from]; + val taken = source.last(); + + workspace[m.from] = source.substring(0, source.length-1); + workspace[m.to] = workspace[m.to] + taken; + } + println(workspace.joinToString("\n")); + } + + val toppers = workspace + .map { it.last() } + .joinToString(""); + println("the final crate toppers were $toppers"); + + + // now for part 2 + val workspace2 = transpose(startWorkspace) + .filter { !(("[" in it) or ("]" in it) or it.isBlank()) } + .map { it.drop(1).trim { it == ' '} } + .toMutableList(); + + for (m in instructions) { + val source = workspace2[m.from]; + val taken = source.substring(source.length - m.count); + workspace2[m.from] = source.substring(0, source.length - m.count); + workspace2[m.to] = workspace2[m.to] + taken; + } + + val toppers2 = workspace2 + .map { it.last() } + .joinToString(""); + println("the final crate toppers with the 9001 were $toppers2"); +} + +fun transpose(workspace: List):MutableList { + val lineLength = workspace.map { it.length }.reduce { a,b -> max(a,b) }; + val numberOfLines = workspace.count(); + val output = MutableList(lineLength) { x -> + List(numberOfLines) { y -> + try { + workspace[(numberOfLines-1)-y][x] + } catch (e: Exception) { + ' ' + } + }.joinToString("") + }; + + return output; +} + +// parse a line like "move 12 from 3 to 10" into a move object +fun parse(i: String): Move { + val i2 = i.substringAfter("move "); + val count = i2.substringBefore(" from ").toInt(); + val i3 = i2.substringAfter(" from "); + val from = i3.substringBefore(" to ").toInt() - 1; + val to = i3.substringAfter(" to ").toInt() - 1; + return Move(count, from, to); +} \ No newline at end of file