day1, part1

This commit is contained in:
efim 2023-12-01 14:01:02 +00:00
parent 4f286742bb
commit c1794a56ec
4 changed files with 1064 additions and 2 deletions

53
day1/day-one.go Normal file
View File

@ -0,0 +1,53 @@
package dayone
import (
"bufio"
"fmt"
"log"
"os"
"unicode"
)
func Run() {
file, err := os.Open("./day1/example.txt")
if err != nil {
log.Print("error opening file")
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
result := 0
for scanner.Scan() {
line := scanner.Text()
first := getFirstDigit(line)
last := getLastDigit(line)
num := first * 10 + last
result += num
}
fmt.Printf(">> day 1 result is %d", result)
}
func getFirstDigit(line string) int {
for _, ch := range line {
if unicode.IsDigit(ch) {
return int(ch - '0')
}
}
return 0
}
func getLastDigit(line string) int {
for i := len(line) - 1; i >= 0; i-- {
simbols := []rune(line)
ch := simbols[i]
if unicode.IsDigit(ch) {
return int(ch - '0')
}
}
return 0
}

4
day1/example.txt Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

1000
day1/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,13 @@
package main package main
import "fmt" import (
"fmt"
dayone "sunshine.industries/aoc2023/day1"
)
func main() { func main() {
s := "yoyo" s := "yoyo!"
fmt.Printf("hello, %s\n", s) fmt.Printf("hello, %s\n", s)
dayone.Run()
} }