This commit is contained in:
Andrew Glaze
2024-12-01 01:53:13 -05:00
commit 79e8198c4b
14 changed files with 569 additions and 0 deletions

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

View 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)
}

View 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)
}
}

View 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")
}
}