summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--day10/Cargo.toml10
-rw-r--r--day10/resources/input.txt110
-rw-r--r--day10/src/main.rs69
3 files changed, 189 insertions, 0 deletions
diff --git a/day10/Cargo.toml b/day10/Cargo.toml
new file mode 100644
index 0000000..4119747
--- /dev/null
+++ b/day10/Cargo.toml
@@ -0,0 +1,10 @@
1[package]
2name = "day10"
3version = "0.1.0"
4authors = ["Federico Igne <git@federicoigne.com>"]
5edition = "2018"
6
7# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
9[dependencies]
10lazy_static = "1.4"
diff --git a/day10/resources/input.txt b/day10/resources/input.txt
new file mode 100644
index 0000000..1115e8e
--- /dev/null
+++ b/day10/resources/input.txt
@@ -0,0 +1,110 @@
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({<{<{{([[{[((<>[]))]}<<<({}{})>(<[]{}>({}[]))>{[(<>())([]<>)]{{<>{}}{()<>}}}>]([<({<><>}{{}<>})[[[]<>](
95([[[([{[(<({([()[]])[{[][]}<{}<>>]}<(({}{}){()[]}){{<>[]}{(){}}}>)[<({<><>}{[]})(<(){}>(<><>))>{<[
96[{{<<{<{(<<({[()()]<()[]>})[{(<>[]){()[]}}{((){})(<>[])}]>}){<[[[[<><>]<[][]>]]<([[][]](<>[]))((<>{})
97<[((<{<(<[<[[<{}()>[[]<>]]([<>[]]<()>)]<<[<>[]]{()()}>]>([({()}((){}))]<{({}())[[]()]}(({}<>)<[]{}>)>
98[{{<(<{({(<<(<()[]><{}<>>)<<<>>>><<(<>[])[{}[]]>>>({[{[]<>}]<<<>[]><{}()>>}<({{}}<()<>>)<[<>()][<><>]>>))})
99([({({{{{<(<{{<>()}({}<>)}({{}<>}<[]<>>)>)>}[{(<{[[]<>][[]{}]}[[()<>]{<>}])){(<(<>[]){<>[]}><<()>[<
100{({[{{(([[<({[{}{}]})><{<<<>[]><{}[]>><{()<>}<()()>>}(<[<>()]{()()}><[{}[]]>)>]]<[[((({}())<[]()>)<<()[]>{
101[(<([{{([[<({{[]()}<(){}>}{({}<>)({}[])})>[(<{[][]}>({<>()}<[]<>>))]]{<{<<{}{}>[()[]]>[{[]<>}{()[]
102([<((<<({([[([()()][()[]])[<<>{}>]]])}]>>)[<<(([[[(<<>()>)[{<><>}([][])]][((<><>)<[]{}>)[[()()]<{}{}
103[[<{[[({[{{({[<>()][{}{}]}){[<()[]>][<()[]>[[]()]]}}}[[[([{}{}]{{}<>})[[{}[]]{(){}}]]]]]}{[{[<(<
104[[<({[<{{<<[[({}[])][<{}<>>({}{})]]{(<<><>>{<>[]})}><<<{{}{}}{{}()}>{{<>}<<>>}]<<[()()]<[]<>>>([<>()]{[]<
105<{{{([{[([{{({<><>}<[]<>>)}[({[]<>}){<{}())[<>[]]}]}{<[{{}{}}[{}{}]]>[[[()()][<>[]]]<{{}<>}
106({[{{([<[<<{{<<>()><{}()>}}[[<<><>>(<>{})][{[]{}}]]><{(([]))<([]{})(<>[])>}(({<>{}}{()})(<{}{}]<<>>))>>]>[{
107<(<([{{[[([[{<[]()>[[][]]}{[{}[]]<{}{}>}]<{<<>()><{}{}>}({{}})>]<((<()<>>[{}[]])({{}}<{}()>)){([[]<>]{<
108(({{(((<<{<{<{{}[]}<[]()>>[<()<>><{}<>>]}>[[(([][])<[]<>>)][[<()()><[]>]{(<><>)<()()>}]]}[<[<{()<>}
109(({{{{(<[<{([{{}()}<<>{}>][<{}[]>])}[{{{[]<>}[[]]}[<(){}>]}(<{[]()}{(){}}>{([]{}){[][]}})]>]>)}[([{[[[<<()<>
110(<{<[([{[(((({()}<[]()>)>))<{<[(<>[]){<>()}][(<>())(<><>)]><[<()()>][((){})<<>()>]>}{{[({}{}
diff --git a/day10/src/main.rs b/day10/src/main.rs
new file mode 100644
index 0000000..11f1f2c
--- /dev/null
+++ b/day10/src/main.rs
@@ -0,0 +1,69 @@
1use std::fs;
2use std::path::Path;
3use std::collections::HashMap;
4use lazy_static::lazy_static;
5
6lazy_static! {
7 static ref SCORE1: HashMap<u8,u32> = {
8 let mut m = HashMap::new();
9 m.insert(b')', 3);
10 m.insert(b']', 57);
11 m.insert(b'}', 1197);
12 m.insert(b'>', 25137);
13 m
14 };
15 static ref SCORE2: HashMap<u8,u64> = {
16 let mut m = HashMap::new();
17 m.insert(b')', 1);
18 m.insert(b']', 2);
19 m.insert(b'}', 3);
20 m.insert(b'>', 4);
21 m
22 };
23}
24
25/* AOC21 Day 10: https://adventofcode.com/2021/day/10 */
26fn main() {
27 let input = Path::new("resources").join("input.txt");
28 let content = fs::read_to_string(input).expect("Unable to read input file");
29 ex1(&content);
30 ex2(&content);
31}
32
33fn ex1(content: &str) {
34 let ex1: u32 =
35 content.lines()
36 .filter_map(|l| check_line(l).err())
37 .sum();
38 println!("Ex1: the result is {}", ex1);
39}
40
41fn ex2(content: &str) {
42 let mut ex2: Vec<u64> =
43 content.lines()
44 .filter_map(|l| check_line(l).ok())
45 .map(|v| v.iter().rev().fold(0, |a,p| a*5 + SCORE2.get(p).unwrap()))
46 .collect();
47 ex2.sort();
48 println!("Ex2: the result is {}", ex2[ex2.len()/2]);
49}
50
51fn check_line(line: &str) -> Result<Vec<u8>,u32> {
52 let acc = Ok(Vec::with_capacity(line.len()));
53 line.bytes().fold(acc, |a,c| match a {
54 Ok(mut a) => {
55 match c {
56 b'(' => { a.push(b')'); Ok(a) },
57 b'[' | b'{' | b'<' => { a.push(c+2); Ok(a) },
58 b')' | b']' | b'}' | b'>' =>
59 if a.pop().filter(|&x| x == c).is_some() {
60 Ok(a)
61 } else {
62 Err(*SCORE1.get(&c).unwrap())
63 },
64 _ => unreachable!()
65 }
66 },
67 Err(_) => a
68 })
69}