Last active
February 23, 2024 21:17
-
-
Save mattford63/044ac7c54c19db38822c49ef4c11676b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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