add aoc 2025 day 3 solution
This commit is contained in:
parent
c851361c45
commit
5ce9c707d3
1 changed files with 65 additions and 0 deletions
65
2025/3/main.go
Normal file
65
2025/3/main.go
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// iterate over all IDs
|
||||||
|
joltages := Decode("input.txt")
|
||||||
|
|
||||||
|
var joltagessum int = 0
|
||||||
|
|
||||||
|
for _, i := range joltages {
|
||||||
|
var tensColumn int = 0
|
||||||
|
var onesColumn int = 0
|
||||||
|
|
||||||
|
for iteration, x := range i {
|
||||||
|
num, err := strconv.Atoi(string(x))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tensColumn == 0 {
|
||||||
|
tensColumn = num
|
||||||
|
} else if num > tensColumn && iteration != len(i)-1 {
|
||||||
|
tensColumn = num
|
||||||
|
onesColumn = 0
|
||||||
|
} else if num > onesColumn && onesColumn >= tensColumn {
|
||||||
|
tensColumn = onesColumn
|
||||||
|
onesColumn = num
|
||||||
|
} else if num > onesColumn {
|
||||||
|
onesColumn = num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum := tensColumn*10 + onesColumn
|
||||||
|
fmt.Printf("joltage found for %s, %d%d %d\n", i, tensColumn, onesColumn, sum)
|
||||||
|
joltagessum = joltagessum + sum
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("total joltage sum", joltagessum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decode(filename string) []string {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
var joltages []string
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
// extract joltage numbers into struct to go over
|
||||||
|
joltages = append(joltages, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
return joltages
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue