From 915065d2e66fbf0df61384864cf55dad6ea3a4ea Mon Sep 17 00:00:00 2001 From: PedroEdiaz Date: Mon, 1 Dec 2025 16:55:48 -0600 Subject: [PATCH] 2025: 01 --- .gitignore | 1 + 2025/01/main.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2025/01/test.txt | 10 +++++++ 2025/Makefile | 7 +++++ 4 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 2025/01/main.go create mode 100644 2025/01/test.txt create mode 100644 2025/Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..989478d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**.txt diff --git a/2025/01/main.go b/2025/01/main.go new file mode 100644 index 0000000..1d03b1c --- /dev/null +++ b/2025/01/main.go @@ -0,0 +1,75 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "regexp" + "strconv" +) + +func main() { + sum := 50 + res1 := 0 + res2 := 0 + + file, err := os.Open("./input.txt") + //file, err := os.Open("./test.txt") + + if err != nil { + return + } + + defer file.Close() + + re := regexp.MustCompile("(L|R)(\\w+)") + + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + // Since re : "(L|R)(\w+)", we can parse it as the following i + i := 0 + { + m := re.FindStringSubmatch(scanner.Text()) + + i, _ = strconv.Atoi(m[2]) + + if m[1] == "L" { + i *= -1 + } + } + + // Handle sum +=i as mod(100) and work w/res2 + { + // Avoid counting rebundant match + if sum == 0 && i < 0 { + res2 -= 1 + } + + sum += i + + for sum < 0 { + sum += 100 + res2 += 1 + } + for sum > 100 { + sum -= 100 + res2 += 1 + } + if sum == 100 { + sum -= 100 + } + } + + // work w/res1 + if sum == 0 { + res1 += 1 + } + } + + if err := scanner.Err(); err != nil { + return + } + + fmt.Println(res1, res1+res2) +} diff --git a/2025/01/test.txt b/2025/01/test.txt new file mode 100644 index 0000000..53287c7 --- /dev/null +++ b/2025/01/test.txt @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 diff --git a/2025/Makefile b/2025/Makefile new file mode 100644 index 0000000..78252ad --- /dev/null +++ b/2025/Makefile @@ -0,0 +1,7 @@ +INPUT = \ + 01/input.txt + +.SUFFIXES: .txt + +$(INPUT): + curl "https://adventofcode.com/2025/day/`echo $@|sed -r 's,^0*(\w+/input).txt$$,\1,'`" -H 'Cookie: session=$(SESSION)' > $@