summaryrefslogtreecommitdiff
path: root/2023/go/internal/day_03/part_01/part_01.go
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2025-11-29 20:23:02 +0100
committerDJ O'Leary <dijitol@proton.me>2025-11-29 20:23:02 +0100
commit4c3a50166bf0f78ce89459c352cc1c6536a8b96d (patch)
tree02e4b66a1435680ef1e92bf82576747b27f16447 /2023/go/internal/day_03/part_01/part_01.go
parentbe2f8528b3829f8263507da301812d852297c168 (diff)
chore: make language folders snake_case
Diffstat (limited to '2023/go/internal/day_03/part_01/part_01.go')
-rw-r--r--2023/go/internal/day_03/part_01/part_01.go123
1 files changed, 123 insertions, 0 deletions
diff --git a/2023/go/internal/day_03/part_01/part_01.go b/2023/go/internal/day_03/part_01/part_01.go
new file mode 100644
index 0000000..49837b3
--- /dev/null
+++ b/2023/go/internal/day_03/part_01/part_01.go
@@ -0,0 +1,123 @@
+package part_01
+
+/**
+ * TRIED:
+ * 530923
+ */
+
+import (
+ "os"
+ "strconv"
+ "strings"
+)
+
+const USE_SAMPLE = true
+
+func Solve() {
+ var content []byte
+ if USE_SAMPLE {
+ content, _ = os.ReadFile("../_input/day_03_part_01_sample.txt")
+ } else {
+ content, _ = os.ReadFile("../_input/day_03.txt")
+ }
+ lines := strings.Split(string(content), "\n")
+
+ validNumbers := walkLines(lines)
+
+ answer := 0
+ for _, number := range validNumbers {
+ answer += number
+ }
+
+ println(answer)
+}
+
+func walkLines(lines []string) []int {
+ var validNumbers []int
+
+ for y := 0; y < len(lines); y++ {
+ line := lines[y]
+
+ if line == "" || line == "\n" {
+ continue
+ }
+
+ // Counts as a symbol...
+ filteredLine := strings.ReplaceAll(line, "\r", "")
+
+ previousWasNumber := false
+ number := ""
+ for x := 0; x < len(filteredLine); x++ {
+ char := filteredLine[x]
+
+ _, err := strconv.Atoi(string(char))
+ isNumeric := err == nil
+
+ if isNumeric {
+ previousWasNumber = true
+ number += string(char)
+ continue
+ }
+
+ if !previousWasNumber {
+ continue
+ }
+
+ isValid := checkValidity(lines, x, y, len(number))
+ if isValid {
+ num, _ := strconv.Atoi(number)
+ validNumbers = append(validNumbers, num)
+ }
+
+ println(number)
+
+ previousWasNumber = false
+ number = ""
+ }
+ }
+
+ return validNumbers
+}
+
+// N = number
+// k = numOfDigits + 1
+// (x-k,y-1) (x-1,y-1) ( x ,y-1)
+// (x-k, y ) N ( x , y )
+// (x-k,y+1) (x-1,y+1) ( x ,y+1)
+func checkValidity(lines []string, x int, y int, numOfDigits int) bool {
+
+ for i := max(y-1, 0); i <= y+1; i++ {
+
+ var debugLine string
+
+ for j := max(x-(numOfDigits+1), 0); j <= x; j++ {
+
+ if lineExists := len(lines) > i; !lineExists {
+ continue
+ }
+
+ if charExists := len(lines[i]) > j; !charExists {
+ continue
+ }
+
+ char := rune(lines[i][j])
+ debugLine += string(char)
+
+ if char == '.' {
+ continue
+ }
+
+ _, err := strconv.Atoi(string(char))
+ isNumeric := err == nil
+ if isNumeric {
+ continue
+ }
+
+ debugLine = "" //nolint:ineffassign // Only used for debugging and needs to be reset
+
+ return true
+ }
+ }
+
+ return false
+}