Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 4: Ceres Search #31

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Development occurs in language-specific directories:
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt)|[day3.py](py/aoc2024/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)||||
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|||
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.ephemient.aoc2024.exe

import com.github.ephemient.aoc2024.Day4
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State

@State(Scope.Benchmark)
class Day4Bench {
private lateinit var input: String

@Setup
fun setup() {
input = getDayInput(4)
}

@Benchmark
fun part1() = Day4(input).part1()

@Benchmark
fun part2() = Day4(input).part2()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.ephemient.aoc2024

class Day4(
input: String,
) {
private val lines = input.lines()

fun part1() = lines.indices.sumOf { y ->
lines[y].indices.sumOf { x ->
Direction.entries.count { (dx, dy) ->
"XMAS".withIndex().all { (i, c) ->
lines.getOrNull(y + i * dy)?.getOrNull(x + i * dx) == c
}
}
}
}

fun part2() = lines.indices.sumOf { y ->
lines[y].indices.count { x ->
if (lines[y][x] != 'A') return@count false
val n = lines.getOrNull(y - 1) ?: return@count false
val s = lines.getOrNull(y + 1) ?: return@count false
val nw = n.getOrNull(x - 1) ?: return@count false
val ne = n.getOrNull(x + 1) ?: return@count false
val sw = s.getOrNull(x - 1) ?: return@count false
val se = s.getOrNull(x + 1) ?: return@count false
(nw == 'M' && se == 'S' || se == 'M' && nw == 'S') &&
(sw == 'M' && ne == 'S' || ne == 'M' && sw == 'S')
}
}

private enum class Direction(val dx: Int, val dy: Int) {
EAST(1, 0),
NORTHEAST(1, -1),
NORTH(0, -1),
NORTHWEST(-1, -1),
WEST(-1, 0),
SOUTHWEST(-1, 1),
SOUTH(0, 1),
SOUTHEAST(1, 1),
;

operator fun component1() = dx
operator fun component2() = dy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ val days: List<Day> = listOf(
Day(1, ::Day1, Day1::part1, Day1::part2),
Day(2, ::Day2, Day2::part1, Day2::part2),
Day(3, ::Day3, Day3::part1, Day3::part2),
Day(4, ::Day4, Day4::part1, Day4::part2),
)

data class Day(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.ephemient.aoc2024

import kotlin.test.Test
import kotlin.test.assertEquals

class Day4Test {
@Test
fun part1() {
assertEquals(18, Day4(example).part1())
}

@Test
fun part2() {
assertEquals(9, Day4(example).part2())
}

companion object {
private val example =
"""
|MMMSXXMASM
|MSAMXMSMSA
|AMXSXMAAMM
|MSAMASMSMX
|XMASAMXAMM
|XXAMMXXAMA
|SMSMSASXSS
|SAXAMASAAA
|MAMMMXMMMM
|MXMXAXMASX
|""".trimMargin()
}
}
Loading