diff --git a/aoc2024/day03/main.go b/aoc2024/day03/main.go index a609288..31d6474 100644 --- a/aoc2024/day03/main.go +++ b/aoc2024/day03/main.go @@ -28,25 +28,22 @@ func run(read io.Reader) (*result, error) { lexer := Lexer(read) active := true - for typ, _ := range lexer.Iter() { - // fmt.Println(typ, string(token)) - switch { - case typ == "TokMUL": + for lexer.NextTok() { + switch lexer.token { + case "TokMUL": if m := readMul(lexer); m != 0 { sum += m if active { sum2 += m } } - case typ == "TokDONT": + case "TokDONT": if readDont(lexer) { - println("don't()", active) active = false } - case typ == "TokDO": + case "TokDO": if readDo(lexer) { - println("do()", active) active = true } } @@ -57,31 +54,15 @@ func run(read io.Reader) (*result, error) { func Lexer(in io.Reader) *lexer { seq := func(yield func(rune) bool) { - defer func() { - if err := recover(); err != nil { - fmt.Println("ERR", err) - return - } - fmt.Println("NO DEFER AND NO PANIC??") - }() - buf := make([]byte, 256) s, _ := in.Read(buf) buf = buf[:s] for len(buf) > 0 { - for i, r := range string(buf) { - println(i, r) - if i >= 72 { - fmt.Println("START") - } + for _, r := range string(buf) { if !yield(r) { return } - if i >= 72 { - fmt.Println("STOP") - } - } s, _ := in.Read(buf) buf = buf[:s] @@ -153,7 +134,6 @@ func (l *lexer) NextTok() bool { l.token = "TokMUL" if string(l.literal) != "mul" { l.token = "TokILLEGAL" - return false } return true case 'd': @@ -165,7 +145,6 @@ func (l *lexer) NextTok() bool { l.token = "TokDO" default: l.token = "TokILLEGAL" - return false } return true @@ -230,7 +209,6 @@ func readMul(lex *lexer) int { if !lex.NextTok() || lex.token != "TokRPAREN" { return 0 } - fmt.Println(a, "*", b) return a * b } diff --git a/aoc2024/day03/main_test.go b/aoc2024/day03/main_test.go index 08fbc75..259c765 100644 --- a/aoc2024/day03/main_test.go +++ b/aoc2024/day03/main_test.go @@ -26,9 +26,10 @@ func TestExample(t *testing.T) { t.Log(result) is.Equal(result.valuePT1, 161) - // result, err = run(bytes.NewReader(example2)) - // is.NoErr(err) - // is.Equal(result.valuePT2, 48) + result, err = run(bytes.NewReader(example2)) + is.NoErr(err) + is.Equal(result.valuePT1, 161) + is.Equal(result.valuePT2, 48) } func TestSolution(t *testing.T) { @@ -40,5 +41,5 @@ func TestSolution(t *testing.T) { t.Log(result) is.True(result.valuePT1 < 186942148) // first attempt too high is.Equal(result.valuePT1, 184122457) - //is.Equal(result.valuePT2, 0) + is.Equal(result.valuePT2, 107862689) }