Day 10 solution

This commit is contained in:
2021-12-10 15:07:12 +00:00
parent 156eb5f2de
commit 8950ee2e3e
7 changed files with 336 additions and 0 deletions

View File

@@ -102,3 +102,12 @@ The solution for "nine" is:
The sum of the risk level of low points is 439 The sum of the risk level of low points is 439
The product of the three largest basins is 900900 The product of the three largest basins is 900900
``` ```
### Day ten
```sh
$ ./aoc2021 --ten
The solution for "ten" is:
The syntax error score is 323613
The autoComplete score is 3103006161
```

View File

@@ -12,6 +12,7 @@ import (
"unsupervised.ca/aoc2021/one" "unsupervised.ca/aoc2021/one"
"unsupervised.ca/aoc2021/seven" "unsupervised.ca/aoc2021/seven"
"unsupervised.ca/aoc2021/six" "unsupervised.ca/aoc2021/six"
"unsupervised.ca/aoc2021/ten"
"unsupervised.ca/aoc2021/three" "unsupervised.ca/aoc2021/three"
"unsupervised.ca/aoc2021/two" "unsupervised.ca/aoc2021/two"
) )
@@ -49,6 +50,8 @@ func main() {
day = eight.Init("eight/input.txt") day = eight.Init("eight/input.txt")
case "nine": case "nine":
day = nine.Init("nine/input.txt") day = nine.Init("nine/input.txt")
case "ten":
day = ten.Init("ten/input.txt")
default: default:
fmt.Printf("%q does not have a solution.\n", flagParts[1]) fmt.Printf("%q does not have a solution.\n", flagParts[1])
help() help()

90
ten/input.txt Normal file
View File

@@ -0,0 +1,90 @@
(((<(([{{((<[[<><>]<()[]>]{[{}()]<(){}>}>[<([]{})>])(<<(<><>>[<><>]>(<{}()><(){}>)>[(([])<[]()>)])){<((<{}()
{(({({<[[[{[{({}{})[[]{}]}[{(){}}((){})]][{((){}){[]{}}}<(()())<<><>>>]}]]]{([<{<<()()>([][])>({[]()}[{}<
[[{<(([<<{(<[[()()]]{[[]()]<{}>}><{{{}()}<<>[]>><(())[()[]]>>)([{[[][]][()()]}{{()()}}](<{{}()}>))}[([{<<
{<[{<{[({[[[[{<>{}}<{}()>]<{<><>}{[][]}>](<<<>[]>[()()]>[{[]{}>[<>()]])]{{{[()()][<><>]}<[{}()][{}{}]
(<[{[{[((<{[[<()()><{}{}>][(<>{}){{}{}}]]{<[<><>]>{{{}[]}(<>[])}}}(<[[[]{}](<>{})]{[[][]][[][]]}>)>)){<{
<<<(<(<[[[<[{{[]<>}([]())}([[]])]([[[]<>][()()]]{<()()>([]{})])>]{[[<{[]{}}([]{})>{(<>[]){{}{}}}]]{({{<>[]
[{<(({(<{<{<<{<><>}>({<><>}<<><>>)}{[{[]<>}[[][]]][<()<>>]}}<[[[()()]{<><>}]((<>)[{}()])]{<{()<>}
<[([{{{({<({(<(){}><[]()>)((()())[[]()})})[<{<<>>((){})}<[<><>]{(){}}>>{{(<>[])({}[])}(<()<>>)}]>})}({{({
{[{{(<[[[<(((([]())[[]<>]){(()())[[]<>]})[{([]<>)[<><>]}<({}{})>])>{[(<<<>{}>(()<>)>)]<{([<><>]<[]()>><<{}[
(<<[<({[(<{[({<><>}<{}<>>)]}<<([<>[]])(<{}{}>[[]{}])>>>(([{[<>[]][{}[])}<(<><>)({}[])>](<<()
{{[<(((<([{(<[<>{}]({}())>){{[{}]<[][]>}[{<>()}{<><>}]}}][<<{<[]<>>[{}<>]}[<{}{}>]>[<[[]<>]<()()>>[[[]
<<<([([{{<({[{[][]}[{}<>]]}<[(()<>)[{}<>]]>){{{[()<>]}(<<>>({}[]))}(([[]()][[]{}])<([]<>)[<><
({[[[([[([<{<{{}()}{<><>}>}>])[{(({([][])[{}()]}{(<><>)[[][]]})<<<{}>[{}[]]>(({}())<()<>>)>)
<[{[[[<({<<[{<<>>{{}{}}}{[()[]]}]<[<[]<>>{<>{}}]>>>((<{{[]{}}{<><>}}>[{({}{})<{}<>>}]))}[({
<({[<{(([[[{<<()<>>((){})><{()[]}<{}()>}}[([<>{}][()<>])]]<(([(){}])<{(){}}[[]()]>)<{[(){}](<>[
[<<{{[<<{[<{({<>[]})({{}<>})}><<<<()[]>(()<>)>>[{{[][]}[[][]]}((<>)<<><>>)]>][(<{[[][]]<[]<>>}>(<((){}){[]<
({[({(<{<<<<{({}())(()<>)}><({{}[]}<<><>>){[{}<>]}>>]<((([{}{}]<[][]>)({[][]}<{}()>)))<<{[[]<>][[]()]}{<<><
<[<<([<[(<<{[[(){}]<[][]>]<({}<>)[(){}]>}<[({}[]){()()}]({{}[]}[()()])>>><(<({{}<>}{[]()})[(<>
[[[([<<[<[<{[[(){}]((){})][(()<>)<()>]}(<{[]{}}{[]}>)>]{{[{{<><>}<()<>>}{[(){}]<<>{}>}]([[[]<
((([[[{<{[<{[{<>{}}]}({<[]>[{}{}]}<({}{})([][])>}>(((({}{})<{}()>){<<>{}>{{}<>}})(<{{}<>}{{}}
[<[{[({<[({{{<<>{}><()[]>}}{([<>()]{{}[]})({()[]}[<>[]])}}[{(<()[]>([]{}))<<()<>><[]()>>}{[<<>
<{{([({<{([<<(()<>)[[]{}]><{()()}<<><>>>>]){{<{{()()}<()<>>}>[{(<><>)<[]()>}]}[((<{}()>){[()[]]{()()}})]
<{[([{{[[[<<[<()<>><[]()>][(<>())(()<>)]>(([{}{}]([][])){<[]<>>([]{})})>{[[<{}[]>((){})]{<<>
{[<([{({{({({[[]()]{(){}}}<([]{})[<>{}]>)<<[()[]]>([()[]])>}{[(({}<>)<()()>)[{(){}}<{}{}>]]}>{[[{
<<({{<<{<[{[{({}{})[()]}(<{}{}><[]()})]<{[[][]]({}[])}{(<>{})<()[]>}>}<[({{}<>}{<>()}){({}[
([[[<<{<([[(({<>()}{[][]})[[()<>][()]]){{{()[]}(<>[])}<([]<>)[[]<>]>}]])>{(({{{[<>{}]{[][]}}}}<[(({}[])<<><>
[<(({<[[{([{<<()()>(()<>)><<[]{}>{()<>}>}{<<{}>>{{[]{}}<()<>>}}]{<{[{}<>]([][])}}<{[()()][{}]}>})(([[<{}
<{(<{(((<{<<(<<>>{<>{}})[([]<>)(()[])]><[([]{})((){})}<<{}[]>(<><>)>>><{{{<>{}}(<>[])}{[()()][()
<{<[<<[({<[[<{{}[]}><({}{})({}[])>]]<[<{{}<>}>]({[{}]{<>[]}})>>[<(([{}<>]){<<>[]>[<><>]})[(<{}()>{[
(([[(([<<(<(<{()()}(<><>)><(<>{})<[][]>>)[<((){})<{}{}>>{{()()}{<>{}}}]>[<<<()()>(<>[])><(()[
<{({{({<[(<{<(<>{})<<>{}>>(({}[]))}(((<>())<{}<>>)(<()>))>([[([]<>)<{}[]>][[<><>]<(){}>]][{<(){}>(
<<{(<(({[<<{[(<><>)[<>{}]]{<[]()>({}{})}}<(<{}[]>)[{<>()}<[]<>>])>>]{((({(<>[]){()<>}}[{[][
(([((<([[([<[{[]{}}({}[])][<{}{}>[()[]]]>(<<{}[]>>)])([{(<[]{}>([]<>))}<[{<>}[{}<>]]([(){}][{}[]})>]
(([{(([{[<{<<<[][]><()<>>>(({}[]){{}<>})>}({{<[]()>({}[])}([(){}][{}<>])}(([[]<>][[][]])[{{}()}<<>
([(<<([{<[<(<<(){}>{[]()}>((()<>){[]<>}))[{([][])}<[<><>]{{}{}}>]>(({(<>[])[(){}]}(({}[])<<>()>)))]>{{[{[[
{(((([<{({[(<[<>[]][[]]><(()[])[[][]]>)]({{{(){}}[<>[]]}((()[])<[][]>)}{{<()<>>([][])}<{()>>})}<<(({<
<[({<<{{(({<{{[]{}}(<><>)}<[()[]]<{}()>>)<<{{}[]}>([()[]]({}{}))>}){({{[(){}][<>()]}[[()()][()[
{<([[{((([[{([[][]]<()<>>)<[<>{}]<{}()>>}]]((<[([]{})[<>()]][<[]>{<>}]><({{}<>}{[]()})(<()()>(<>{}))>)))){(([
{(([{([{((({[{()[]}((){})]{{[]()}[()()]}}<<<{}[]>>({()()}{[]()})>)((({<>[]}[<>{}])(([]{})))[[{()[]}{<>{}
(<(<[[(((<[<<{{}[]}[[]{}]>[{()[]}<(){}>]>]>)[[([[{()<>}<<>()>]<({}{})<{}{}>>]){{[{<>[]}{()()}
(([[(([(<{([[<()()>[{}{}]][<[]{}><[]()}]]{<<{}<>>>})[[{([]())<[]()>}[<{}{}>[<>{}]]][((()())([]()))([[]
[<([<{({({[[({()<>}[{}{}>)]{([{}()]{{}})[[[]{}]<{}{}>]}]})<{{{{(<>)([]{})}[<[]()><{}{}>]}}{{<[{}()]{{}
<([{[([<(([[(<{}()>([]{}))({{}}(<>()))][{(()<>]([]{})}[[[]<>]{()[]}]]]((<[[]<>][<>]><{{}{}
<<([[<{[[([[{<[]<>><[]{}>}][([<><>]([]()))<[()()]([]<>)>]][[<[()<>][<>[]]>({<>{})[<>{}])]<
<{([[(<(({{<({()}[{}[]])(<[]<>>)>}{{[([][])({}<>)]<<{}{}>{<>[]}>}[[[[]()]({}())]((())[[][]])]>}(
{[[{[<[<<(<{<(<>()){{}<>}>}[<<{}[]>[[][]]>((()[])[()()])]}[{<{{}{}}[{}{}]><[[][]][(){}]>}[[[<>{}]<
{<<{[(<{{[[([<[]()>{{}()}][{<>{}}[<>()]])]({(<[]{}>){{[][]}([]())}}<[[<>]<<>{}>]<{[]()}({}{})>>)](
[<[[[{<[{[(([{<>[]}<{}<>>][{()<>}[[]<>]]){([[]()]<()>)<[{}{}][{}{}]>})[((<{}{}><<>()>){([]<>]<[][]>})({<<
{<(((<{<{{{<[([]{}){[]{}}]<[[]<>][<>()]>>(<[<><>][<>{}]><[(){}][()()]>)}((<{{}<>}<()[]>>{{[]{}}{
(<<[[[[{[{{(<(<>{}){[]}>{<[]<>>([][])}){{([]())(()())}}}}){[[{(<<><>><()<>>)<(()[])<{}()>>}]]}}({[(<{[[]()
{({(<{<<<<((<[()<>]((){})>)){<<{<>}([][])>>(<<()()>([]())><[{}{}][{}<>]>)}>>([([[[[]<>]<[]()>]{<()[]><<>()
<{[(({{([(({<<[]<>]>{<<><>>[<><>]}}<(<()()>)[(()<>)]>){[(<{}{}>[{}<>])]})]{(<<[[[][]]([]{})]>>)[<([[()]
{({{<(<<{[([((()<>)<{}<>>)<[<>[]]{{}<>}>](<([][])(<><>}>))[[([[]{}][<><>]){[{}()]({}{})}]<([{}[]]{<><>}
<({[(([[[<<((([]{})<()[]>)((<>)<[]>))(<({}<>)[()()]>[([]<>){{}<>}])>[{[([])([]())]<[()()]<{}<>>>}
{{{[[<{{{[[[(({}[]))]{([{}()][[][]])[[[]<>]([]<>)]}]]((([[<>]{[]{}}]<[<>()]<(){}>>)({[(){}][
[{{([{{<(<<({[(){}]{()()}}[[[]{}]<<>[]>])>>[<<<{{}()}({}[])>><(<()()>(()()))({[]{}})>><{({<>{}}{<>[]})[<{}
<<<<{{<<[[<[((()[])<{}{}>)<(()[])({}())>](({<>}{{}[]}}{[(){}]{()<>}})>({(<{}<>>(()[]))<<{}[]>{{}[]}>}<{{[][]}
<<((({[{[{<([([]())<()()>][<{}[]>([])]){({()}<()()>)<([]())(<><>}>}><<<{{}<>}[[]<>]>[[()](<><>)]>
(<{[[[[([([[<{<>{}}<[]()>>{<<>[]>}]{((<>{}){[]<>})<(()())([]{})>}](<[([]<>)(<><>)]{<[]<>>(<
[{<(<<<[(<{<[[[][]](<>())][<{}{}><{}()>]><{<[][]><<>[]>}{({}<>)}>}<<[({}())([]())]<<{}{}><()<>>>>[(
[[([({[{([[(<[(){}]<[]{}>>((())(()()))]]((({<><>}(()())){{{}()}}){{[<>()]{{}()}}<[[]<>]<{}()>>})]<(({<[][]>
<<{[(<({{({(<{{}<>}(()<>)><{[]{}}[[]{}]>){[<<>()>{{}}]((()[])[[]{}])}}[((({}<>)[()()])<[{}[]]<<>[]>>
[<{<({{({{{({{()[]}[()[]]})<{(<><>)[()<>]}{{<>{}}}>}<<{({}{})({}<>)}[[()<>][[]{}]]>[([{}[]](()))(<<>
{{({{{[[[(<[<{<>()><{}[]>>{<[]<>>[<>]}]>[<<[<>()]([]{})>{(<>)(<><>)}>(<(<>{})([]<>)>(<(){}>[{}[]]))
<{[{[{[(<<({[({})([][])][[(){}]([][])]}<{<{}<>>{[][]}}{((){}){{}{}}}>)[<{[[]{}][{}{}]}({()()}([]{}))>[[[<
({<<{[<(({[[([[][]])[(<>[])[{}()]]]<{<{}<>><[]<>>}<({}{})<{}()>>>}(<(<()[]>[{}[]])<([])<<>[]>>>{<[<>()]
<<{<{{([(({([{[]<>}[{}{}]]([()()][[]{}]))}[<{{[][]>{{}[]}}{[<>[]]{{}()}}>{((()<>)[(){}])}]))][({[<(<[]
({<[[{((<[[[({<>[]}{[]{}})({{}[]][<>[]])](({()<>}<{}[]>)(({}<>)<[]<>>))][<<<(){}>>{(<>{}){(
({(((<{[([{[(([][])([]<>))[{[][]}<[][]>]]}((<((){})(<><>)>(([][]){<>}))([({}[])<[]()>]{<<>[]>{(){}}}))]({<<
([[[[[([<<{<[<[]<>>([]())]{{<>{}}<<><>>}>[<[()[]][[]<>]>({<>{}}<()})]}[[[(()[])({}())]]((((
([(<<{<(<{<{[[<>]]<[<>[]]({}())>}><<{(()<>)(<><>)}{(<>[])<{}[]>}><<[<>()]<<>>><{{}{}}>>>}>)>}>{{
({<<{([({{([{{<>}({}{})}([[]()]{[]()})][{<<>()>([])}<[[]{}]}])(([[[]{}]{()[]}]([{}()]<()<>>))
(<{<[{[(<{([(((){}))[((){})<()()>]]{{[[][]]<(){}>}((<><>)<[][]>)})}{((<([]{})<<>[]>>{<[]{}>{{}<>}}))[({{<>(
<<<{{([[[{[{(((){})[<>[]])([{}()){()<>})}{{({}())}{{<><>}}}][<<[[]<>](<>)>{[()()]{{}()}}>[[<{}{
<(<[{[([[(({[<{}<>>](([]{})([]()))}({<{}[]><()<>>}<[[][]]<[][]>>))<{[<()()><(){}>]([<><>]({}[]))}>)[[
<({<<[(({{{([{{}<>}<()()>](({}{})<()<>>))((<(){}>([]{}))([<><>]))}{{<{{}{}}{<>[]}>[{(){}}<(){}>]}(
{{{([[([[<[(<<[]{}><[]()>>(<{}>(<>{})))[([[]{}](()<>))((<><>)[{}()])]]>]<([<((<>[])(<>))[{()()}({})]>]
{[<{((([<[({{{()[]}{()()}}((<>{})[[]<>])>){(<[[][]]>[(()())(<>[])])[<{<>[]}({}<>)>]}][(([[{}()]({}
{<{[(({{[({[[[()()][(){}]]{<[]()>(()[])}]<(<<>()>({}{}))>})<(<<([]<>)(()())>{((){}){<>()}}>)>]{{([[[{}]]]((<<
<<{({[{[[<[({(<>{})({}[])}<([]())[(){}]>)<<{(){}}(()<>)>{({}{})<{}<>>}>]{<[[()()]{{}<>}]({{}}<()()>)>[<
({([{<<{[{(({(()())([][])}({{}[]}((){}))))}([{<{()[]}({}())>[({})[[]{}}]}<([<><>]([]()))>](<{(<>())(
([<{{({<{[[{<{[]<>}>}([{[][]}[[][]]])]({[{[]<>}[[]<>]](({}[])[()()])}{({()()))<{{}[]}{[]{}}>})][[([<()
((({<<(({(<{[<<>[]>]{[(){}]<()()>}}{<{[]}{<><>>>([<>[]]<<><>>)}>{<({(){}})[({}{})([]{})]>({{<>[]}<{}[]>}<<[
[{<({[[([<<[(({}[])((){}))]({[[][]][[][]]})>(([{[][]}[<>()]]{{<>()}(<>)}){<{<><>}){[()[]]{{}[]}}})>])]({(
{(<(([{{<{[[[(()[])(()<>)][{<>{}}[[][]]]]][{{<{}<>>{{}[]}}({()<>}[[]{}])}{[<{}{}>[[]<>]](<<>{}><(){}>
[(<[<<<({{[<(({}())<()()>){[[]{}]}>({({}())([]{})}([[]()]))]{[[<{}{}><[][]>]<<{}[]>({}())>]{{{()[]}{[][]}
([{[{<<<[{{({[{}()]}<<<>[]>{<>[]}>)((([]<>))([<>{}]))}{{[([]{})<(){}>}((()[])<[]()>)}({[(){}
[({[<[[[(((<<[[]]<[]<>>>>{{([]<>){()[]}}})(({(<><>)({}())})<({{}[]}{[]()})[{()<>}([][])]>)
<[[{[[{{[[[<<{()[]}<[][]>>{<<><>>[{}]}>{[<{}<>><[]<>>](((){}))}]{{<((){}){<>{}}>{(<>())[<>[]]}}<<{{}()}((
(<[{(<<([[([[<{}{}>[<>[]]]][[{{}()}]<[<>()][()<>]>])<{[(<>{})(()[])]{[{}()][<>()]}}<((<>[]){<>()})(<()

25
ten/main.go Normal file
View File

@@ -0,0 +1,25 @@
package ten
import "fmt"
type Ten struct {
navigation navigation
}
func Init(filepath string) *Ten {
ten := &Ten{
navigation: navigation{},
}
ten.navigation.load(filepath)
ten.navigation.parse()
return ten
}
func (d *Ten) Answer() string {
return fmt.Sprintf("The syntax error score is %d", d.navigation.syntaxScore())
}
func (d *Ten) FollowUp() string {
return fmt.Sprintf("The autoComplete score is %d", d.navigation.autoCompleteScore())
}

134
ten/navigation.go Normal file
View File

@@ -0,0 +1,134 @@
package ten
import (
"bufio"
"os"
"sort"
)
var syntaxScores = map[rune]int{
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
var autoCompleteScores = map[rune]int{
'(': 1,
'[': 2,
'{': 3,
'<': 4,
}
var pairs = map[rune]rune{
')': '(',
'}': '{',
']': '[',
'>': '<',
}
type errorType string
const (
corrupted errorType = "corrupted"
incomplete errorType = "incomplete"
)
type syntaxError struct {
line string
symbol rune
violation errorType
stack []rune
}
type navigation struct {
input []string
validInput []string
errors []syntaxError
}
func (n *navigation) load(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
n.input = append(n.input, scanner.Text())
}
return nil
}
func (n *navigation) parse() {
for _, input := range n.input {
stack := []rune{}
var e *syntaxError
for _, r := range input {
switch r {
case '(', '{', '[', '<':
stack = append(stack, r)
continue
case ')', '}', ']', '>':
if len(stack) < 1 || stack[len(stack)-1] != pairs[r] {
e = &syntaxError{
line: input,
symbol: r,
violation: corrupted,
}
} else {
stack = stack[:len(stack)-1]
}
}
if e != nil {
break
}
}
if e == nil && len(stack) > 0 {
e = &syntaxError{
line: input,
symbol: stack[0],
violation: incomplete,
}
}
if e != nil {
e.stack = stack
n.errors = append(n.errors, *e)
} else {
n.validInput = append(n.validInput, input)
}
}
}
func (n *navigation) syntaxScore() (score int) {
for _, e := range n.errors {
if e.violation == corrupted {
score += syntaxScores[e.symbol]
}
}
return score
}
func (n *navigation) autoCompleteScore() int {
scores := []int{}
for _, e := range n.errors {
if e.violation == incomplete {
total := 0
for i := len(e.stack) - 1; i >= 0; i-- {
total = (total * 5) + autoCompleteScores[e.stack[i]]
}
scores = append(scores, total)
}
}
// Find the middle value
sort.Ints(scores)
medianIndex := int(len(scores)/2) + 1
return scores[medianIndex-1]
}

65
ten/navigation_test.go Normal file
View File

@@ -0,0 +1,65 @@
package ten
import (
"testing"
)
func Test_read(t *testing.T) {
n := navigation{}
if err := n.load("test_input.txt"); err != nil {
t.Log(err)
t.FailNow()
}
if len(n.input) != 10 {
t.Logf("Expected 10 inputs, found %d", len(n.input))
t.Fail()
}
}
func Test_parse(t *testing.T) {
n := navigation{}
n.load("test_input.txt")
n.parse()
corruptedLines := 0
for _, e := range n.errors {
if e.violation == corrupted {
corruptedLines++
}
}
if corruptedLines != 5 {
t.Logf("Expected 5 corrupt inputs, found %d", corruptedLines)
t.Fail()
}
}
func Test_syntaxScore(t *testing.T) {
n := navigation{}
n.load("test_input.txt")
n.parse()
score := n.syntaxScore()
if score != 26397 {
t.Logf("Expected syntax score of 26397, found %d", score)
t.Fail()
}
}
func Test_autoCompleteScore(t *testing.T) {
n := navigation{}
n.load("test_input.txt")
n.parse()
score := n.autoCompleteScore()
if score != 288957 {
t.Logf("Expected autoComplete score of 288957, found %d", score)
t.Fail()
}
}

10
ten/test_input.txt Normal file
View File

@@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]