added 2024 part 1

This commit is contained in:
2025-06-05 22:45:37 +02:00
parent 66007c5389
commit f1b41dc9c7
33 changed files with 2225 additions and 0 deletions

29
2024/05/Kotlin/.gitignore vendored Normal file
View 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
2024/05/Kotlin/.idea/.gitignore generated vendored Normal file
View 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

View 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
2024/05/Kotlin/.idea/kotlinc.xml generated Normal file
View 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="2.0" />
<option name="languageVersion" value="2.0" />
</component>
</project>

View File

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

6
2024/05/Kotlin/.idea/misc.xml generated Normal file
View File

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

8
2024/05/Kotlin/.idea/modules.xml generated Normal file
View File

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

15
2024/05/Kotlin/Kotlin.iml Normal file
View 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>

View File

@@ -0,0 +1,78 @@
import java.io.File
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
fun main() {
val filenames = listOf("example.txt", "input.txt")
filenames.forEach { filename ->
val input = readInput(filename)
val (rules, updates) = extractData(input)
val (correct, incorrect) = sumMiddleUpdates(rules, updates)
println("${filename.dropLast(4)} Correct Middle Sum: $correct")
println("${filename.dropLast(4)} Incorrect Middle Sum: $incorrect")
}
}
fun readInput(filename: String) = File("src/$filename").readText()
fun extractData(input: String): Pair<Map<Int, List<Int>>, List<List<Int>>> {
var updates = false
val pageOrderRules = HashMap<Int, List<Int>>()
val records = ArrayList<List<Int>>()
input.lines().forEach { line ->
if (line.isBlank()) {
updates = true
} else if (updates) {
records.add(line.trim().split(',').map { it.toInt() })
} else {
val (l, r) = line.split('|')
if (pageOrderRules.containsKey(l.toInt())) {
pageOrderRules[l.toInt()] = pageOrderRules[l.toInt()]!!.plus(r.toInt())
} else {
pageOrderRules[l.toInt()] = listOf(r.toInt())
}
}
}
return Pair(pageOrderRules, records)
}
fun sumMiddleUpdates(rules: Map<Int, List<Int>>, updates: List<List<Int>>): Pair<Int, Int> {
var sumCorrect = 0
var sumIncorrect = 0
for (update in updates) {
var correctUpdate = true
for ((index, page) in update.withIndex()) {
val rule = rules.getOrDefault(page, listOf())
val subset = update.drop(index + 1)
if (!(rule.containsAll(subset) || subset.isEmpty())) {
correctUpdate = false
break
}
}
if (correctUpdate) {
sumCorrect += update[update.size / 2]
} else {
sumIncorrect += fixUpdate(update, rules)
}
}
return Pair(sumCorrect, sumIncorrect)
}
// Similar to Bubble Sort for logic
fun fixUpdate(update: List<Int>, rules: Map<Int, List<Int>>): Int {
val fixedUpdate = update.toMutableList()
var swapped: Boolean
do {
swapped = false
for ((index, page) in fixedUpdate.withIndex()) {
val rule = rules.getOrDefault(page, listOf())
val subset = fixedUpdate.drop(index + 1)
if (!(rule.containsAll(subset) || subset.isEmpty())) {
fixedUpdate[index] = fixedUpdate[index + 1]
fixedUpdate[index + 1] = page
swapped = true
}
}
} while (swapped)
return fixedUpdate[fixedUpdate.size / 2]
}

View File

@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

1362
2024/05/Kotlin/src/input.txt Normal file

File diff suppressed because it is too large Load Diff