Skip to content

Implicits and tests cleanup #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions scripts/init_day.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ AOC_DAY="$1"

cat <<EOT >> "src/main/scala/Day${AOC_DAY}.scala"
import cats.derived.*
import cats.implicits.*
import cats.syntax.all.*

object Day${AOC_DAY}:

Expand All @@ -24,9 +24,8 @@ import Day${AOC_DAY}Suite.*

class Day${AOC_DAY}Suite extends ScalaCheckSuite:

test("day${AOC_DAY} == 42") {
test("day${AOC_DAY} == 42"):
assertEquals(day${AOC_DAY}, 42)
}

object Day${AOC_DAY}Suite:

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day1.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cats.implicits.*
import cats.syntax.all.*
import cats.Monoid
import scala.annotation.tailrec
import Day1.Digit.*
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day10.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cats.data.{NonEmptyList, NonEmptyMap, NonEmptySet}
import cats.implicits.*
import cats.syntax.all.*
import cats.Order
import scala.annotation.tailrec
import Day10.PipeType.*
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day2.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cats.implicits.*
import cats.syntax.all.*
import cats.Monoid
import Day2.Color.*
import Day2.Validity.*
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day3.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cats.derived.*
import cats.implicits.*
import cats.syntax.all.*
import cats.Traverse

object Day3:
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day4.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cats.implicits.*
import cats.syntax.all.*

object Day4:

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day5.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cats.implicits.*
import cats.syntax.all.*
import fs2.{Pure, Stream}
import scala.annotation.tailrec
import scala.math.Ordering
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day6.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cats.implicits.*
import cats.syntax.all.*
import scala.math.Ordering.Implicits.infixOrderingOps
import scala.math.Numeric.Implicits.infixNumericOps
import Day6.*
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/Day7.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cats.{Order, Semigroup, SemigroupK}
import cats.data.NonEmptyList
import cats.implicits.*
import cats.syntax.all.*
import scala.util.Try
import Day7.HandType.*

Expand All @@ -15,6 +15,7 @@ object Day7:

object HandType:
given Order[HandType] = Order.by(_.strength)
given Ordering[HandType] = Order[HandType].toOrdering

trait Comparator[A]:
def compare: (A, A) => Int
Expand All @@ -34,6 +35,7 @@ object Day7:

object CardLabel:
given Order[CardLabel] = Order.by(_.strength)
given Ordering[CardLabel] = Order[CardLabel].toOrdering

def parse(c: Char): Option[CardLabel] = Try(CardLabel.valueOf(s"$c")).toOption

Expand Down Expand Up @@ -66,6 +68,7 @@ object Day7:
handTypeComparator,
List[Hand => CardLabel](_.c1, _.c2, _.c3, _.c4, _.c5).map(nthCardComparator)
).reduce.toOrder
given Ordering[Hand] = Order[Hand].toOrdering

def parse(s: String): Option[Hand] =
s.toList.traverse(CardLabel.parse).collect { case c1 :: c2 :: c3 :: c4 :: c5 :: Nil => Hand(c1, c2, c3, c4, c5) }
Expand Down Expand Up @@ -168,6 +171,7 @@ object Day7:
handTypeComparator,
List[HandJ => CardLabelJ](_.c1, _.c2, _.c3, _.c4, _.c5).map(nthCardComparator)
).reduce.toOrder
given Ordering[HandJ] = Order[HandJ].toOrdering

def parse(s: String): Option[HandJ] = Hand.parse(s).map(from)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day8.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cats.data.NonEmptyList
import cats.implicits.*
import cats.syntax.all.*
import cats.Order
import fs2.{Pure, Stream}
import scala.util.Try
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Day9.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cats.data.NonEmptyList
import cats.implicits.*
import cats.syntax.all.*

object Day9:

Expand Down
66 changes: 22 additions & 44 deletions src/test/scala/Day10Suite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Day10Suite.*

class Day10Suite extends ScalaCheckSuite:

test("parsing smallInput1") {
test("parsing smallInput1"):
val expected = Field(rows =
Vector(
Vector(Horizontal, NorthAndEast, Vertical, SouthAndEast, SouthAndWest).map(Pipe.apply),
Expand All @@ -18,9 +18,8 @@ class Day10Suite extends ScalaCheckSuite:
)
)
assertEquals(Field.parse(smallInput1), Some(expected))
}

test("parsing smallSimplifiedInput1") {
test("parsing smallSimplifiedInput1"):
val expected = Field(rows =
Vector(
Vector.fill(5)(Ground),
Expand All @@ -31,85 +30,74 @@ class Day10Suite extends ScalaCheckSuite:
)
)
assertEquals(Field.parse(smallSimplifiedInput1), Some(expected))
}

test("start position for smallInput1 is Pos(row = 1, col = 1)") {
test("start position for smallInput1 is Pos(row = 1, col = 1)"):
assertEquals(Field.parse(smallInput1).flatMap(_.startPos), Some(Pos(row = 1, col = 1)))
}

test("start position for smallSimplifiedInput1 is Pos(row = 1, col = 1)") {
test("start position for smallSimplifiedInput1 is Pos(row = 1, col = 1)"):
assertEquals(
Field.parse(smallSimplifiedInput1).flatMap(_.startPos),
Some(Pos(row = 1, col = 1))
)
}

