From 70637c2a63a9f99e8e97e679ad896729b3764607 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 14 Dec 2021 20:27:14 +0100 Subject: Add part1 of day10 --- Cargo.toml | 2 +- day10/Cargo.toml | 8 ++++ day10/example.txt | 10 +++++ day10/input.txt | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day10/src/lib.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 day10/Cargo.toml create mode 100644 day10/example.txt create mode 100644 day10/input.txt create mode 100644 day10/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index f26e2c2..9f555f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["day1", "day2", "day3", "day4", "day6", "day7", "day9"] +members = ["day1", "day2", "day3", "day4", "day6", "day7", "day9", "day10"] diff --git a/day10/Cargo.toml b/day10/Cargo.toml new file mode 100644 index 0000000..40d2066 --- /dev/null +++ b/day10/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day10/example.txt b/day10/example.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/day10/example.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..df5a5a3 --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,110 @@ +{[[<{(<{[{{[{[[]{}]{<>{}}}({<>()}{{}{}})]}{({[<>{}]({}[])})[{[{}[]](()<>)}<[{}{}]{()<>}>]}}]<(({{{(){}}{[][] +<<<({((<{<([([<>{}]<{}<>>]{<{}<>>({}<>)}])>}{({(([()[]]{[]()})({{}<>}<(){}>))[(<<><>>[()[]]){{()[]}< +<<<[(({{(([[[[<><>][<><>]]]([[{}{}](<><>)][(<>[]){[]()}])])([[(<{}<>>(()[]))(<[][]>)]]{{<[[]{}]{< +({({([{{[[[{(<(){}><[]{}>)}]]][(<<([()[]](()[]))>[[[{}[]]{[]<>}](<()>[()<>]]]>(<(({}<>){<>[]}){<[]()>}>{[{( +[([{(<[<<[({<[()<>][{}]>({{}{}})}({[<>{}][()()]}[([][]){()[]}]))]>>]>)<<[((([[{{()()}(<>{})}[(<>( +{[[{<[[[[([[{[()[]]<[]()>}<({}())<[]<>>>]({<{}[]>}<<{}<>>>)](([(<>{}){[][]}]<[(){}]({}{})>)[((()){[]})<{{} +{{<<((({(<[({<<>[]><{}{}>}((<>[])([]())))[((()[]){[]{}})]]{<([{}[]]<<>[]>)(({}[]}{[][]})><<([][])([][])>>} +([[(<({[[{[{<[<><>]({}())>}](<[[{}[]](<><>)]{{()<>}[{}]}>{((<><>]<()()>)([(){}]<()>)})}{[<{({}())<<><>>}[(() +(<((([[<<[<[{[()[]][[]{}]}{([]<>){[]{}}}]>]>>{{([[({<>()}(<>{}))]<{(()<>)([])}([<>{}}{{}()})> +(<({{<(<{[{<{[(){}][<>{}]}{[[][]]{<>{}}}>}<(([[][]][<>()])[[{}{}][{}()]])>]}>{{{([<(()())> +(<[{<<(<<<((<[<>[]]>)({[[]<>]}<({}<>)([][])>))[{<[[]()](<>())>{<[]<>><{}()>}}(<<{}<>>(<>[] +<[[(((([[(<[{((){})<{}<>>}{({}<>><{}<>>}]([{<>()}([]<>)]<{()()}({}[])>)>)]{{({([{}<>])(<<>() +<({<(<{[{<<[[[{}[]][()[]]](<()()><()>)]>>[(((([]<>){(){}})<{{}()}({}())>)[<[[]()]{<>[]}><[()[]][<>()]>])[(<<< +([[[{(<<<([[[<<>[]>]]{<([]<>){[]{}}><<[]>({}<>)>}]<(<<{}<>>([])><{<>[]}<{}<>>>)([{[]()}<<> +<({<<(<{(<<(({[]<>}{{}{}})(<<>[]><<>[]>))(<<<><>><[]{}>>{[()[]][()[]]})>>){{<{([{}()]{<>()}){<[][]>({}<>)}} +(([{{(<[([([([[]()]({}))<<[][]><{}()>>][[[{}<>][<>{}]]]){[{[[][]]<{}{}>}<<()<>>(<>())>]}])<{{[(({})<[]()>){[< +{(<<[<{<<{(([[()<>]{<>[]}]{[{}{}]{<>()}})<([[][]]{[]<>}){(()<>)<[]<>>}>)[[{{[]}}]]}<{{((<>)<{}>){ +<{<{<{{[<{{<<<<>[]>{()[]}>><[<<>()>{{}[]}]<[{}]{[]()}>>}}>[[{(((<>{}]({}()))<<{}()>[[]()]>)}]{{({[<>() +<[[<<([{([{{[[()()]<{}<>>][{{}()}<()()>]}({([]<>)<<>[]>}{[[][]][()[]]})}[((<{}{}><()[]>){<<>()>((){} +<<{([[{[<{[<([()()]{()<>}>[<[]<>>[[]<>]]>{{[{}{}][()<>]}<[[]{}][{}{}]>}]<((({}<>)<<><>>){{<><>}[()<>]})> +[[[[[{({((([<[<>{}]<{}()>>[<{}[]>[[]{}]]]{{{()<>}<{}[]>>([()()]{{}()})})))}[{{[({<{}[]>}<[[]()]>)( +{[[<[([{((<[<[{}[]][<><>]>({{}{}}[{}{}])]{{({}{}){{}[]}><[{}[]]([]())>}>[{<{()<>}([]())>}]))(<[([[()][[][]] +[(({[((<{<{<<[[]<>]<{}[]>>>{[<[]{}>[[]{}]]{((){})<[]>}}}<<<{{}{}}<<>()>>{<<>[]>([]{})}>>>}{[{{{<[]()>(()<>)}< +{[[[{<([{[<{([<>()]{<>[])){<<><>>([]<>)}}<{[(){}]<()<>>}>>([[<()<>>((){})]][<<[]{}><()()>>(<[ +[{(<{[({[{[{{(<>{})({}{})}<(<>[])>}([<()<>>[()<>]]{(()<>)<{}()>})]{([{{}{}}(()<>)][{()()>])[<<[][]>(<>[])> +[[{[{([<<<<([{[][]}{<>()}]([[]<>]<<>[]>))<{[<>()][<>{}]}[((){})]>><[[<{}<>>([]())]]>><<{{(() +<{[<<[[[<{{[<<[]{}>[()<>]><{()()}<()[]>>][[[<>{}][[]()]][<<>>{{}[]}]]}{<<<[]()>({}<>)>>(<[[]()]{<>[]} +{(([({{<(<<[(({}{})[()[]])<<{}{}>(()[])>](([()[]]<{}()>){{<><>}<[][]>})>((<{()()}[{}()]>[{<>()} +<(({(([[<[{[{<{}>(())}{<{}[]>[<>[]]}]{<{()}><<()()>[{}{}]>}}]({<<<()[]>{()<>}>>}[[{(<>){[]}}((()) +{{<{<<(([[[{{[{}{}]{<>[]}}}{[<{}<>>[{}<>]]{{[][]}{[]]}}]({<([][])[<>]>{(()())<<><>>}}([([][]){{}()}]<{()[]}{[ +[<<<<<[{<{{({[[]()](<>())}{{[]{}}([][])})}(<<[{}{}]({}<>)>[<<>>{()}]>)>({[(<[][]>{{}[]})][[(()())<(){}>]{<()< +{({<<([[(<(([(()[])]{{{}<>)((){})}))[([<(){}>[<>()]]{{<><>}})<({<>{}}(<>[]))>]>({[{([]()){(){ +<{[<(<{[(<<{(({}[])(<>()))}[{<[]<>><()[]>}<<{}{}>(<>{})>]>{{{<{}<>>[{}()]}}[<<()()}>(([]{})([] +{<[(([{{{[(<<[{}()]>{(<>())[[]{}]}>){{[{{}[]}{()<>}]{(()<>)<<><>>}}}]}{(<[([[]][{}[]])]>)[[((<{}[]})[ +([[[({({{[<<[<[]<>><()>]>{([<><>])(<<><>><<>()>)}>[(([[][]]{()<>}>)([{(){}}({}<>)])]][(<<{{}{}}{() +{([({[{{{[(<<<<><>>([][])>[<[]<>>[()<>]]>)[[(<()[]>)(<<>()>[()<>])]({<[][]>{[][]}}[<()()>[<>[]]])]](<<{<<><> +<((({{<[[({[(([]<>)[[]<>])]{{<{}>{[]<>}}(([][])[()[]])}}{[{<()<>>[()[]]}{{[]<>}{[]()}}]}){([{{{}{}}[( +{<<[[{[<(([[{([][])[<>()]}<{<><>}>](<<[][]>[[]{}]>[{<>}{{}<>}])]{<([()<>]<()[]>)(<[][]>(<>[]))>[{[[]< +([{((<[<<([[[{<>()}(<>())]([(){}](<>[]))][[<[]>]]]<{<[{}[]][[][]]>}([<[]()><<>{}>]<{(){}}[{}{}]>)>)>({([ +([{(<<[<{<[[(<[][]>{<><>}){<<>()>}]{({()[]}<<>()>)}][{({(){}}[()()])<[<><>]<<>[]>>}]>}<<[({< +{<({[({{<[[({{(){}}<<>[]>}<({}[]){()())>)<{([]{})([]())}>]]>(<{[((<>[]){[]()})<<()<>>({}<>)>]{{{()[]}}} +{({[[<(<{[[{<<[]()>[()()]>([<>{}]{<>[]))}<{([]())(()<>)}<<[]><[][]>>>][({<{}><[]()>})[<[()[]][()()]>{<{}<>> +{({(((<{<[<(<({}<>)[()()]>{<[]{}><()<>>})[<({}<>)><(<>)<()()>>]>(<(([][])<()<>>)<<<><>>([])>>{(<[]{}> +[{{[([<<(<[<({<><>}{[]()})[{<><>}]>{<(()[])><[<><>]>}][<{[{}<>][[]<>]}(([]{}))>[[([])<{}[]>]{<(){}>({}())}]] +{[<<((<<[([[([()[]]({}()))(<{}()>{(){}})][((<>[]]{{}{}})((()<>)<<><>>)]]{[<(<>[])<[]>>([<>()]{[] +((({({{<[(((<[<>[]]([]<>)>[<[]{}>{<><>}])<{[<>()][()<>]}{<{}<>>[[]()]}>))<[<{(<><>)<()<>>}>[<[[]<>]<[]{ +{<<<{<({([({((<><>)[[][]]){[()<>]{<><>}}}<[{[]<>}[<>[]]][[()<>]{(){}}]>)<[[[[]<>]]((<><>))}(<<()<>><<><>>><[[ +(({{[({<{<{[[<<>[]>([][])]{<<>()>{{}()>}]{[<<>[]>[{}{}]][{()()}{[]<>}]}}([({{}[]}{{}()}){(()<>){()<>}} +(<([({{{{<[<{{<>[]}{{}}}<[{}{}]<{}{}>>><{{(){}}([]{})}>](<[([]())[()]]>[(<[]<>>[[]{}])])>[{(<[[][]]<{} +((({[{{{([<{{<<><>><[][]>}{({}())<<>{}>}}[{([]())}[<<>()>({}[])]]>{<[<<>[]]<()<>>][[()[]][<>[]]]>[{[[] +<(<<[[<{({{[(([][]))([<><>][{}{}])]}})}>]](<([{{[{<{[][]}[{}<>]>([{}()])}]}([{{(())}{{{}{}}<[]{} +<([<[{{({[(({({}{})<{}()>}))[[((<><>)<{}>)[[[][]]((){})]]<<<[]()>{()[]}>>]]{<<(<[]<>>[<>{}]){({}())}>>}}{[[ +[<<[(({<({{{[{[][]}{(){}}][<()()>]}((<{}{}>>[{{}[]}])}{<[(<>[])[(){}]](<<><>>({}<>))><(({}()) +[([{<{(([[[([{{}[]}<(){}>])]<{([[]()][[]<>])([{}{}]([][]))}>]({<<([][]){()<>}>>{([()<>]([])){{{}()}{[][]} +{<({[<[(({{({<(){}><[][]>}[{<>()}({})])<[[{}]{<><>}]<<[]{}>([]{})>>}[(({[]<>})([(){}]<()[]>))]}))]><<( +{<<[[<[[[((<[<{}<>><<><>>]<({}[])([]{})>>{<{()<>}>{([]{})[()[]]}}>[[<({}<>)([]{})>(<{}()>[[]()])][[<()<>><[] +{((({(<{[[[({<<>[]><{}<>>}[{[][]}[<>()]]){([<>()][[]()])({[]<>}{{}<>})}]<<{[{}<>]<{}()>}<(()){(){}}>>{ +<[[{<<([<[{{(({}()){[]{}}){{{}[]}{<>()}}}}{<{[<><>]<{}{}>}(({}()){()<>})>(<(<>{})[<>()]>{<<><>>{[][]}} +{{([{(((({<{[({}{}){{}{}}]{(<>{})[{}{}]}}[{[()[]][{}()]}{(()[])(()())}]><({(<>[])}((())<[]>))>}<{{{{{}<> +({(([({(<<<((({}[])((){}))((<><>)[[][]]))((<()<>><(){}>){{{}()}(()[])})><([[{}<>][()[]]])(([<> +<<<<<{({{([(([{}{}]<{}()>))][<<{<>[]}{<>}>({()<>}{{}()})>[([<><>]{{}()})([[]{}])}]){({[([]<>)<[][]>][{<><> +((<[<<{{<([{{[{}<>]{{}<>}}([[][]]<<>{}>)}{[[[]{}]])]{[<<{}<>>{{}{}}><({}<>)>]([(<><>)[{}()]])})(< +<(((<[(((<([[{(){}}<<>()>]]<{<<>{}><(){}>}<{[][]}{[]{}}>>)>{<<{{<><>}{[]<>}}{{<>[]}<[]()>}>[{{{}()}{[]<> +{[<{((<<[<<([(()()){{}()}](([]{}){<>()>))>{({((){})({}())}[[()][<><>]])<([{}[]][[]{}])(([]{}){()<>} +(<[({[(([[{[[<{}{}><<>{}>][<<>{}>]]([{<>()}<<>{}>]{{[]{}}({}())})}([({{}()}{{}()})][[<<>[]>[<>{}]]{<<>[])}]) +{<{(((<([[[<[(<>())[()[]]]<<()[]><{}<>>>>]<{({<>()}(()<>)){<<><>>{{}[]}}}{(<()<>><<>[]>){{[]()}[[]()]}}>][(<[ +<<[[<(({{{{<{{[]()}({}<>)}[(<>{}){[]{}}]>[<{[][]}{{}{}}>]}{{(<<>()><{}{}>)[{{}[])[(){}]]}(([()[]][{}[]]){< +(<(<<<([{((<[[{}[]]{{}}]<({}<>)<()<>>>})<(<[<>{}]<{}<>>><([]<>)([]<>)>)[{<{}<>>[()()]}[(<>[])] +<{(([([[<<([{{<><>}[<><>}}{[{}<>]{{}<>}}]{([[]()]<()[]>)[({}<>){[][]}]})({[((){})<<>[]>][{<>()}[[]<>] +<<<{{<{[({<<[[[]][[][]]]({[]()}{{}[]})>>{[({{}<>}{[][]})[[{}()]{[]}]]}}([[[[<>[]]<{}[]>]{[{}{ +{<{{([([<{[[{([]<>)[()<>]}][{<()()>}<{{}<>}([]{})>]]{{[{[][]}<{}[]>]([<>()]{{}{}})}{(({}{})(( +<{<((<{<{([<((()<>){()[]}){[[]<>](()[])}><(([]()])<[[]()]{{}<>}>>]{({<()><()()>}[[()<>]<<>[]>] +[<[{[(<<[(<<<(<>[])[[]]>{<(){}>}><([<>[]]{<>{}})[{<><>}{[]()}]>>{[(<[]<>>)<[{}<>]{<>()}>]{<(()())[[][]]>[ +[<<<{[(<<[({{{<>[]}<[]()>}{<<>[]>(<>[]]}})]>>{[[<({([][]){{}()}}[{<>()}({}())])({(()[])(<> +[<[[<{<{{{{{<([]())[<>()]><{()<>}[<><>]>}}}}}><<{(([{<[][]>{[]()}}[{[][]}{{}<>}>][(({}())({}()) +<[[{{[<{<<[<(<<>[]>)<<()()>{{}[]}>>{<{()<>}[<><>]>[{{}{}}{()}]}]{(<([]<>)({}())>{<[]{}>}){[({}())]{( +({{[(([(<{[[{(()<>)({}())}<({}{}){{}}>](((()())<{}()>)[<{}<>><{}()>])]({{([][])({}<>)}<({}())<[] +{[(<[<{[(([<(<[]()><{}()>)<<[][]><()>>>][<<[{}[]]<{}{}>](<<>>{<>[]})>[{(<>())[<>]}<{[]<>}(( +[{{(<{{[{<<<<(<><>)>(({}[]))><<[[]{}][<>()]>>>({[({}{}){<>{}}]<[<><>]>})>}<[<{{<{}{}>{<><>}}}>]<( +{<{<<{[{<[[[<<{}[]>{()<>}>(<()[]>({}<>))]{<((){})[<><>]><{<>{}}{<>()}>}]({([()[]]{[]()})}([{()()}}<(<>() +[<<([(<[<[[([(<>{}){()<>}][<{}()><{}()>])]((([()<>]{()<>})[[<><>]]))][{([{{}{}}]<{<>}[{}()]>)[[{ +<[[{([({<<<(<([]<>)<<><>>>[{[]()}<(){}>])>>>({(([[<>{}]([]<>)]{[(){}](()())})([{<>()}([]<>)]))<[{[{}()]{<>[]} +<(([{<{[[<({[({}()){{}[]}]{<{}[]>[{}<>]}}[[[{}<>](<><>)][<()()>({}<>)]])<{{[{}<>]{<>[]}}}([[{}[]]<<>{}>])>> +{(<[([[[{[<[[<[]()>[{}{}]][<()[]>{[]()}]]>[[[(<>[]){<>[]}][{<>{}}<()[]>]]]](<{{([]())[()()]]<[{}[]][[]( +[([[[<<<{({[[{[][]}<()[]>]][{{[]()}([]<>)}({()<>})]))[<{<[<>{}]{[]{}}>({<>{}}[()()])}><<([<>[]]{{}[]})[<[][] +{(({({<{([[[[<[]{}>(<>{})]{({}())(()<>)}]]<{{<[]()>(()())}{([][])}}<{[[][]]<()<>>}{(<>{})({} +[{{<[{(<{({<[{[]<>}[[]()]][[{}<>]{{}()}]>(<[()()]>([{}[]]{(){}}))}{{{({}<>)<<><>>}({[]<>)<{}>)}})([<({ +(<[{{{{<(<[<{{()()}{[]{}}}<([]{}){<>()}>>[({{}<>}{{}[]})]][[({{}()}(<>[])){<[][]>[<>()]}]]>]>{[( +[{<(<<{[<(<[<({}[])({}{})>]{<{{}()}<<><>>>{<{}{}>(()())}}>)>][({<[[<(){}>(<>())](({}{}){{}()} +{([[{<{<{(([<([]<>)[<>]>{(<>[]){<>[]}}][<[(){}]{{}()}>((<>[]))])<({[()<>](()[])}){<[()<>]{<>{}}>{([])<() +<[{[{(<<({[((<[]{}>{[]{}})([()[]]<(){}>))<{<<>[]>[{}]}{<{}<>>{()}}>]{{{(<>())}(({}<>))}<<{()<>}({}())>([{}< +<(<[{{<{{[([{{(){}}<<>{}>}(<(){}>)][[{{}<>}(()<>)]]){([<<>{}>[()()]][{[]()}<{}()>])}]<{([{[]}([][] +<[[{<{[<{([<[<[]<>][{}]]<({}{})([])>>]({{(<><>)[[]<>]}<<{}[]>[[]<>]>}))}([<(({()()}{[]<>}))<{<()[]><[]()>}((( +({{{<{([{<[{{[{}<>](<><>)}[[<>()]({}[])]}[[<(){}>({}<>)]<(()())({}())>])>[<<<[[][]][(){}]><([ +((<{[<[[(((<[<<>()>([]())]<(()<>){()[]}>>[<{(){}}{{}}>[[(){}}<[]{}>]])<[[[[][]][{}()]]{[{}()][ +(({[[<{({[[[[[<><>]<[]{}>]<(<>{}){[]()}>][(([]<>){()[]})([<>{}]<()<>>)]]]}[[<{<(<><>)<{}[]>>}<<{<> +<{({[{(([[{(<(()[])<(){}>>{<{}{}>{<><>}})[<(()())<[]{}>>]}[{{{[]}[()[]]}[<()[]><<>{}>]}{<[<><>]{[] +[<[<<{<{{<[(<{(){}}[<>()]><({}<>)<<>{}>))<<<()<>>({}<>)><[<><>][()]>>](({({}()){()()}}<[[]<>]<<>{}>>)<[[[] +{<{(<(<[<([{<{{}[]}(()[])>[({}())<[][]>]}<<{()()}[<><>]>>]<{{[[]{}]{<><>}}(({}{})[[]])}({(<>[])<<>{}> +{{{[[((<[(<<[[{}()]<[]<>>]{<()[]>}>[<<()()>>([<><>]((){}))]>{<<[{}{}][{}()]>[(()())[<>[]]]>{{ +<({<{[{<[<({{[<><>]{<>[]}}<{[]<>}<<><>>]}{{[{}{}]{{}{}}}{<[]<>>{[][]}}})(<({()<>}<{}[]>){([]{}) +{<(<<[<{([{[({{}<>}[()()]){(()<>)[{}<>]}]}])}>]{{(<[<<((()())<<>()>)>{([[]{}]((){}))<({}<>)({}< +[<(<[[{{[([[<<[]{}>{[]<>}>{(<>[])[{}{}]>]([[[]<>](()())])])([[<{<>{}}(<><>)><({}<>)<()>>][({[]<>} +[<{({{[<([{<{[()[]][(){}]}>[([<>[]]{{}})]}<<((()<>}(()()))<[(){}]>>((({}())[{}<>]){({}<>)<<>[]>})>]{<<{<{}[] +[[<{<{({(<{<{({}())[{}]}([()[]]{{}()})>}((([{}{}]{()<>})<[<>()]{<>[]}>)[([()<>]<<><>>)[[()[]]{[]}]]]>){([[[ +{<(([<<[<{(((<()<>>{{}()})({[]{}}[[]{}]))(([{}[]]{[]<>})<<[]>(<>{})>))}[{{<(<><>]<<>>>}}({[({}[]) +[[{{[([<[({[{(<>()}[<>[]]}{{<>{}}<[]{}>}]{{{()[]}([]{})}([()<>]{()<>})}}[<{(<>)<<><>>}{{<>[]}<<>[]>}>({< +{{[{{{<([<{<{<{}<>>}{[(){}]{{}()}}><{{[]()}<{}>}>)[[{[{}{}]{()<>}}<(<>{}){[]{}}>][[(()<>)(<>())]({[]<>}[()[] +((<({<{[(<[{[<<>><{}()>][[{}[]>]}{({()<>}[{}[]])[(()())]}][<<{<>{}}<<><>>>[{[][]}{<>()}]>]>[<({({}()){{ +[([<<({({[<{(((){}){<>{}})(({}<>))}{<[[]()][{}{}]>{([][])<[][]>}}>]<<(<{<>()}[()[]]>[<(){}>[<>()>])<<[{}( diff --git a/day10/src/lib.rs b/day10/src/lib.rs new file mode 100644 index 0000000..f483420 --- /dev/null +++ b/day10/src/lib.rs @@ -0,0 +1,103 @@ +use std::collections::VecDeque; +use std::str::FromStr; + +pub struct SyntaxLines { + lines: Vec, +} + +impl FromStr for SyntaxLines { + type Err = (); + + fn from_str(input: &str) -> Result { + let lines: Vec<_> = input + .trim() + .split('\n') + .map(|x| x.to_owned()) + .collect::>(); + + Ok(SyntaxLines { lines }) + } +} + +impl SyntaxLines { + pub fn part1(&self) -> u32 { + let mut result: u32 = 0; + + for line in &self.lines { + let mut qopen = VecDeque::new(); + let mut panic_char = ' '; + + for character in line.chars() { + match character { + c if character == '(' + || character == '[' + || character == '{' + || character == '<' => + { + qopen.push_back(c); + } + c if character == ')' + || character == ']' + || character == '}' + || character == '>' => + { + match qopen.pop_back() { + Some(bracket) => { + let mut error = false; + if c == ')' && bracket != '(' { + error = true; + } else if c == ']' && bracket != '[' { + error = true; + } else if c == '}' && bracket != '{' { + error = true; + } else if c == '>' && bracket != '<' { + error = true; + } + + if error { + panic_char = c; + } + } + None => panic!("Wtf?"), + }; + } + _ => {} + }; + + if panic_char != ' ' { + if panic_char == ')' { + result += 3; + } else if panic_char == ']' { + result += 57; + } else if panic_char == '}' { + result += 1197; + } else if panic_char == '>' { + result += 25137; + } + break; + } + } + } + + result + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let input: SyntaxLines = include_str!("../example.txt").parse().unwrap(); + let result = input.part1(); + assert_eq!(result, 26397); + } + + #[test] + fn test_puzzle_input() { + let input: SyntaxLines = include_str!("../input.txt").parse().unwrap(); + let result = input.part1(); + assert_eq!(result, 392367); + } +} -- cgit v1.2.3-18-g5258