Compare commits

..

No commits in common. "fb3a5ce5d226a39a468e559bc66d34601a25ca57" and "3c4b32fc786f15c9a914d2b785120c5f2c10c534" have entirely different histories.

6 changed files with 30 additions and 27 deletions

View File

@ -15,9 +15,6 @@ fun Iterable<Long>.lcm(): Long = reduce(Long::lcm)
fun readInput(name: String) = File("src", "$name.txt")
.readLines()
fun String.splitInts(): List<Int> = split(" ").filter(String::isNotBlank).map(String::toInt)
fun String.splitLongs(): List<Long> = split(" ").filter(String::isNotBlank).map(String::toLong)
fun listOfIntegerLists(input: List<String>): ArrayList<List<Int>> {
val output = ArrayList<List<Int>>()
var currList = ArrayList<Int>()

View File

@ -2,7 +2,6 @@ package aoc2022
import println
import readInput
import splitInts
/*
--- Day 5: Supply Stacks ---
@ -107,7 +106,7 @@ move 1 from 1 to 2
data class StackOp(val num: Int, val src: Int, val dest: Int)
fun part1(input: List<String>): String {
val numItems = input.first { it.startsWith(" 1") }.splitInts().max()
val numItems = input.first { it.startsWith(" 1") }.split(" ").filter { it.isNotBlank() }.map { it.toInt() }.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)
@ -132,7 +131,7 @@ move 1 from 1 to 2
}
fun part2(input: List<String>): String {
val numItems = input.first { it.startsWith(" 1") }.splitInts().max()
val numItems = input.first { it.startsWith(" 1") }.split(" ").filter { it.isNotBlank() }.map { it.toInt() }.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)

View File

@ -2,7 +2,6 @@ package aoc2023
import println
import readInput
import splitInts
/*
--- Day 4: Scratchcards ---
@ -49,21 +48,31 @@ 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.splitInts().toSet()
val numberSet = numbers.splitInts().toSet()
val winSet = wins.split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet()
val numberSet = numbers.trim().split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet()
val matches = winSet.intersect(numberSet)
1 shl (matches.size - 1)
}
}.sumOf { it!! }
}
fun rec(winTable: List<Int>, pos: Int): Int {
var sum = winTable[pos]
if (sum > 0) {
for (j in pos + 1..(pos + sum).coerceAtMost(winTable.lastIndex)) {
sum += rec(winTable, pos)
}
}
return sum
}
fun part2(input: List<String>): Int {
val winTable = input.map {
"Card +(\\d+): (.*)".toRegex().matchEntire(it)?.destructured
?.let { (id, game) ->
val (wins, numbers) = game.split(" | ")
val winSet = wins.splitInts().toSet()
val numberSet = numbers.splitInts().toSet()
val winSet = wins.split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet()
val numberSet = numbers.trim().split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet()
val matches = winSet.intersect(numberSet)
matches.size
}!!

View File

@ -2,7 +2,6 @@ package aoc2023
import println
import readInput
import splitLongs
/*
--- Day 5: If You Give A Seed A Fertilizer ---
@ -148,14 +147,14 @@ humidity-to-location map:
if (i.contains(":")) {
val (type, data) = i.split(":")
when (type) {
"seeds" -> seeds = data.splitLongs().toLongArray()
"seeds" -> seeds = data.trim().split(" ").map { it.toLong() }.toLongArray()
else -> {
val from = type.split("-")[0]
fromId = typeMap[from]!!
}
}
} else if (i.isNotBlank()) {
val (destRangeStart, sourceRangeStart, length) = i.splitLongs()
val (destRangeStart, sourceRangeStart, length) = i.split(" ").map { it.toLong() }
val rangeLists = mapOfMaps.getOrPut(fromId) { ArrayList() }
rangeLists.add(LongRange(sourceRangeStart, sourceRangeStart + length - 1) to destRangeStart)
}
@ -193,8 +192,7 @@ humidity-to-location map:
if (i.contains(":")) {
val (type, data) = i.split(":")
when (type) {
"seeds" -> seeds = ArrayDeque(
data.splitLongs().chunked(2)
"seeds" -> seeds = ArrayDeque(data.trim().split(" ").map { it.toLong() }.chunked(2)
.map { LongRange(it[0], it[0] + it[1] - 1) })
else -> {
@ -203,7 +201,7 @@ humidity-to-location map:
}
}
} else if (i.isNotBlank()) {
val (destRangeStart, sourceRangeStart, length) = i.splitLongs()
val (destRangeStart, sourceRangeStart, length) = i.split(" ").map { it.toLong() }
val rangeLists = mapOfMaps.getOrPut(fromId) { ArrayList() }
rangeLists.add(LongRange(sourceRangeStart, sourceRangeStart + length - 1) to destRangeStart)
}

View File

@ -2,7 +2,6 @@ package aoc2023
import println
import readInput
import splitInts
/*
--- Day 6: Wait For It ---
@ -51,8 +50,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<String>): Int {
val times = input[0].split(":")[1].splitInts()
val distances = input[1].split(":")[1].splitInts()
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 result = times.withIndex().map { (i, tt) ->
IntRange(1, tt - 1).count {
it * (tt - it) > distances[i]

View File

@ -2,7 +2,6 @@ package aoc2023
import println
import readInput
import splitInts
/*
--- Day 9: Mirage Maintenance ---
@ -73,21 +72,23 @@ fun main() {
fun delta(i: List<Int>): Int {
if (i.all { it == 0 }) return 0
val d = i.zipWithNext { a, b -> b - a }
return d.last() + delta(d)
val p = delta(d)
return d.last() + p
}
fun delta2(i: List<Int>): Int {
if (i.all { it == 0 }) return 0
val d = i.zipWithNext { a, b -> b - a }
return d.first() - delta2(d)
val p = delta2(d)
return d.first() - p
}
fun part1(input: List<String>): Int {
return input.map { it.splitInts() }.sumOf { delta(it) + it.last() }
return input.map { it.split(" ").map { it.toInt() } }.sumOf { delta(it) + it.last() }
}
fun part2(input: List<String>): Int {
return input.map { it.splitInts() }.sumOf { it.first() - delta2(it) }
return input.map { it.split(" ").map { it.toInt() } }.sumOf { it.first() - delta2(it) }
}
// test if implementation meets criteria from the description, like: