Skip to content

Instantly share code, notes, and snippets.

@glitchcowboy
Created January 3, 2024 15:49
Show Gist options
  • Save glitchcowboy/ad45f82e4f5ecb302f6f82b61cd89f9c to your computer and use it in GitHub Desktop.
Save glitchcowboy/ad45f82e4f5ecb302f6f82b61cd89f9c to your computer and use it in GitHub Desktop.
AoC-Day1.go
package main
import (
"os"
"encoding/csv"
"regexp"
"fmt"
"strings"
"strconv"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
file, err := os.Open("input.txt")
check(err)
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
check(err)
re := regexp.MustCompile(`\d`)
var sum int64 = 0
for _, eachline := range records {
digits := eachline[0]
words := []string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
mod_words := []string{"ze0ro", "on1ne", "tw2wo", "thr3ree", "fou4ur", "fiv5ve", "si6ix", "sev7ven", "eig8ght", "ni9ine"}
for i:=0;i<=9; i++{
digits = strings.ReplaceAll(digits,words[i],mod_words[i])
}
digit_slice := re.FindAllStringSubmatch(digits,-1)
//final calibration number
calibration_string := digit_slice[0][0] + digit_slice[len(digit_slice)-1][0]
calibration_int, err := strconv.ParseInt(calibration_string,10,16)
check(err)
sum += calibration_int
}
fmt.Println(sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment