Skip to content

Commit b21660a

Browse files
committed
AoC 2021, day 10.
1 parent 848da35 commit b21660a

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

2021/day10/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2021/day10/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day10"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

2021/day10/src/example.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[({(<(())[]>[[{[]{<()<>>
2+
[(()[<>])]({[<{<<[]>>(
3+
{([(<{}[<>[]}>{[]{[(<()>
4+
(((({<>}<{<{<>}{[]{[]{}
5+
[[<[([]))<([[{}[[()]]]
6+
[{[{({}]{}}([{[{{{}}([]
7+
{<[[]]>}<{[{[{[]{()[[[]
8+
[<(<(<(<{}))><([]([]()
9+
<{([([[(<>()){}]>(<<{{
10+
<{([{{}}[<[[[<>{}]]]>[]]

2021/day10/src/input.txt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[{<<<{{[(<{{{{[]()}{[]()}}<[[]<>]<[]()>>}([[[][]]]<[{}<>]((){})>)}>)[<([<<(){}>{(){}}>[[<>{}](<>[
2+
<<{(<({[<{{<[<()()><{}{}>][[()[]][(){}]]>((<[]<>>(<>{}))((<>{}){<>()}))}<{({{}}[<>{}])({{}{}}{{}
3+
(<[(<<[((({<[{()[]}]>}{[[[{}[]]{{}{}}][{{}()}<<>()>]]})))<[([(<[{}{}]>{<<>{}>})<((<>()){[][]})<{<
4+
{({<[[[(<{{<<[[]()]>]}{<({<>{}}([]<>))[({}{}){()()}]>((<[][]>{[]()})[(()<>)[{}{}]])}}>){[<[{[{[]}{<><>
5+
(<{([(<[{[{<[<()<>>({}[])]>}][<{({<>}(<><>))}({[<>{}](<>())}{[{}()]<()<>>})><[<<{}<>><()[])>[<{}[]
6+
{<{[({<<<[[<(({}))>(([<><>][<><>])<<()()][{}{}]>)]][<[{<{}{}>(<>[])}](({{}[]}(()()))<<[]()>
7+
(<{[[(({(<([([[]{}][()[]]){{()[]}[<><>]}](<(<><>){[][]}>({{}{}}{{}()})))>){((([{()}[{}[]]]))[<[
8+
{<[(<([<{(<[[({}{})({}())]]>[(<<{}<>>([]())><({}[]}{()[]}>)([<(){}>][[{}<>][<><>]])])([{{<()()>[()[]]}[[<>](
9+
{(({([([<((<({<>{}}<[]>)(<[]{}>)>))({{{[<><>]<<><>>}[(<>{})[{}()]]}{[<[]{}>([]())]{({}{})}}}<({([]())}({<>()
10+
<<{<[<(([<[{[{[]{}}([][])]{{<><>}{<><>}}}{({<>[]})({<>[]})}]}<<[[[[]()]{[]()}]<{{}{}}[{}{}]>](<(()())[[][]]>[
11+
[{[<[{(([<<{{(()[])(<>[])}}{(<<>{}>([]<>))}]>({{((<><>)[[]{}])((()<>))}[[({}[])({}{})]([{}()]([]())
12+
{[{[{[{({([<{[<>()][(){}]}<[[]()]{(){}}>>]{{(([][])([]{}))}})}(<{{<({}())(<>[]>><[()()]>}{[((){}){<>{}}]
13+
[{{[<<[[(<({<{()()}(()[])>(<{}[]>)}[(<<>{}><[][]>)[({}<>]([]{})]])[<[<<>()><[]<>>]{<[][]>[<>]}>{[{{}()
14+
{[(([<({[<(({([][])<{}()>}{<{}[]><[]<>>))(<{[]<>}[()()]>))({{({}{})}<<<>[]>([][])>}[[{()[]}<<>{}>]{(<>[])
15+
{[<[(({{{{[((([]())[{}()])[[{}[])<{}<>>])((<()[]>{{}[]})(<<><>>(<>{})))]}}}[([[({[{}[]]<(){}>})(
16+
{[{((<[<{{([{([]())(<>{})}[<{}[]><[][]>]])<((<[]{}>{()[]})[(()())(<>())])(({{}()}([]())))>}}>[{[<<<((
17+
[{[[{{{<{<<({<{}{}>([][])}{([]{})[(){}]})<{(<>[]){()[]}}{[(){}][[]<>]}>>>}[[([([<>{}][[]{}])[
18+
[((<({<({([<(<<>()>)[{()<>}{[]<>}]>[[{{}[]}<{}()}]{{()[]}}]]{[<<[][]><<><>>><(<>)>]<{{()<>}[[]<>]}{(
19+
[<{[[[(<(({(<({}<>){<>[]}>){(<()<>>{()()})[{[]()}([]())]}}{[[<()[]){<>{}}]<<<>{}>[(){}]>]<[[()<>]<()[
20+
<{<<[[[[{[<<{({}<>){()[]}}([<>()]{<>})><(<<><>)<()<>>)[<[]()>[<>{}]]>>(<<({}{})[[][]]>([<>{}]({}<>))>{<<<
21+
{[[{<({<{(<[(([]{}){[]{}})]<[[[][]][{}{}]]{[{}<>}{{}<>}}>><({[()[]]<{}{}>}{({})})<<<()[]>[{}()]
22+
<[[{{([({[{(<{()()}>{(()[])(<>[])}){<[<>[]]{[]()}>[<[]<>>[<>()]]}}]})]<{{(([<<{}()>>(([]()){{}()})]<[(<>[]
23+
[(<<<({{<([{(((){})[{}[]])[{[]{}}(()<>)]}]{({{<>{}}[<><>]}{(()<>)})[[([]{})<()[]>]{(<>[]]<{}{}>}]})[{[[(()
24+
[<[{{[({{{({(<[][]>{[]})[{{}()}<[]{}>]}<[<<>()>({}{})]<{[][]}<[]{}>>>)}<[[{<<>()>(<>{})}<<()<>>[<>{}]>]]<<<
25+
<<<(<<<(<[{<(<(){}>((){}))({()<>}{<><>})>}([<({}())[<>()]><{{}()}([])>][{(())}(([])<<>{}>)})]>)>[[{((
26+
{[[(((<[{[{[<({}())((){})>[{<>{}}<<>{}>]]{{<<>()>{[]()}}<[()]({}())>}}(([{<><>}][<<>[]><[][]>])<[({}[]]]({
27+
{(<(({<({<[([<[][]><()()>]({{}{}}[{}<>]))<<(()[])[<>[]]>{{[]{}}(<>[])}>]((<[{}[]]>{<()()>[<>{
28+
<([{[([(<[([{<[]{}>[<>()]}[{<><>}]])[([([]<>){()[]}])[({{}[]})<[<>[]]<[]<>>>]]]>)])[(({<<{{[<>{}]{<>()}}}
29+
(<({{[<<{<{<{{()}{[]<>}]{{(){}}[[]()]}>{{{[][]}[()]}[(()<>)(<>[])]}}{{(<[]><(){}>){(<>())([])}}{[(<>)({}<>)
30+
{<[({{<<{([([((){})](<{}[]><<>{}>))([<<>[]><()()>])][<{{[]<>}(()<>)}<<()[]>{(){}}>>{(<()[]>[{}])
31+
[<[[{[{{[{(<[<()<>>]<[(){}][{}]>>)}([{{{(){}}[<>{}]}((()<>)<[]<>))}][({<[]{}>{[][]}}({{}{}
32+
{[[([<((<(([(<<><>>{[]()})]<{[()<>]{{}{}}}{[{}[]]<[]{}>}>)((<<[]<>>>({<>()}))(<[[]{}]>{{[]{}}[(
33+
{({[([{(<{{(<[<>[]]<[]<>>>(<{}[]>[<>[]]))<([{}<>]{{}()})<[()()]([]{})>>}(<{<<>{}>([][])}><(({}[])
34+
{<<{[([{({[[<<<>[]>[[]{}]>]({{<>[]}(()<>)}{[{}<>]<()[]>})]<({[()()][{}[]]}<<()()><(){}>>)>}[([
35+
<{{([<{{{{([(([]<>){<>{}})][<<<><>>[(){}]>])}{<([({}<>)<<>[]>])({{[]()}(()())){{<>[]}({}{})})>(<({<><>})><
36+
<({<{[<<({(<{[{}[]]{{}{}}}{[(){}][[]<>)}>[(<(){}><{}{}>){(<>)[[]{}]}])}(<[<(()())><<<>()>{()()}>][
37+
{[[<<{<({<[(([{}]<<>[]>))[<{<><>}({}{})>]]{([{()<>}{[]()}][{()()}<(){}>])(<({}{})<[]{}>>[[<>{}]
38+
{<[({{{[(<(({{()<>}(()[])}[<<>{}>{{}<>}])<{({}[])<()()>}{(()())<{}()>}>}([<(()())([][])>[([]){()<>}]]
39+
[{<[{{(([{{<({<>[]}(<>()))<[[][]]>>(([{}]({}())){[()[]]((){})})}{((<<>[]>[[]<>])<[<>[]}([]())>)
40+
[<<<{[((({<[((()<>)<()[]>){{<>{}}({}())}]{{<[]()>[[]()]}{[()]<{}[]>}}>[[{[<>()]}((<><>><()<>>)](([[]()](()))[
41+
(([<<{<[<[({{<()<>><[]{}>}{{()()><<><>>}})<[[{(){}}[[][]]][[[]<>]]]{{((){})}[[<>{}]<[][]>]}>][[({({})[[]
42+
<<((<({[(({[[[{}{}][<>]](<[]{}>[<>{}])]{(([]())(()<>))<[[]()]([]<>)>}}{[(<{}()>((){}))<{{}{}}[()<>]>]({<
43+
{<{(([(([{{{({<>}({}{}))[[[][]]([]<>)]}<<(()[])[[]]>((<><>)<[][]>)>}}[[[(<[]{}>)]{{([]()){{}<>}>[<[
44+
([[[{[([{{<{<<()()>(()[])>{({}()){<>[]}}}><(((()())({}[]))([()[]]<<>[]>))[[{{}[]>]{((){})}]>}<[[[[
45+
{[[[{(<[{(({<<{}()>{<>{}}>[[[]()]<{}{}>]}{[[{}[]]{{}}]((<>))}))[{{{(()[])<{}{}>}<<[][]>{<>()}>}{[{<>{}}<(){}>
46+
<({[<[(<<{[{<{{}<>}(()())>(<(){}>[<><>])}[([{}]{()[]})<{<>{}}{{}}>]]<{([()[]]<()[]>)[([]())([]<>)]}((((){}
47+
{[<<[{([((([(({})[{}<>])({[]()}{<>{}})][{{{}<>}}(({}<>))]){<{<<>()>[(){}]}(({}<>)<<>[]>)>[<[()[]]([][])
48+
<<([<<{[<<<<<(<>{})<{}{}>>>{([()()]<<><>>)(((){}))}>{<<[[]<>][(){}]>[(()<>){[][]}]>{(<()()){<>{}})<[[]{}
49+
[{<{[{{([<({({(){}}<{}<>>){[{}{}]{()<>}}}[{<{}[]><<>[]>}<[[]()][{}<>]>])<{([{}[]][<>[]])<[<>()](()[])>}[(<
50+
({([{{<[[[({<(<>())<<>()>>([()[]](<><>))}{<(<>[])<{}()>>({{}[]}{{}<>})})[<{[[][]]({}[])}{{{}[]}}
51+
{<[([{<[[<([{<[]<>>(()<>)}[(<>{})<{}{}>]][({<><>}<[]()>)(<()()>[{}{}])])>]]<[[({([<><>]{()})<{<>()}{<>
52+
[(<[[{[{{{<({[()()][[]<>]}<(<>[]){[]{}}>)<[{()()}{[]}]<[{}{}]<{}[]>>>>}{([<[()[]]>{([][]){()[]}}])}}}]<{<[
53+
{{<{<((<<{[<{[<>()]<<>[]>}<<[]()><{}()>>>({(<>[])<<><>>}{(<>[])(()())})]}>[[[[{<[]()>[()[]]}((<>{})[()<>])
54+
[<<[<[{(<({{[{[]<>}<{}()>][[<>[]][{}<>]]}<(({}{})({}[]))({[]<>}[{}{}])>}<[(<[]{}>[()()])][[({}[])(()
55+
<{[{[<<(<{([{({}{}){<>{}}}{[()](()<>)}][[({}())({}<>)][<()<>>{<><>}]])}<({{((){})(()[])}}(<[{}[]]<[][]>><{
56+
{(<{{[[[<<{{<([]<>)<{}()>)({{}{}}([]{}))}[{({}{})}<[()]>]}{[[<{}<>>](({}{})([][]))]{([{}<>]{()<>
57+
<([[{{<(<{(<((<>())[[]{}])>((<{}<>>{()<>})[{()()}(()<>)]))}[{(<{{}{}}[{}()]>)}([[[{}[]]<<><>>][<()()>[
58+
[(<[{<({{<<<<{<>{}}(<><>)>[{[][]}[[][]]]>{([(){}](<><>))((()<>))}><<{{{}<>}{{}<>}}({<>}<[]<>>)>>>{{{(([])[{}{
59+
((({[[[[<<<{<<[][]><<>()>>[<()[]>(()<>)]}{{<<>{}><<>[]>}{[{}{}][()()]}}><{[[{}[]]<{}[])]{{<>[]}
60+
[({[[<<<<{[<(<[]<>>[<>()])[[<><>]<<>()>]>>{[{(<><>)[()()]}{(()())[<>{}]}]{{<<><>><[][]>}({<>[]}(()<>
61+
{[[{(<<([[{(<[{}]>{{<>{}}{{}[]}})}]{<({[(){}][()()]}{(<>{})[[]]})>{<{[[]()][<>[]]}(<[]()>[<>{}]]><{[{}()
62+
({{[[<<({<<{(<<><>>[{}<>]><((){})>}{{{[]()}([]())}[{{}{}}[<>]]}><<[[{}[]]([][])]<[{}[]]{{}{}}
63+
<(((<(<<<[[[<{[][]}>{<[]>{<>[]}]]{<<{}[]>[()()]>}]([([()()]<[]()>){{{}()}}]<(({}{})(()()))
64+
[{{<[{[<<<[[<<{}<>>({}<>)>[<[]()>(()())>]<<<()[]><[][]>><[[]{}]([]{})>>]>><[{(<([])<{}()>>(<{}<>><<>{}>)
65+
<<<(<[[[<(<<[[[][]][{}{}]]>>(<([[]()))({{}[]}[[][]])>))><[{(<({}<>)(()<>)>{[{}<>]})[({<><>}[<>()])<
66+
<{<{[(<<([[(<[[]<>][{}[]]>(<()<>>(<>{}>))]]<{{([{}()]{<>()})<(<>()){()}>}{<<()()>(<>[])>{(())[[]{}]}}}(<{[{}
67+
{[[[[[[{[[{[[[()()][()[]]]{{{}()}(())}]}{<([{}()]{<>()})<[()<>>[<>()]>>[(<[][]>{[]<>})[<{}<>>]]}]]((({({(){}
68+
(<{(<([([{({{{[][]}{[]<>}}[<{}[]>[<>[]]]})}(<[{{(){}}(<><>)}((()[]))](([{}()]([])){([]<>]{{}()}})>[[[{(
69+
<{{{<<[<({([[[()[]]]][{(()[])(()())}])}){<{[{<[]()><[][]>}]([{[]()}<{}[]>][([]())[()[]]])}[<[(<>()){
70+
(<{{[<<[<{[[[(<>[])[<>()]][[[]()][{}()]]][([<>])<[<>[]][[]<>]>]][[({{}<>}[{}{}])({[]()}{()<>})]]}(<([
71+
{[({([{(((([{<[]()>}<[()()]([])>]{<[()[]]{[]<>}>[<<>{}><()>]}))))<([<[[<()[]><<>()>]][(<()[]><{}<
72+
({([<[[{([([<{<>()}>[({}[]){<><>}]][((<>){{}{}})(({}())<{}{}>)])])<((({({}{})[<><>]}(((){}))){((
73+
{[(<[[[{{[{<<{()()}(()())>({()()}[{}{}])><[{(){}}[[][]]]>}<[<(<>{})(<>())>({{}[]}{[]<>})]<{<{}<>>}{<{}<
74+
<<[({<<{([([{{<>}[<>()]}]{[[[]()]{<>()}](([]{}))})<<[{[]{}}([]())][<[]{}>{[]{}}]>({({}[])}
75+
<<({[[{{<[<[{[(){}][()[]]}]({{<>}<<>{}>}((<>())<{}()>))>]{{{({[][]}[[]<>])({<><>}({}))}{[{()()}[<>{}]][<<
76+
{<<[<{<{{({{((()[])[[][]])[{[][]}[<>{}]]}{[{<><>}([][])][[[]]((){})]})){{{([{}{}]((){})){{<><>}
77+
<[<{{(([(<<[<{{}[]}{[]<>}><([]{})[[]()]>]{{<()<>>([]())}{<()<>><<>[]>}}><[{[<>[]]<[]()>}<<[][]>{[]<>}>][{(()
78+
<([{{[({[([{(<()()>[<>[]]){<[]{}><[]{}>}}])]<{[[<[{}<>][[]()]>[{{}()}[<>[]]]]{{([]{}]{()<>}}[[()<
79+
({{{<<<[<<{(<([][])(<>{})>((()[])))}{({<()()>[<>{}]}{{{}[]}(()[])})<[{()())<<>[]>]({<>}<[]<>>)
80+
<<<{(<({((<({<()>(<><>)})[[<{}()>(<>())]]>(<<({}[])[<>[]])>({{[]()}(<><>)}(([][])<<>()>)))){[[([<>][{}])
81+
(<((<([({[<<{<{}()>(<><>)}{[[]{}]({}())}>>(<(<<>[]>)<[<>()]<<>{}>]><{<{}<>>}{{<><>}((){})}>)]{[<(<{
82+
{(([<{(<{<<<(([][])(()[])){<()[]><{}()>}>{(([]<>)<<>()>)<<{}<>>(<>())>}>{(<{{}<>}<[][]>>[[<>{}]<<>{}>])([(<
83+
{{{{<([([[[[(({}{})<<>>){{<><>}<()[]>}]([[(){}]<{}()>][[<>]<{}{}>])][<([[]<>][{}{}])[<[]<>>[[]<>]]>{<{[
84+
[<(({{<(({[({[[]<>]<[]<>>}<{{}<>}<(){}>>)[([()()][()[]])<<()()><<>()>>]](<<[{}[]]{[][]}>({[]}[<>{}])>{[[[][]
85+
(({({{([{{({<<(){}>(()[])}}{{([]<>)[{}[]]}})}<(<{[<><>]}>[[<{}>[{}()]]])<{{{<>()}<<>[]>}}[(<<
86+
[<((<((<[<{{(<<>[]><()<>>)[<<><>><<><>>]}{([()()][(){}])}}><<<<(<>())[{}]>[([]<>)({}{})]>[[<{
87+
{<<(({({{{(<{{()[]}}[({})({}())]>)}}}(<[[[[{()<>}<()<>>]([[]{}][<>{}])](<<{}[]>{{}<>}>(<{}<>>{[]{}})>]][({{
88+
<<{([[[<((<{{(<>[])[()[]]}([[]()])}>))>]]]<<({[(({<{[][]}({}[])>{<{}{}>[<>{}]}}<<[<>()][[]()]>{
89+
<[[({([<[<{([{[][]}[<><>]]{([]<>)(()<>)})[<(()())<[]<>>>[<[]<>>(()())]]}>(<(<[[]()]<[][]>>{[[]()]{[][]}}
90+
[<([[({[{{<{{([][])}(<<><>>(()<>))}[[<{}{}>{{}[]}]{{[]{}}}]>}}[<<[{({}{}){{}()}}({[]{}}([]{}))][{[()()]
91+
((({<(<{(<([<{[]{}}<(){}>><{[][]}(()<>)>](<[[]()]({}[])>{[()<>]}))<{(<[]()>[[][]]>[<[][]>[<>()
92+
{({<({(<([<([([]{})({}[])])>])<((<[[[]{}]{<>[]}]{{<>[]}<<><>>}>)(<({[]{}}{[]()})[{<>[]}<{}{}>]>{({
93+
{{{[<<<{<(<{<[[]<>]<[]()>>}<<(()[])([]())><((){})<{}[]>>>><(<((){})<[]>>(([][]))){<[[]{}][()[]
94+
[<({[(({([<(<[{}<>]>(<()[]>{[]{}})){{{[]()}}[<()<>>[<>[]]]}>]({<<{[]}[<>()]>(<<><>>{[]{}})>[<({}{})[()<>]>[[<

2021/day10/src/main.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::collections::VecDeque;
2+
3+
type Chunk = VecDeque<char>;
4+
type Input = Vec<Chunk>;
5+
6+
fn main() {
7+
let example = parse_input(include_str!("example.txt"));
8+
let input = parse_input(include_str!("input.txt"));
9+
solve(&example);
10+
solve(&input);
11+
}
12+
13+
fn parse_input(s: &str) -> Input {
14+
s.lines().map(|it| it.chars().collect()).collect()
15+
}
16+
17+
fn solve(input: &Input) {
18+
let part1: Vec<(&Chunk, i32)> = input.iter().map(cost_first_error).collect();
19+
println!("Part 1: {}", part1.iter().map(|i| i.1).sum::<i32>());
20+
let mut part2: Vec<i64> = part1
21+
.iter()
22+
.filter(|i| i.1 == 0)
23+
.map(|it| it.0)
24+
.map(cost_fixing)
25+
.collect();
26+
part2.sort();
27+
println!("Part 2: {:?}", part2[part2.len() / 2]);
28+
}
29+
30+
fn cost_first_error(input: &Chunk) -> (&Chunk, i32) {
31+
let mut matched = Chunk::new();
32+
for i in input {
33+
match i {
34+
'{' | '(' | '[' | '<' => matched.push_back(*i),
35+
_ => {
36+
if matched.pop_back().unwrap_or('#') != matching(i) {
37+
return (input, cost(i).unwrap());
38+
}
39+
}
40+
}
41+
}
42+
43+
return (input, 0);
44+
}
45+
46+
fn cost_fixing(input: &Chunk) -> i64 {
47+
let mut matched = Chunk::new();
48+
for i in input {
49+
match i {
50+
'{' | '(' | '[' | '<' => matched.push_back(*i),
51+
_ => {
52+
assert!(matched.pop_back().unwrap_or('#') == matching(i));
53+
}
54+
}
55+
}
56+
let mut score: i64 = 0;
57+
while matched.len() > 0 {
58+
let cur = cost(&matched.pop_back().unwrap()).unwrap();
59+
score *= 5;
60+
score += cur as i64;
61+
}
62+
return score;
63+
}
64+
65+
fn cost(c: &char) -> Result<i32, &str> {
66+
match c {
67+
')' => Ok(3),
68+
']' => Ok(57),
69+
'}' => Ok(1197),
70+
'>' => Ok(25137),
71+
// cost of adding matching pair.
72+
'(' => Ok(1),
73+
'[' => Ok(2),
74+
'{' => Ok(3),
75+
'<' => Ok(4),
76+
_ => Err("invalid char"),
77+
}
78+
}
79+
80+
fn matching(c: &char) -> char {
81+
match c {
82+
')' => '(',
83+
']' => '[',
84+
'}' => '{',
85+
'>' => '<',
86+
_ => 'x',
87+
}
88+
}

0 commit comments

Comments
 (0)