adventofcode/2017/day09/day09.go
2017-12-09 10:19:22 -06:00

91 lines
1.3 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
)
func main() {
inp := StdinToStrings()
for i := range inp {
ln, gb := RemoveGarbage(inp[i])
fmt.Print(ln)
v := FindGroupValue(ln)
fmt.Println(" :: Value:", v, ":: Garbage:", gb)
}
}
func FindGroupValue(inp string) int {
var ret, depth int
for i := 0; i < len(inp); i++ {
switch inp[i] {
case '{':
depth++
ret += depth
case '}':
depth--
}
}
return ret
}
func GetOuterGroup(inp string) string {
var ret string
gCount := 1
for i := range inp[1:] {
if gCount == 0 {
break
}
ret += string(inp[i])
switch inp[i] {
case '{':
gCount++
case '}':
gCount--
}
}
return ret
}
func RemoveGarbage(inp string) (string, int) {
var ret string
var isGarbage, ignore bool
var cnt int
for i := 0; i < len(inp); i++ {
if isGarbage {
if ignore {
ignore = false
continue
}
switch inp[i] {
case '!':
ignore = true
continue
case '>':
isGarbage = false
default:
cnt++
}
} else {
switch inp[i] {
case '<':
isGarbage = true
default:
ret += string(inp[i])
}
}
}
return ret, cnt
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}