1
1
package jurisk .adventofcode .y2024
2
2
3
+ import cats .effect .IO
4
+ import cats .effect .IOApp
5
+ import cats .implicits .catsSyntaxParallelTraverse1
3
6
import jurisk .utils .FileInput ._
4
7
import jurisk .utils .Parsing .StringOps
5
8
6
- object Advent07 {
9
+ object Advent07 extends IOApp . Simple {
7
10
type Input = List [Equation ]
8
11
9
12
private def concatLongs (a : Long , b : Long ): Long = {
@@ -48,13 +51,19 @@ object Advent07 {
48
51
def parse (input : String ): Input =
49
52
input.parseLines(Equation .parse)
50
53
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)
53
62
54
- def part1 (data : Input ): Long =
63
+ def part1 (data : Input ): IO [ Long ] =
55
64
solve(data, allowPipe = false )
56
65
57
- def part2 (data : Input ): Long =
66
+ def part2 (data : Input ): IO [ Long ] =
58
67
solve(data, allowPipe = true )
59
68
60
69
def parseFile (fileName : String ): Input =
@@ -63,10 +72,12 @@ object Advent07 {
63
72
def fileName (suffix : String ): String =
64
73
s " 2024/07 $suffix.txt "
65
74
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 ()
72
83
}
0 commit comments