days 3-5 i guess!
This commit is contained in:
parent
46e919eb23
commit
a15dcf85cf
@ -3,7 +3,7 @@ import java.io.File
|
||||
//TIP Press <shortcut raw="SHIFT"/> twice to open the Search Everywhere dialog and type <b>show whitespaces</b>,
|
||||
// then press <shortcut raw="ENTER"/>. 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");
|
||||
|
||||
|
29
day04/.gitignore
vendored
Normal file
29
day04/.gitignore
vendored
Normal file
@ -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
|
8
day04/.idea/.gitignore
vendored
Normal file
8
day04/.idea/.gitignore
vendored
Normal file
@ -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
|
6
day04/.idea/inspectionProfiles/Project_Default.xml
Normal file
6
day04/.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -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>
|
10
day04/.idea/kotlinc.xml
Normal file
10
day04/.idea/kotlinc.xml
Normal file
@ -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>
|
26
day04/.idea/libraries/KotlinJavaRuntime.xml
Normal file
26
day04/.idea/libraries/KotlinJavaRuntime.xml
Normal file
@ -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>
|
6
day04/.idea/misc.xml
Normal file
6
day04/.idea/misc.xml
Normal file
@ -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>
|
8
day04/.idea/modules.xml
Normal file
8
day04/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/day04.iml" filepath="$PROJECT_DIR$/day04.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
day04/.idea/vcs.xml
Normal file
6
day04/.idea/vcs.xml
Normal file
@ -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>
|
15
day04/day04.iml
Normal file
15
day04/day04.iml
Normal file
@ -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>
|
28
day04/src/Main.kt
Normal file
28
day04/src/Main.kt
Normal file
@ -0,0 +1,28 @@
|
||||
import java.io.File
|
||||
|
||||
//TIP Press <shortcut raw="SHIFT"/> twice to open the Search Everywhere dialog and type <b>show whitespaces</b>,
|
||||
// then press <shortcut raw="ENTER"/>. 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<Int,Int>, b:Pair<Int,Int>): 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<Int,Int>, b:Pair<Int,Int>): 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<Int, Int> {
|
||||
return Pair(a.substringBefore("-").toInt(),a.substringAfter("-").toInt());
|
||||
}
|
29
day05/.gitignore
vendored
Normal file
29
day05/.gitignore
vendored
Normal file
@ -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
|
8
day05/.idea/.gitignore
vendored
Normal file
8
day05/.idea/.gitignore
vendored
Normal file
@ -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
|
6
day05/.idea/inspectionProfiles/Project_Default.xml
Normal file
6
day05/.idea/inspectionProfiles/Project_Default.xml
Normal file
@ -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>
|
10
day05/.idea/kotlinc.xml
Normal file
10
day05/.idea/kotlinc.xml
Normal file
@ -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>
|
26
day05/.idea/libraries/KotlinJavaRuntime.xml
Normal file
26
day05/.idea/libraries/KotlinJavaRuntime.xml
Normal file
@ -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>
|
6
day05/.idea/misc.xml
Normal file
6
day05/.idea/misc.xml
Normal file
@ -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>
|
8
day05/.idea/modules.xml
Normal file
8
day05/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/day05.iml" filepath="$PROJECT_DIR$/day05.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
day05/.idea/vcs.xml
Normal file
6
day05/.idea/vcs.xml
Normal file
@ -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>
|
15
day05/day05.iml
Normal file
15
day05/day05.iml
Normal file
@ -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>
|
89
day05/src/Main.kt
Normal file
89
day05/src/Main.kt
Normal file
@ -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<String>(0) {""};
|
||||
var addingToWorkspace = true;
|
||||
val sInstructions = MutableList<String>(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<String>):MutableList<String> {
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user