test("start position for smallInput2 is Pos(row = 2, col = 0)") {
test("start position for smallInput2 is Pos(row = 2, col = 0)"):
assertEquals(Field.parse(smallInput2).flatMap(_.startPos), Some(Pos(row = 2, col = 0)))
}

test("start position for smallSimplifiedInput2 is Pos(row = 2, col = 0)") {
test("start position for smallSimplifiedInput2 is Pos(row = 2, col = 0)"):
assertEquals(
Field.parse(smallSimplifiedInput2).flatMap(_.startPos),
Some(Pos(row = 2, col = 0))
)
}

test("start position for big input is defined") {
test("start position for big input is defined"):
assert(Field.parse(bigInput).flatMap(_.startPos).isDefined)
}

test("one-step moves from start found for smallInput1") {
test("one-step moves from start found for smallInput1"):
assertEquals(
for {
field <- Field.parse(smallInput1)
start <- field.startPos
} yield field.oneStepFrom(start),
Some(Set(Pos(row = 1, col = 2), Pos(row = 2, col = 1)))
)
}

test("one-step moves from start found for smallSimplifiedInput1") {
test("one-step moves from start found for smallSimplifiedInput1"):
assertEquals(
for {
field <- Field.parse(smallSimplifiedInput1)
start <- field.startPos
} yield field.oneStepFrom(start),
Some(Set(Pos(row = 1, col = 2), Pos(row = 2, col = 1)))
)
}

test("one-step moves from start found for smallInput2") {
test("one-step moves from start found for smallInput2"):
assertEquals(
for {
field <- Field.parse(smallInput2)
start <- field.startPos
} yield field.oneStepFrom(start),
Some(Set(Pos(row = 2, col = 1), Pos(row = 3, col = 0)))
)
}

test("one-step moves from start found for smallSimplifiedInput2") {
test("one-step moves from start found for smallSimplifiedInput2"):
assertEquals(
for {
field <- Field.parse(smallSimplifiedInput2)
start <- field.startPos
} yield field.oneStepFrom(start),
Some(Set(Pos(row = 2, col = 1), Pos(row = 3, col = 0)))
)
}

test("#2 one-step moves from start found for big input") {
test("#2 one-step moves from start found for big input"):
assertEquals(
for {
field <- Field.parse(bigInput)
start <- field.startPos
} yield field.oneStepFrom(start).size,
Some(2)
)
}

test("loop for smallInput1") {
test("loop for smallInput1"):
assertEquals(
Field.parse(smallInput1).flatMap(_.loop),
Some(
Expand All @@ -119,16 +107,14 @@ class Day10Suite extends ScalaCheckSuite:
)
)
)
}

test("loop for smallInput1 is same of smallSimplifiedInput1") {
test("loop for smallInput1 is same of smallSimplifiedInput1"):
assertEquals(
Field.parse(smallInput1).flatMap(_.loop),
Field.parse(smallSimplifiedInput1).flatMap(_.loop)
)
}

test("loop for smallInput2") {
test("loop for smallInput2"):
assertEquals(
Field.parse(smallInput2).flatMap(_.loop),
Some(
Expand All @@ -142,47 +128,39 @@ class Day10Suite extends ScalaCheckSuite:
)
)
)
}

test("loop for smallInput2 is same of smallSimplifiedInput2") {
test("loop for smallInput2 is same of smallSimplifiedInput2"):
assertEquals(
Field.parse(smallInput2).flatMap(_.loop),
Field.parse(smallSimplifiedInput2).flatMap(_.loop)
)
}

test("steps count to farthest point in loop is 4 for smallInput1") {
test("steps count to farthest point in loop is 4 for smallInput1"):
assertEquals(stepsCountToFarthestInLoop(smallInput1), Some(4))
}

test("steps count to farthest point in loop is same for smallInput1 and simplified one") {
test("steps count to farthest point in loop is same for smallInput1 and simplified one"):
assertEquals(
stepsCountToFarthestInLoop(smallInput1),
stepsCountToFarthestInLoop(smallSimplifiedInput1)
)
}

test("steps count to farthest point in loop is 8 for smallInput2") {
test("steps count to farthest point in loop is 8 for smallInput2"):
assertEquals(stepsCountToFarthestInLoop(smallInput2), Some(8))
}

test("steps count to farthest point in loop is same for smallInput2 and simplified one") {
test("steps count to farthest point in loop is same for smallInput2 and simplified one"):
assertEquals(
stepsCountToFarthestInLoop(smallInput2),
stepsCountToFarthestInLoop(smallSimplifiedInput2)
)
}

test("loop branches has same length for big input") {
test("loop branches has same length for big input"):
assertEquals(
Field.parse(bigInput).flatMap(_.loop.map(l => l.firstPath.length == l.secondPath.length)),
Some(true)
)
}

test("steps count to farthest point in loop is 6_828 for big input") {
test("steps count to farthest point in loop is 6_828 for big input"):
assertEquals(stepsCountToFarthestInLoop(bigInput), Some(6_828))
}

object Day10Suite:

Expand Down
Loading