add aoc 2025 day 2 solution
This commit is contained in:
parent
2910d6313c
commit
40fd202244
3 changed files with 84 additions and 0 deletions
8
2025/2/README.md
Normal file
8
2025/2/README.md
Normal file
|
|
@ -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
|
||||||
1
2025/2/input.txt
Normal file
1
2025/2/input.txt
Normal file
|
|
@ -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
|
||||||
75
2025/2/main.go
Normal file
75
2025/2/main.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue