diff options
Diffstat (limited to '2021/day9')
-rw-r--r-- | 2021/day9/Cargo.toml | 8 | ||||
-rw-r--r-- | 2021/day9/input | 100 | ||||
-rw-r--r-- | 2021/day9/input.example | 5 | ||||
-rw-r--r-- | 2021/day9/src/main.rs | 67 |
4 files changed, 180 insertions, 0 deletions
diff --git a/2021/day9/Cargo.toml b/2021/day9/Cargo.toml new file mode 100644 index 0000000..b5e5273 --- /dev/null +++ b/2021/day9/Cargo.toml | |||
@@ -0,0 +1,8 @@ | |||
1 | [package] | ||
2 | name = "day9" | ||
3 | version = "0.1.0" | ||
4 | edition = "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/day9/input b/2021/day9/input new file mode 100644 index 0000000..57b8a7c --- /dev/null +++ b/2021/day9/input | |||
@@ -0,0 +1,100 @@ | |||
1 | 5678998989432198943298876799876586789998999434988989999876544345789987654545678931299765458679432456 | ||
2 | 4556987678949987899097655679767345678987898949876579899987432134889987653237889542989654323488921347 | ||
3 | 3245696569298765678976544598653234567896567899885467799876598245678998542126898769878943212367892356 | ||
4 | 7656789689019654569895432987743125678923456998764345689987987656789987632015789998767932103456789969 | ||
5 | 8767898789298965698789321296532012789912367897651234993398999867897898543234567897657899344868999898 | ||
6 | 9898909899987976987679434987432123567893458976543349892109234978935987654945678956545888956779019787 | ||
7 | 2999312999876899876548976798543256798954578987985498789290125989124698878876789545434677897892197656 | ||
8 | 1098929989765678987537897899674345689765989299876989687989234998995789989989895432123456998999976545 | ||
9 | 2987898763234567895426899999965457999879999101999878576978949876789891096593987643034587899698765434 | ||
10 | 3496987654345979971015678989876598943989898999998765465767898765678992987432398854234567934569876745 | ||
11 | 4985499795659898965434599575989699432396797878999874323456789654567989989321239954346789123456987659 | ||
12 | 9875324989798787896545789434798789101234986767899983212345896543459878976310198765457991014569999868 | ||
13 | 9766219878989656789659897321679893212379875456798432101236998532598767895421239876678962195998941979 | ||
14 | 9854398768878968998767896432567954523459864323987543212456897431999656789532345987789653979896532989 | ||
15 | 8765498656867978979898998543459995745569876434598798654567896569898545678945478998999969867789549899 | ||
16 | 9876986545457899656979999876598989856978987945679898767878987798767634599999567899019898656678998789 | ||
17 | 9989978432345898789767899987987679997899199896789979878989498987656424989987678999198776547578999678 | ||
18 | 8998764321256789995456789999976567989921099798996568989595349998843213577898789878998652123469996578 | ||
19 | 7987653210127897654365898998765465678932989699765439995430157898632102356999896569986541012345987989 | ||
20 | 6398865432238976543234567899901234567899876587996549899321298976543214467899954398997632123657898994 | ||
21 | 5219976554349899432129698999893345678998765346789659798932349998654323578999876497989543234567899543 | ||
22 | 4323987665656798921098989998789556989987654223878998687893499969875987679987988986578994547678978932 | ||
23 | 9495999877897987892987878987678967898998743012567896546979987855987898789876599965489789659789459891 | ||
24 | 8989892998999876789976567897567998957989432123456985434569756234598939898765498744345699967891298789 | ||
25 | 7678793459998965678975456976456789549876543254569876324598542123459329999654399534234898898999987678 | ||
26 | 6547689567987854769894345994347695434987656767878987765987643044678998998765987620123456789998976567 | ||
27 | 2134578979876543456789234789234589323698787878989998987896543235799987989976798731245977898987894348 | ||
28 | 1023456798989874767890125696345678934569898999598999199987654376789876476799987654356898987656791239 | ||
29 | 2434587997898765889931459895456989545679999545457889298998775479898784345678998767487929876545789545 | ||
30 | 3679679876779976789432345796587997656798789432345678987989986567987673234789769876598934987656899696 | ||
31 | 4598798785669899996543456998798998787989699421234567896767897678976543125898654987679875898967998989 | ||
32 | 5689987674357698999876567899899899899876568994365898965658998789985432016789543298789876789878987977 | ||
33 | 6898998543234567899997878965956789921965434589456789984347899899876543123895432109897989898989995456 | ||
34 | 7987899932123456789398989954345997899875325678987896543256789976987754544789549212976799997699984378 | ||
35 | 8996998761014767891239997896456896898765456789198987632149899965599865765678998999765678986569876467 | ||
36 | 9965987654145678910949876789587965789876567991019996543234999843479876878989576788974599875454987678 | ||
37 | 7894398743234599999898765678998954678987878992398897755345898764567987989995435567893987654323499789 | ||
38 | 6989219894346789987769989789989563867999989989987789976456789879679998999876323467932198763212599890 | ||
39 | 5678923998456799976545699899876432345693299979876667896597999998998999998987212348942019874343989921 | ||
40 | 4567894986568897895434567921987321276789129864985456997678999987897999987658301239953198765454678934 | ||
41 | 3656789997679936789323989210995490987899298743234357898789878546786789876543212367893239887565789995 | ||
42 | 2345678998789325679999898929876989898998987654101256789898765435545678987665325456789357997676799989 | ||
43 | 3476789019995434569878787899989978789997987643214347999999854321236789898775434567895467898789989878 | ||
44 | 4567892129876765698765676789998767679876987654765656789998763210145698769887655698986578969899878767 | ||
45 | 5688943436987986997654565678987654598765498769876777899989954321234789656998767899987699656998766456 | ||
46 | 7899894545698999876543234567896543987654329989987888989679895452345678946989878957899988997987654345 | ||
47 | 9956789656789123987632123479987659876543213597898999876565789667458789239878989745987567689998765456 | ||
48 | 2345678967894234696541014578898767998632101456789767987434578978569890198767897659986456578909876567 | ||
49 | 3456789878954356986532123456789878996543212347890345898323467999998932987656789798765343469219989878 | ||
50 | 4567896989996969897543234587990989987654323456921256789212356989897949876545678999973212458998596989 | ||
51 | 8689965699989898798656945678991399898785434567892345892101249876786899865434567899865393567967445698 | ||
52 | 8792123989876767679987898789789989789897545679953556789234598765645697654323798999976989998953234567 | ||
53 | 9893439876565654567898989897678976567998976789767787896545987543234789763212346789899877899762146788 | ||
54 | 1999598765434123456999878978567985445879987899879898987657998652101679654101235898765766794321017899 | ||
55 | 2398987654321014567894569865379876234567898999989999698769876543213568969233346789654545689987634799 | ||
56 | 9987898998432123798923498765456989445789999998993987569899989984623479598944567896543234568976545678 | ||
57 | 8765659996543235689212349878579876556895698987894595456989999876534689457899698965432124589987678989 | ||
58 | 5434347987656345678901656987689987667894987656889696579878989987678994345678969999943012679799889998 | ||
59 | 6521236798965456789892997899893198789989876645678989698769878998789101234589456987899323569654999987 | ||
60 | 7632345899878569898799789910999099895469985434569878997645568999893214456678967965678954578967998976 | ||
61 | 6543656901989689987687678921398987994398996945698969876431467989965323467889899876789995678979876765 | ||
62 | 7654567899798799976546469892987856789987889896987654986542349878976545578996791987899989789998765434 | ||
63 | 8765678978669899865435346789896543899765678789998743297656598967897767899545932398999878999987654321 | ||
64 | 9889889765554987654321234599787012987654885678987654198787987656899898989959893999989656798999766432 | ||
65 | 6998999984323498765634345987652123498753234569988621019899876547999979569899789899877545987899997949 | ||
66 | 5656789993212549876765678999543934995432123458976542323999987656898763456798656789765434876789989898 | ||
67 | 3245678995301234987876789987659899876541012367897643434589998867897652367987545679654321345899876767 | ||
68 | 5346889876418345698997895698998767987632123456789656565678939978998743489876434599865632346989765656 | ||
69 | 6757895987567896789298994349898657898743246577898969996799320989987654578987645689976543559876534345 | ||
70 | 7878934599678987892129789656798745569854768688957898789978999999898965789998786792987654667987621234 | ||
71 | 8989321698789998943345678969959433498769879799346789698767688998789896999979897891099788789599732345 | ||
72 | 9399934789899999874456989298743212349899989890123699545654567987678797899765998999988999995498543456 | ||
73 | 1239895698999889965569890129654101257999897921234568932343879876587689999854349998877989654397654767 | ||
74 | 3398789987898779897698789298765233398998765434345678921012998765434567899873234987765678965298765878 | ||
75 | 5497679876789657789987678999874345479999876845456989532323459764323456789984749896584567994349896989 | ||
76 | 9986545985698745678998545899985456567899987656567896543456798765436578999875698785323456789956997891 | ||
77 | 8765439876789434567899676789876567898989998787898987654578899877748789854976987654212345679897989930 | ||
78 | 9854323987896567678998787898998678959678999898999498767999901987659899765987998765401256798769878921 | ||
79 | 9965214598987679789799898997679789344569989969894329898999892398767978979998999878919347987656767932 | ||
80 | 9876329699998799897689999999569896103498767456789212959998789999898956798939389989898959876545658993 | ||
81 | 3987898989899899954599987878979975212997656345892101345894569889999345987821278998767898765434545689 | ||
82 | 2198987676799989323989765468998764329876543234789292456793298767893239876710367987656797654323434578 | ||
83 | 3999997545989878939878954359899765478987832145678989667894987656789123985321459977645498967410123467 | ||
84 | 9889998659878967899867895456789876569998953256899679998965976545993239876432598766532359978823234589 | ||
85 | 9775879769767456798756789767898987878999866367996567899876987636789998987543679854321267898764345699 | ||
86 | 8654569898659345986545678978987898989398765456789456999987898747898767987654678965432878929875456789 | ||
87 | 8743453986545239875434567899876789999219876567894347898998999898989654599865679876543989434986578999 | ||
88 | 9432012987432129854323456789765898998723987678989256987899298969678913479876789987767998745797989378 | ||
89 | 6543123497654098765212345678954767896545698989879129876789197654599904567989894398979997656998990165 | ||
90 | 8656934598843259876323467989653457997959789698768998695778986543489895989199965219497998797899989234 | ||
91 | 9879899789654348985434578999942346789898994599545986434569999642376789891019876101356989989987678946 | ||
92 | 9998778998765467897665689678799457898767895987439876545678998921235679792198989212349879978998567897 | ||
93 | 9897669899989878998786789545678968998656789876545989656789987892346895679987654323498768967899456789 | ||
94 | 8789456789999989659887895434567899876545457987676798767899876789457893568999876654599545656789577897 | ||
95 | 7678345678999896543998976125678923965432346798787899879954965878967932477899987765987434346899789956 | ||
96 | 8543234567898765432159876546789019876553456899898965989965954767898921276789998879876521235678993234 | ||
97 | 8732145678969876643345987657893247987864579965999754296899843459899632345899879989987432346789210123 | ||
98 | 9543234689654987754456799768985356798977678954987653124989752576789543656946965399876545457894321234 | ||
99 | 7687657995323499767569894978976467899398789432098764349876641345689654568939873212999856567965432345 | ||
100 | 8798767897435679898989943989989878910239896545139975668965432456799767899123982101298767878978644567 | ||
diff --git a/2021/day9/input.example b/2021/day9/input.example new file mode 100644 index 0000000..6dee4a4 --- /dev/null +++ b/2021/day9/input.example | |||
@@ -0,0 +1,5 @@ | |||
1 | 2199943210 | ||
2 | 3987894921 | ||
3 | 9856789892 | ||
4 | 8767896789 | ||
5 | 9899965678 | ||
diff --git a/2021/day9/src/main.rs b/2021/day9/src/main.rs new file mode 100644 index 0000000..75bf1e9 --- /dev/null +++ b/2021/day9/src/main.rs | |||
@@ -0,0 +1,67 @@ | |||
1 | use std::env; | ||
2 | use std::fs::File; | ||
3 | use std::io::{BufRead, BufReader}; | ||
4 | |||
5 | fn main() { | ||
6 | let reader = open_file(); | ||
7 | |||
8 | let mut points: Vec<Vec<u32>> = Vec::new(); | ||
9 | |||
10 | for line in reader.lines() { | ||
11 | points.push( | ||
12 | line.unwrap() | ||
13 | .trim() | ||
14 | .chars() | ||
15 | .map(|x| x.to_digit(10).unwrap()) | ||
16 | .collect(), | ||
17 | ); | ||
18 | } | ||
19 | |||
20 | let nine = vec![9]; | ||
21 | |||
22 | let mut risk_level: u32 = 0; | ||
23 | |||
24 | for (idx, height) in points.iter().enumerate() { | ||
25 | for (ndx, num) in height.iter().enumerate() { | ||
26 | let up = points | ||
27 | .get(((idx as isize) - 1) as usize) | ||
28 | .unwrap_or_else(|| &nine) | ||
29 | .get(ndx) | ||
30 | .unwrap_or_else(|| &9); | ||
31 | |||
32 | let down = match points.get(idx + 1) { | ||
33 | Some(line) => match line.get(ndx) { | ||
34 | Some(number) => number, | ||
35 | None => &9, | ||
36 | }, | ||
37 | None => &9, | ||
38 | }; | ||
39 | let left = match height.get((ndx as isize - 1) as usize) { | ||
40 | Some(number) => number, | ||
41 | None => &9, | ||
42 | }; | ||
43 | let right = match height.get(ndx + 1) { | ||
44 | Some(number) => number, | ||
45 | None => &9, | ||
46 | }; | ||
47 | if num < up && num < down && num < right && num < left { | ||
48 | risk_level += 1 + *num; | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | println!("{}", risk_level); | ||
53 | } | ||
54 | |||
55 | fn open_file() -> BufReader<File> { | ||
56 | let args: Vec<String> = env::args().collect(); | ||
57 | |||
58 | if args.len() != 2 { | ||
59 | eprintln!("Usage: {} <filename>", args[0]); | ||
60 | std::process::exit(1); | ||
61 | } | ||
62 | |||
63 | let filename = &args[1]; | ||
64 | |||
65 | let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename)); | ||
66 | BufReader::new(file) | ||
67 | } | ||