76 lines
978 B
Go
76 lines
978 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
sum := 50
|
|
res1 := 0
|
|
res2 := 0
|
|
|
|
file, err := os.Open("./input.txt")
|
|
//file, err := os.Open("./test.txt")
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
re := regexp.MustCompile("(L|R)(\\w+)")
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
// Since re : "(L|R)(\w+)", we can parse it as the following i
|
|
i := 0
|
|
{
|
|
m := re.FindStringSubmatch(scanner.Text())
|
|
|
|
i, _ = strconv.Atoi(m[2])
|
|
|
|
if m[1] == "L" {
|
|
i *= -1
|
|
}
|
|
}
|
|
|
|
// Handle sum +=i as mod(100) and work w/res2
|
|
{
|
|
// Avoid counting rebundant match
|
|
if sum == 0 && i < 0 {
|
|
res2 -= 1
|
|
}
|
|
|
|
sum += i
|
|
|
|
for sum < 0 {
|
|
sum += 100
|
|
res2 += 1
|
|
}
|
|
for sum > 100 {
|
|
sum -= 100
|
|
res2 += 1
|
|
}
|
|
if sum == 100 {
|
|
sum -= 100
|
|
}
|
|
}
|
|
|
|
// work w/res1
|
|
if sum == 0 {
|
|
res1 += 1
|
|
}
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return
|
|
}
|
|
|
|
fmt.Println(res1, res1+res2)
|
|
}
|