Skip to content

Commit

Permalink
2024: day 4 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
plagov committed Dec 6, 2024
1 parent f2badef commit 9ef851f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
48 changes: 37 additions & 11 deletions src/main/kotlin/io/plagov/advent/aoc2024/Day04.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package io.plagov.advent.aoc2024
class Day04 {

private lateinit var grid: Array<CharArray>
private var count = 0
private var countPartOne = 0

fun partOne(input: String): Int {
grid = input.lines().map { it.toCharArray() }.toTypedArray()


for (row in grid.indices) {
for (col in grid[row].indices) {
val currentChar = grid[row][col]
Expand All @@ -25,7 +24,34 @@ class Day04 {
}
}

return count
return countPartOne
}

fun partTwo(input: String): Int {
grid = input.lines().map { it.toCharArray() }.toTypedArray()
var countPartTwo = 0

for (row in grid.indices) {
for (col in grid[row].indices) {
val center = grid.getElement(row, col)
val downRight = grid.getElement(row + 1, col + 1)
val downLeft = grid.getElement(row + 1, col - 1)
val upLeft = grid.getElement(row - 1, col - 1)
val upRight = grid.getElement(row - 1, col + 1)
if (listOf(downRight, downLeft, upLeft, upRight).any { it == null }) {
continue
}
if ((center == 'A' && downRight == 'S' && downLeft == 'M' && upLeft == 'M' && upRight == 'S') ||
(center == 'A' && downRight == 'S' && downLeft == 'S' && upLeft == 'M' && upRight == 'M') ||
(center == 'A' && downRight == 'M' && downLeft == 'M' && upLeft == 'S' && upRight == 'S') ||
(center == 'A' && downRight == 'M' && downLeft == 'M' && upLeft == 'S' && upRight == 'S') ||
(center == 'A' && downRight == 'M' && downLeft == 'S' && upLeft == 'S' && upRight == 'M')) {
countPartTwo++
}
}
}

return countPartTwo
}

private fun checkUpRight(row: Int, col: Int) {
Expand All @@ -36,7 +62,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -48,7 +74,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -60,7 +86,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -72,7 +98,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -84,7 +110,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -96,7 +122,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -108,7 +134,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand All @@ -120,7 +146,7 @@ class Day04 {
return
}
if (second == 'M' && third == 'A' && forth == 'S') {
count++
countPartOne++
}
}

Expand Down
18 changes: 5 additions & 13 deletions src/test/kotlin/io/plagov/advent/aoc2024/Day04Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ class Day04Test {

private val day4 = Day04()

private val sampleInput = """
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
""".trimIndent()

@Test
fun partOne() {
Assertions.assertEquals(2557, day4.partOne(input))
}

@Test
fun partTwo() {
Assertions.assertEquals(1854, day4.partTwo(input))
}
}

0 comments on commit 9ef851f

Please sign in to comment.