From 05763e8c43149dda7a36d4029f69c376b01e66ab Mon Sep 17 00:00:00 2001 From: Alexander Avery Date: Wed, 4 Dec 2024 13:55:51 -0500 Subject: [PATCH] solve day 4 part 1 --- 2024/day04.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/2024/day04.go b/2024/day04.go index fa801b0..ee446d8 100644 --- a/2024/day04.go +++ b/2024/day04.go @@ -1,13 +1,56 @@ package adventoc2024 -import "io" +import ( + "bufio" + "io" +) type Day4 struct{} func (d Day4) Part1(r io.Reader) string { - return "" + wordsearch := make([][]rune, 0) + s := bufio.NewScanner(r) + var sum int + + for s.Scan() { + line := []rune(s.Text()) + wordsearch = append(wordsearch, line) + } + + search := make([]rune, 4) + for startx := range wordsearch { + for starty := range wordsearch[startx] { + for dx := -1; dx < 2; dx++ { + for dy := -1; dy < 2; dy++ { + for i := 0; i < 4; i++ { + x := startx + dx*i + y := starty + dy*i + search[i] = runeAt(wordsearch, x, y) + } + if string(search) == "XMAS" { + sum++ + } + } + } + + } + } + + return printi(sum) } func (d Day4) Part2(r io.Reader) string { return "" } + +func runeAt(r [][]rune, x, y int) rune { + if x >= len(r) || x < 0 { + return rune(0) + } + + if y >= len(r[x]) || y < 0 { + return rune(0) + } + + return r[x][y] +}