summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGard Spreemann <gspr@nonempty.org>2021-12-17 15:12:04 +0100
committerGard Spreemann <gspr@nonempty.org>2021-12-17 15:12:04 +0100
commit5d8de703455a92c6f9c28d76cd00f908b020ddee (patch)
treec57c212d012bf282594db06fdc53f8ec46835268
parent9aa2cd4a4a97c71b47a4677aaa7b280a2e2eb2cc (diff)
Day 10
-rw-r--r--10/Cargo.toml15
-rw-r--r--10/input.txt94
-rw-r--r--10/src/part-1.rs47
-rw-r--r--10/src/part-2.rs57
-rw-r--r--10/test.txt10
5 files changed, 223 insertions, 0 deletions
diff --git a/10/Cargo.toml b/10/Cargo.toml
new file mode 100644
index 0000000..062a146
--- /dev/null
+++ b/10/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "day-10"
+version = "0.1.0"
+authors = ["Gard Spreemann <gspr@nonempty.org>"]
+edition = "2021"
+
+[[bin]]
+name = "part-1"
+path = "src/part-1.rs"
+
+[[bin]]
+name = "part-2"
+path = "src/part-2.rs"
+
+[dependencies]
diff --git a/10/input.txt b/10/input.txt
new file mode 100644
index 0000000..36e53ba
--- /dev/null
+++ b/10/input.txt
@@ -0,0 +1,94 @@
+[{<(<[<<(<{[[<[]{}><()[]>]]<(([]<>)<(){}>)({[]{}}([]()))>}>{{{<(()<>)[[]()]><(<>())[[]<>]>}{({()
+<<[{([[{{[[[({()()}[<>[]]){<<>[]>([]<>)}]{[{{}{}}<{}{}>]{{[]<>}{[]{}}}}][{{{(){}}<<>[]>}}]]}{
+([<{([<[([({[{[]{}}{{}{}}]}{[{<>{}}{(){}}]})[{[[(){}][(){}]]{<<>[]}{{}[]}}}]])]><({{{[[{{}{}}]<{<>
+{{((<([<[<[((<{}>{()()})<[{}[]]{<>{}}>)<({[]{}}([][])){{[]<>}{{}<>}}>]>]><[[[[[<<>()>(()[])](<[][]>{<>{}}
+{[[<[[((<[{{<[[]{}](()[])>}{<({}[])[[]<>]>}}{[(<()()><()>)({[]()}[<>()>)]}]((<{[<>[]]{{}()}}>[{<()<>>{[]
+({{((<[{{{([{[(){}][<>]}([<>()][[]()])]{<{{}<>}<{}<>>>{{()[]}{()()}}})<<[(<><>)<[]()>][((){}){[]{}}]>>}}
+{<(<{[[([[(<[(<>[])]<{()}<()[]>>><{<{}[]><[]>}(<<>{}><()()>)>)]]{((({{<><>}{{}{}}}{<[]()><{}<>>})<[{(
+<[{(<{<((<(<{(<>{})}<<[]>(<>{})>>){{(<()[]>[[]<>])}[{[[][]]<<>()>}{[{}()]<(){}>}]}>{(((<<>
+(<[{[<((((<[[[{}{}]{{}<>}]({[][]}[(){}])][{(<>[])<[]<>>}<[(){}]>]>)))<({{[<([]<>)<()<>>>[{
+{{<<[<({[{([(<[][]>{[][]})<<<>[]>([]())>])[(<<[]<>>[{}()]>{([]{})})]}](<[<(<<>[]>)>{(<[]{}>[<>[]])<<[]()>((
+[[[{<{[[[([[<<<>>[{}()]}[({}[])<()<>>]]]{<<<[]{}>[<>[]]><{[]<>}{[]{}}>>{(<{}{}>{<><>})({[]()}{{}<>})}})]<<<[(
+(({([<{{{{<<<({}())[()<>]>{[[]()][()[]}}>>}<({({{}<>}[[]{}])(({}{}){()()})}{[(<>{}){<>{}}]{[<>[]]({}<>)}})>}(
+{(([{(([<[{(<{[]<>}<()<>>>[([]{}}(<>{})])}({[<<>[]><<>{}>]}<[<()[]>[<>{}]]{<()<>>{(){}}}>)]>([<((<{}()>
+({{<{{{{{{({<{<>{}}>(<<>[]>[[][]])})]{[{{{(){}}[()<>]}<[<>][[]{}]>}{[<{}<>>{<>}]{{[]{}}(<>{})
+{{(([{([{([<([[]{}]{<>[]})>{(((){})[()()]){{{}<>]<()()>}}])(({{{<>{}}{[]{}}}[((){})[<>]]}<{(()[])[
+{<[((<<(([{{<{[][]}>{[()<>]{()[]}}}}]))<<((<[<[][]>[[]()]][({}){[]<>}]>))>>>>))<(<<([<<[{<<><>>[(
+[([[(<([(({<([{}][<>[]]){<{}{}><{}[]>}>(<({}[])>(<()()>))}{[[{{}()}{<><>}][{()[]}<()()>]>[{<<><>>{
+[{[<{(<[([[[{[{}<>]({}())}(<<>>[[]<>])]{({<>()}{()()}){{[]()>{[]()}}}]]{[[{({}()){[][]}}((<><>))][[
+<{(<[<<<{{[{{<{}<>>[[]<>]}[{()<>}[()<>]]}{[{{}[]}{()}]{<[]<>>([][])}}]({[[{}]{()<>}](({}{}))}>}{<[<({}()){()
+<(<[<([({[<([<[]{}>{[]<>}]{{[]{}}{{}{}}})<<{()<>}({}{})><<[]()>{()<>]>>>(([{[]()}({}())]([[]()]<{}()>))
+{[<<([[<([<<(<[]<>>){<<>()>(<><>)}><<{<><>}[{}[]]>([{}()])>>])>][{({[<<[<>]{<>()}>><<[<>()]{{
+<<<[<<{[<<(({<{}<>>{{}<>}}[<[]<>>({}[])]))><{{<[[]()][(){}]><{{}()}(<>)>}[((<>{})(()<>))<[(){}][[][]]>]>>>]}
+(([{{[{{<<{{[({}<>)(<>{})](<(){}><{}{}))}}([{({}())<<><>>}{<(){}>}]{{[{}{}]{(){}}}<([]<>){()()}>})
+({{([{{({[{<<({}[])(()[])>[{{}()}[{}{}]]><<{()<>}{{}}>>}<[((()<>)([]{}))[{()()}(()<>)]]{[{(){}}{()
+<[<{<[{({(([((<>{}){()[]}){{{}{}}{[]<>}}]{{({}<>)<{}[]>}<(()<>}(()[])>}))(<{({<><>}[[]()])<[{}()](
+[[{[({{<{{{{[<<>[]]({}{})]<([]{})(<>{})>}{{({}{})[{}{}]}{<<>[]><{}>}}}{<(<<>[]>{{}{}})[(()<
+[([(<{{[<[{[<(<>())<[]()>>{{{}}<[][]>}][[{[][]}<{}[]>]({{}()}[[][]])]}{{{<{}{}><{}<>>}{[()[]]<()[]>}}<<{[]{}}
+[[(((({<<[<<({{}()}){<()()><<>[]>}>{((<><>)({}())){<[]{}><()[]>}}><[{<{}>}({{}{}}[<>()])]{{({}{})}[<
+((<<[{{[[([[<[{}()]((){})>[{()[]}<<>{}>]]([<{}()>][{{}[]}[()]])]{({[()()][[]()]}(((){}){{}{}}))[{(<>())}
+{[[<{<{[{{[(<<<>[]>([]())>{(()[])[<>{}]}){<(<>())(<>{})>{<[]<>>[{}[]]}}]({<<<>[]><{}()>>([[][]])}<[<{
+[(<[<{<[([[{{{<>[]}[[]{}]>({[][]}[[][]])}{[(<>[])[[]]]({()()}({}[]))}]])<[<<<{<>{}}{[][]}>((()){()<>})>>
+({<[<<[{[<({<{{}{}}{<>[]}>{{()}{[]<>}}}[<[{}<>][()[]]>])((<([]())((){})><[(){}]{{}()}>)[{<[]{}>[{}{}]}[
+<[[([([(<(<[[(()[])]{[[]<>]({}{})}]>)>[<[{<<<>()>({}{})><([][])<{}{}>>}<([()[]]<(){}>)[([]{})[{}<>]]>]><[{({
+[<{([([(<{<<{[()[]][()}}<<{}{}>>><<{[]<>}((){})>[{[]()}]>>}[(({{<>[]}[<>()]}<({}{})[{}[]]>)<[{<>{}}([][])
+[<{([((<{({{[<<>()>{[]{}}](<[]()>[{}{}])}})(<{([<>()]{[][]})[{{}()}]}<((()[])(())){([]<>)([]<>)}>>{((({}()){
+((<{{{<<[[<[[{[]<>}<{}[]>]<<(){}><<>>>]<<[[][]>[[][]]>{{<><>}{[]{}}}>>{{{{<>[]}{<>{}}}[[[]<>]<{
+[<<[{[<({[[{{{{}[]}[<><>]}{<<>[]>[{}[]]}}<<(()[])[<>()]>[[<>{}]]>]]}{<({<([]())(<>())>[[{}[]][<>{}]]}){([
+([<{({({{<[({(<><>){(){}}}<{[]{}}{[]())>)({<(){}>[<>[]]})]<[([{}{}]<{}[]>)[[()[]]]]{[{{}<>}]<(()<>)
+[([{<{<{(<<(({[]{}}({}()))){{(<>())(<><>)}(([]{})<{}[]>)}><[<[{}{}]({}{})>]{[<[]()>(<>{})]<({}()
+((<{[(<<[([<{([]{}){<><>)}[[[][]]({}())]>[[{<>()}[()<>]](({}<>)([]))]][<((()[])<()[]>)[[<>()]]>])<<{[
+{[[<{<<[{{(((<{}[]>(()))[<<>()>{[][]}])){<([[]{}]{{}[]})<{{}()}{()()}>}}}[{[[<(){}>{{}()}]
+{[{[<<[{[{([(<[]()>(()<>))]<{[{}<>]<{}()>}<{{}{}}[()[]]>>)[{{<{}>(())}<(<>)[<>[]]>}(<{[]<>}[()()]]<{<>{}}
+[{{[{{[<{[(<{[()()][<>[]]}<([][])(()<>)>>({{{}()}[()<>]}[<[]{}><[]}]))([<<<><>>({}[])>({()[]}[<>])][([(
+[({<[[<{<[(<(<[][]><[]()>){[()()]{()()}}>)[<[<()>(()[])][(<><>)]>((({}())<<>[]>))]]<[{[<<>{}>[{}<>]](<{}{}>((
+<{<(<((<<[[<([<><>][()[]])(<<>{}>[<>[]])>]({<<[]>(()<>)>(((){}){<>[]})}{[[[]](<>())]<<(){}><<>[]>>
+[<{(<{{<(({(<<[]()>><<()<>>>)<{<{}()><()()}}(<[][]>{{}{}})>}[{[{[][]}[()<>]]{([][])(<>[])}}[{[{}()][
+{{(([[[{[{({([()()]([]<>))<[{}{}](<>())>}{{{()<>}{()()}}({()[]}[{}{}])})<([([]{})<()[]>]{<<>()>})>}[[
+(((<[{<[{([[{[[]<>]{<>{}}}[[{}<>](<>{})]]<<[(){}][[]{}}>{[[][]]}>][{{[[]{}]([]{})}[([])<<>>]}
+<[<[[([[[[{{{([]()){(){}}}<[[]{}](<><>)>}}]]]{([[<[(()())(()[]]][[<>{}]<<>[]>]>(<[<>]<<><>>>({{}{}}([]{
+({(([<<[{{[(<({}<>)<[]<>>>(<{}<>>{(){}}))]{(({{}[]})({{}<>}((){}]))[({{}()}[()<>])<({}()){<>()}>]}}}[<
+<[<({[[<<([[((<>())[<><>]){[<>[]]<<>>}]]{([<{}[]>{<><>}])})<<[<([]<>)(()<>)>(<{}()>([]<>))]>[
+({({{[[<<([[({()[]}[<><>]){(()<>){<><>}}][{{<>[]}(<><>)}{[[]<>]<[]>}]])>([[<(({}[])<()>)(<()>[<>{
+<([{(({<[{([<[[]<>]<()<>>><(<>())({})>][[(<>[]){[]{}}]{(<>[])<()[]>}])({[{{}()}{[]()}]}{{[[]{}]({}<>)
+[([{({(({{<<<<(){}>[<>[]]>([<>()][<>[]])>[{(()())[()<>]}<{[][]}[<>{}]>]>}<[<([[]()][<>()])((<><>){[]})>
+<({[{{[{{[([{({}()){{}{}}}<(()())[[][]]>][<[<>()][{}[]]><<<><>>>])([[({}{})(()<>)]{<<><>>([]<>)}]{{<()[]
+<{{<{(([(<<<{{[]()}[[]()]}>{{[{}<>]<()<>>}[{{}{}}[{}[]]]}>>([([<()()>[{}<>]])([<()[]>[<>()]]{([]{})}
+{<({<{[{{({[<{{}()}[()()]>{({}<>)}][<<()()>{[][]}>{<<>()><{}{}>}]})}{(<{<[()()]>}<{({}){()()}}(<[]<>><
+(({{({{{<[[({((){})[<>[]]}[{<>{}}[[]()]])]{<<(<>){{}()}>[<<>>{{}{}}]>([(()[])][([]())<<>()>])}][<[[
+[{[[[{<<<[<{({[]()}({}{}))[{<>}{<>[]}]}>[((<<><>>[[]<>])[[[]<>]<[][]>]){[[{}()][{}<>]]({[]
+<<[{({[[[<[[[<[][]>]{{[]()}(()<>)}]{[[(){}}<{}()>][{<>{}}({}{})]}](({([]{})<()()>}[({}())]))>(<{(<(){}>)}
+({<([[(<(({{<{{}[]}[[]<>]>}[<(<>[])<{}{}>><[[]<>][[]{}]>]}<[({<>{}}{{}()})(<[]()>(()<>))]([<{}()
+((<[{[{[{{<[{[<>{}]}[<[][]><()>]]{{<()[]>}<[(){}]{{}<>}>}>}<(([[[]{}]<[]<>>]{[{}{}]<()[]>})[[{[]{}}{{}
+[([(<([<(<<{(<{}{}>{()<>})[([]())[[]]]}[{{[]<>}((){})}<{{}[]}([]())>]><{[[<>()]]<({}())[{}<>]>}{[({}<>)
+[{[<([<{<{{[[{{}{}}{<><>}]<[[]()]{[]{}}>][{({}{}){[][]}}<<{}{}>([]()]>]}[<<({}<>){()<>}>({(){}}<{}{}>)><([{}
+<{((<{{<{({(([<>{}]{()<>}))(<({}[])(<>())><{{}()}[<>[]]>)}]}(<{{(({}<>)[{}{}])<<<>[]>>}}((<<<
+<[<({<({{([{<<<>[]><[]{}>>(<()()>({}[]))}(((()())<{}[]>))][[[<(){}>[()()]][[{}[]]]]])}([(([{[][]}(()())][{()
+[({{[<((<{{{<[[][]]({}{})>{(<>[]){[]}}}([{()[]}{[]{}}])]}{[<<[(){}]<[]<>>>{({}<>)([]())}>]
+{[{(<({<[[(<<({}<>){[]{}}>(({}{})(<>[]))>({[()[]]<<>{}>}<[[][]]({}())>))(([[<>[]]([]{})][<{}>>){{[[]
+{<([[(<<[(<{<<<>{}><<>>>(({}[])<()<>>)}<{<[]{}><()()>}[{(){}}]>>){<<<{{}{}}{()<>}>{{()[]}{()}>><<<<>[
+<{[{(<{[<<(<<([]{})(())>[({}[]){{}()}]>)(<{[[]()]}>[{(()<>)(()<>)}{[{}<>]{()()}}])>[(({<<>[]>({}[]}}{([]
+{{{([{[([[[{[{(){}}{[]()}][<<>()>]}([[[][]](<><>)])]]]{(<[(<<>[]>[{}<>])(({}[])({}{}))]{<([]{})<[][]>>((
+({{[(<<<[({<[{[]<>}<{}<>>]<<()>[(){}]>>[<<<>{}>{{}[]}><({}{})>]})][(<{[(<>())]}(([<><>){[][]})
+([[([{[<<{(<{<[]()>[(){}]}[(<><>){()<>}]>){{[<()()><<>[]>](<[][]>{[]<>})}<[{<>()}{()[]}]{{<><>}[<><>]}>}}{<<
+<[<([<[(({<{{<(){}><<>[]>}{{<>{}}((){})}}<<[{}]({}[])}{<()()><()<>>}>>[[[{<><>}([][])](({}<>)((){}
+<<[<[[<(<{(<{[[][]]{(){}}}<{{}()}<()[]>>><{<{}>}{{<>{}}[()<>]}>)}><{[<{(()[]]<()()>}<{(){}}
+([([(<({{(<<{[[]<>]({}())}{{[][]}<()[]>}>>)}})(<{{<({<()[]>([]{})}[{{}<>}[<>()]])><[({<>()}{<>[
+<(({{(<(<[{<[{{}[]}[<>{}])(<()<>>({}{}))>[<[<>[]]{{}}>[({}<>)[[]{}]]]}<[[{[]<>}{[]<>}]<(()()){<
+{(<(([{([<({[[<>[]]{<>[]}]{{[]<>}{()}}})<[<[[]()][<>{}]>{[<>{}]<()<>>}]<(<[]>[[]()])([<>()](()<>))]>>])
+{{[([[<<([(<{{{}[]}<()[]>}({[]{}}[[]{}])>(<{(){}}<(){}>><[[]{}]>))])>{{<<{<[()[]]<[]<>>>}{{(()
+{{<[{[<([({<[{[][]}<<>()>]{{[]{}}({}())}]({{[]<>}}(<()<>>(<>[])))}([<[()()][[][]]>((<>{}){()<>})]
+{[[{<{<<{((<<<<><>><[][]>>([()<>]<{}[]>)>{((<><>)({}()))([{}{}][<>])})<({({}[])({}())}<[()[]][{}<>]
+<({{{(<{({<<(<[]<>>{{}})>[[({}<>)(()())]{[[]{}]({}{})>]>({<<()<>>[<>[]]>((()<>)([]{}))}[[([]<>)<[]<>>]{{{}<>}
+{<[{([([{<[{<[<><>]([]())>((<>[])([][]))}]>(({[<[]>[[]<>]]{{{}[]}{[]{}}}}){(<{{}{}}([]<>)>)})}
+(<[{{<([<[({<{{}{}}><{{}<>}([]{})>}<<[()()]{{}()}>[[[]()]({}{})]>)[[{<()[]><[]()>}<[<>()][(){}]>]]]<{(<<[]{}>
+<{([[<{<([({{[<>{}]({}[])](<[][]>{{}[]})}{{{<>[]}<{}()>}<[{}[]]{{}()}>})([<[[][]]{<>()}>[<<>()>({
+<{[{(<<{[<(<{{()<>}{[]()}}[([][])<{}[]>]>[[[{}<>}[()[]]][({}[]){()<>}]]){[<[{}[]][[][]]>(<{}>([][]))][[
+{[{<([<<({<<(<<><>><{}[]>)[(<>()){[]{}}]>(<{{}<>}[[]<>]>([{}{}]))>}([{<(()[])[()<>]>{{()()}}}[[<[][
+<{(<[{<<[[[({([]{})([]<>)}{([][])([][])})[[<[]{}><<><>>]<[{}<>]({}[])>]]<{[[{}{}]({}[])]<{[]<>}(()<>)>}[({<
+(<[{([[[{[<([<{}[]>][(<>[])(()<>)])<[{[][]}([]())]>>][{([(<>())[()[]]])}({({()[]}[{}{}])}{[<[]<>>([]()
+<({{[([[<<[(({{}}{{}()})<<()()><<><>>>)<[<{}[]><()<>>][<<>{}]([]{})]>]>>{<((<<[]>(())>{{()<>}}
+[[[[<{[[({[{{[()[]]([]())}}<<{{}<>}(<>)>({{}{}}<[][]>)>]<{(<<>()>{(){}})[{()()}<<>>]}([{{}]<{}<>>][[()()](
+(([[<(([{<({<<()()>>{<()<>><{}{}>}}{((<>()){()[]})<([]{}){()[]}>})>(<[[<[]()>{[][]>][[[]]<<>
+{[<[({<(<<[[((<>())(<>[])){(<>[])(<>[])}][({[]{}}{{}<>})[<(){}>((){})]]]>>[<<{<<()[]>[[][]>>(
+{<<(<([<<<((({[][]}{<><>})[{[][]}([]{})])<[<{}{}>({}())][((){})[[]()]]>)<[{(()<>)(()[])}](<[
diff --git a/10/src/part-1.rs b/10/src/part-1.rs
new file mode 100644
index 0000000..c066310
--- /dev/null
+++ b/10/src/part-1.rs
@@ -0,0 +1,47 @@
+use std::io::{BufRead};
+
+const SCORES: [usize; 4] = [3, 57, 1197, 25137];
+
+fn num(c: char) -> usize {
+ match c {
+ '(' => 0,
+ '[' => 1,
+ '{' => 2,
+ '<' => 3,
+ ')' => 4,
+ ']' => 5,
+ '}' => 6,
+ '>' => 7,
+ _ => panic!("Unknown character: {}", c)
+ }
+}
+
+pub fn main() {
+ let stdin = std::io::stdin();
+ let handle = stdin.lock();
+
+ let mut score: usize = 0;
+ for l in handle.lines() {
+ let line = l.unwrap();
+ //println!("{}", line);
+ let mut opened: Vec<usize> = Vec::new();
+ for c in line.chars() {
+ let i = num(c);
+ if i < 4 { // Open.
+ opened.push(i);
+ }
+ else { // Close.
+ let last_opened = opened.pop().unwrap_or(usize::MAX);
+ if i-4 == last_opened { // Valid
+ }
+ else { // Invalid.
+ score += SCORES[i-4];
+ //println!("Illegal {}.", c);
+ break;
+ }
+ }
+ }
+ }
+
+ println!("Total score: {}", score);
+}
diff --git a/10/src/part-2.rs b/10/src/part-2.rs
new file mode 100644
index 0000000..76f8a2a
--- /dev/null
+++ b/10/src/part-2.rs
@@ -0,0 +1,57 @@
+use std::io::{BufRead};
+
+fn num(c: char) -> usize {
+ match c {
+ '(' => 0,
+ '[' => 1,
+ '{' => 2,
+ '<' => 3,
+ ')' => 4,
+ ']' => 5,
+ '}' => 6,
+ '>' => 7,
+ _ => panic!("Unknown character: {}", c)
+ }
+}
+
+pub fn main() {
+ let stdin = std::io::stdin();
+ let handle = stdin.lock();
+
+ let mut scores: Vec<usize> = Vec::new();
+ for l in handle.lines() {
+ let line = l.unwrap();
+ //println!("{}", line);
+ let mut corrupted = false;
+ let mut opened: Vec<usize> = Vec::new();
+ for c in line.chars() {
+ let i = num(c);
+ if i < 4 { // Open.
+ opened.push(i);
+ }
+ else { // Close.
+ let last_opened = opened.pop().unwrap_or(usize::MAX);
+ if i-4 == last_opened { // Valid
+ }
+ else { // Invalid.
+ corrupted = true;
+ break;
+ }
+ }
+ }
+
+ if !corrupted {
+ //println!("Processing uncorrupted, incomplete line. Got {} opened.", opened.len());
+ scores.push(0);
+ for i in opened.into_iter().rev() {
+ let last_score: &mut usize = scores.last_mut().unwrap();
+ *last_score *= 5;
+ *last_score += i + 1;
+ }
+ }
+ }
+
+ scores.sort();
+ assert!(scores.len() % 2 == 1);
+ println!("Score: {}", scores[scores.len() / 2]);
+}
diff --git a/10/test.txt b/10/test.txt
new file mode 100644
index 0000000..b1518d9
--- /dev/null
+++ b/10/test.txt
@@ -0,0 +1,10 @@
+[({(<(())[]>[[{[]{<()<>>
+[(()[<>])]({[<{<<[]>>(
+{([(<{}[<>[]}>{[]{[(<()>
+(((({<>}<{<{<>}{[]{[]{}
+[[<[([]))<([[{}[[()]]]
+[{[{({}]{}}([{[{{{}}([]
+{<[[]]>}<{[{[{[]{()[[[]
+[<(<(<(<{}))><([]([]()
+<{([([[(<>()){}]>(<<{{
+<{([{{}}[<[[[<>{}]]]>[]]