From 40fd20224473cccc49cafee598593b4fb9ace8ec Mon Sep 17 00:00:00 2001 From: Nico Date: Tue, 2 Dec 2025 22:42:06 +1100 Subject: [PATCH] add aoc 2025 day 2 solution --- 2025/2/README.md | 8 ++++++ 2025/2/input.txt | 1 + 2025/2/main.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 2025/2/README.md create mode 100644 2025/2/input.txt create mode 100644 2025/2/main.go diff --git a/2025/2/README.md b/2025/2/README.md new file mode 100644 index 0000000..e780b8b --- /dev/null +++ b/2025/2/README.md @@ -0,0 +1,8 @@ +# aoc 2025 day 2 + +run with: +```sh +go run main.go +``` + +you will need to put your input into input.txt first though. replace input.txt with your puzzle input diff --git a/2025/2/input.txt b/2025/2/input.txt new file mode 100644 index 0000000..a6d92df --- /dev/null +++ b/2025/2/input.txt @@ -0,0 +1 @@ +197-407,262128-339499,557930-573266,25-57,92856246-93001520,2-12,1919108745-1919268183,48414903-48538379,38342224-38444598,483824-534754,1056-1771,4603696-4688732,75712519-75792205,20124-44038,714164-782292,4429019-4570680,9648251-9913729,6812551522-6812585188,58-134,881574-897488,648613-673853,5261723647-5261785283,60035-128980,9944818-10047126,857821365-857927915,206885-246173,1922-9652,424942-446151,408-1000 diff --git a/2025/2/main.go b/2025/2/main.go new file mode 100644 index 0000000..a3db9aa --- /dev/null +++ b/2025/2/main.go @@ -0,0 +1,75 @@ +package main + +import ( + "fmt" + "os" + "regexp" + "strconv" + "strings" +) + +type Range struct { + From int + To int +} + +func main() { + // iterate over all IDs + var invalidsum int = 0 + ranges := Decode("input.txt") + + for _, i := range ranges { + fmt.Printf("\niterating over range: %d to %d\n", i.From, i.To) + + // +1 makes the range inclusive + for x := range i.To-i.From+1 { + // to get the "real" number as we took it away initially + num := x+i.From + + // extract half of the number + numstring := strconv.Itoa(num) + mid := len(numstring)/2 + + // if both halves are the same, it is an invalid number + if numstring[:mid] == numstring[mid:] { + fmt.Printf("found invalid id %d\n", num) + invalidsum = invalidsum + num + } + } + } + + fmt.Println("sum of invalid ids:", invalidsum) +} + +func Decode(filename string) []Range { + f, err := os.ReadFile(filename) + + if err != nil { + panic(err) + } + + fstring := string(f) + fromrangeregex := regexp.MustCompile("^[0-9]*") + torangeregex := regexp.MustCompile("[0-9]*$") + + var ranges []Range + + for _, i := range strings.Split(fstring, ",") { + // get range from IDs to iterate over + fromrangestr := fromrangeregex.FindAllString(i, 1) + torangestr := torangeregex.FindAllString(i, 1) + + var a Range + + fromrange, _ := strconv.Atoi(fromrangestr[0]) + torange, _ := strconv.Atoi(torangestr[0]) + + a.From = fromrange + a.To = torange + + fmt.Printf("\nextracted ranges to iterate over: %d to %d\n", fromrange, torange) + ranges = append(ranges, a) + } + + return ranges +}