@@ -29,13 +29,17 @@ object Day04:
2929 }
3030
3131 object WordCheckResult :
32- def from (b : Boolean ): WordCheckResult = if b then Found else NotFound
32+ def from (isFound : Boolean ): WordCheckResult = if isFound then Found else NotFound
3333
3434 case class Grid (rows : List [List [Char ]]):
3535
3636 val toVectors : Vector [Vector [Char ]] = rows.map(_.toVector).toVector
3737
38- val toStore : GridStore = Store (p => toVectors.get(p.row).flatMap(_.get(p.col)), s = Pos .zero)
38+ val toStore : GridStore =
39+ Store (
40+ p => toVectors.get(p.row).flatMap(_.get(p.col)),
41+ s = Pos .zero
42+ )
3943
4044 val allPositions : List [Pos ] =
4145 rows.zipWithIndex.flatMap((row, rowIndex) => row.zipWithIndex.map((_, colIndex) => Pos (rowIndex, colIndex)))
@@ -67,44 +71,44 @@ object Day04:
6771 verticalPositions,
6872 ascDiagonalPositions,
6973 descDiagonalPositions
70- ).map(wordCheck(_ , w, store))
74+ ).map(positions => wordCheck(positions(w.length) , w, store))
7175
72- def wordCheck (positions : Word => Pos => List [Pos ], w : Word , store : GridStore ): WordCheckResult =
73- WordCheckResult .from(w.toOptionalChars == store.experiment(positions(w) ))
76+ def wordCheck (positions : Pos => List [Pos ], w : Word , store : GridStore ): WordCheckResult =
77+ WordCheckResult .from(isFound = w.toOptionalChars == store.experiment(positions))
7478
7579 /* XMAS
7680 ....
7781 ....
7882 .... */
79- def horizontalPositions (w : Word )(from : Pos ): List [Pos ] =
80- List .range(start = from.col, end = from.col + w.length ).map(col => Pos (from.row, col))
83+ def horizontalPositions (wordLength : Int )(from : Pos ): List [Pos ] =
84+ List .range(from.col, from.col + wordLength ).map(col => Pos (from.row, col))
8185
8286 /* X...
8387 M...
8488 A...
8589 S... */
86- def verticalPositions (w : Word )(from : Pos ): List [Pos ] =
87- List .range(start = from.row, end = from.row + w.length ).map(row => Pos (row, from.col))
90+ def verticalPositions (wordLength : Int )(from : Pos ): List [Pos ] =
91+ List .range(from.row, from.row + wordLength ).map(row => Pos (row, from.col))
8892
8993 /* ...S
9094 ..A.
9195 .M..
9296 X... */
93- def ascDiagonalPositions (w : Word )(from : Pos ): List [Pos ] =
94- List .range(start = 0 , end = w.length ).reverse.map(i => Pos (row = i + from.row, col = w.length - i - 1 + from.col))
97+ def ascDiagonalPositions (wordLength : Int )(from : Pos ): List [Pos ] =
98+ List .range(0 , wordLength ).reverse.map(i => Pos (row = i + from.row, col = wordLength - i - 1 + from.col))
9599
96100 /* X...
97101 .M..
98102 ..A.
99103 ...S */
100- def descDiagonalPositions (w : Word )(from : Pos ): List [Pos ] =
101- List .range(start = 0 , end = w.length ).map(i => Pos (row = i + from.row, col = i + from.col))
104+ def descDiagonalPositions (wordLength : Int )(from : Pos ): List [Pos ] =
105+ List .range(0 , wordLength ).map(i => Pos (row = i + from.row, col = i + from.col))
102106
103107 // part 2
104108 def crossWordCheck (w : Word , store : GridStore ): WordCheckResult =
105- def ascDiagonalCheck : Word => Boolean = wordCheck(ascDiagonalPositions, _, store).toBoolean
106- def descDiagonalCheck : Word => Boolean = wordCheck(descDiagonalPositions, _, store).toBoolean
107- WordCheckResult .from(
108- (ascDiagonalCheck (w) || ascDiagonalCheck (w.reverse)) &&
109- (descDiagonalCheck (w) || descDiagonalCheck (w.reverse))
109+ def isAscDiagonalFound : Word => Boolean = wordCheck(ascDiagonalPositions(w.length) , _, store).toBoolean
110+ def isDescDiagonalFound : Word => Boolean = wordCheck(descDiagonalPositions(w.length) , _, store).toBoolean
111+ WordCheckResult .from(isFound =
112+ (isAscDiagonalFound (w) || isAscDiagonalFound (w.reverse)) &&
113+ (isDescDiagonalFound (w) || isDescDiagonalFound (w.reverse))
110114 )
0 commit comments