First event, first part
This commit is contained in:
commit
7b25351140
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
#
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
|
||||||
|
# Problem data
|
||||||
|
input*.txt
|
||||||
1000
20231201/input
Normal file
1000
20231201/input
Normal file
File diff suppressed because it is too large
Load Diff
57
20231201/trebuchet.go
Normal file
57
20231201/trebuchet.go
Normal 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)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user