summaryrefslogtreecommitdiffstats
path: root/2021/day10
diff options
context:
space:
mode:
Diffstat (limited to '2021/day10')
-rw-r--r--2021/day10/Cargo.toml8
-rw-r--r--2021/day10/input94
-rw-r--r--2021/day10/input.example10
-rw-r--r--2021/day10/src/main.rs65
4 files changed, 177 insertions, 0 deletions
diff --git a/2021/day10/Cargo.toml b/2021/day10/Cargo.toml
new file mode 100644
index 0000000..40d2066
--- /dev/null
+++ b/2021/day10/Cargo.toml
@@ -0,0 +1,8 @@
1[package]
2name = "day10"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
diff --git a/2021/day10/input b/2021/day10/input
new file mode 100644
index 0000000..a529340
--- /dev/null
+++ b/2021/day10/input
@@ -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<[(({<[({<[{{{()()}<<>[]}}({<>[]}<[]<>>)}]{[<<[]()><()()>>[({}<>){[]{}}]][<<()()>[<>]>{{()<>}}]}>})]>}))({
diff --git a/2021/day10/input.example b/2021/day10/input.example
new file mode 100644
index 0000000..b1518d9
--- /dev/null
+++ b/2021/day10/input.example
@@ -0,0 +1,10 @@
1[({(<(())[]>[[{[]{<()<>>
2[(()[<>])]({[<{<<[]>>(
3{([(<{}[<>[]}>{[]{[(<()>
4(((({<>}<{<{<>}{[]{[]{}
5[[<[([]))<([[{}[[()]]]
6[{[{({}]{}}([{[{{{}}([]
7{<[[]]>}<{[{[{[]{()[[[]
8[<(<(<(<{}))><([]([]()
9<{([([[(<>()){}]>(<<{{
10<{([{{}}[<[[[<>{}]]]>[]]
diff --git a/2021/day10/src/main.rs b/2021/day10/src/main.rs
new file mode 100644
index 0000000..dcb44a8
--- /dev/null
+++ b/2021/day10/src/main.rs
@@ -0,0 +1,65 @@
1use std::collections::HashMap;
2use std::env;
3use std::fs::File;
4use std::io::{BufRead, BufReader};
5
6fn main() {
7 let reader = open_file();
8 let points = HashMap::from([(')', 3), (']', 57), ('}', 1197), ('>', 25137)]);
9 let comp_points = HashMap::from([(')', 1), (']', 2), ('}', 3), ('>', 4)]);
10 let pairs = HashMap::from([('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]);
11 let opening = vec!['(', '[', '{', '<'];
12 let closing = vec![')', ']', '}', '>'];
13 let mut final_scores = Vec::new();
14
15 let mut score = 0;
16
17 for line in reader.lines() {
18 let mut stack: Vec<char> = Vec::new();
19 let mut flag = false;
20 if let Ok(ln) = line {
21 for char in ln.chars() {
22 if opening.contains(&char) {
23 stack.push(char);
24 } else if closing.contains(&char) {
25 let t = stack.pop().unwrap();
26 if char == *pairs.get(&t).unwrap() {
27 } else {
28 score += points.get(&char).unwrap();
29 flag = true;
30 break;
31 }
32 }
33 }
34 if !flag {
35 let mut score: u64 = 0;
36 stack.reverse();
37 for c in &stack {
38 score *= 5;
39 score += comp_points.get(pairs.get(c).unwrap()).unwrap();
40 }
41 final_scores.push(score);
42 }
43 }
44 }
45
46 final_scores.sort_unstable();
47 let idx = final_scores.len() / 2;
48
49 println!("part 1: {}", score);
50 println!("part 2: {}", final_scores[idx]);
51}
52
53fn open_file() -> BufReader<File> {
54 let args: Vec<String> = env::args().collect();
55
56 if args.len() != 2 {
57 eprintln!("Usage: {} <filename>", args[0]);
58 std::process::exit(1);
59 }
60
61 let filename = &args[1];
62
63 let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename));
64 BufReader::new(file)
65}