Skip to content

Commit d0b9353

Browse files
committed
feat(2017): refactor day 9
1 parent f85e3fa commit d0b9353

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

2017/days/9/day09.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/markkovari/advent_of_code/aoc-go-common"
6+
)
7+
8+
type Day09 struct{}
9+
10+
func (d Day09) Part1(input string) string {
11+
score, _ := solve(input)
12+
return fmt.Sprintf("%d", score)
13+
}
14+
15+
func (d Day09) Part2(input string) string {
16+
_, garbage := solve(input)
17+
return fmt.Sprintf("%d", garbage)
18+
}
19+
20+
func solve(input string) (int, int) {
21+
score, depth, garbage, totalGarbage := 0, 0, false, 0
22+
skip := false
23+
for _, c := range input {
24+
if skip { skip = false; continue }
25+
if c == '!' { skip = true; continue }
26+
if garbage {
27+
if c == '>' { garbage = false } else { totalGarbage++ }
28+
} else {
29+
if c == '<' { garbage = true } else if c == '{' { depth++; score += depth } else if c == '}' { depth-- }
30+
}
31+
}
32+
return score, totalGarbage
33+
}
34+
35+
func main() { common.Run(2017, 9, Day09{}) }

0 commit comments

Comments
 (0)