adventofcode/2021/day10/main.go

95 lines
1.9 KiB
Go

package main
import (
"fmt"
"sort"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
fix(h.StdinToStringSlice())
}
func fix(inp []string) {
var clean []string
var fixScores []int
var score int
for i := range inp {
var opens []byte
valid := true
for _, b := range inp[i] {
switch b {
case '(':
opens = append(opens, byte(b))
case '[':
opens = append(opens, byte(b))
case '{':
opens = append(opens, byte(b))
case '<':
opens = append(opens, byte(b))
case ')':
if opens[len(opens)-1] != '(' {
valid = false
score += 3
} else {
opens = opens[:len(opens)-1]
}
case ']':
if opens[len(opens)-1] != '[' {
valid = false
score += 57
} else {
opens = opens[:len(opens)-1]
}
case '}':
if opens[len(opens)-1] != '{' {
valid = false
score += 1197
} else {
opens = opens[:len(opens)-1]
}
case '>':
if opens[len(opens)-1] != '<' {
valid = false
score += 25137
} else {
opens = opens[:len(opens)-1]
}
}
if !valid {
break
}
}
if valid {
var fixLineScore int
for bi := len(opens) - 1; bi >= 0; bi-- {
switch opens[bi] {
case '(':
inp[i] = inp[i] + string(')')
fixLineScore = (fixLineScore * 5) + 1
case '[':
inp[i] = inp[i] + string(']')
fixLineScore = (fixLineScore * 5) + 2
case '{':
inp[i] = inp[i] + string('}')
fixLineScore = (fixLineScore * 5) + 3
case '<':
inp[i] = inp[i] + string('>')
fixLineScore = (fixLineScore * 5) + 4
}
}
clean = append(clean, inp[i])
fixScores = append(fixScores, fixLineScore)
}
}
sort.Ints(fixScores)
acScore := fixScores[len(fixScores)/2]
fmt.Println("# Part 1")
fmt.Println("Syntax Checker Score:", score)
fmt.Println()
fmt.Println("# Part 2")
fmt.Println("Autocomplete Score:", acScore)
}