aoc/2023/day-01/main.go
2023-12-01 11:02:00 +01:00

61 lines
935 B
Go

package main
import (
"embed"
"fmt"
"io/fs"
"log"
"os"
"strconv"
"strings"
)
//go:embed *
var input embed.FS
func main() {
data, err := fs.ReadFile(input, "input")
if err != nil {
fmt.Printf("ERROR: failed to read input: %s", err)
os.Exit(1)
}
totalVals := 0
lines := strings.Split(string(data), "\n")
for _, v := range lines {
if v == "" {continue}
values := ""
chars := strings.Split(v, "")
for i := 0; i < len(chars); i++ {
num, err := strconv.ParseInt(chars[i], 10, 64)
if err != nil {
continue
}
values = values + fmt.Sprint(num)
break
}
for i := len(chars)-1; i >= 0; i-- {
num, err := strconv.ParseInt(chars[i], 10, 64)
if err != nil {
continue
}
values = values + fmt.Sprint(num)
break
}
int, err := strconv.Atoi(values)
if err != nil {
log.Fatal(err)
}
totalVals = totalVals + int
}
fmt.Printf("The total is: %d\n", totalVals)
}