Skip to content

Commit d6476dc

Browse files
committed
Misc optimisation
1 parent 3b00364 commit d6476dc

File tree

6 files changed

+52
-27
lines changed

6 files changed

+52
-27
lines changed

scala2/src/main/scala/jurisk/adventofcode/y2024/Advent07.scala

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package jurisk.adventofcode.y2024
22

3+
import cats.effect.IO
4+
import cats.effect.IOApp
5+
import cats.implicits.catsSyntaxParallelTraverse1
36
import jurisk.utils.FileInput._
47
import jurisk.utils.Parsing.StringOps
58

6-
object Advent07 {
9+
object Advent07 extends IOApp.Simple {
710
type Input = List[Equation]
811

912
private def concatLongs(a: Long, b: Long): Long = {
@@ -48,13 +51,19 @@ object Advent07 {
4851
def parse(input: String): Input =
4952
input.parseLines(Equation.parse)
5053

51-
def solve(data: Input, allowPipe: Boolean): Long =
52-
data.filter(_.validate(allowPipe)).map(_.result).sum
54+
def solve(data: Input, allowPipe: Boolean): IO[Long] =
55+
data
56+
.parTraverse { equation =>
57+
for {
58+
valid <- IO(equation.validate(allowPipe))
59+
} yield if (valid) equation.result else 0
60+
}
61+
.map(_.sum)
5362

54-
def part1(data: Input): Long =
63+
def part1(data: Input): IO[Long] =
5564
solve(data, allowPipe = false)
5665

57-
def part2(data: Input): Long =
66+
def part2(data: Input): IO[Long] =
5867
solve(data, allowPipe = true)
5968

6069
def parseFile(fileName: String): Input =
@@ -63,10 +72,12 @@ object Advent07 {
6372
def fileName(suffix: String): String =
6473
s"2024/07$suffix.txt"
6574

66-
def main(args: Array[String]): Unit = {
67-
val realData: Input = parseFile(fileName(""))
68-
69-
println(s"Part 1: ${part1(realData)}")
70-
println(s"Part 2: ${part2(realData)}")
71-
}
75+
override def run: IO[Unit] =
76+
for {
77+
realData <- IO(parseFile(fileName("")))
78+
result1 <- part1(realData)
79+
_ <- IO(println(s"Part 1: $result1"))
80+
result2 <- part2(realData)
81+
_ <- IO(println(s"Part 2: $result2"))
82+
} yield ()
7283
}

scala2/src/main/scala/jurisk/adventofcode/y2024/Advent13.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object Advent13 {
143143
input.parseSections(Machine.parse)
144144

145145
def part1(data: Input): N =
146-
data.flatMap(_.solve(BruteForce(100))).sum
146+
data.flatMap(_.solve(LinearEquations)).sum
147147

148148
def part2(data: Input): N = {
149149
val adjusted = data.map(m =>

scala2/src/main/scala/jurisk/adventofcode/y2024/Advent14.scala

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package jurisk.adventofcode.y2024
22

3-
import cats.implicits.{
4-
catsSyntaxEitherId,
5-
catsSyntaxOptionId,
6-
none,
7-
toFunctorOps,
8-
toUnorderedFoldableOps,
9-
}
3+
import cats.implicits.catsSyntaxEitherId
4+
import cats.implicits.catsSyntaxOptionId
5+
import cats.implicits.none
6+
import cats.implicits.toFunctorOps
7+
import cats.implicits.toUnorderedFoldableOps
108
import jurisk.geometry.Coords2D
119
import jurisk.geometry.Direction2D
1210
import jurisk.geometry.Direction2D.NE

scala2/src/main/scala/jurisk/adventofcode/y2024/Advent20.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ object Advent20 {
6868
def isEmpty(c: Coords2D): Boolean =
6969
field.at(c).contains(false)
7070

71+
val emptyCoords = field.allCoords.filter(isEmpty)
72+
7173
(for {
72-
c1 <- field.allCoords.filter(isEmpty)
74+
c1 <- emptyCoords
7375
c2 <- c1.allCoordsWithinManhattanDistance(maxCheat)
7476
if isEmpty(c2)
7577
if c1 != c2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package jurisk.adventofcode.y2024
22

33
import Advent07._
4-
import org.scalatest.freespec.AnyFreeSpec
4+
import cats.effect.testing.scalatest.AsyncIOSpec
5+
import org.scalatest.freespec.{AnyFreeSpec, AsyncFreeSpec}
56
import org.scalatest.matchers.should.Matchers._
67

7-
class Advent07Spec extends AnyFreeSpec {
8+
class Advent07Spec extends AsyncFreeSpec with AsyncIOSpec {
89
private def testData = parseFile(fileName("-test-00"))
910
private def realData = parseFile(fileName(""))
1011

1112
"part 1" - {
1213
"test" in {
13-
part1(testData) shouldEqual 3749
14+
part1(testData) asserting {
15+
_ shouldEqual 3749
16+
}
1417
}
1518

1619
"3267: 81 40 27" in {
@@ -23,17 +26,23 @@ class Advent07Spec extends AnyFreeSpec {
2326
}
2427

2528
"real" in {
26-
part1(realData) shouldEqual 1038838357795L
29+
part1(realData) asserting {
30+
_ shouldEqual 1038838357795L
31+
}
2732
}
2833
}
2934

3035
"part 2" - {
3136
"test" in {
32-
part2(testData) shouldEqual 11387
37+
part2(testData) asserting {
38+
_ shouldEqual 11387
39+
}
3340
}
3441

3542
"real" in {
36-
part2(realData) shouldEqual 254136560217241L
43+
part2(realData) asserting {
44+
_ shouldEqual 254136560217241L
45+
}
3746
}
3847
}
3948
}

scala2/src/test/scala/jurisk/adventofcode/y2024/Advent13Spec.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ class Advent13Spec extends AnyFreeSpec {
2424
}
2525

2626
"part 2" - {
27-
"test #0" in {
27+
"test #0 Z3" ignore {
2828
val example = testData.head
2929
val expected = Some(80 * 3 + 40)
3030
example.solve(Z3(Internal)) shouldEqual expected
3131
example.solve(Z3(External)) shouldEqual expected
32+
}
33+
34+
"test #0 linear equations" in {
35+
val example = testData.head
36+
val expected = Some(80 * 3 + 40)
3237
example.solve(LinearEquations) shouldEqual expected
3338
}
3439

0 commit comments

Comments
 (0)