File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
main/scala/eu/sim642/adventofcode2024
test/scala/eu/sim642/adventofcode2024 Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -36,11 +36,30 @@ object Day4 {
3636 grids1.map(countHorizontal).sum + grids2.map(countDiagonal).sum
3737 }
3838
39+ def countCrossMAS (grid : Grid [Char ]): Int = {
40+ def countCross (grid : Grid [Char ]): Int = {
41+ grid
42+ .slidingGrid(3 )
43+ .flatten
44+ .count(w => w(0 )(0 ) == 'M' && w(0 )(2 ) == 'S' && w(1 )(1 ) == 'A' && w(2 )(0 ) == 'M' && w(2 )(2 ) == 'S' )
45+ }
46+
47+ val grids = Seq (
48+ grid,
49+ grid.map(_.reverse),
50+ grid.transpose,
51+ grid.transpose.map(_.reverse)
52+ )
53+
54+ grids.map(countCross).sum
55+ }
56+
3957 def parseGrid (input : String ): Grid [Char ] = input.linesIterator.map(_.toVector).toVector
4058
4159 lazy val input : String = scala.io.Source .fromInputStream(getClass.getResourceAsStream(" day4.txt" )).mkString.trim
4260
4361 def main (args : Array [String ]): Unit = {
4462 println(countXMAS(parseGrid(input)))
63+ println(countCrossMAS(parseGrid(input)))
4564 }
4665}
Original file line number Diff line number Diff line change @@ -24,4 +24,12 @@ class Day4Test extends AnyFunSuite {
2424 test(" Part 1 input answer" ) {
2525 assert(countXMAS(parseGrid(input)) == 2454 )
2626 }
27+
28+ test(" Part 2 examples" ) {
29+ assert(countCrossMAS(parseGrid(exampleInput)) == 9 )
30+ }
31+
32+ test(" Part 2 input answer" ) {
33+ assert(countCrossMAS(parseGrid(input)) == 1858 )
34+ }
2735}
You can’t perform that action at this time.
0 commit comments