Skip to content

Commit

Permalink
Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
hduprat committed Dec 1, 2024
1 parent 924d10b commit 88f9e0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/Day01.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
import kotlin.math.abs

data class LocationLists(val left: List<Int>, val right: List<Int>)

fun main() {
fun getLocationLists(input: List<String>): LocationLists {
val leftList = mutableListOf<Int>()
val rightList = mutableListOf<Int>()

input.forEach {
val splits = it.split(Regex("\\s+"),2)
leftList.add(splits[0].toInt())
rightList.add(splits[1].toInt())
}

return LocationLists(leftList.sorted(), rightList.sorted())
}

fun part1(input: List<String>): Int {
return input.size

val locationLists = getLocationLists(input)

return locationLists.left.foldIndexed(0) {index, acc, elt ->
val distance = abs(elt-locationLists.right[index])
distance + acc
}
}

fun part2(input: List<String>): Int {
return input.size
}
val locationLists = getLocationLists(input)

// Test if implementation meets criteria from the description, like:
check(part1(listOf("test_input")) == 1)
return locationLists.left.fold(0) { acc, elt ->
val similarityScore = elt * locationLists.right.count { it == elt }
similarityScore + acc
}
}

// Or read a large test input from the `src/Day01_test.txt` file:
val testInput = readInput("Day01_test")
check(part1(testInput) == 1)

// Read the input from the `src/Day01.txt` file.
val input = readInput("Day01")

check(part1(testInput) == 11)
part1(input).println()

check(part2(testInput) == 31)
part2(input).println()
}
18 changes: 18 additions & 0 deletions src/_template.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fun main() {
fun part1(input: List<String>): Int {
return input.size
}

fun part2(input: List<String>): Int {
return input.size
}

val testInput = readInput("DayXX_test")
val input = readInput("DayXX")

check(part1(testInput) == 1)
part1(input).println()

check(part2(testInput) == 1)
part2(input).println()
}

0 comments on commit 88f9e0b

Please sign in to comment.