2024 day 1 part 1 - initial version
This commit is contained in:
parent
3809fa6163
commit
71c18eaeba
3
2024/day-04/go.mod
Normal file
3
2024/day-04/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module mziesel.nl/aoc/d4
|
||||
|
||||
go 1.23.0
|
10
2024/day-04/input_test.txt
Normal file
10
2024/day-04/input_test.txt
Normal file
@ -0,0 +1,10 @@
|
||||
MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX
|
108
2024/day-04/main.go
Normal file
108
2024/day-04/main.go
Normal file
@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
content, err := os.ReadFile("./input_test.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file: %s\n", err)
|
||||
}
|
||||
|
||||
var field [][]string
|
||||
|
||||
x_axis := 0
|
||||
y_axis := 0
|
||||
|
||||
for _, b := range content {
|
||||
char := string(b)
|
||||
if char == "\n" {
|
||||
x_axis++
|
||||
y_axis = 0
|
||||
continue
|
||||
}
|
||||
|
||||
if y_axis == 0 {
|
||||
field = append(field, make([]string, 0))
|
||||
}
|
||||
|
||||
field[x_axis] = append(field[x_axis], char)
|
||||
|
||||
y_axis++
|
||||
}
|
||||
|
||||
var found [][]pos
|
||||
|
||||
for x, _ := range field {
|
||||
for y, _ := range field[x] {
|
||||
// empty array for the found chars
|
||||
seq := []pos{}
|
||||
checkXMAS(field, x, y, seq, &found)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("found: ", len(found))
|
||||
}
|
||||
|
||||
type pos struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func seqToString(field [][]string, seq []pos) string {
|
||||
str := ""
|
||||
for _, p := range seq {
|
||||
str += field[p.X][p.Y]
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func checkXMAS(field [][]string, x int, y int, current_seq []pos, found *[][]pos) {
|
||||
bound_x := len(field)
|
||||
if x == -1 || x >= bound_x {
|
||||
return
|
||||
}
|
||||
bound_y := len(field[x])
|
||||
if y == -1 || y >= bound_y {
|
||||
return
|
||||
}
|
||||
|
||||
char := field[x][y]
|
||||
|
||||
seq_str := seqToString(field, current_seq)
|
||||
// fmt.Printf("x: %d y: %d seq: '%s' curr_char: '%s'\n", x, y, seq_str, char)
|
||||
|
||||
if (seq_str == "" && char == "X") ||
|
||||
(seq_str == "X" && char == "M") ||
|
||||
(seq_str == "XM" && char == "A") ||
|
||||
(seq_str == "XMA" && char == "S") {
|
||||
|
||||
pos := pos{X: x, Y: y}
|
||||
current_seq = append(current_seq, pos)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
if seqToString(field, current_seq) == "XMAS" {
|
||||
*found = append(*found, current_seq)
|
||||
}
|
||||
|
||||
// y
|
||||
// x 0 1 2 3
|
||||
// 0 1 2 3
|
||||
// 0 1 2 3
|
||||
// 0 1 2 3
|
||||
|
||||
checkXMAS(field, x+1, y, current_seq, found)
|
||||
checkXMAS(field, x-1, y, current_seq, found)
|
||||
checkXMAS(field, x, y+1, current_seq, found)
|
||||
checkXMAS(field, x, y-1, current_seq, found)
|
||||
|
||||
checkXMAS(field, x+1, y+1, current_seq, found)
|
||||
checkXMAS(field, x+1, y-1, current_seq, found)
|
||||
checkXMAS(field, x-1, y+1, current_seq, found)
|
||||
checkXMAS(field, x-1, y-1, current_seq, found)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user