day1
This commit is contained in:
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