Skip to content

Commit

Permalink
[2024] Solution for Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarnung committed Dec 4, 2024
1 parent 1dced51 commit 1ecd5d2
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
128 changes: 128 additions & 0 deletions 2024/day04/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"fmt"
"log"
"os"

"github.com/kfarnung/advent-of-code/2024/lib"
)

var xmasString []rune = []rune("XMAS")

func part1(input string) int64 {
parsedInput := parseInput(input)
count := int64(0)
for x, line := range parsedInput {
for y, char := range line {
if char != 'X' {
continue
}

if findXmas(parsedInput, x, y, 1, 0) {
count++
}

if findXmas(parsedInput, x, y, 0, 1) {
count++
}

if findXmas(parsedInput, x, y, 1, 1) {
count++
}

if findXmas(parsedInput, x, y, 1, -1) {
count++
}

if findXmas(parsedInput, x, y, -1, 0) {
count++
}

if findXmas(parsedInput, x, y, 0, -1) {
count++
}

if findXmas(parsedInput, x, y, -1, -1) {
count++
}

if findXmas(parsedInput, x, y, -1, 1) {
count++
}
}
}

return count
}

func part2(input string) int64 {
parsedInput := parseInput(input)
count := int64(0)
for x, line := range parsedInput {
for y, char := range line {
if char != 'A' {
continue
}

if x == 0 || y == 0 || x == len(parsedInput)-1 || y == len(parsedInput[x])-1 {
continue
}

if parsedInput[x-1][y-1] == 'M' && parsedInput[x+1][y+1] == 'S' && parsedInput[x-1][y+1] == 'M' && parsedInput[x+1][y-1] == 'S' {
count++
}

if parsedInput[x-1][y-1] == 'M' && parsedInput[x+1][y+1] == 'S' && parsedInput[x-1][y+1] == 'S' && parsedInput[x+1][y-1] == 'M' {
count++
}

if parsedInput[x-1][y-1] == 'S' && parsedInput[x+1][y+1] == 'M' && parsedInput[x-1][y+1] == 'S' && parsedInput[x+1][y-1] == 'M' {
count++
}

if parsedInput[x-1][y-1] == 'S' && parsedInput[x+1][y+1] == 'M' && parsedInput[x-1][y+1] == 'M' && parsedInput[x+1][y-1] == 'S' {
count++
}
}
}

return count
}

func findXmas(parsedInput [][]rune, x, y int, dx, dy int) bool {
for i := 0; i < len(xmasString); i++ {
if x < 0 || x >= len(parsedInput) || y < 0 || y >= len(parsedInput[x]) || parsedInput[x][y] != xmasString[i] {
return false
}

x += dx
y += dy
}

return true
}

func parseInput(input string) [][]rune {
var result [][]rune
for _, line := range lib.SplitLines(input) {
if len(line) == 0 {
continue
}

result = append(result, []rune(line))
}

return result
}

func main() {
name := os.Args[1]
content, err := lib.LoadFileContent(name)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Part 1: %d\n", part1(content))
fmt.Printf("Part 2: %d\n", part2(content))
}
45 changes: 45 additions & 0 deletions 2024/day04/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"strings"
"testing"

"github.com/kfarnung/advent-of-code/2024/lib"
"github.com/stretchr/testify/assert"
)

var input = strings.Join([]string{
"MMMSXXMASM",
"MSAMXMSMSA",
"AMXSXMAAMM",
"MSAMASMSMX",
"XMASAMXAMM",
"XXAMMXXAMA",
"SMSMSASXSS",
"SAXAMASAAA",
"MAMMMXMMMM",
"MXMXAXMASX",
"",
}, "\n")

func TestPart1(t *testing.T) {
assert.Equal(t, int64(18), part1(input))

inputContent, err := lib.GetInputContent()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, int64(2583), part1(inputContent))
}

func TestPart2(t *testing.T) {
assert.Equal(t, int64(9), part2(input))

inputContent, err := lib.GetInputContent()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, int64(1978), part2(inputContent))
}
2 changes: 1 addition & 1 deletion private

0 comments on commit 1ecd5d2

Please sign in to comment.