Skip to content

Commit

Permalink
[2024/4] Ceres Search (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 4, 2024
1 parent 5643c24 commit 24b32ab
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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 |||| | | | | | | | | | | | | | | | | | | | | | | 6 |
| 2024 |||| | | | | | | | | | | | | | | | | | | | | | | 7 |

## 🛷 How to run

Expand Down Expand Up @@ -165,6 +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` | |

## 🕯️ Useful commands

Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/adventofcode/year2024/Day04CeresSearch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package adventofcode.year2024

import adventofcode.Puzzle
import adventofcode.PuzzleInput

class Day04CeresSearch(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override fun partOne() = WordSearch(input).countXmas()

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

fun countXmas(): Int {
val xmas = "XMAS"
val length = xmas.length

return (0 until rows).flatMap { row ->
(0 until cols).flatMap { col ->
val horizontal = (0 until length).map { y -> row to col + y }.filter { (_, y) -> y < cols }
val vertical = (0 until length).map { x -> row + x to col }.filter { (x, _) -> x < rows }
val rightDiagonal = (0 until length).map { d -> row + d to col + d }.filter { (x, y) -> x < rows && y < cols }
val leftDiagonal = (0 until length).map { d -> row + d to col - d }.filter { (x, y) -> x < rows && y >= 0 }

setOf(horizontal, vertical, rightDiagonal, leftDiagonal)
.filter { candidate -> candidate.size == length }
.map { coordinates -> coordinates.map { (x, y) -> grid[x][y] }.joinToString("") }
}
}
.filter { candidate -> candidate == xmas || candidate == xmas.reversed() }
.size
}

companion object {
operator fun invoke(input: String) = WordSearch(input.lines().map { row -> row.toList() })
}
}
}
}
Loading

0 comments on commit 24b32ab

Please sign in to comment.