Skip to content

Commit

Permalink
[2024/4] Ceres Search (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 4, 2024
1 parent 24b32ab commit ed6755f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
| 2021 |||||||||||| ||| | | | | | | | | | | | 26 |
| 2022 |||||||||||||| | | | | | | || | | || 28 |
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
| 2024 |||| | | | | | | | | | | | | | | | | | | | | | | 7 |
| 2024 |||| | | | | | | | | | | | | | | | | | | | | | | 8 |

## 🛷 How to run

Expand Down Expand Up @@ -165,7 +165,7 @@ e.g. `HandyHaversacks`)*
| [**2024**](https://adventofcode.com/2024) | 1 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [[Code](src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day01HistorianHysteriaSpec.kt)] | `1970720` | `17191599` |
| | 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [[Code](src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day02RedNosedReportsSpec.kt)] | `572` | `612` |
| | 3 | [Mull It Over](https://adventofcode.com/2024/day/3) | [[Code](src/main/kotlin/adventofcode/year2024/Day03MullItOver.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day03MullItOverSpec.kt)] | `174960292` | `56275602` |
| | 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [[Code](src/main/kotlin/adventofcode/year2024/Day04CeresSearch.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day04CeresSearchSpec.kt)] | `2483` | |
| | 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [[Code](src/main/kotlin/adventofcode/year2024/Day04CeresSearch.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day04CeresSearchSpec.kt)] | `2483` | `1925` |

## 🕯️ Useful commands

Expand Down
21 changes: 18 additions & 3 deletions src/main/kotlin/adventofcode/year2024/Day04CeresSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import adventofcode.PuzzleInput
class Day04CeresSearch(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override fun partOne() = WordSearch(input).countXmas()

override fun partTwo() = WordSearch(input).countMasCrossed()

companion object {
private data class WordSearch(
val grid: List<List<Char>>,
) {
private data class WordSearch(val grid: List<List<Char>>) {
private val rows = grid.size
private val cols = grid.first().size

Expand All @@ -33,6 +33,21 @@ class Day04CeresSearch(customInput: PuzzleInput? = null) : Puzzle(customInput) {
.size
}

fun countMasCrossed(): Int {
val mas = "MAS"
val length = mas.length

return (0..rows - length).flatMap { row ->
(0..cols - length).map { col ->
val topDiagonal = (0 until length).map { d -> row + d to col + d }
val bottomDiagonal = (0 until length).map { d -> row + length - 1 - d to col + d }

setOf(topDiagonal, bottomDiagonal).map { coordinates -> coordinates.map { (x, y) -> grid[x][y] }.joinToString("") }
}
}
.count { diagonals -> diagonals.all { diagonal -> diagonal == mas || diagonal == mas.reversed() } }
}

companion object {
operator fun invoke(input: String) = WordSearch(input.lines().map { row -> row.toList() })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package adventofcode.year2024

import adventofcode.PuzzleBaseSpec

class Day04CeresSearchSpec : PuzzleBaseSpec(18)
class Day04CeresSearchSpec : PuzzleBaseSpec(18, 9)

0 comments on commit ed6755f

Please sign in to comment.