day1
This commit is contained in:
52
app/build.gradle.kts
Normal file
52
app/build.gradle.kts
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Kotlin application project to get you started.
|
||||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.10.2/userguide/building_java_projects.html in the Gradle documentation.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
|
||||
alias(libs.plugins.kotlin.jvm)
|
||||
|
||||
// Apply the application plugin to add support for building a CLI application in Java.
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Use the Kotlin JUnit 5 integration.
|
||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
|
||||
|
||||
// Use the JUnit 5 integration.
|
||||
testImplementation(libs.junit.jupiter.engine)
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation(libs.guava)
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = "org.example.AppKt"
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
this.testLogging {
|
||||
this.showStandardStreams = true
|
||||
}
|
||||
}
|
55
app/src/main/kotlin/org/day01/Day01.kt
Normal file
55
app/src/main/kotlin/org/day01/Day01.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
package org.example
|
||||
|
||||
import kotlin.io.println
|
||||
import kotlin.math.abs
|
||||
|
||||
class Day01 {
|
||||
fun parse(input: String): List<List<Int>> {
|
||||
val lhs: MutableList<Int> = mutableListOf()
|
||||
val rhs: MutableList<Int> = mutableListOf()
|
||||
|
||||
for (line in input.lines()) {
|
||||
val split = line.split(" ").map { str -> str.toInt() }
|
||||
lhs.add(split[0])
|
||||
rhs.add(split[1])
|
||||
}
|
||||
|
||||
return listOf(lhs, rhs)
|
||||
}
|
||||
|
||||
fun part1(input: String): Int {
|
||||
val lists = parse(input)
|
||||
val lhs = lists[0].sorted()
|
||||
val rhs = lists[1].sorted()
|
||||
|
||||
var sum = 0
|
||||
for (i in 0..<lhs.count()) {
|
||||
sum += abs(lhs[i] - rhs[i])
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
fun part2(input: String): Int {
|
||||
val lists = parse(input)
|
||||
val lhs = lists[0].sorted()
|
||||
val rhs = lists[1].sorted()
|
||||
|
||||
var sum = 0
|
||||
for (i in 0..<lhs.count()) {
|
||||
var rhs_idx = 0
|
||||
val target = lhs[i]
|
||||
var count = 0
|
||||
while (rhs[rhs_idx] < target) {
|
||||
rhs_idx += 1
|
||||
}
|
||||
while (rhs[rhs_idx] == target) {
|
||||
count += 1
|
||||
rhs_idx += 1
|
||||
}
|
||||
|
||||
sum += target * count
|
||||
}
|
||||
return sum
|
||||
}
|
||||
}
|
15
app/src/main/kotlin/org/example/App.kt
Normal file
15
app/src/main/kotlin/org/example/App.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* This source file was generated by the Gradle 'init' task
|
||||
*/
|
||||
package org.example
|
||||
|
||||
class App {
|
||||
val greeting: String
|
||||
get() {
|
||||
return "Hello World!"
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
println(App().greeting)
|
||||
}
|
36
app/src/test/kotlin/org/day01/Day01Test.kt
Normal file
36
app/src/test/kotlin/org/day01/Day01Test.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
package org.example
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertIs
|
||||
|
||||
class Day01Test {
|
||||
@Test fun part1() {
|
||||
val day = Day01()
|
||||
val test = """3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3"""
|
||||
|
||||
val output = day.part1(test)
|
||||
println(output)
|
||||
assert(output == 11)
|
||||
}
|
||||
|
||||
@Test fun part2() {
|
||||
val day = Day01()
|
||||
|
||||
val test = """3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3"""
|
||||
|
||||
val output = day.part2(test)
|
||||
println(output)
|
||||
assert(output == 31)
|
||||
}
|
||||
}
|
14
app/src/test/kotlin/org/example/AppTest.kt
Normal file
14
app/src/test/kotlin/org/example/AppTest.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* This source file was generated by the Gradle 'init' task
|
||||
*/
|
||||
package org.example
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
class AppTest {
|
||||
@Test fun appHasAGreeting() {
|
||||
val classUnderTest = App()
|
||||
assertNotNull(classUnderTest.greeting, "app should have a greeting")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user