This commit is contained in:
2021-12-12 00:18:01 +01:00
parent 56c99bc5a9
commit e197a5c546
7 changed files with 442 additions and 2 deletions

8
december_10/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "december-10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

10
december_10/example.txt Normal file
View File

@@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]

94
december_10/input.txt Normal file
View File

@@ -0,0 +1,94 @@
<[<<({{<{<[[([()][()()])<{[]<>}{<>}>><<<[]{}>>{<<><>>({}{})}>][<<<{}<>>([])>{<{}><()>}>]><(<{(()[]){[](
<{([({{[(<[({({}{})[()<>]}{{<>{}}[(){}]})]>({(<<<><>>{{}()}>)<<<[]><{}<>>><[{}[]]<<>[]>>>}))[{(<{([]
([<([({(<([<({()()}(<>[]))[<[]()>{<>{}}]>]<{{{{}{}}{()()})({[]}[<>{}])}>)>)}){<<{{(<<<()<>><<><>>>
[{{[<<[{([[{{[<>{}]<()<>>}[(()<>)[{}<>]]}<<[{}[]]([]<>)>{{[]{}}{()()}}>]][[{<({}{})<[]{}>>
<<{{<([(((([{<[]<>>{()<>}}(<<>[]>{(){}})]([{{}<>}<[]>](<[][]><{}()>)))))[({{{<[]{}>[()]}<<<><>>
<(<[{(<[{{{<{[[]<>]{<>()}}><[[{}()][(){}]]>}}<[<[<()()>({}<>)]><[{[]()}([][])](<(){}>[<><>])>]{
{[[<{{<<[[<<(({}<>){<><>})[[<>()][[]{})]>[[[{}[]]<<>>]{(<>[])}]>{{<<()[]>{<>{}}>[{[]<>}<[]{}>]
({[((((([{[({[{}[]]<[]()>}[({}{})<<>[]>])<[(()[])<{}()>]{[[]<>]{[]()})>](([({}<>)(()[])][<[]{}>])
{[[{[({(<<{<([{}<>])<({}<>)({}<>}>>[<[{}()]([]<>)>((<>())<[]{}>)]}<([(<>())[(){}]]{[{}<>]<[][]>}){<{<><
(<{<<[<<([[([<<><>>]<<{}()><[]<>>>)]][{(({<>{}}(()())))(<[()()]<[]()>>)}[[<<<>()><<>{}>>([{}<>][{}[]])]{<{(
<<[({{{{{[<([((){})([]())]<((){})({}[])>)[{[{}()]{[]<>}}]>([<{{}<>}><({}<>)({}[])>]<[{<>{}}
(([[{{({{{[<<{[]()}({})>{{{}()}[[]<>]}>{<([])>[[[]{}]]}](([<[]{}>]<<[]<>><{}[]>>))}}}(([[[{[<><>
<<[(<[{{<((<[<()>[<><>]][[<><>]([]())]>[({<>})<<()<>><{}[]>>])[[(<[]<>>([]())){{<>[]}(()())}]]){(<(<[]<>>
({[<<[[{<[[{{[()()]{()[]}}}]<{([{}{}]{[]()})[[()[]]<<>>]}>]>}[({((<{<><>)>{{[]()}{{}[]}}){([{}[]]<{}{}>)<
([{{<{[(<[<<[({}())({}())]>>[{[<<>[]>[<>()]]<(<>[]){()[]}>}(<<<>[]>[<>[]]>{<<>[]>{{}()}})]]{
<[{{<[((<([[{<<><>>{()}}{<{}[]><[]()]}]])<{({<()>{()[]}}(<(){}><[]<>>)){[({}[]){<>[]}]}}<<([()()]<()(
<{<{[(({(({({<[]>([])}<<()<>>{<>()}>){[[()[]]<{}()>]{({}[])[()<>]}}}<[<<{}()><[]{}>>({[][]}[()<>])}{<<[]
<[{{[<({(<[<[[()<>]({}[])]([{}[]]([]))>]{[{{<>[]}[[]{}]}(({}<>)<()[]>)]<<[()[]]>({(){}}<{}()>)>}>)}<<[[[[<<>(
{{<(<[((({(<[<[]{}>[(){}]](<{}<>>((){}))>){(({[]{}}<{}[]>){{<><>}[[]{}]}))}<([<(<>)>[<<>{}>[{}
<([({[<[(({[(({}))(<{}()><<>{}>)]<[{{}[]}{{}<>}]{[{}()]{{}()}}>})([[<(<>())({}{})>[<[]<>>[
{((<<<{<(<[[[<{}<>}]<(<><>)>]]<(([(){}]{[]()}))<({{}<>}<()<>>){<()<>>{(){}}}>>>{({({()[]}[<>
([<[[((<(<[<([{}[]])>{<(()<>)((){})>{<<>>(()<>)}}][{[(<>[])(<>[])]}[[{{}[]}[()()]]<<[]<>>{(){}}>]]><({(
[{<([[[{([{<<([]<>){<><>}>[<(){}>]><{[<>[]](()<>)}[{()()}]>}<({{{}{}}}{[{}{}]<<>()>})<(<[]<
(<({[{{[[(<[<(<>[])[[][]]>]{((<>[])({}<>))({[]<>})}>({<[()[]]]<{()[]}>})){(<(<<>[]>[<>{}])>{[({}<
(<{<(({[(({[[<<>{}>({}<>)]{({}{}){(){}}}]{(<(){}>[[][]])<{()<>}>}}[[[{[]{}}[()]]](([[]()][(){}]))])
([({<{[{<[{<({{}()}{{}()})>}]>}<<[[{{((){})<<>{}>}(({}[]){<>[]})}]]><[{{<({}<>)(()())>[{<>()}{{}()}]}{<{
<[{[({[<<[[<<{{}{}}<{}{}>>([<>][(){}])><<<{}{}>>>]]{([{[[]()][[][]]}<{[]<>}>]<<[<>[]][[]()]><<()[
[{({{{<<{[{([[()]({}[])]{[()()]})(((<>{})[[][]])<{{}{}}>)}]}>>}[((({{<([[]()][[]])}}{{<({}[])[{}
{[({<{<<{[(([<<>[]>({}{})>))]{(<{{{}<>}<<>{}>}>)}}><(<(({([]())(<>[])}{{[]()}{[][]}})<{(()[])[<>{}]}>){({
[<((<[<<(<<{<(<>)<[]{}>>[<[]{}><[]()>]}<((()[])((){}))[({}[]){[]{}}]>>([<[{}()][[]{}]>{{()[]}}][{[{}<>]<[]<
[<[({({[([{<(([]<>)({}[]))<{{}[]}[<>[]]>><[<{}[]>({}<>)]<(())>>]]([[[{[]<>}({}[])](<[]{}>{<>()
{[<<{(<[{(<[({[]{}}{[]{}}}]({<()[]><<>[]>}{<[]()>[{}{}]})>)}{[{(((<><>))(<[]<>>(()[])))[[<[]()>[{}()]](((
({[<<([[{(<<<{<>()}<{}<>>>[[[]{}){()}]><[[(){}]<[]<>>]({(){}}[[]{}])>>{[{[()<>][{}()]}][[[{}{}]<<>()
([(<((<{[[[[[{{}()}{()[]}]]<([[]<>])<[()<>][[][]]>>]][{([{()[]}(()[])]({<>{}}{[]()}))}({([[]
(({{[<<{(([{<{()()}[[]()]>{<()<>>[[]{}]}}{[<()()>[[]{}]]{([]())({}[])}}][(<{<>[]}[()()]><<()>(()())>)
[<(<{{{<{(<{(<<>[]><<>()>){<{}()><()<>>}}(<[<>{}]{<>{}}><[{}{}][<>()]>)>)}{{{(<(<><>)(<>())><{[]}([][
<<<{([[<[[[(([[]{}])<({}())(<>())>)({[<>{}]{[]()}}<<(){}>{(){}}>)]{([(()()>({}[])]((<><>)<
{{({<<<[[<(<{{{}<>}({}[])}{<{}<>><<>()>}>{({{}<>}({}()}){[[]<>]([]{})}}){{[[[][]](()())]}([<<>{}
<<[[<{{({<{[{<<>()><()()>}<[()<>]<()()>>]{(([]<>)[{}[]]){{{}<>}{[]<>})}}>{{<[<{}<>>({}())]
({[<[[{<<{{[{(<>{})}]{<[{}{}][[]()]>[[[]{}][()()]]}}[(<<{}[]>{<>[]}>{{{}<>}[(){}]}){{[<>{}]<{}{}>}{
(([<{<[[([[(((()<>))({{}[]}<()<>>))[[({}[])(())]<{(){})>]][{((()){[]()}){{()<>}{<><>}}}[{{()}{()[]}}]]]){{(({
{<[{(<[{[{{[<([]<>)<{}<>>>]}}]}]><{{<[[([{<>()}[[]{}]]({{}()}{<>{}}))]{([<[]{}>(()<>)]<[[]<>]([])
{[<<[<(<[{<<[{{}()}{<>{}}](<{}<>>({}<>))>><{<({}[])<()<>>>[[<>[]][<><>]]}[<(<><>){{}<>}>[<[]()>{[]}]]>}]{[<{<
(<({{[{<(<<<{[{}<>]({}[])}{[[]{}]<()[]>}>(<[[]()][()()]><[()[]][<>()]>)>>[(<<<{}()>{<><>}>>){<<{<><>}(
(<{[[{{<(<(<({{}<>}{{}()}){[()<>]<{}[]>}>[(<{}{}><[]()>)<({}<>){()()}>]){[[([]{}){{}[]}]<[{}{}]<[][]>>][{<()
[(<([({[[[<[[[[][]][{}{}]]([{}()]{<>{}})]{<{()()}<()<>>><{{}<>}<(){}>>}>]{{[([[][]]<()()>)
(<(<<({{(<[{{[()<>][<>{}]}[<[]<>>]}<<{[]{}}>[[{}()]{()()}]>]{[([()()]<<>>)({()<>}[<>[]])][{{[][]}(<>())}
[[((({(({{{[{{<>[]}[<>{}]}[[{}<>]([]())]][{(<><>)}]}][(<{{[][]}(<><>)}<[[]()]>>([[{}()]<[]<>>][<[][]>[()()]])
<{({(([((<[<{(()[])<[][]>}{<<><>><[]<>>}><<([]<>)([]<>)>>]{[<([]{}]{<>{}}>]}>[{[(([]{})[{}{}])[<<>{}
{(([<{(({<({{{{}{}}}[[[]]{[]()}]}{<[[]]<<>()>>(<()>[()<>])})>{{[(([]<>){{}()}){[{}]{{}<>}}]}([[([]{})]<[[](
<{[({<{[[[{{{[{}{}]}}([<[][]>[<>[]}][<<>()>{()}])}((<({}[])[<><>]>(((){})[{}()]))[[<{}[]>[(
{<[([(<((<<<<[{}[]]({}{})>((()())[(){}])><([[]{}][()<>])<<<>[]>([]<>)>>><[[((){}){[][]}}{[[]]
[{(<{<{<<({({(()<>){{}[]}}([[]()]{()<>}))(([[]<>](())))}<{({()[]}{[][]})}])>>}>(({[{[{{([]())(<><>)
<[[<([<<({[[(<[]()><<><>>)({<>})]{{<<><>>[()[]]}([()<>](()[]))}]((<{<>{}}<(){}>>))}<<[(<(){}><<>{}>)[[()[
<[{<<<{[{<{(<[<>{}][{}()]>((<>[])[<><>]))<[({}<>)<<>[]>]<(<><>)(<>{})>>}({<<[][]>([]<>)>([<>{}><<>[]>
[<<(<{((<([<({{}{}](<>[]))[[()<>]([][])]>((([])<(){}>))]){<{{<()[]>(()<>)}(<[]<>>([][]))}><[([(
{[[[{{<[[{<[<<<>[]>{<><>}>{<<>{}><[]<>>}]((<{}()>(<>[])))><<{<<>{}>{(){}}}([{}{}]<<>()>)><[{<>{}}]>>}]
((((([[(([{<({()[]}<()<>>)><<<<>()>({}{})>{{(){}}(<>[])}>}<<<{<><>}>><(((){})<[]()>){{()()}({}<>)}>
<<[<[{<[<[<{[[()[]]({}[])]{<{}<>>[{}{}]}}[([[][]])[([]())[[]<>]]]>[(<<{}()>(()<>)>{{[]{}}<{}{}>})({([
[{<(<{(<[<[[[{()<>}[[][]]][(()<>){{}[]}]](<{<>{}}[()[]]>(<[]>[{}()]))]([([<>{}]<()[]>){{<>{}}{()
<[<({<({[{({{(<>{}){[]()}}<{[]<>}[(){}]>}({[<>[]]{[]<>}}))<{([{}[]])(({}[]){(){}})}[{[[]<>]}[({}())[(){}]]]
[[[{<[((<[<{([[]<>]{<><>})<<{}[]>[[][]]>}{[[<>{}]]<<{}>([]<>)>}>{<{{<>[]}}[(<><>)[[]<>]])<{[(){}]<()()>}>}]{{
([(<({[[{<([{{<>{}}([]<>)}[[{}[]]{<>{}}]](<[{}<>]{[]()}><((){})[[][])>)){({{{}{}}{{}{}}}[<{}{}>({}<
<[{(((({[[{{({{}[]]<<>>)<{[]()}<()<>>>}((<(){}>([]{})))}{({[()()]([]<>)}<([]<>)[()()]>)<[{{}[
<[({[<<<<{[({{(){}}[{}<>}}(<<>[]>[[]()])){(<()[]><<>()>)}]}><({<[{()()}]({[]{}}<{}>)>[({[]
<<([[<{{([{<{([]{})[(){}]}{([]{})<{}[]>}><<(<>{})[<>()]>({{}<>})>}<[(<<>()><{}()>)<{()<>}<<>>
<[[[({[({<[{((<>[]]<{}<>>){<<>()>{[]()}}}<{<{}{}>[<>{}]}{{{}[]}{{}()}}>]{<{{()()}<[]<>>}((
[[({(([[(([<[([]<>){{}()}]>(<{{}{}}(()<>)>[{<><>}{{}<>}])]{{<[[]<>]>[[{}()]]}([<{}()>][([]{})<{}
<(([<{([[<[((([]<>)<<><>>){[[]()]({}{})})({{(){}}((){}]}({{}()}))]([<{<>()}<[]{}>><{[]{}}<(){}>>])>
<<[[{<<{[(<<{{[][]}[[]<>]}<<[][]><<>[]>]>([{<>}[<>[]]]{{[]()}<{}()>})>{({({}<>){[]<>}}[{<>{}}[<>[]]
((<<<<[[(({[[<()<>>[(){}]]{(<>()){<>{}}}]}{(([<>[]><<>()>)<<(){}><()()>>)[(<{}{}>((){}))[{[][]}(()<>)]]})){
[({(<{{<<<{<[{[]()}<<><>>][[()[]][<>[]]]>}>([([<[]<>>]{<<>()><()<>>})({<{}[]><()()>}[[[]()]])]<{<(()
([(<({[([{{{((()())[<>[]]){{[]<>}[{}{}]}}([{()<>}<{}()>])}}<{[({(){}}(<><>)}<(<>())>][<[(){}]{(){}}
{[[{((((([[{{[[]{}]({}{})}{[[]<>](()[])}}[{[()()]}((<>{})<(){}>)]]{(<{<>()}<(){}>>){(<{}()>{{}<>})({<>(
([[{<[<[[<<{<{()<>}[<>()]>[[[]{}]{{}{}}]}<[{{}[]}]>>{<{{<>{}}([]())}>[(<<>()>)(<{}()>[<>()])]}>{({<[()<>]
{(([{[{{(({(<[()[]][<>[]]>)<[(()<>){{}{}}]>}[(<<{}{}><{}<>>>){<[()()]<()()>>({[][]}<<><>>)}
<[{<(<<[{[({[({}<>)<<>[]>](<<>()>(<><>))})[([{<><>}<()()>])]]}]><<[<({{{<>}}<([]][<>{}]>}[<(())<<><>>>])
(<(({<[[{{[<[{<>[]}[()<>])((()()){{}[]})>]}}]]((<<<((({}{})<{}>){{{}<>}{<>}})[({{}()}{<>()}){{{}()}}]
{[<[{(([<[{{[([]{})<[][]>]{([][])[()<>]}}<<(()<>)>>}[[(({}{}){{}<>})]]]{{[(<{}{}>[{}()])({()()}
[{{((<<[[((<<{[]<>}<{}>>(<{}<>>{[]()})>({<()>([][])><[{}{}]{{}()}>))(({([]<>)([]())}<{[][]}<()>>)
({([{({[({[<({{}{}}([]()))(((){}){{}{}}>>({(()()){{}()}})]<[[{{}{}}<{}[]>]{<[][]><<>[]>}]({<<>[]>
{[({[{{[<[{([<{}{}>[(){}]](<{}<>>{{}()}))([([]()){()[]}]([{}<>]{<>{}}))}]>[{{[[{[][]}]]<((()[])([]{}))<[()
[[(({{{(([[[{[()()]([]())}((<>){{}{}])]]({<[<>]({}{})>([<>{}]{{}{}})})]))}[(<[[(([[][]]{<>{}}){{(){}}})
<(<({<{<[<(<{<()><<><>>}<<{}[]>([]<>)>>)<{<{[]{}}[{}()]>}([(()[]){[]<>}]{{<>()}{{}<>}})>}{[[{(()[]){(
[(<(<([{<[{<(<()>([]<>))[{()()}<{}<>>]><{([]())<<>()>}(<{}{}>[()[]])>}]<{<({[][]}<[]()>){{{}[
[<({<{(<<[[[<{()[]}{{}[]}>{[<><>]}]{[<()<>>({}<>)]}]][[{<[<>[]>([]())><{()()}{[]{}}>}([({}{})[[](
<<[[{{<<([[{[(()())[()]][[{}[]]{{}}]}[[({}<>){[]{}}]{{<><>}[()[]]}]]{<[(<>())]>{({{}()}(()<>))}}])
{<[{({[({{<({[{}()]([][])}([{}<>]{[]()}))[<(<><>)([]{})><{()()}(<>[])>]>{{(<[]<>>[{}])}[<{(){}}(<>
<{{(({([[<[{<(<>)([]{})>{<<>{}>{(){}}}}]{[<([]{})<{}{}>>[[<>{}]{()<>}]][{{<><>}([]())}({()<>}<()>)]}>]
{<{[{[({[(({<[{}()]({}[]}>[<()>]}{(<<>>)}))]})]{<{<{[[{(<>())[()[]]}[[{}{}][[]]]][([[]()]<{}<>>)[<[]{}>([]<>
((<{{<[[<{{([([]{})<<>()>]<(<>{})(()<>)))([{()()}(<><>)]<(<>)(<>{})>)}([<{(){}}<<>{}>>]<[([]<
[<<<{(((<<{([<()[]>({})]({<>()}(()())))({([])<<>>}((()[])[()[]]))}{<[<{}()>]<[{}{}]({}<>)>>{<<
[[[<<<<[{(<<[{{}[]}]<(<><>)(<>[])>><<<<><>][[][]]>(([]{}))>>([<[[]<>]<[]<>>><{()<>}([][])>]))}<(
<<{[[[{{{{[[(([]{}))([<>[]]<()>)][{(<>()){[]()}})]{(({<><>}{<>[]})[<<>[]>[()()]])}}}<{(<((<><>)(<>{}))<{

122
december_10/src/main.rs Normal file
View File

@@ -0,0 +1,122 @@
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut line_vec: Vec<Vec<char>> = Vec::new();
for line in reader.lines(){
let line_as_string = line.unwrap();
let line_as_char_vec = line_as_string.chars().collect::<Vec<char>>();
line_vec.push(line_as_char_vec);
}
let mut round_depth: u32 = 0;
let mut square_depth: u32 = 0;
let mut curly_depth: u32 = 0;
let mut pointy_depth: u32 = 0;
let mut current_opened: Vec<char> = Vec::new();
let mut corrput: bool = false;
let mut error_scores_2: Vec<u64> = Vec::new();
let mut illegal_chars: Vec<char> = Vec::new();
for (index, line) in line_vec.iter().enumerate() {
current_opened = Vec::new();
for symbol in line {
match symbol {
'(' => {round_depth += 1; current_opened.push('(');},
'[' => {square_depth += 1; current_opened.push('[');},
'{' => {curly_depth += 1; current_opened .push('{');},
'<' => {pointy_depth += 1; current_opened.push('<');},
')' => {
if round_depth > 0 && current_opened[current_opened.len()-1] == '(' {
round_depth -= 1;
current_opened.pop();
} else {
corrput = true;
illegal_chars.push(symbol.to_owned());
break;
}
},
']' => {
if square_depth > 0 && current_opened[current_opened.len()-1] == '[' {
square_depth -= 1; current_opened.pop();
} else {
corrput = true;
illegal_chars.push(symbol.to_owned());
break;
}
},
'}' => {
if curly_depth > 0 && current_opened[current_opened.len()-1] == '{' {
curly_depth -= 1;
current_opened.pop();
} else {
corrput = true;
illegal_chars.push(symbol.to_owned());
break;
}
},
'>' => {
if pointy_depth > 0 && current_opened[current_opened.len()-1] == '<' {
pointy_depth -= 1;
current_opened.pop();
} else {
corrput = true; illegal_chars.push(symbol.to_owned());
break;
}
},
_ => println!("No")
}
}
if corrput {corrput = false}
else if round_depth == 0 && square_depth == 0 && curly_depth == 0 && pointy_depth == 0 {
} else {
round_depth = 0;
square_depth = 0;
curly_depth = 0;
pointy_depth = 0;
let mut score: u64 = 0;
for i in 0..current_opened.len() {
let symbol = current_opened[current_opened.len()-1 - i];
score = score * 5;
match symbol {
'(' => score += 1,
'[' => score += 2,
'{' => score += 3,
'<' => score += 4,
_ => println!("ERROR")
}
}
error_scores_2.push(score);
score = 0;
}
}
let mut error_score: u32 = 0;
for symbol in illegal_chars {
match symbol {
')' => error_score += 3,
']' => error_score += 57,
'}' => error_score += 1197,
'>' => error_score += 25137,
_ => println!("ERROR")
}
}
println!("Error score: {}", error_score);
error_scores_2.sort();
println!("Error scores 2: {}", error_scores_2[(error_scores_2.len()-1) /2 ] );
}

View File

@@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

View File

@@ -0,0 +1,100 @@
9876567896542101249889965434567898765698785435678989898897654789424901245699887678932398943999888667
5995456976543294398767896223456789654988654523599976677789765678919893456989776587891987899898765456
6976577897664989985458989012345678969876543212388965435678996789909789579876543456789996789789894347
7988678989979876564344678923576789989997432101267896323499989898898678999998432345899985345678932123
8998789867891998432123799939678999899976543243458986545689869987674567989987543467899874257899653999
9999898756890986573334987898989989789987654367867897656799878998543459876797665678901965346789869878
9899999646789987654579876567898775679998965489989998767899989019651279765798786989219875457896998767
8789798756789298777689987679987654598999996797597899878999999198762998754569987895423996567934987656
6679659987894399888798998989876543497899987965456953989998998999879896543456998976534597789759876547
5589943198965989999897899492998612986789998954349892199987687889998794332346799989645679898967965431
3469894239899876543956789321299101975679989421298789098996545678987653201236789899898789987899876672
2345789345798765432346695490985423986789879910197678987987657899998543212345897679929899896989987983
1234689956789854301234579989976639897897567891997569896599868998987654324596896578910998745878998754
2345789897892969219345989879897798789995456879875456789476979997699865537788912345921987634569898765
6456898798901298998996898768789987656789345967965345799324989896549876745689302399899876547679769876
7587897689432987887989997657567894345693233459874234678912398765434998656793212989678987756789643987
8798976568999876776577896543479965456789102398762123489323489886755789767894349876598998767896542398
9899435456987985325456789632568997697898912987542012395434678987867899878995998765456789878984321459
0987321249876543101267896521456789789967894599653423569545989999878999989789854321239899989876532967
1965433968987653274378976410367899996456789698769545678956799896989998795698765430198999898998649798
9876549899899985365467995321278998975345698999978996789967998785599987654569896542987898767989798689
6998659789789876476567896793345897683234567899999987897898998654359876543756987669896797659878987543
5109898654677986587678987854456789432126788999876798956799987543298765432345798798765896738769996432
4315986543456997678989998999967896569335999998765329545689499856988664321234569899896954323657987571
5423498642347899789496889987898987698946789999854101234599398767976543210145778954987893212345898430
5439987656456789892345678966989998987956797898943212345678939878987854321236899892198962101236789321
7998798798987899921234568945679899876897976987657723876789548989298975432445689789299753213445678932
9886649899198988430345678936799786765689765698876545687899956994349996547556895678987654567576789545
8765434989019876521234569647987654554599654769987656998989899865456987698667894689598765678689997696
9976765678929998432349678956987833223498943459899789899876799976967898789788923499439878999793239987
9897878789997987653658989769876521014587894598769898776655689989898989899899312678921999999890145698
8798989899876598967967899889987432123456789998655949654234678998789879902954201259890987898921236799
7689994939875439879878925995698544536568999897543239862124559897674567893965354345679896567892357892
4567943129876623989989213989987656697879998795432124983012346789543456999878465456798765456789469921
3459321098765012399992101978798777788989989689321013964323456797652345999986577567987654343459567890
2398939987654123689764219865669998999599876579544329865434567968531237889499787679898763212378978921
9497898999953234569765439754358999886498768478995449876545678954320145679329898799769894324567899652
8976867898764345678996598767467898765349954265689656987787889765621234569210989987657975434678996543
7654656789876656899789679898978987643299863134598767898898999875432349878929876976549876545789989656
6543445678989778956678989939989498656987654249699878989929986976543456789998665989434987656899879767
7651534567899899434579998545695349897898975398988989878212965987757567899987554599321399767948768998
8710123458989910125678997656789232998969876987767899966109854398869678988995463498442459878932356799
9321234667965431249989798767992101239654989876456789954398743239989899767896332987643467989201345689
5432345678976542458995679898943356896543298765334567895987652159899998456889101298776578993212456789
6597656789987843567894623999764877987654697654123456789298789018789997349778922999887789989999597890
7679787894599756778943219989885989598969798743034568994398732124679876234567939899998999678878998921
8789898923987967889994398879996795459998999842126678985459543435678965445678998798999987567567899932
9893969219876598997889986768998954356987899763234589876768954747899876789789998697989876423479927893
7932356929854329545678965657889653235696598654345678989879865656789987899899986545877988765589436789
6541467899773210124567894545678932123798429785456799393989876769893198999999876623456899887896545895
5432379987654432396779943234799543234987439896567893212394987899979999098789985212346799998997657994
7654567898878643989998954545988954345976545987678954493913598998769892197679654303457897659398778989
8769879999997654976867995659877899457898656798789895989897679989654793986598765412598986543239889678
9878998998898769865456789798765678967899769899896799879789789979543679995439874323459995432123996567
7989997896799898765347899899854569998989878998965988964569899865432568954321986445578989932019765456
6497986725788987653234567945965789889965989987654567953478921984321467899439797569789677892198654345
5345975414667898784123489939876998767894395698753457892345890997632345678997698978993566789249985656
4239864303458999894234567892999899656910234989884878921456789896543456799986569989432345698956976798
3199878212367898765645678901988789945991299878976789530345698789654867899875456599531234987899897899
4289989343478999976789789899876567899889989656987897321234799678965798999654323478920134986798799954
5678999656789799987899995767998345789767678967998976532367987579877899998975634567921299875532678995
6989998767895678998999864545695456897654589898959897745459995459989999897996796778932989984321556889
9899899878984569019998753434976568933743456789345679856599989398999989796789989899749878975410345678
8798789989573498929987642123988989421012345891234567967987673267899875685889878998659867896541236789
4685678997431567898998543234599895434999496910126779878998432156999764534678967989898656789532345892
3434567986542459987987654456988789659878989321234569989109843245699843123599345678987745699546456943
2123456797656567896499766569877679998765678932457998997998764556789965654789234678976534678998567894
3234597899997698945349879698767567899654367893459897566899976778999877765692123567894323559898778965
4345798999889999431299989987656458999865458954598798445645987899989989876789012989963212346789889986
5477899998678898740989999898742347899876767895987654320136798989578997987894329898954433577894999897
6567999876545689659878998765431236789987899999898765631247899975459876598965498787895954589912398789
9878989987434567998767849897310135689298946789769987545698999767210986439878987676799867678923497679
9989678998523456987656234998421234793139435697654398656989987654329798323999776545789878789539987567
9694569876313456986543123499562376891098546789452239769878898976498643202987632124699989897698776466
8543456965424569997931012987654567942987657895321019898967679987679784419876543245678993919987654345
7432459876436678998765178998765678993498767987432123987843567898789876323987987656789892101298764257
6556795998545689209874357899876899789569978998693239876732598999898765435899398767897789912999865369
7867954297656789398765456976989965678979999109989346985321487899989876546789219978945679899899876459
8998969199887895459876587895697654567898989235979997976210356789767998769894349989234598788789989678
9549998989998979567989698914598543456987678949868789997921234899854319898965998799015987675679898989
5434987879999767989999789323987632369876567998659699989892347999995423997999897678929876564798797890
6565976765789658999989899939876521379765476896543569876789456789989599886789764567899975433459656891
8679765554567967898968979899985435498654345789432498765689567994678987765989953456989876321298967892
9998654443456898987956758789876657987421235679321989654699778976989876564567892349876543210987898943
9877843312345789876541245696987789876530123789432979795699899987898765433548953478988664329876789654
8765432103556898765720476795498898987545434599999868989789954698999654321238954567899965498765678967
9876556314587899654321297954349967898687567899889747678999643999999876534567896898999876999874789878
6987787323458998965432379965212456789798678998777634567898759789989987845678997899789999894323699989
5598898954569987897763567899101345679899789997664523467969898698678998756989998955567987643214569999
4459999767698876999654578998932956789929899876543212348954989543559899967899989643456798752101478989
3358999878987855798965689987999897993210998987654101247899878942345789878998765432017986543212399976
2256789989876544687899899876789789894321987598785613345678967921456789989999976543128987664343457895
1034598795987432356789998965645698795452399439896723456789456892967895698789897669239898765674688934
2129889643498321768899987854134987689943998921987854578891256799898934987676789778349769876989799423
5345679791987542379998996543029876567899877892398987689932345689789329876565679889498959987899898912
7956789989998673498987987632198765456789766799459998996543867795679499965434798999987898898942967923
8987999878998794567896496544349896777895445678967899329654989894898987654323456789876987769321259894
9499998767899895678954349876456997889954334589979997598767894943767899865410387899985396543210345789
5323987756789987989765212989567898996563125678989987679878943432356789876521239999876987654421256895
6212976545678998999986433497678999765431016789990198789989432101245678989432445689989998765632867934

View File

@@ -1,3 +1,104 @@
fn main() {
println!("Hello, world!");
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
const RADIX: u32 = 10;
static mut visited: Vec<Vec<bool>> = Vec::new();
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut line_vec: Vec<Vec<u32>> = Vec::new();
for line in reader.lines(){
let line_as_string = line.unwrap();
let line_as_int_vec = line_as_string.chars().map(|x| x.to_digit(RADIX).unwrap()).collect::<Vec<u32>>();
line_vec.push(line_as_int_vec);
}
let height = line_vec.len();
let width = line_vec[0].len();
let mut danger_value = 0;
let mut low_points: Vec<(usize, usize)> = Vec::new();
for line_nr in 0..height {
for col_nr in 0..width {
let upper = if line_nr > 0 {line_vec[line_nr - 1][col_nr]} else { 99 };
let lower = if line_nr < height - 1 {line_vec[line_nr + 1][col_nr]} else { 99 };
let left = if col_nr > 0 {line_vec[line_nr][col_nr - 1]} else { 99 };
let right = if col_nr < width - 1 {line_vec[line_nr][col_nr + 1]} else { 99 };
let val = line_vec[line_nr][col_nr];
if val < upper && val < lower && val < left && val < right {
danger_value += 1 + val;
low_points.push((line_nr, col_nr));
}
}
}
for _ in 0..height {
unsafe { visited.push(vec![false; width]); }
}
let mut basins: Vec<Vec<(i32, i32)>> = Vec::new();
let mut sizes: Vec<i32> = Vec::new();
for point in low_points {
let test = get_neighbours(point.0 as i32, point.1 as i32, height, width, &line_vec);
if test != None {basins.push(test.expect("msg"));}
}
unsafe {
for bas in basins {
println!("Size of basin: {}", bas.len());
sizes.push(bas.len() as i32);
}
}
sizes.sort();
let mul_result = sizes[sizes.len()-1] * sizes[sizes.len()-2] * sizes[sizes.len()-3];
println!("Danger value: {}", danger_value);
println!("Multiplication result: {}", mul_result);
}
fn get_neighbours(line_nr: i32, col_nr: i32, height: usize, width: usize, line_vec: &Vec<Vec<u32>>) -> Option<Vec<(i32, i32)>>{
unsafe {
if ( line_nr < 0 || line_nr > (height-1) as i32) || ( col_nr < 0 || col_nr > (width-1) as i32) {
return None;
}
if visited[line_nr as usize][col_nr as usize] {
return None;
}
if line_vec[line_nr as usize][col_nr as usize] == 9 {
return None;
}
visited[line_nr as usize][col_nr as usize] = true;
let mut result_vec: Vec<(i32, i32)> = Vec::new();
let l_res = get_neighbours(line_nr, col_nr-1, height, width, &line_vec);
let r_res = get_neighbours(line_nr, col_nr+1, height, width, &line_vec);
let u_res = get_neighbours(line_nr-1, col_nr, height, width, &line_vec);
let d_res = get_neighbours(line_nr+1, col_nr, height, width, &line_vec);
if l_res != None { result_vec.append(&mut l_res?); }
if r_res != None { result_vec.append(&mut r_res?); }
if u_res != None { result_vec.append(&mut u_res?); }
if d_res != None { result_vec.append(&mut d_res?); }
result_vec.push((line_nr, col_nr));
return Some(result_vec);
}
}