fix(aoc2024): clean up day 3
This commit is contained in:
parent
b3c36c1af1
commit
c077b91c41
|
@ -25,7 +25,9 @@ func run(read io.Reader) (*result, error) {
|
|||
sum := 0
|
||||
sum2 := 0
|
||||
|
||||
lexer := Lexer(read)
|
||||
lexer, stop := Lexer(read)
|
||||
defer stop()
|
||||
|
||||
active := true
|
||||
|
||||
for lexer.NextTok() {
|
||||
|
@ -52,7 +54,7 @@ func run(read io.Reader) (*result, error) {
|
|||
return &result{sum, sum2}, nil
|
||||
}
|
||||
|
||||
func Lexer(in io.Reader) *lexer {
|
||||
func Lexer(in io.Reader) (*lexer, func()) {
|
||||
seq := func(yield func(rune) bool) {
|
||||
buf := make([]byte, 256)
|
||||
s, _ := in.Read(buf)
|
||||
|
@ -72,10 +74,10 @@ func Lexer(in io.Reader) *lexer {
|
|||
}
|
||||
}
|
||||
next, stop := iter.Pull(seq)
|
||||
lex := lexer{iter: next, stop: stop}
|
||||
lex := lexer{iter: next}
|
||||
lex.readRune()
|
||||
lex.readRune()
|
||||
return &lex
|
||||
return &lex, stop
|
||||
}
|
||||
|
||||
type lexer struct {
|
||||
|
@ -83,15 +85,12 @@ type lexer struct {
|
|||
next rune
|
||||
|
||||
iter func() (rune, bool)
|
||||
stop func()
|
||||
|
||||
buf []rune
|
||||
token string
|
||||
literal []rune
|
||||
}
|
||||
|
||||
func (l *lexer) Stop() { l.stop() }
|
||||
|
||||
func (l *lexer) readRune() {
|
||||
if l.rune == -1 {
|
||||
return
|
||||
|
@ -172,16 +171,6 @@ func (l *lexer) NextTok() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (l *lexer) Iter() iter.Seq2[string, string] {
|
||||
return func(yield func(string, string) bool) {
|
||||
for l.NextTok() {
|
||||
if !yield(l.token, string(l.literal)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readMul(lex *lexer) int {
|
||||
if lex.token != "TokMUL" || string(lex.literal) != "mul" {
|
||||
return 0
|
||||
|
|
Loading…
Reference in New Issue
Block a user