Add .gitignore and main.go with input parsing logic
This commit is contained in:
commit
9d630fd828
3 changed files with 47 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
input.txt
|
4
day1/example.txt
Normal file
4
day1/example.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
42
day1/main.go
Normal file
42
day1/main.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Add table
Reference in a new issue