solutions for the Advent of Code 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
861 B

package main
import (
"bufio"
"fmt"
"io"
"strconv"
"unicode"
)
type day01 struct {
}
type buffer struct {
first bool
d1 rune
last bool
d2 rune
}
func (b *buffer) value() (value uint64) {
var d2 rune
if b.last {
d2 = b.d2
} else {
d2 = b.d1
}
str := fmt.Sprintf("%c%c", b.d1, d2)
value, _ = strconv.ParseUint(str, 10, 64)
b.first, b.last = false, false
return
}
func (b *buffer) record(digit rune) {
if !b.first {
b.d1 = digit
b.first = true
} else {
b.d2 = digit
b.last = true
}
}
func (d day01) solve(r io.Reader) string {
var (
buffer buffer
total uint64
buf = bufio.NewReader(r)
)
for {
b, _, err := buf.ReadRune()
if err != nil {
return fmt.Sprintf("%d", total+buffer.value())
}
if unicode.IsDigit(b) {
buffer.record(b)
} else if b == '\n' {
total += buffer.value()
}
}
}