-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2fdec9
commit c366a5b
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.lemon.advent.year2024 | ||
|
||
import org.lemon.advent.lib.`2d`._ | ||
|
||
private object Day04: | ||
|
||
def phrase(wordsearch: Seq[String], coords: Seq[Coord]) = coords.map(wordsearch(_)).mkString | ||
|
||
def part1(input: String) = | ||
val lines = input.linesIterator.toSeq | ||
val area = Area(lines) | ||
|
||
(area.rows ++ area.cols ++ area.diagonals) | ||
.map( | ||
_.sliding(4) | ||
.map(phrase(lines, _)) | ||
.count(s => s == "XMAS" || s == "XMAS".reverse) | ||
) | ||
.sum | ||
|
||
def part2(input: String) = | ||
val lines = input.linesIterator.toSeq | ||
val area = Area(lines) | ||
val mas = Set("MAS", "MAS".reverse) | ||
|
||
area.upDiagonals | ||
.map( | ||
_.sliding(3) | ||
.map(coords => (coords.head, phrase(lines, coords))) | ||
.filter((_, s) => mas.contains(s)) | ||
.filter((start, _) => mas.contains(phrase(lines, area.downDiagonal(start.up.up).take(3).toSeq))) | ||
.size | ||
) | ||
.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.lemon.advent.year2024 | ||
|
||
import org.lemon.advent._ | ||
import org.lemon.advent.year2024.Day04._ | ||
|
||
class Day04Test extends UnitTest: | ||
|
||
test("part 1 example") { | ||
val in = """|MMMSXXMASM | ||
|MSAMXMSMSA | ||
|AMXSXMAAMM | ||
|MSAMASMSMX | ||
|XMASAMXAMM | ||
|XXAMMXXAMA | ||
|SMSMSASXSS | ||
|SAXAMASAAA | ||
|MAMMMXMMMM | ||
|MXMXAXMASX""".stripMargin | ||
part1(in) shouldBe 18 | ||
} | ||
|
||
test("part 1") { | ||
part1(read(file(2024)(4))) shouldBe 2514 | ||
} | ||
|
||
test("part 2 example") { | ||
val in = """|MMMSXXMASM | ||
|MSAMXMSMSA | ||
|AMXSXMAAMM | ||
|MSAMASMSMX | ||
|XMASAMXAMM | ||
|XXAMMXXAMA | ||
|SMSMSASXSS | ||
|SAXAMASAAA | ||
|MAMMMXMMMM | ||
|MXMXAXMASX""".stripMargin | ||
part2(in) shouldBe 9 | ||
} | ||
|
||
test("part 2") { | ||
part2(read(file(2024)(4))) shouldBe 1888 | ||
} |