Skip to content

Instantly share code, notes, and snippets.

@mattford63
Last active February 23, 2024 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattford63/044ac7c54c19db38822c49ef4c11676b to your computer and use it in GitHub Desktop.
Save mattford63/044ac7c54c19db38822c49ef4c11676b to your computer and use it in GitHub Desktop.
// A file
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func firstRE(r *regexp.Regexp ,s string) string {
return r.FindString(s)
}
func Reverse(s string) (result string) {
for _,v := range s {
result = string(v) + result
}
return
}
func lastRE(r *regexp.Regexp, s string) string {
return r.FindString(Reverse(s))
}
func calibration(lines []string) (total int) {
r, _ := regexp.Compile("[0-9]")
total = 0
for _, l := range lines {
firstdigit := firstRE(r,l)
lastdigit := lastRE(r,l)
numberString := firstdigit + lastdigit
number, _ := strconv.Atoi(numberString)
total += number
}
return
}
func main() {
// example := `1abc2
// pqr3stu8vwx
// a1b2c3d4e5f
// treb7uchet`
// exampleLines := strings.Split(example, "\n")
// exampleCalibration := calibration(exampleLines)
// fmt.Println(exampleCalibration)
dat, err := os.ReadFile("day1.txt")
check(err)
lines := strings.Split(string(dat), "\n")
fmt.Println(calibration(lines))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment