First event, first part

This commit is contained in:
2023-12-01 19:28:01 +01:00
commit 7b25351140
3 changed files with 1081 additions and 0 deletions

1000
20231201/input Normal file

File diff suppressed because it is too large Load Diff

57
20231201/trebuchet.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func ExtractCalibration(s string) string {
firstDigit := 0
lastDigit := 0
index := 0
for i := 0; i < len(s); i++ {
if num, err := strconv.Atoi(string(s[i])); err == nil {
firstDigit = num
index = i
break
}
}
for j := len(s) - 1; j >= index; j-- {
if num, err := strconv.Atoi(string(s[j])); err == nil {
lastDigit = num
break
}
}
return fmt.Sprint(firstDigit) + fmt.Sprint(lastDigit)
}
func main() {
file, err := os.Open("input")
check(err)
var total int = 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
calibration := ExtractCalibration(line)
num, err := strconv.Atoi(calibration)
_ = err
total += num
}
fmt.Println(total)
}