generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hduprat
committed
Dec 1, 2024
1 parent
924d10b
commit 88f9e0b
Showing
2 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |