chore: state before changes that were working
This commit is contained in:
parent
669e2cfb2c
commit
7b9a71f84c
|
@ -24,36 +24,18 @@ func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
|||
func run(read io.Reader) (*result, error) {
|
||||
|
||||
sum := 0
|
||||
sum2 := 0
|
||||
|
||||
lexer := Lexer(read)
|
||||
active := true
|
||||
for typ, token := range lexer.Iter() {
|
||||
|
||||
for typ, _ := range lexer.Iter() {
|
||||
// fmt.Println(typ, string(token))
|
||||
switch {
|
||||
case typ == "TokMUL":
|
||||
if m := readMul(lexer); m != 0 {
|
||||
sum += m
|
||||
if active {
|
||||
sum2 += m
|
||||
}
|
||||
}
|
||||
case typ == "TokDONT":
|
||||
if readDont(lexer) {
|
||||
println("don't()", active)
|
||||
active = false
|
||||
}
|
||||
|
||||
case typ == "TokDO":
|
||||
if readDo(lexer) {
|
||||
println("do()", active)
|
||||
active = true
|
||||
}
|
||||
if m := readMul(lexer); m != 0 {
|
||||
fmt.Println(typ, string(token))
|
||||
sum += m
|
||||
}
|
||||
}
|
||||
|
||||
return &result{sum, sum2}, nil
|
||||
|
||||
return &result{sum, -1}, nil
|
||||
}
|
||||
|
||||
func Lexer(in io.Reader) *lexer {
|
||||
|
@ -63,8 +45,7 @@ func Lexer(in io.Reader) *lexer {
|
|||
buf = buf[:s]
|
||||
|
||||
for len(buf) > 0 {
|
||||
for i, r := range string(buf) {
|
||||
println(i, r)
|
||||
for _, r := range string(buf) {
|
||||
if !yield(r) {
|
||||
return
|
||||
}
|
||||
|
@ -90,8 +71,8 @@ type lexer struct {
|
|||
iter func() (rune, bool)
|
||||
stop func()
|
||||
|
||||
buf []rune
|
||||
token string
|
||||
buf []rune
|
||||
token string
|
||||
literal []rune
|
||||
}
|
||||
|
||||
|
@ -137,24 +118,7 @@ func (l *lexer) NextTok() bool {
|
|||
case 'm':
|
||||
l.loadString("mul")
|
||||
l.token = "TokMUL"
|
||||
if string(l.literal) != "mul" {
|
||||
l.token = "TokILLEGAL"
|
||||
return false
|
||||
}
|
||||
return true
|
||||
case 'd':
|
||||
l.loadString("don't")
|
||||
switch string(l.literal) {
|
||||
case "don't":
|
||||
l.token = "TokDONT"
|
||||
case "do":
|
||||
l.token = "TokDO"
|
||||
default:
|
||||
l.token = "TokILLEGAL"
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
case '(':
|
||||
l.loadRune("TokLPAREN")
|
||||
return true
|
||||
|
@ -198,7 +162,7 @@ func readMul(lex *lexer) int {
|
|||
if !lex.NextTok() || lex.token != "TokLPAREN" {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
if !lex.NextTok() || lex.token != "TokNUMBER" {
|
||||
return 0
|
||||
}
|
||||
|
@ -219,29 +183,3 @@ func readMul(lex *lexer) int {
|
|||
fmt.Println(a, "*", b)
|
||||
return a * b
|
||||
}
|
||||
|
||||
func readDont(lex *lexer) bool {
|
||||
if lex.token != "TokDONT" || string(lex.literal) != "don't" {
|
||||
return false
|
||||
}
|
||||
if !lex.NextTok() || lex.token != "TokLPAREN" {
|
||||
return false
|
||||
}
|
||||
if !lex.NextTok() || lex.token != "TokRPAREN" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func readDo(lex *lexer) bool {
|
||||
if lex.token != "TokDO" || string(lex.literal) != "do" {
|
||||
return false
|
||||
}
|
||||
if !lex.NextTok() || lex.token != "TokLPAREN" {
|
||||
return false
|
||||
}
|
||||
if !lex.NextTok() || lex.token != "TokRPAREN" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -26,10 +26,9 @@ 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.valuePT2, 48)
|
||||
}
|
||||
|
||||
func TestSolution(t *testing.T) {
|
||||
|
@ -41,5 +40,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, 0)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user