2017 Day 9 Complete

This commit is contained in:
Brian Buller 2017-12-09 10:19:22 -06:00
parent d4d4e5c91c
commit 52faafccdf
3 changed files with 106 additions and 0 deletions

90
2017/day09/day09.go Normal file
View File

@ -0,0 +1,90 @@
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
}

1
2017/day09/input Normal file

File diff suppressed because one or more lines are too long

15
2017/day09/test.input Normal file
View File

@ -0,0 +1,15 @@
{}
{{{}}}
{{},{}}
{{{},{},{{}}}}
{<a>,<a>,<a>,<a>}
{{<ab>},{<ab>},{<ab>},{<ab>}}
{{<!!>},{<!!>},{<!!>},{<!!>}}
{{<a!>},{<a!>},{<a!>},{<ab>}}
{<>}
{<random characters>}
{<<<<>}
{<{!>}>}
{<!!>}
{<!!!>>}
{<{o"i!a,<{i<a>}