commit 9d630fd82893c687cacbe684ca8c975b28be58d2 Author: Luna Komorebi Date: Mon Dec 18 14:33:45 2023 +0100 Add .gitignore and main.go with input parsing logic diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06c798b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +input.txt diff --git a/day1/example.txt b/day1/example.txt new file mode 100644 index 0000000..7bbc69a --- /dev/null +++ b/day1/example.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet diff --git a/day1/main.go b/day1/main.go new file mode 100644 index 0000000..f6f19a0 --- /dev/null +++ b/day1/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "io" + "os" + "regexp" + "strconv" + "strings" +) + +func readInput() string { + stdin, err := io.ReadAll(os.Stdin) + + if err != nil { + panic(err) + } + str := string(stdin) + return str +} + +func main() { + input := readInput() + lines := strings.Split(input, "\n") + re := regexp.MustCompile(`\d`) + var answer int + for _, l := range lines { + matches := re.FindAllString(l, -1) + matches_len := len(matches) + if matches_len == 0 { + continue + } + first := matches[0] + last := matches[matches_len-1] + num_str := first + last + num, err := strconv.Atoi(num_str) + if err != nil { + panic(err) + } + answer = answer + num + } + println(answer) +}