feat(aoc2024): complete day 3 part 2
Some checks failed
Go Test / build (pull_request) Failing after 57s
Some checks failed
Go Test / build (pull_request) Failing after 57s
This commit is contained in:
parent
127257c494
commit
6a17669107
|
@ -28,25 +28,22 @@ func run(read io.Reader) (*result, error) {
|
||||||
lexer := Lexer(read)
|
lexer := Lexer(read)
|
||||||
active := true
|
active := true
|
||||||
|
|
||||||
for typ, _ := range lexer.Iter() {
|
for lexer.NextTok() {
|
||||||
// fmt.Println(typ, string(token))
|
switch lexer.token {
|
||||||
switch {
|
case "TokMUL":
|
||||||
case typ == "TokMUL":
|
|
||||||
if m := readMul(lexer); m != 0 {
|
if m := readMul(lexer); m != 0 {
|
||||||
sum += m
|
sum += m
|
||||||
if active {
|
if active {
|
||||||
sum2 += m
|
sum2 += m
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case typ == "TokDONT":
|
case "TokDONT":
|
||||||
if readDont(lexer) {
|
if readDont(lexer) {
|
||||||
println("don't()", active)
|
|
||||||
active = false
|
active = false
|
||||||
}
|
}
|
||||||
|
|
||||||
case typ == "TokDO":
|
case "TokDO":
|
||||||
if readDo(lexer) {
|
if readDo(lexer) {
|
||||||
println("do()", active)
|
|
||||||
active = true
|
active = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,31 +54,15 @@ func run(read io.Reader) (*result, error) {
|
||||||
|
|
||||||
func Lexer(in io.Reader) *lexer {
|
func Lexer(in io.Reader) *lexer {
|
||||||
seq := func(yield func(rune) bool) {
|
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)
|
buf := make([]byte, 256)
|
||||||
s, _ := in.Read(buf)
|
s, _ := in.Read(buf)
|
||||||
buf = buf[:s]
|
buf = buf[:s]
|
||||||
|
|
||||||
for len(buf) > 0 {
|
for len(buf) > 0 {
|
||||||
for i, r := range string(buf) {
|
for _, r := range string(buf) {
|
||||||
println(i, r)
|
|
||||||
if i >= 72 {
|
|
||||||
fmt.Println("START")
|
|
||||||
}
|
|
||||||
if !yield(r) {
|
if !yield(r) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if i >= 72 {
|
|
||||||
fmt.Println("STOP")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
s, _ := in.Read(buf)
|
s, _ := in.Read(buf)
|
||||||
buf = buf[:s]
|
buf = buf[:s]
|
||||||
|
@ -153,7 +134,6 @@ func (l *lexer) NextTok() bool {
|
||||||
l.token = "TokMUL"
|
l.token = "TokMUL"
|
||||||
if string(l.literal) != "mul" {
|
if string(l.literal) != "mul" {
|
||||||
l.token = "TokILLEGAL"
|
l.token = "TokILLEGAL"
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
case 'd':
|
case 'd':
|
||||||
|
@ -165,7 +145,6 @@ func (l *lexer) NextTok() bool {
|
||||||
l.token = "TokDO"
|
l.token = "TokDO"
|
||||||
default:
|
default:
|
||||||
l.token = "TokILLEGAL"
|
l.token = "TokILLEGAL"
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
@ -230,7 +209,6 @@ func readMul(lex *lexer) int {
|
||||||
if !lex.NextTok() || lex.token != "TokRPAREN" {
|
if !lex.NextTok() || lex.token != "TokRPAREN" {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
fmt.Println(a, "*", b)
|
|
||||||
return a * b
|
return a * b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,10 @@ func TestExample(t *testing.T) {
|
||||||
t.Log(result)
|
t.Log(result)
|
||||||
is.Equal(result.valuePT1, 161)
|
is.Equal(result.valuePT1, 161)
|
||||||
|
|
||||||
// result, err = run(bytes.NewReader(example2))
|
result, err = run(bytes.NewReader(example2))
|
||||||
// is.NoErr(err)
|
is.NoErr(err)
|
||||||
// is.Equal(result.valuePT2, 48)
|
is.Equal(result.valuePT1, 161)
|
||||||
|
is.Equal(result.valuePT2, 48)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSolution(t *testing.T) {
|
func TestSolution(t *testing.T) {
|
||||||
|
@ -40,5 +41,5 @@ func TestSolution(t *testing.T) {
|
||||||
t.Log(result)
|
t.Log(result)
|
||||||
is.True(result.valuePT1 < 186942148) // first attempt too high
|
is.True(result.valuePT1 < 186942148) // first attempt too high
|
||||||
is.Equal(result.valuePT1, 184122457)
|
is.Equal(result.valuePT1, 184122457)
|
||||||
//is.Equal(result.valuePT2, 0)
|
is.Equal(result.valuePT2, 107862689)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user