diff --git a/src/Utils.kt b/src/Utils.kt index 49e1893..2dff11a 100644 --- a/src/Utils.kt +++ b/src/Utils.kt @@ -15,6 +15,9 @@ fun Iterable.lcm(): Long = reduce(Long::lcm) fun readInput(name: String) = File("src", "$name.txt") .readLines() +fun String.splitInts(): List = split(" ").filter(String::isNotBlank).map(String::toInt) +fun String.splitLongs(): List = split(" ").filter(String::isNotBlank).map(String::toLong) + fun listOfIntegerLists(input: List): ArrayList> { val output = ArrayList>() var currList = ArrayList() diff --git a/src/aoc2022/Day05.kt b/src/aoc2022/Day05.kt index 5d78e7e..d1cd670 100644 --- a/src/aoc2022/Day05.kt +++ b/src/aoc2022/Day05.kt @@ -2,6 +2,7 @@ package aoc2022 import println import readInput +import splitInts /* --- Day 5: Supply Stacks --- @@ -106,7 +107,7 @@ move 1 from 1 to 2 data class StackOp(val num: Int, val src: Int, val dest: Int) fun part1(input: List): String { - val numItems = input.first { it.startsWith(" 1") }.split(" ").filter { it.isNotBlank() }.map { it.toInt() }.max() + val numItems = input.first { it.startsWith(" 1") }.splitInts().max() val stackOps = input.filter { it.startsWith("move") }.map { val line = it.split(" ") StackOp(line[1].toInt(), line[3].toInt() - 1, line[5].toInt() - 1) @@ -131,7 +132,7 @@ move 1 from 1 to 2 } fun part2(input: List): String { - val numItems = input.first { it.startsWith(" 1") }.split(" ").filter { it.isNotBlank() }.map { it.toInt() }.max() + val numItems = input.first { it.startsWith(" 1") }.splitInts().max() val stackOps = input.filter { it.startsWith("move") }.map { val line = it.split(" ") StackOp(line[1].toInt(), line[3].toInt() - 1, line[5].toInt() - 1) diff --git a/src/aoc2023/Day04.kt b/src/aoc2023/Day04.kt index 58db81c..9fc11fc 100644 --- a/src/aoc2023/Day04.kt +++ b/src/aoc2023/Day04.kt @@ -2,6 +2,7 @@ package aoc2023 import println import readInput +import splitInts /* --- Day 4: Scratchcards --- @@ -48,8 +49,8 @@ Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 "Card +(\\d+): (.*)".toRegex().matchEntire(it)?.destructured ?.let { (id, game) -> val (wins, numbers) = game.split(" | ") - val winSet = wins.split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet() - val numberSet = numbers.trim().split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet() + val winSet = wins.splitInts().toSet() + val numberSet = numbers.splitInts().toSet() val matches = winSet.intersect(numberSet) 1 shl (matches.size - 1) } @@ -61,8 +62,8 @@ Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 "Card +(\\d+): (.*)".toRegex().matchEntire(it)?.destructured ?.let { (id, game) -> val (wins, numbers) = game.split(" | ") - val winSet = wins.split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet() - val numberSet = numbers.trim().split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet() + val winSet = wins.splitInts().toSet() + val numberSet = numbers.splitInts().toSet() val matches = winSet.intersect(numberSet) matches.size }!! diff --git a/src/aoc2023/Day05.kt b/src/aoc2023/Day05.kt index 194b3ef..1dd0946 100644 --- a/src/aoc2023/Day05.kt +++ b/src/aoc2023/Day05.kt @@ -2,6 +2,7 @@ package aoc2023 import println import readInput +import splitLongs /* --- Day 5: If You Give A Seed A Fertilizer --- @@ -147,14 +148,14 @@ humidity-to-location map: if (i.contains(":")) { val (type, data) = i.split(":") when (type) { - "seeds" -> seeds = data.trim().split(" ").map { it.toLong() }.toLongArray() + "seeds" -> seeds = data.splitLongs().toLongArray() else -> { val from = type.split("-")[0] fromId = typeMap[from]!! } } } else if (i.isNotBlank()) { - val (destRangeStart, sourceRangeStart, length) = i.split(" ").map { it.toLong() } + val (destRangeStart, sourceRangeStart, length) = i.splitLongs() val rangeLists = mapOfMaps.getOrPut(fromId) { ArrayList() } rangeLists.add(LongRange(sourceRangeStart, sourceRangeStart + length - 1) to destRangeStart) } @@ -192,8 +193,9 @@ humidity-to-location map: if (i.contains(":")) { val (type, data) = i.split(":") when (type) { - "seeds" -> seeds = ArrayDeque(data.trim().split(" ").map { it.toLong() }.chunked(2) - .map { LongRange(it[0], it[0] + it[1] - 1) }) + "seeds" -> seeds = ArrayDeque( + data.splitLongs().chunked(2) + .map { LongRange(it[0], it[0] + it[1] - 1) }) else -> { val from = type.split("-")[0] @@ -201,7 +203,7 @@ humidity-to-location map: } } } else if (i.isNotBlank()) { - val (destRangeStart, sourceRangeStart, length) = i.split(" ").map { it.toLong() } + val (destRangeStart, sourceRangeStart, length) = i.splitLongs() val rangeLists = mapOfMaps.getOrPut(fromId) { ArrayList() } rangeLists.add(LongRange(sourceRangeStart, sourceRangeStart + length - 1) to destRangeStart) } @@ -241,7 +243,7 @@ humidity-to-location map: sourceRanges.addFirst(LongRange(newSource, newSource + newSourceLength - 1)) found = true break - // funnily, this is unused, but it should also be a possibility + // funnily, this is unused, but it should also be a possibility // } else if (sourceRange.first < mapRange.first && sourceRange.last > mapRange.last) { // val destLength = mapRange.last + 1 - mapRange.first // val dest = destPos + mapRange.first - sourceRange.first diff --git a/src/aoc2023/Day06.kt b/src/aoc2023/Day06.kt index f2ec9d3..f85880d 100644 --- a/src/aoc2023/Day06.kt +++ b/src/aoc2023/Day06.kt @@ -2,6 +2,7 @@ package aoc2023 import println import readInput +import splitInts /* --- Day 6: Wait For It --- @@ -50,8 +51,8 @@ Distance: 9 40 200 // distance = (bt * (bt+1))/2 + bt * (tt - 2 * bt) // Spend 30 minutes figuring out why I don't win any race when the boat is actually accelerating! fun part1(input: List): Int { - val times = input[0].split(":")[1].split(" ").filter { it.isNotBlank() }.map { it.toInt() } - val distances = input[1].split(":")[1].split(" ").filter { it.isNotBlank() }.map { it.toInt() } + val times = input[0].split(":")[1].splitInts() + val distances = input[1].split(":")[1].splitInts() val result = times.withIndex().map { (i, tt) -> IntRange(1, tt - 1).count { it * (tt - it) > distances[i] diff --git a/src/aoc2023/Day09.kt b/src/aoc2023/Day09.kt index d310444..d802139 100644 --- a/src/aoc2023/Day09.kt +++ b/src/aoc2023/Day09.kt @@ -2,6 +2,7 @@ package aoc2023 import println import readInput +import splitInts /* --- Day 9: Mirage Maintenance --- @@ -72,23 +73,21 @@ fun main() { fun delta(i: List): Int { if (i.all { it == 0 }) return 0 val d = i.zipWithNext { a, b -> b - a } - val p = delta(d) - return d.last() + p + return d.last() + delta(d) } fun delta2(i: List): Int { if (i.all { it == 0 }) return 0 val d = i.zipWithNext { a, b -> b - a } - val p = delta2(d) - return d.first() - p + return d.first() - delta2(d) } fun part1(input: List): Int { - return input.map { it.split(" ").map { it.toInt() } }.sumOf { delta(it) + it.last() } + return input.map { it.splitInts() }.sumOf { delta(it) + it.last() } } fun part2(input: List): Int { - return input.map { it.split(" ").map { it.toInt() } }.sumOf { it.first() - delta2(it) } + return input.map { it.splitInts() }.sumOf { it.first() - delta2(it) } } // test if implementation meets criteria from the description, like: