Skip to content

Commit

Permalink
2023 - Added time-measure for constructor/init
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 4, 2023
1 parent f8af711 commit 9f0e4d3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
28 changes: 20 additions & 8 deletions src/main/kotlin/no/rodland/advent_2023/Day04.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ class Day04(val input: List<String>) : Day<Long, Long, List<Day04.Game>> {
}

override fun partTwo(): Long {
return partTwoWithArray()
return partTwoWithMutableList()
}

@Suppress("unused")
private fun partTwoWithArray(): Long {
val ar = parsed.toTypedArray()
ar.forEach { game ->
val mutableList = parsed.toTypedArray()
mutableList.forEach { game ->
val endIndex = game.id + game.numberWinning
(game.id..<endIndex).forEach { i ->
mutableList[i] = mutableList[i].copy(numCards = mutableList[i].numCards + game.numCards)
}
}
return mutableList.sumOf { it.numCards }
}

private fun partTwoWithMutableList(): Long {
val mutableList = parsed.toMutableList()
mutableList.forEach { game ->
val endIndex = game.id + game.numberWinning
(game.id..< endIndex).forEach { i ->
ar[i] = ar[i].copy(numCards = ar[i].numCards + game.numCards)
(game.id..<endIndex).forEach { i ->
mutableList[i] = mutableList[i].copy(numCards = mutableList[i].numCards + game.numCards)
}
}
return ar.sumOf { it.numCards }
return mutableList.sumOf { it.numCards }
}

@Suppress("unused")
Expand All @@ -51,8 +63,8 @@ class Day04(val input: List<String>) : Day<Long, Long, List<Day04.Game>> {
return map { line ->
// Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
val id = line.substringAfter("Card").substringBefore(":").trim().toInt()
val winning = line.substringAfter(":").substringBefore("|").split(" +".toRegex()).filterNot { it.isBlank() }.map { it.trim().toInt() }.toSet()
val drawn = line.substringAfter("|").split(" +".toRegex()).filterNot { it.isBlank() }.map { it.trim().toInt() }
val winning = line.substringAfter(":").substringBefore("|").split(" ").filterNot { it.isBlank() }.map { it.trim().toInt() }.toSet()
val drawn = line.substringAfter("|").split(" ").filterNot { it.isBlank() }.map { it.trim().toInt() }
Game(id, winning, drawn)
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/test/kotlin/no/rodland/advent/TestMisc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,21 @@ data class AOCTest<I, T>(
day: Int,
part: Part,
live: Boolean,
) : this(day.padDate() + "." + part.toString() + if (live) ".LIVE" else ".TEST", function, data, expected, numTests)
init: Boolean = false,
) : this(day.padDate() + "." + part.toString() + if (live) ".LIVE" else ".TEST" + if (init) ".INIT" else "", function, data, expected, numTests)
}

fun Int.padDate(): String = if (this < 10) "0$this" else this.toString()

enum class Part { ONE, TWO }
enum class Part { ONE, TWO, INIT }

class AOCTestSuite<I, T, S>(
val livePart1: AOCTest<T, I>,
val livePart2: AOCTest<S, I>,
val testPart1: AOCTest<T, I>,
val testPart2: AOCTest<S, I>,
val initPart1: AOCTest<T, I> = livePart1.copy(numTests = 1),
val initPart2: AOCTest<S, I> = livePart2.copy(numTests = 1),
val initLive: AOCTest<*, *> ,
val initTest: AOCTest<*, *> ,
)

fun <T, S, U> defaultTestSuiteParseOnCall(
Expand All @@ -132,6 +133,8 @@ fun <T, S, U> defaultTestSuiteParseOnCall(
AOCTest(part2, liveData, livePart2, numTestPart2, day = day, part = Part.TWO, live = true),
AOCTest(part1, testData, testPart1, 1, day, part = Part.ONE, live = false),
AOCTest(part2, testData, testPart2, 1, day, part = Part.TWO, live = false),
AOCTest(part1, liveData, livePart1, 1, day = day, part = Part.ONE, live = true),
AOCTest(part2, liveData, livePart2, 1, day = day, part = Part.TWO, live = true),
)


Expand All @@ -142,13 +145,19 @@ fun <T, S> defaultTestSuiteParseOnInit(
livePart1: T,
testPart2: S,
livePart2: S,
initLive: () -> Unit = {},
initTest: () -> Unit = {},
numTestPart1: Int = 1000,
numTestPart2: Int = 1000,
numInitLive: Int = 100,
numInitTest: Int = 100,
) = AOCTestSuite(
AOCTest({ liveDay.partOne() }, Unit, livePart1, numTestPart1, liveDay.day, Part.ONE, true),
AOCTest({ liveDay.partTwo() }, Unit, livePart2, numTestPart2, liveDay.day, Part.TWO, true),
AOCTest({ testDay.partOne() }, Unit, testPart1, 2, liveDay.day, Part.ONE, false),
AOCTest({ testDay.partTwo() }, Unit, testPart2, 2, liveDay.day, Part.TWO, false),
AOCTest({ initLive() }, Unit, Unit, numInitLive, liveDay.day, Part.INIT, live = true),
AOCTest({ initTest() }, Unit, Unit, numInitTest, liveDay.day, Part.INIT, live = false),
)


19 changes: 14 additions & 5 deletions src/test/kotlin/no/rodland/advent_2023/Day02Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ internal class Day02Test {
private val resultOne = 2545
private val resultTwo = 78111

val test = defaultTestSuiteParseOnInit(Day02(data02), Day02(test02), resultTestOne, resultOne, resultTestTwo, resultTwo)
val test = defaultTestSuiteParseOnInit(
Day02(data02),
Day02(test02),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day02(data02) },
{ Day02(test02) },
)

@Nested
inner class Init {
@Test
fun `02,1,live,init`() {
report(test.initPart1)
fun `02,-,test,init`() {
report(test.initTest)
}

@Test
fun `02,2,live,init`() {
report(test.initPart2)
fun `02,-,live,init`() {
report(test.initLive)
}
}

Expand Down
23 changes: 17 additions & 6 deletions src/test/kotlin/no/rodland/advent_2023/Day03Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,36 @@ internal class Day03Test {
"......755.",
"...$.*....",
".664.598..",
)
)

private val resultTestOne = 4361
private val resultTestTwo = 467835
private val resultOne = 533775
private val resultTwo = 78236071

val test = defaultTestSuiteParseOnInit(Day03(data03), Day03(test03), resultTestOne, resultOne, resultTestTwo, resultTwo, numTestPart1 = 5, numTestPart2 = 5)
val test = defaultTestSuiteParseOnInit(
Day03(data03),
Day03(test03),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day03(data03) },
{ Day03(test03) },
numTestPart1 = 5,
numTestPart2 = 5
)

@Nested
inner class Init {
@Test
fun `03,1,live,init`() {
report(test.initPart1)
fun `03,-,test,init`() {
report(test.initTest)
}

@Test
fun `03,2,live,init`() {
report(test.initPart2)
fun `03,-,live,init`() {
report(test.initLive)
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/test/kotlin/no/rodland/advent_2023/Day04Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@ internal class Day04Test {
private val resultOne = 25571L
private val resultTwo = 8805731L

val test = defaultTestSuiteParseOnInit(Day04(data04), Day04(test04), resultTestOne, resultOne, resultTestTwo, resultTwo, numTestPart2 = 20)
val test = defaultTestSuiteParseOnInit(
Day04(data04),
Day04(test04),
resultTestOne,
resultOne,
resultTestTwo,
resultTwo,
{ Day04(data04) },
{ Day04(test04) },
numTestPart2 = 20
)

@Nested
inner class Init {
@Test
fun `04,1,live,init`() {
report(test.initPart1)
fun `04,-,test,init`() {
report(test.initTest)
}

@Test
fun `04,2,live,init`() {
report(test.initPart2)
fun `04,-,live,init`() {
report(test.initLive)
}
}

Expand Down

0 comments on commit 9f0e4d3

Please sign in to comment.