Skip to content

Commit

Permalink
Use scalacheck in 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 18, 2023
1 parent 9e2f94b commit 5391856
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 39 deletions.
20 changes: 14 additions & 6 deletions src/test/scala/eu/sim642/adventofcode2023/Day5Test.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eu.sim642.adventofcode2023

import Day5._
import Day5.*
import org.scalatest.funsuite.AnyFunSuite
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class Day5Test extends AnyFunSuite {
class Day5Test extends AnyFunSuite with ScalaCheckPropertyChecks {

private val exampleInput =
"""seeds: 79 14 55 13
Expand Down Expand Up @@ -43,11 +44,18 @@ class Day5Test extends AnyFunSuite {
test("Part 1 examples") {
val input = parseInput(exampleInput)

val seed2expectedSoil = Table(
("seed", "expectedSoil"),
(79, 81),
(14, 14),
(55, 57),
(13, 13),
)

val seed2soil = input.rangeMaps.head
assert(seed2soil(Interval(79)) == Set(Interval(81)))
assert(seed2soil(Interval(14)) == Set(Interval(14)))
assert(seed2soil(Interval(55)) == Set(Interval(57)))
assert(seed2soil(Interval(13)) == Set(Interval(13)))
forAll(seed2expectedSoil) { (seed, expectedSoil) =>
assert(seed2soil(Interval(seed)) == Set(Interval(expectedSoil)))
}

assert(lowestSeedLocation(input) == 35)
}
Expand Down
17 changes: 13 additions & 4 deletions src/test/scala/eu/sim642/adventofcode2023/Day6Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Day6.*
import Day6Test.*
import org.scalatest.Suites
import org.scalatest.funsuite.AnyFunSuite
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class Day6Test extends Suites(
new NaiveSolutionTest,
Expand All @@ -17,14 +18,21 @@ object Day6Test {
|Distance: 9 40 200""".stripMargin


abstract class SolutionTest(solution: Solution) extends AnyFunSuite {
abstract class SolutionTest(solution: Solution) extends AnyFunSuite with ScalaCheckPropertyChecks {

test("Part 1 examples") {
val races = parseRaces(exampleInput)

assert(solution.raceWins(races(0)) == 4)
assert(solution.raceWins(races(1)) == 8)
assert(solution.raceWins(races(2)) == 9)
val expectedRaceWins = Table(
("i", "expectedRaceWins"),
(0, 4),
(1, 8),
(2, 9),
)

forAll(expectedRaceWins) { (i, expectedRaceWins) =>
assert(solution.raceWins(races(i)) == expectedRaceWins)
}

assert(solution.multiplyRaceWins(races) == 288)
}
Expand All @@ -43,5 +51,6 @@ object Day6Test {
}

class NaiveSolutionTest extends SolutionTest(NaiveSolution)

class QuadraticSolutionTest extends SolutionTest(QuadraticSolution)
}
64 changes: 43 additions & 21 deletions src/test/scala/eu/sim642/adventofcode2023/Day7Test.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eu.sim642.adventofcode2023

import Day7._
import Day7.*
import org.scalatest.funsuite.AnyFunSuite
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class Day7Test extends AnyFunSuite {
class Day7Test extends AnyFunSuite with ScalaCheckPropertyChecks {

private val exampleInput =
"""32T3K 765
Expand All @@ -12,42 +13,63 @@ class Day7Test extends AnyFunSuite {
|KTJJT 220
|QQQJA 483""".stripMargin

test("Part 1 examples") {
import Part1._
test("Part 1 handType") {
val handExpectedType = Table(
("hand", "expectedType"),
("AAAAA", HandType.FiveOfAKind),
("AA8AA", HandType.FourOfAKind),
("23332", HandType.FullHouse),
("TTT98", HandType.ThreeOfAKind),
("23432", HandType.TwoPair),
("A23A4", HandType.OnePair),
("23456", HandType.HighCard),
)

assert(handType(parseHand("AAAAA")) == HandType.FiveOfAKind)
assert(handType(parseHand("AA8AA")) == HandType.FourOfAKind)
assert(handType(parseHand("23332")) == HandType.FullHouse)
assert(handType(parseHand("TTT98")) == HandType.ThreeOfAKind)
assert(handType(parseHand("23432")) == HandType.TwoPair)
assert(handType(parseHand("A23A4")) == HandType.OnePair)
assert(handType(parseHand("23456")) == HandType.HighCard)
forAll(handExpectedType) { (hand, expectedType) =>
assert(Part1.handType(parseHand(hand)) == expectedType)
}
}

test("Part 1 ordering") {
import Part1._

assert(cardOrdering.lt('2', 'A'))
assert(handTypeOrdering.lt(HandType.HighCard, HandType.FiveOfAKind))
}

assert(totalWinnings(parseHands(exampleInput)) == 6440)
test("Part 1 examples") {
assert(Part1.totalWinnings(parseHands(exampleInput)) == 6440)
}

test("Part 1 input answer") {
assert(Part1.totalWinnings(parseHands(input)) == 247815719)
}

test("Part 2 examples") {
import Part2._
test("Part 2 handType") {
val handExpectedType = Table(
("hand", "expectedType"),
("QJJQ2", HandType.FourOfAKind),
("32T3K", HandType.OnePair),
("KK677", HandType.TwoPair),
("T55J5", HandType.FourOfAKind),
("KTJJT", HandType.FourOfAKind),
("QQQJA", HandType.FourOfAKind),
)

assert(handType(parseHand("QJJQ2")) == HandType.FourOfAKind)
forAll(handExpectedType) { (hand, expectedType) =>
assert(Part2.handType(parseHand(hand)) == expectedType)
}
}

assert(handType(parseHand("32T3K")) == HandType.OnePair)
assert(handType(parseHand("KK677")) == HandType.TwoPair)
assert(handType(parseHand("T55J5")) == HandType.FourOfAKind)
assert(handType(parseHand("KTJJT")) == HandType.FourOfAKind)
assert(handType(parseHand("QQQJA")) == HandType.FourOfAKind)
test("Part 2 ordering") {
import Part2._

assert(cardOrdering.lt('J', '2'))
assert(handOrdering.lt(parseHand("JKKK2"), parseHand("QQQQ2")))
}

assert(totalWinnings(parseHands(exampleInput)) == 5905)
test("Part 2 examples") {
assert(Part2.totalWinnings(parseHands(exampleInput)) == 5905)
}

test("Part 2 input answer") {
Expand Down
31 changes: 23 additions & 8 deletions src/test/scala/eu/sim642/adventofcode2023/Day9Test.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eu.sim642.adventofcode2023

import Day9._
import Day9.*
import org.scalatest.funsuite.AnyFunSuite
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class Day9Test extends AnyFunSuite {
class Day9Test extends AnyFunSuite with ScalaCheckPropertyChecks {

private val exampleInput =
"""0 3 6 9 12 15
Expand All @@ -15,9 +16,16 @@ class Day9Test extends AnyFunSuite {

val histories = parseHistories(exampleInput)

assert(extrapolate(histories(0)) == 18)
assert(extrapolate(histories(1)) == 28)
assert(extrapolate(histories(2)) == 68)
val expectedExtrapolateds = Table(
("i", "expectedExtrapolate"),
(0, 18),
(1, 28),
(2, 68),
)

forAll(expectedExtrapolateds) { (i, expectedExtrapolate) =>
assert(extrapolate(histories(i)) == expectedExtrapolate)
}

assert(sumExtrapolated(histories) == 114)
}
Expand All @@ -31,9 +39,16 @@ class Day9Test extends AnyFunSuite {

val histories = parseHistories(exampleInput)

assert(extrapolate(histories(0)) == -3)
assert(extrapolate(histories(1)) == 0)
assert(extrapolate(histories(2)) == 5)
val expectedExtrapolateds = Table(
("i", "expectedExtrapolate"),
(0, -3),
(1, 0),
(2, 5),
)

forAll(expectedExtrapolateds) { (i, expectedExtrapolate) =>
assert(extrapolate(histories(i)) == expectedExtrapolate)
}
}

test("Part 2 input answer") {
Expand Down

0 comments on commit 5391856

Please sign in to comment.