From dcdeb4e1821143ac62472c54d563636164748700 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Wed, 8 Nov 2023 14:07:24 +0100 Subject: [PATCH 01/15] scaffolding for AoC --- .gitignore | 3 +++ Cargo.toml | 7 +++++++ README.md | 29 +++++++++++++++++++++++++++++ day_01/Cargo.toml | 15 +++++++++++++++ day_01/src/main.rs | 6 ++++++ 5 files changed, 60 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 day_01/Cargo.toml create mode 100644 day_01/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..835ad4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +debug*/ +target*/ +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d948d6b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = ["day_*"] + +[profile.release] +lto = true +panic = "abort" +opt-level = 3 diff --git a/README.md b/README.md new file mode 100644 index 0000000..9156286 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Advent of Code 2023 + +## Progress: + +- [Day 1](#) +- [Day 2](#) +- [Day 3](#) +- [Day 4](#) +- [Day 5](#) +- [Day 6](#) +- [Day 7](#) +- [Day 8](#) +- [Day 9](#) +- [Day 10](#) +- [Day 11](#) +- [Day 12](#) +- [Day 13](#) +- [Day 14](#) +- [Day 15](#) +- [Day 16](#) +- [Day 17](#) +- [Day 18](#) +- [Day 19](#) +- [Day 20](#) +- [Day 21](#) +- [Day 22](#) +- [Day 23](#) +- [Day 24](#) +- [Day 25](#) diff --git a/day_01/Cargo.toml b/day_01/Cargo.toml new file mode 100644 index 0000000..e628253 --- /dev/null +++ b/day_01/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_01" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_01" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_01/src/main.rs b/day_01/src/main.rs new file mode 100644 index 0000000..cf19eb6 --- /dev/null +++ b/day_01/src/main.rs @@ -0,0 +1,6 @@ +use std::io::Result; + +fn main() -> Result<()> { + println!("it's not december yet"); + Ok(()) +} From 6470c10c8b97175232d6bfdb339bd51ff4193c78 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 1 Dec 2023 10:10:52 +0100 Subject: [PATCH 02/15] feat(day_01): completed day one ... --- Cargo.toml | 1 + README.md | 2 +- day_01/src/lib.rs | 32 ++ day_01/src/main.rs | 8 +- day_01/src/part_01.rs | 50 +++ day_01/src/part_02.rs | 75 ++++ input/day_01 | 1000 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1166 insertions(+), 2 deletions(-) create mode 100644 day_01/src/lib.rs create mode 100644 day_01/src/part_01.rs create mode 100644 day_01/src/part_02.rs create mode 100644 input/day_01 diff --git a/Cargo.toml b/Cargo.toml index d948d6b..31f8775 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = ["day_*"] +resolver = "2" [profile.release] lto = true diff --git a/README.md b/README.md index 9156286..6ba101c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Progress: -- [Day 1](#) +- [Day 1](https://github.com/ankjevel/adventofcode/tree/2023/day_01) ⭐️ ⭐️ - [Day 2](#) - [Day 3](#) - [Day 4](#) diff --git a/day_01/src/lib.rs b/day_01/src/lib.rs new file mode 100644 index 0000000..7a2b29b --- /dev/null +++ b/day_01/src/lib.rs @@ -0,0 +1,32 @@ +pub mod part_01; +pub mod part_02; + +pub type Input = Vec; + +pub fn parse_input(input: &str) -> Input { + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|string| string.parse().unwrap()) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + 1abc2 + pqr3stu8vwx + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec!["1abc2".to_owned(), "pqr3stu8vwx".to_owned()] + ); + } +} diff --git a/day_01/src/main.rs b/day_01/src/main.rs index cf19eb6..879aa49 100644 --- a/day_01/src/main.rs +++ b/day_01/src/main.rs @@ -1,6 +1,12 @@ use std::io::Result; +use day_01::{parse_input, part_01::main as part_01, part_02::main as part_02}; + fn main() -> Result<()> { - println!("it's not december yet"); + let input = parse_input(include_str!("../../input/day_01")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + Ok(()) } diff --git a/day_01/src/part_01.rs b/day_01/src/part_01.rs new file mode 100644 index 0000000..5451696 --- /dev/null +++ b/day_01/src/part_01.rs @@ -0,0 +1,50 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .map(|row| { + let digits: Vec = row + .chars() + .into_iter() + .map(|c| { + if c.is_digit(10) { + return Some(c.to_digit(10).unwrap()); + } else { + return None; + } + }) + .filter(|o| match o { + Some(_val) => true, + None => false, + }) + .map(|val| val.unwrap()) + .collect::>(); + let first = digits.first().unwrap_or(&0).to_owned(); + let last = digits.last().unwrap_or(&0).to_owned(); + format!("{}{}", first, last).as_str().parse().unwrap_or(0) + }) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 1abc2 + pqr3stu8vwx + a1b2c3d4e5f + treb7uchet + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 142); + Ok(()) + } +} diff --git a/day_01/src/part_02.rs b/day_01/src/part_02.rs new file mode 100644 index 0000000..9364c6a --- /dev/null +++ b/day_01/src/part_02.rs @@ -0,0 +1,75 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .map(|row| { + let digits: Vec = row + .chars() + .enumerate() + .into_iter() + .map(|(i, c)| { + if c.is_digit(10) { + return Some(c.to_digit(10).unwrap()); + } + let chars = row.chars().skip(i).take(5).collect::(); + if chars.starts_with("one") { + return Some(1); + } else if chars.starts_with("two") { + return Some(2); + } else if chars.starts_with("three") { + return Some(3); + } else if chars.starts_with("four") { + return Some(4); + } else if chars.starts_with("five") { + return Some(5); + } else if chars.starts_with("six") { + return Some(6); + } else if chars.starts_with("seven") { + return Some(7); + } else if chars.starts_with("eight") { + return Some(8); + } else if chars.starts_with("nine") { + return Some(9); + } else { + return None; + }; + }) + .filter(|o| match o { + Some(_val) => true, + None => false, + }) + .map(|val| val.unwrap()) + .collect::>(); + let first = digits.first().unwrap_or(&0).to_owned(); + let last = digits.last().unwrap_or(&0).to_owned(); + + format!("{}{}", first, last).as_str().parse().unwrap_or(0) + }) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + two1nine + eightwothree + abcone2threexyz + xtwone3four + 4nineeightseven2 + zoneight234 + 7pqrstsixteen + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 281); + Ok(()) + } +} diff --git a/input/day_01 b/input/day_01 new file mode 100644 index 0000000..e0e1244 --- /dev/null +++ b/input/day_01 @@ -0,0 +1,1000 @@ +xt36five77 +two8five6zfrtjj +eightthree8fiveqjgsdzgnnineeight +7chmvlhnpfive +1tcrgthmeight5mssseight +eightoneqxspfzjk4zpfour +fdbtmkhdfzrck9kxckbnft +9six9 +goneightczdzjk18589 +41two3eightfscdmqjhdhtvsixld +t8929 +fourtwoxsxqqmqf3sixfoursixmmjhdlx +bcbsfd14cjg +95three6threendpqpjmbpcblone +tdmvthreeonefive8574 +5eight82sixtwonev +ninemg2shhmsqh +thmlz4 +xtxjmm2tbbntrmdqxpkdjgh1vzjvtvg7nine +vf19fourddfsvmzeight9 +mmg6fivegcthdonesix1eight +7rzzdknxtbxdchsdfrkfivenjtbrjj +2sdzxhxp +vzvkjvngfjsxmponep9jppnqnbt8jtwo +85fourfivetwo6xvhfxone9 +fivecgtwotwo3oneighth +pbmninesixzcqs5 +ztlhxfmr4mcjnd3two +nfr27zdxchz +sgljlqlcxsnjgppxgqxppqszzgbp7 +five78eightthree +mleightwo68 +threemnnbjncqq68fourmjdfqzqxbdqvfftggf +twofive35llpqrhptnv +pslmgrznfzt823sevennx55 +72fivesevenvnxpkoneightgdm +1sixfdjtgxbflfkvv +77three9 +five6skbsrr +fjvlsthreeffzbjlsm2 +6dpkrlbxfdv5nine1499 +jvt9one28nine29 +57pbgvccprdgnine49three +qmthreekcsttzmgd1nineone7dvhsjg +eightqpfhvqnpbbrbmsz76 +vvqjsevenfourhmdn45 +2eightxgkdtdkfbtcjgrlthreefivekm2 +rfpklsh1threenine +nrhbjqpxssmqbkdbzg2 +556ghndxkrg9 +3six8 +zhkkgqllq9eightninebspgktnh1krfbzdcthreekzxzgszgpd +933five +sixsix5qmbqd +7sevenxckssckcrqnpmfiverqtcdclnkzpztt +5eightpdsvjknine813tnz +k81one +6threegsx48 +three3xbqvcfgjjfsixfiveone779 +threenjtlxnonexqhv1 +5ninefourtwofsqdbchtpkfiveninencsdrnvnbf +vdveightsixthree1 +2mrxnnqngfmshdz +qfourmjpltwolblkzlkdfr41vmskjgrqkv +8hbjcnseighttwoseven +28lvggdhbjgqth +nineshpzfshzm68ttlqbqxxrvdxrkvdkbcnfdkrhcqpqrfmpxtwo +9hhfninefourfiveczdbrn1 +2dpzvd39eightwohjc +3zrhnzvq79fourxgldtbthree5 +two1716 +bgkfjkeightsevenf9gclhlprxlk3 +nbf2rdglrfourthree86xfpqckg +hslr84mkdcqpl3qtszqfngthreeonenine +7bhgqcphnb2fpjrbvrlstvghqbkfive1 +6lkfdcddkbsevenxsixseven2fsg2eightwos +3xjprzkdsckltdone +1qbkshbrcddjtjz4jhrdmpnrq3mmfive +27five3sevenfourjhhv +5fivevhkttflkone4 +5jcthxvhglfourkjrhfour +3lbgzfkljfzkf3 +9tmpczsvskqqtveight1 +kfrjhgsixfive9three1 +onesixpdf8onethree1 +onesixnine5 +1lscknhb +fourhrnmcfgpzffhdtbdcpdbxqh3 +twoffcbgcjnsix667 +sevenfc14 +8kzzrbz2threecclhhjn1l +kqqpzbbqkchmkdl46twothree453 +8two3jqxmnz +eightpszqltlncjpfrdfzffltnthree4 +eight8eightsixdcgqnbvt1lbhlchdzkf8j +64nine +3twofivetwosevenfour9 +lhlntlsixtwojqhsevenone74 +2242eight865p +dnhfbgzcvsx5krv26 +szdnjvh9 +hrzczjbfphxvjqs8one19five2six +lvvkjjvgcjbkhbdnf6ninesqgqhthseventbkzhjbnrb +crmcpjhfmgeighteight316 +vmfqlrnjjnv7fourkqbsfsnnqxfqsixthreesix +7sevensj9919one +hrvtbr2lqkkhn25three318 +rngggthlrfoursix2twopv +3srcpjlqt43eighthscbffksixfoursix +885sixbftpfive +qpfkxbqfph9sevenc2 +9zrtbhztseven +5bcgdvdtzjjbseven41nineeight +two3szknm +klfivethreenine2qdpbp4eightwoqf +1b +8llkrslcbcsfivecqclmfrxbkzfive +twoseven1 +six3ninefivehtxjqjgv6onepkfgjcczft5 +oneskjsh5jmnhsxhfoureightfourcml +thjptkn9rpjkbkvgm2 +glpbbfjgfivep1696xhcprcqxone +5fivegnsbzt8gtxvhfmt46 +fivefoursevenjkxqlcx7onenine +lfourghzdksztjmfour4threepdnfsz6 +two7one +1onesixdzh +7ftphkqlonesxsrlgkczjthreefive3nine +62ninefivefkbmhvlltfourtwo2one +rgoneighttwortjczm24seven +chqxcv3fdqnjngqkkkrvlcgtdfdsvztx +three2dvfpsgjfgs16fmvninefourfrvg +five4bgbfrj52 +98sixsevenfourzdsfjddbmbrffkt1two +nine9xhkcmmvb81onehsmxclb +6146four +slmhsix4hhtrdfdjpk2fiveonec +1oneightd +86sevendgdbnrvgjnmtxrmqtt +six56three2hslccxbnzpmbrctd4 +seven8nine3fiveddvpdjgqrjtwovhbdmtz +five32 +769three1 +mdcmrbj19 +threethreevc6bknnspdvbfzsevenghqnkxnhfxjf +sevensevenzvqkvvgzn12two9bfzfpxjhfslqs +8mmqrvzklbfivenineeightwohnp +zngczscnv8threesevenfourtwo +695eight8szhqcklnc1 +sevenjvnfbbnnine9ninefour64two +four5threelqtwoxhvhjnkgz2 +ztnzbgmtdtwonineeight3 +twosxfour6xsshbnhsixsix1 +threesevendmfivesevendxhsdhsevenqnrvfrgpfl5 +71dbpsk4 +eightxqzh1clmrdljnxh +jqjthreetwo3 +23sixhsvmstwofive +mhvttrxpllkfpxzcfvjtpkfsgd21jtrcrgp +1lcqlznsbdzksklm38frfhdpt +8vjtzvxvfive +9xbcgqhdclrzdnzspllpfrqhnxztftninethree1 +seven5two +fourj5ldxlnine +sixtwosix73jjtrvt +9qqbmccrg8xdgcqmnine84 +fivedmsdlsfour2rmjxqthxfcsgkqgsxxqbtwonesj +one631p +6x2 +93nptkkseightkkgnsnqkpfourone +173 +eighteightvzvxr2pqknnccxqkznc +ftbsixnine4 +fiverpz9ldmbrqhg +7pzbjtnqcfour4319eightcxpgl +tchdxgqsqeight8ftllckrz1 +kz2five +scgflstwo6zzdxdcs +535svzdxtwo4 +7fourone2 +kqczhqqttf74seven +ninesixnttqqzmdg264 +sevenfourthree2one9 +eight1rnnsppqbjzv7jknltmlb2 +6eighteight5cljzvzdtqq8sqftf +ninebnpxdqs1 +seven2nineeight +hjnnqsnfftjstkrlknvlqlkfsdbgz8 +rcbhxcfmkc3ptnt2 +threesix378twostfsjjvmnseven +hllssqjbc41 +six9zbkrrvmxngjone5xqjbxcrkhzr +nineone57 +3pls +sfcdbhkdrpkdrtxtvlkqfiveqvbxhvm8 +fourthreemeight7nine9 +five5fivesfnxfour +h3two2sixgkninetwo1 +53ccblnvbqhjxgbxrs1tk +twotwofxvcmsld2zbjlqzvgzkqvnthfvsftwo1 +eightninevdxxdglskbfzz8sevenfour8 +flhjjt6seveneightlqpfvnpxvthreesix +threefiveonegpcjjvv6fourseven +9pbxlqdkprzcxp +cblkxjmpgrfk5 +qbntftctnine5five3bsevenbskhvtm +7eightsix3threehzn +5nhndlxkthreeseventwocdvnhzrcszhd +six5onekbctwo +fivezhtlpveightvdfltnxpvrzmczdzb9q +nine9xsfq8vtdpmcfgm2 +onesl8mqzgdrstwofour +4onedqtrcc58ghlgxqjlbv +132zvtptdrqt +ftwone1tkzzthvdsevenseventtgvqnbzf +prxrsfvdbt73zblz6four84seven +onefive1xllptjtwothreehsdlgrmjssfgk +twofiveeight781mgjxgzlmgkv +483fbgsx5 +four82onefour +229683five +2onevncxhvscnbtngbzlbbpdnnntr +275cpjnine +j4threefour7dzqleight +ddgmsvmfz7brjdxt5 +sevenfzjtxjtbmj8fivefive6 +tqprcctwofive8sixzmgvphmdsmjtwocbpqc +sevenseven4fourbcscfcprcr5647 +tghsix8 +two83six9sfp99 +fourthreefive3seventwo +7threepx +oneeight69sixgdzbbmlsh +23onelrdvbzljtcdjqckntwo7 +six8onefivepgs +83gnxnjgvdr2four5nine6 +lgfcvptthreesixthreeeightthree23 +seven8gsqznnpb3 +dqr478fivezxtwo +4cgkjbl261 +pnjeight84twodnhh4 +zeightwozhxshspt45 +8rzkpmzlvszvdvonesixeight +twotwothreeseven95threethree +sixbr293sixthreesixnine +3sixsixthree9 +q17ninenineseven8 +jmhbhqjxvc72qsrhzzpnljeight775 +456jnrtpfseven +gjcbnbdkt94dsjrgdq1 +five56g6 +rvjq8dkngcfjdd +twofive43 +sixzz9threektkqmfq4gkxxzbg +2four7four +four3eightsvmnfive6 +ninelvxcxdrxk1mvhtptlpqvff +qrbtlxbkdrjzkznqp6 +z2xdrtqzzrbt9five3five5 +twoeightsevenfive83 +73lzgb1 +oneseventwotwothreemr4dfkjlgshjzvlxqqoneightvvr +5twonvhrbtgfxdlvxrgnpkeightzjpd1 +twotwo6rdfc +six84sevenfschptts1fg7 +8kzqv +hnbjfivettpzkb8six +nine8zcrzgqntworxnj2three5 +1qbjtsqqrttwones +gnjmvqrhnsfive5prbzfsjteightqgfcx2one +fivefour8btcrpntwo6oneeight +kfxbxn4283kcjpkbskshjdss9 +3threethreeh +bmzjktnnrl8fcmmbtgzkj +vhnine84seventwonine +gq639rhqlbzp21qbseven +6rnineptdmntjsix6cnhhbcn +sixscrdone3mhgnmflzxftwonvkxvrftqcxzcfhdgxlvreightwos +6nine6gngneightlvqmmjjtdkrklqbthffhtwothree +bsn4rrgvrjdddhdfsvsffbqnrsbvtfive +twozflqcsix53jcsc5twohdeightwofb +73rx +xldqxlmfcdrnthree47tz6 +foursix6ksrzponeonepdbfpjzspp88 +czvdzccj9nine7 +92one +9one8three +onevfzkmmcrtwotwo2five +8eighttwo6xpfxtvvpx +83four9 +3fourdmtqtvqthree2ztvvdjzmfc +8fourpb7jjlcbkxkxfthree44 +9cchfourltlxpbjpdrdplrqgrqpk1 +fiveeightxlxmr5 +bgrczrdsfrcfrpkprdncbkd55qnhjnzkbpninekbmgnlqgmxfqgndc +6jhcjhb57sfive8oner +six6twofive3ckthree3 +xfxkfourtwokqcpvhzjjzsrxjm6 +four85 +p3foureight3rl +f6four2eight295 +1onevonelslgmbq +pzj6pmjone94 +bkmcjnllrmcfpmqckl3fivenljfgeightvrjfb +kgsczqtwo8qnjhtpfltonepvfive +fourfive5pfnlnjqd +vgrsfxtkcxfstsevennine89 +njkblck4phphvmq7 +ghflplsgkljsfk5 +rtbm32lntjflptp435twosmtb +45four +fiveone41pqgcjt +threesdnllrxbxmhtcmjvdktqqkzs1 +5one14zmxblkh3 +4mzzqpmsfour5pnzonerdseven +9bhcxsnxdv7 +twoxxjnsxxjxfnrtnknseven29c +four54gkcrncmjtpvfbdqr +mmvgl8onekzq +tprfvqnine54rqjkchkxtcrk6gxjgbfqpxg1 +threeghtxbhkkrvxxcrvslhnvczxnine5four4 +bkrkeight4jvjvmnine +sevensevenmphmhvhtjxcdcrcgbdvlmcr6mczlh +eight74seveneight5eight4one +bspq9fiveklkbeighttwo +51four +sevent79zngvzhhlxjrnrbkvnine +six3threepsjhtfoureight9 +44mvfhcthpbv +3fourmrdzqlvb63hrzhqxc +7fivesixcnxfourpfour +csix4 +qqtjk43 +eightnhqpxttth9ddbfjpqjmfour83three +slx6fdm9bspmbxdqjfmkhr +stlxrvseven1gnmnqrnlthreenine +56zjfdrb +sbcxqp8 +7eightsix9eightseventwo +7twosixfour +1eight6 +twor59eightfourkpsix +571fivefdpntmjqgnineftpjtkh5 +two7nsdz +pmf5twoseven4prgqz +7vfmvlgtvkbxnq +eightstdrhhtctthree61fourbmcvhmxtkdhcltxpr7 +fdjcssixninetdgtbkgj4snzpqlninehvvkthree +122pqtcthreesixeight8 +hfztkh2ksjnj43nine +sdlnm59onekdbdrdksixktk37 +4mf114 +8foursixninetwoxbcnq +vbhspqrkvcvpfour68xncb3kdbr +eight841 +2fdr7fivehtnvbk7nine34 +eightsixone1qzp4nxzrslmzrsix +two3zdjntkkcmdpheightfive +44one +17txlqkttvtzhzkjeightcqtpqzxlktxm +two8xcvpzppmzntzr +pbqeightwoncjsvrlzrkspcnqsrrfivefour5 +9foursix1onevflhjngtthree +gp3qjmnsksn3nineeight4 +39fdsdqonefleightwopsp +2dfzsfssnpbzfour +stzzclzgskgbztvrfqgbrms6 +5zpq81threegdg +6three9ninethreeeightsevenninehhgqhxfhlv +1tqftcgtwo +zpkrjxfpzf5jmpqvxrgp6threexbjlzclxpneightscbrsgnine +two4bmlztrzn1 +seven9klmsfmjtjr7sph +sixsix4 +stldx46three15four98 +gthb7nine +lninethreebrzzntwo4 +6fvhgpqj5sevencgmm +75two7qktbnpcxtk +fbkzzsixgshhmzdgt9 +xfivenineseven1dtzsfive78 +four3ptvfmkkhzjsix +j9txxgmsvxx +seven9lmxhzthbnbbxjmbqkpsntbfourone +712fourlqknine4 +xlpnrk64qnjjvszbtkgktbn +lznpdmrcxmzhsjqkc2cjm3zcx +615sevensix +phgjnmqfsqgjtqvzhpc3234qpkpjmxbfr +tnrtmcdljzq1three62 +bcndkzgzp5sixhflsix3lgc +dqgmbkxlv4onetwo +freightwomsix916zrcsjgkktprb91 +fourbcmprdvkbl9vxzfkcjfsqdlbndmrsq +sevenninezpblthreesix2threeqrcdpjqjgm5 +5jnnine +j9fivesix1zhplxlvxsninefour +dm68htnmqdsnzjvqrrj +8four9pfvjjhrbzmlvhvtseven +two256six6qdpldblvleight +ninecprgdrmc2eight91mggqdvhcf +sqbccsh81t +1twovfhbngpggbgff8eight +bxdstkqnbhkvt2three2fg29 +one7threehxcttwosixfourpx +brnine1nineone +6fffrqfjprmdx6sixseven +7four4fourx8sixfour +951seveneightnine +fivefoursix5tg6dpdxrckdh +829schqjxjdxncs6 +seven1twockldvjsbeight +csjv58dshcsdrkgsixrdl +nineeightstjnxcgzbxrldsevenfour857 +oneqpndxmthreemnmlnqnffv7 +twotwo8qkdfcxvd253bhd +sevenfour3 +t2sevenfgmjjrg63fourrvlz +4six9fivelbkzffrvsoneightxsj +nkkdsbp7kkjchhfckqqxfourthree9 +7eightone4fiveninevxtrmbhfbrone +onemjzfsvsbh2vmsxdmcncjlnvtsqszv +eight26 +sixseven7four +cpkzctmhpmpgtwoqfeight79 +vplxpx9tndmcpsone2 +eightone2bqlmdmf +8sevenfivetwod5one +fxmhpcdckl5czfcmeight +9216gdptpmninetkfjszftzdfive +ninefivemrvmdt5nshfxgg5 +9qclonefive91 +six28ninethreefive8two +76five +njdtpktmsqvbfourv9mmonesix +5qxckljjpninedstsfdthreevfrsmtn3 +tzrrbjlbq7eight +bzl5 +3eightsrnlmlvhtwo +vthqnine1zfive2six6four +cseight1oneone +mgnkflp4grkdrrrtfzmjkbtblfivebgcrmgggxjgkm3 +5three1vfrseveneight +seven42seven178 +8bnrrzbbvghcqdbtwo +fivegxdzcmbzsix6sktwo2dp5 +xqdtmb2sevenxpvdlfsqqkgfxqjqq8468 +rscsljnnkd2seven +mzeightwo49ngtzr +threesk3four3five +7shzxkjkgrt71 +nxcnnmkqgv1mdmxtkjtwo9bhbbfive +kdxmcg578six7nxqqbm4ddsxzxmtkq +8rjdkmqfivevpfthbjkbt9mnine +5pxfive +hgnfjtwofive2fxxeightmx +pxpqvld69 +onesixtxmxttxqfour1rxfrvsgdsveight +95nine9onetwo +48ftnmcxrmxc2vqsgrsqskzs +one4threeseventhree86 +one9ninensqxone5zqfvmpd6eightwozgb +527two3croneeight +sixzdzfour291eight +5vzlblhll42hnrrrjj +xzdkkpseven9one1vlsixfour +4tbzbrthree +8hceighthtd +9four4 +eighteight7eight6hfthree +6fourbbx1 +5onepfzgneightsixxb +xzrjpnfvxr61 +two1ninenine6pbhkzzgjq6 +sdltwonecbbffldnfdlkh131ggkrqqg +kxrzqltp3vfh39 +nine79djhzhhgxvglg +5four1one833 +1twosevenseven3three +4nineseven +three42kzqllpf +zkxflrctvsevengkpnqkrrj2 +6oneleight9ncnmxbrnkf +qthreenine4vpbzzzfnine +792 +hjmn85vtfjgtqv +one3sblncblnvf +ssxxddpqbdznrczqgsix64three4 +2four4fourtbeighttmm +fournineeightfive1twofkjcsrklpxlsdvxnt7 +lrlzz8eightmpspsk3nineseveneightnine +v4 +eight9gfzone +jscpppmpnfkhcxlj1pbtbltmj6one5 +8nqrmzrbvzrnine +two34m73twoklqqdlcrrh1 +7fourtmpxnf +twocpbznkqnqsixt86 +81sevenfsdldftgzvnhhhf +ggntprxxqrpknineeight8fourlmvsr5 +sevenztwoseventfour7eight +fkcbvhmqqk1sevenseventwoseventhree +three6ltvtkkhgdpsixsixsltgdngvrttwokz +2fourv +c29fsczzfiveqmrfivejbglcqhl6 +4two3 +nine2kphnrztjltxgq1sevenone +lpbcgone8 +vcznvhllrbfx4275 +fhlsdjmppnrgqhqqxt921 +4four2sqzzcvh +19nbcvlmhlkqfshhckzkzpqbxfour +seven19 +6xprmkgfgntwo4bqk +threexf5threefive4 +3fqone +5g2x68cfghcmtwo8 +1fourq +pflnjchgnine25one5eightxvjkkxb3 +twontnqpcqc17 +cxdmgmvmfssevenone6onezdmnltchtnfour +cpcthreeb76zlglhtkdxrlxjsevenzmvd +sixbhjbcpfjeightjcdthzccpzeight1kknxfck +bhkplfzcdqdfsffcone3seven +ninethreeqnxmgmjct2 +ninexjlfivelqznkhngnsvnqhqsqdglsix78 +9zbqrkmvvnine +1ninehqtmlsd6zjmbgbhjm8 +fourvmht91nine551 +8eightmfgjteight4one +6eight1lhfhmqvjsixjpdgnjrzzbtbcgb +lxptvkmftqffksixtpfnvkxzvkldfpeightnhncz6 +lnsvgtjs2nbsqbkjs +8twohtdv32 +9cbdbvvmninekzbrfchsseven1 +8onethree7 +hk7fourxvqdqmnsdnine +hxbjdsk1vkjbsqn6six3nine7four +485 +5four1kmbcxhjseven +2hjfhtmfg3sixonetwo3five +dvfk7sixninethree +onesix827 +87seventhree +twosix16lfdfkxbmbcfive +1mssxhvgvmxfoursbjdnbrts27rllfm17 +rrvpckn9fourgvrhhzppncrqqml7keightwop +knvmsveightxpbmxptkjbtmzzn3pzbl +47eight1 +815vd5gnbgone +xbclfszchvone21 +7ttwofourthreehjhpjmtwogrng4 +twothreeoneseven9qd66 +9dhbgmqgr7threekfhzkqqg +fivekvxpqrkkffc6mjrpksfcqfln9 +63two97 +txbn163qkbddlcgdznine9 +dztphkcfnnninekjqdb89one +mqvtrtz9 +7hcnmcs +foursix7nine +sevenstm9gfjvjcnmjnnhbgmrkgkccsflm +two2jgfqdmlkdndmsp1rqlrqonel +eight2qfive1oneqjnine +mtnxzx7tthreeshdxpcrhrk +vzdgktfcq1xtfxqzzfdpng693btkcqhxpsmczbxq +zbvgtnzmlghvhnklhmhjnxckone4six +98eight2seven3hhbxgvnhbg +seventwo7seventhree5ltlptmhjp +1rf75fivegx +2nineonemlhxmhdt7six3 +nine6sevenlzndhbnpkxdhdtcqpzgvkskvjkhphpqnxl +qnqpnlxcjr3phkkpnccxr2sixrslhrtf2 +12mrkvqsjdcc47six +onehhdveight35jgnmgvqgxdr +hgoneight5 +m9eight384eight1 +1p6 +3sixnine41seventhree4 +5dvssdf +blchhcfourgzvssbhqdxdbxt14zvbknhnll7 +7twotgqrxddm5 +btlkbgbtwotwo5 +five63ninesnqx +fivemkvjpslfoursixfivepjvtx4 +74threeeighteight6cpbxbqgzh +twoeight7bh +rgtwo7fnck7 +five4sixnvjtcmx12one +ghqgdfour33 +tjpdztkfour666dxgzjzlgnz +ninegnqrqpbrz8seven7sevenhfbone +npzlpklnvgone6nrjcpz8 +2seven6m8 +seven3fivegqvxgrjcnine2xppl +nbmvdgk9grz4 +four291 +tdnfgkzkxbfpkpcthreenineoneone1 +7nineflqptxmdjgttpll +one1jnhcvthree1 +foureightkxfcqlkp2fclpr8bzxtfbl8grdsntkrst +six16fivezqbchmjbb +zjhkd5two8 +745shv +l64dqvsixthreethree +rffrrmjsfj2jblgqnl7gjfcqtszfour7 +fcqcmchhc7t +twofour834ninefpvbvps +gmhhjr94svpddnmxvd98threerlljccvq +ninetwosevenb71three +eight4xllkpmjkrp4bv +zhvzsfourrkflmjpjhfourfjbd32jkvrpzqg +2frgbkngmsix9stldncmsczh +eight9three42grlbfps85 +1lrshxpgxlsnrlfjlxk2 +jfhmhcml8mvbfkxhmoneeight9 +five8sevenlzvpzpsbjfourseven1fkcfdf +4vmldcjpbbsnfourseven4three +mfchhbqk7bdlrqhone2fiveone +hnncfrgmnkg5six +dbone55seven +geightwoonevhxbqbpfkx6two67 +sixcrnq1hnrsttfxvjeighteighttwoone +fiveonetwo7six +9nqrmt5n9fkpbhd7 +two1fivesixtsrq +684 +ckbxntxfhthreefour7sixseventwo +stdhzqdpnxnine6tn7kjxftfbksevennhfxznjxgchm +8czxhfptlknjlhn5clhzdfgntx741 +3nine64vvgsfour61pzb +rloneight7lfrfqqb8fivefour3 +vnhbtkbqone4sixsevennmrgmmptvreight4 +bdvjp354992jgfhzzvhv +fourp1 +five7oneseven +oneone954threefivesixsfr +sixfivevmzlzdfjvq5 +4bs +threetwo64ccks3 +onetxqh3ljpnkr9onegsmkq +five24seven6threethreezfqzgzjztgntkcfzt +3fourv2 +eightoneeight6qseven47seven +559ninexcdkqvszsq1 +95sixthreetwo8fourthreesix +ksxtrn1gqxthvxjltfivejpmgvz8 +ninezdkfvgceightnine7sevenfiveeight1 +h43zktkckcjrg5nmzjkrzmjqsix3 +44pqlgmmzvgzpslmnccdjlvq +4threefoursix87 +92gnjfsxzlcn71qzcbvq6nhmgsvmbrvvjzqlndhl +hkmjkc9fourpbgjlvqxhbhnzvmlnfrqsixsixksix +mspbpsgzn31ninesix +7xqtttthreegr4fourrbflxfznfs5three +rtpknqgsix7sixthreethree1one +qqlqh8seven +six11eightkh73one +eight9mjzpmhcr999fourfive +fzeightwo7smqq +7dzzxnttq +4vfk9 +ninermjmpprxmsdgrpgzmmqtjxkpfour5kscxs +h5lnhnrqhvltpsns +twosfdsxcjjhg871755 +1one5 +933zdzz1five2 +4zxh9j +bnlmdd9 +nine4xchhpgnfjtwohggvlkkxthznnf7four +3foursixhmsddvmqkgpjvone34 +bjfxjlseventhreeninesevenfivethree9 +qhqgxtjvslnvbvjsixjplmfq8 +8ffjpthreerzkdjkjjzxvqkspsevenfive +3three4two1 +fiveldkk7ninesevenmfnjfjmdgeightprjrq +4rqbz6eight3lbgbpgcveightthreefive +sixdsmvltdv2six3fourxtkzsvjdbvqfmthree +3one2xgzvczlxvfdpjqgninefourfourzft +sixcnfznvcb8four +3threethree +trzqdpslthree8 +3fivefourninefivethreefive8nine +1sevensevennineninezmghprscjskqppgmh +6zfoneqkxhxmtwofour3four9 +twosix6dxftmjgclkkmzxmkglmqvsrtjcp +ninejkbk8v5fivefivethree +9twoneqv +1cq51lklfoureight2 +8sixfournine3one +5plgtszkt6856six +qoneqjph11hgvhhkglsreight1pkxrltrzm +nine25mlxmkmcg4xlrntfsrrxmlhdtzt1 +fivetfdzmsq2 +pdhnkbjrj5fiverjpgmntqkpmx +fourvgc1msdnxrbpbcjfive +14tbcfmjl3sevenvj +ninezrcfqvg9 +5eighteightkkvtsbrkjlthreegspdm +99four5two9 +pvlvjvhmg5krzl149sixtknbgfsgvf +44tvzgpz1 +8threenmhmptmshpcsix +5rdjmjnmkcd +ctwo6 +two1dhtmsdtsds +ndjd4sbzm5fourseven82 +rfqsxkdhcr1twotwo +4qrt5threetwosevenffjdvtzhgfd +6421 +sjgfshzjg5four +threeqpvpvdhgjhpgqxsrhhp5dqjqlr +five7ncchvpcfmglmfzstrmvhfnqndzrmvb5 +fvnfmcrh684bjq63dxnnlznrn +threedccxvrtbj62bfqfzlpxms8five +893shxkgpvpfour36nine7 +dtoneightbrmthree6 +6seven58nine1mgqqtzfsszlrgghmlknx +four64 +five5dklfour4 +lgv2eightvcp +pnfkrfnncv3lgqlrznone +sgcthreegxrnfkpb731 +8twothreetnxlmjtwoneb +txkrmcklpdhclrklxz41lhddmkp +rqndtwosixq8 +btbndntqsllxc6vzfrdqm2sixfiveeight +rhvfourp1seven5cxcc +jlzzqkvfxqltgbnj1 +24szksixthreeone62 +2fourfpmjltjnqvgrgxxbggtseven6qms +78prqkfbf6sevenfivesixeight +drtwoszsevenckqrxfseven3sixseven +zbsj6four +hrcxgvdbd6 +six9three4onefournnsxgvjtrdthree +441 +fjnjksnvgjznxflonetwovcnmvhdgjfggfvxmmg2hppfcgjqv2 +kjszbvrcqznbf4eight5four +twolmbrdmx8kpltmqznslfsixsix +6bjrfthk8 +gmzzsthreensrx3 +sevenz63xlscxeight +fivehmrcphp7nine53 +fivepmqvjqllgthree5vkpn94nine +seventhree9sbklvfvpsixr6 +one56six +pfivetmqrbqxccpxsbcc87five8 +mtmnvlhfsix75ppk9rrqzbbplmbseven +547mmntlxflkztkkftxkrrk +5four9threelcqlsrlvg2 +gglpjhvjnnnrcbj3n55 +xgdbjcrxdf5jjdfrrkzkdzpjkrlstwogbzcjq +cfmgxkjcfq9bkcpkrpltp +fcqkjhqgpkthree4sixthree84frmfqqkrs +onetwosveight68ftgjft3four +jvlmg2eight3bdptwo4sixseven +three1bhdkfnvt +6eight12fiveseven +onedbxgcqnltfour35 +sxtwone5336psninexnrjsxsix +four79one47jvmmhhggcthreecgkxhnlm +17eight2fivetzcfmrlxvd +rxlhkjlm778oneone +8one1one499nine +six2fqd +9smpkfchhzn8seventrpbvcssscseven +nbmqctcldmnvnbnzns9twofive2three +1five1two +89brvbgnine +vcbrztfjzn4 +4fvmlvxjd +lsixoneplt7two9 +kphbsssvsnjgjnvqjcbh2nkmfeightseven +three4xsrmfldqgpqxbpsevenfour +rzd8xzmcdch +bjffone41xzone +xkmhqhgr2lxpv4jnsrnfour +31cplcztdkhsix +6tkzzqqckvdlkxrfourrveightwoch +26vkvbdvggfcsjccrkzbh +3nr +ninefourklmqgsix3cvnr +mlnzksvhnvvbxpsfnspl6hpqkdmg +btwone1jpgjdmgbfive +817sixqtvhxpfglj5kzmbtwofive +1tworcsevennine +xseven69six11 +four3sevenjmgqzf3two +mbrfzsj2 +8xgsdninefourseven +six14snscgfnconeeight4 +seven4fivefoureightfive1 +threegjgdnqvhqlbdv24fivexdvpdthree +9ldkmtzghbtwohqzzntwofive +fourxh8 +eightfkxhflcone3xscqdqqkrk +qzlbtgc2sevens7 +rfjgmfbjt185hztszz111 +pxsixssnfgtfour9 +zxflhgjnmbgfdqtzs4twojzxbrsix +7cc153fivel8 +bzoneightlrvmnrh98 +threetwogd6bhdkrjhxt2three +hlrscbd8rvppfclqdtseven +twofivelbjh3gbnbdbpthtgpqjmxjlmzjq9zqll +2fczpbhqjqfgzvzzkqgmsevenone58three +fourzvmlt3sggpjzssljc8twoeighttwo +ninescglkgftppbm187 +five1vll26two +czghbj5827 +nine7threerngqxk +four5bjbhfglffourseveneightfourj8 +djqrxfive4five8 +3xzhpqc6six +1htdxrgtrhvpgz +5bkxxsccdftjmhkfjqvjzjnine +pxpdfdnnpqzspxd9xrpvrxhnsix9four3 +gtxlzxdninexxhseven5 +xcdkvdg7nineeightsdjvkhzgmone +zrnrrpxdfcnine2qgjxzfxcqgghbdk9 +twozrckqbppsixhh7pmrmnktnrb7five +tdxpjz2kccgqzcslm8sixsixtwo +8tgcb32 +eight391gxm99five8 +one3fivegrpjr4trqxj5eighteight +7eightzsnndnine8fourgphhbhxn +7five8seven6mhkksix +8jvccfldpqfourgltcqbkpkfourtwohbvnnine2 +rrvmvtvbrmsgnstbghfl28 +66fourninekjzfzrj +seventwoninethree3plvfoureight5 +fmpzrfpqqbfivenine7 +nrkbbdqdthree1fkbfivefourqlksbfdsjtthplhjhlvgh +176seven +3fourfiveseven681 +five88rzxk3mqgtkvgpdx +pgttnnvnhtl4rljpqjzhphmkztwokdqspbth +54eightgngggmdmnxgd +d6jnkq78cgvkrgseven +sixeight7eight1dmvvprm +72twotwo2kntsrccqkmzbn4 +snctbphhl2jgpghbvhg +one5xxvhfive6twozmlxddppmb +145eight5jcpmqhkpgrdbvlzl3 +two4vtlfddtzrzlmkgx15 +2fourseven9pxqzdf3 +6ninec3 +ninefsix54 +sevenbzgrdbbvfmbkvb8 +vntfxdprf91rmgvxlxhntsztmggjr1 +six4spdffrjfthree595four +pxfftjkdnninexkjvskrtpmjkhnncflz26 +dkjmxmhpc7eight7 +hsbdfeight9hhrjcn8twothreehjpnbhslcc +437kzcqvsmxkknfour +8twoone54sixvmlcjdnrgnqbmlrvdtnh +one587 +4kxxsrcseven33dcvbbgkbcd4fourfour +nine56 +oneeightpz4rqbrcdvjbx55 +threefive33ninexrlj +3bxjdxlmlgs +gqmkpqpppfour9fouroneeightseven +492 +one398threetwo6dseven +j8vlfrninebdgnineljtvbeight +nnnhx28sixtwo +fiverbgztggr6ktrsfvbtqlbtdshqjxjhj +qmdjfthzfour318hrffjj +977dpssrfgfxfxkk6 +rvklzfdfour3twofour8five +czbjbfdcbs1rgkfscgntmhksixhqgbrmzvzrg +3xqlbqbcxnfq +six29 +jhnfthree7 +five1821 +578blhpbtrjthree83qtflsj +fgzqnrvfcqkfrdmgkndmrxfctgvsbfive2eight5 +feight8eight +3qmfbdztrgpb5 +ck5three14lzmffcljxcr +3twoqgjqlvjeight5nddhpflj1 +vvdsttjjjgnrzgfournkkhp622onesix +two7seven6 +7941 +28two7 +ninejhxnjctz3jtxmlmjg +vmjpxvqg24pzjfsxvcg5seven2 +xpqbffpvmn5 +32eightseventhreekqdgtcxgjxvv1seven +2eight3tllbkklghb7 +sevenfour1 +tjsjfldclvqq1sxcl +mrdqvsxk15jbxstdph +9fivefivebbtcvhqjjdvvqeight +fourdtbkzvhrbvzqnnzghnpv1231 +bz34five1 +six26two2 +bczkffnrtmsrctwoninelbnxoneeight2 +sevenstvffmtkqqxg57hthreeeight +4fivesevenrdfcdrxd4 +bbbrjsjrmn8px +317vncvpdrh91threetwosix +9sevenzpfivethreefivenineseven7 +twodsnvkbvfbtwo62tt6 +threesctkeight3vdjrnflh9nckgmzhdf4 +eightsixtwo39skhnzf +foursix6 +3sixzp5fdzjv +ntwone9xzspfhrthreehsgpqdone62 +five9597tgrfbl9 +six4rvrcbbjzfdcgctsxjxmjrpc4thmmj8 +3sixmdglgdj2zzrkgsdgk2 +cbfxpgftninekthreexvmhxmkx1fourf +76twoone +7six32two1fivefspjtdsix +nrcntbgdtjsevenztsmsgfmfour9thslsmhgnk2three +21ltslbrnineseven7 +41 +sevencdsnmzkspseven49 +jbzkthreebjgvhfhcftwojbzdbmcdlff29 +sevengjlph11fivexhzdrs +fzkznzxgbsfq4 +gvpqjgrfdbdtrpqseven11mmjpkz5 +ksfjpnchqzqone15eightsixfivenine +2skjmpvdfoursixtwones +dmtzhqfivethree3eighteightoneseven +one6fourrrrtkkjvr +229spkrxlg +4xjqzfourdtmq +mkv5six +8jdzlpqvc89two +2foursix69k +xbqrmktzfive4 +five9rjrvcpfbseightfkmlgbvqkbqj +qnmkvkmckfxqmdtwosevendj6sevensixfive +mkdvknghvsgzrbbjqngbsqeight6mjxfivenineq +6two97mxm +two26jjqjs +1scslcns +pckdk4onesevenpnxq8lxxbzvftcbeightl +qczvprdbeight3twosixsevenfivenbzj +462mfgmhfseven4vmv +gqsrhltninethreedbkrjdfivetwo1 +ppsix99nine8qvhbnn48 +nine35fxlxqfctwonezf +four2twofive1 +847dsgonethree1vkmhmtv +8gnfbqmzszxdfkv5five +22kvqd +five1sevenl59 +kgshxdjdfjqzthhplhjqxbjpbxggtz1 +fourbzbzvtfj8twoone6629 +hn28six1 +xqxknfivevxhljqpsixtwo3 +hsconethree1fourthree +three5gnlgcthree36rkvlpkcvronefour +rvjqkndonenine3hbrtlzhcbfbkcfive +hhrmnbnct6rdxxtkxkcrbdfg5 +6xsqkbthreerjgbzfhb +sixnlxzmblfzx5cspjqthree +4twondrxjb +4three6b +4four3three16 +eight3hshdzptsq6twosfour3 +eight37twotrxvpxk2ksbldctj +fourseven8fkmtqzdbfourseven +6vngvcmplx +jhr6gfplmzpr2fourfive +eightnine49twoeight2nfndmmb +three7threej4xnffjtnckltwo +onegzlnjsgzlg82mkbfhtmhfour +85vnseight5fivevcqrgvgrtp +47chxx6pkrdxvmrvvfxbl5 +34ctkstrjxsnfourseven +two9zcrghthreethree +veight37 +two44nine449 +58eight98cspxfhftvx +cxpththree7pbmhhmkfzfvthree +8nine2hbmdnvbthree +1four6ncdvzqjqhx1 +1bgqspl958lrj +7nvmqrnthreejbzgnzvzpgkr69 +7576threesix +twoc83pt +fourkdnsvcq9sevendmhsdgt54threej +zrjts8sixsix237flm +8eightrndfour +two9jsix5gcxf +fivefour7nineseven1qtcdqbp1four +fourzvkqhdninetwoftscrmsd64nxsgx +q1tdsskthree +mkhttggvjh9ctzffdqdjnheightninegmxqxhqrfqgbgzt +ninep2fourf +fiveeight2zxjpzffvdsevenjhjvjfiveone +15737seven +pdrss6oneone4fournine +7b From 1f75ed04fc6a3ae513e61f92cfaf61896ea1b56f Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 2 Dec 2023 18:49:16 +0100 Subject: [PATCH 03/15] feat: completed day 2 --- README.md | 2 +- day_01/src/part_02.rs | 2 +- day_02/Cargo.toml | 11 +++++ day_02/src/lib.rs | 76 ++++++++++++++++++++++++++++++++ day_02/src/main.rs | 12 +++++ day_02/src/part_01.rs | 40 +++++++++++++++++ day_02/src/part_02.rs | 45 +++++++++++++++++++ input/day_02 | 100 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 day_02/Cargo.toml create mode 100644 day_02/src/lib.rs create mode 100644 day_02/src/main.rs create mode 100644 day_02/src/part_01.rs create mode 100644 day_02/src/part_02.rs create mode 100644 input/day_02 diff --git a/README.md b/README.md index 6ba101c..278d98e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Progress: - [Day 1](https://github.com/ankjevel/adventofcode/tree/2023/day_01) ⭐️ ⭐️ -- [Day 2](#) +- [Day 2](https://github.com/ankjevel/adventofcode/tree/2023/day_02) ⭐️ ⭐️ - [Day 3](#) - [Day 4](#) - [Day 5](#) diff --git a/day_01/src/part_02.rs b/day_01/src/part_02.rs index 9364c6a..c6032ea 100644 --- a/day_01/src/part_02.rs +++ b/day_01/src/part_02.rs @@ -14,7 +14,7 @@ pub fn main(input: &Input) -> Result { if c.is_digit(10) { return Some(c.to_digit(10).unwrap()); } - let chars = row.chars().skip(i).take(5).collect::(); + let chars = row.chars().skip(i).collect::(); if chars.starts_with("one") { return Some(1); } else if chars.starts_with("two") { diff --git a/day_02/Cargo.toml b/day_02/Cargo.toml new file mode 100644 index 0000000..9792e74 --- /dev/null +++ b/day_02/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_02" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_02/src/lib.rs b/day_02/src/lib.rs new file mode 100644 index 0000000..a1f734f --- /dev/null +++ b/day_02/src/lib.rs @@ -0,0 +1,76 @@ +pub mod part_01; +pub mod part_02; + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub struct Bag { + red: u32, + green: u32, + blue: u32, +} + +impl Bag { + pub fn new() -> Self { + Self::init(0, 0, 0) + } + + pub fn init(red: u32, green: u32, blue: u32) -> Self { + Self { red, green, blue } + } +} + +pub type Input = Vec>; + +pub fn parse_input(input: &str) -> Input { + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|string| { + string.split(": ").collect::>()[1] + .split("; ") + .collect::>() + .iter() + .map(|record| { + let games = record.split(", ").collect::>(); + let mut bag = Bag::new(); + + games.iter().for_each(|list| { + let cubes = list.split(' ').next().unwrap().parse::().unwrap(); + if list.ends_with("red") { + bag.red = cubes; + } + if list.ends_with("green") { + bag.green = cubes; + } + if list.ends_with("blue") { + bag.blue = cubes; + } + }); + bag + }) + .collect() + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green + Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![ + vec![Bag::init(4, 0, 3), Bag::init(1, 2, 6), Bag::init(0, 2, 0)], + vec![Bag::init(0, 2, 1), Bag::init(1, 3, 4), Bag::init(0, 1, 1)] + ] + ); + } +} diff --git a/day_02/src/main.rs b/day_02/src/main.rs new file mode 100644 index 0000000..a41bdc7 --- /dev/null +++ b/day_02/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_02::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_02")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_02/src/part_01.rs b/day_02/src/part_01.rs new file mode 100644 index 0000000..a9ba85c --- /dev/null +++ b/day_02/src/part_01.rs @@ -0,0 +1,40 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .enumerate() + .filter(|(_, games)| { + let possible_games = games + .into_iter() + .filter(|game| game.red <= 12 && game.green <= 13 && game.blue <= 14) + .collect::>() + .len(); + possible_games == games.len() + }) + .map(|(index, _)| u32::try_from(index + 1).unwrap()) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green + Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue + Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red + Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red + Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 8); + Ok(()) + } +} diff --git a/day_02/src/part_02.rs b/day_02/src/part_02.rs new file mode 100644 index 0000000..4d3e35d --- /dev/null +++ b/day_02/src/part_02.rs @@ -0,0 +1,45 @@ +use std::io::Result; + +use crate::{Bag, Input}; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .map(|games| { + games.into_iter().fold(Bag::new(), |mut bag, game| { + if game.red > bag.red { + bag.red = game.red; + } + if game.green > bag.green { + bag.green = game.green; + } + if game.blue > bag.blue { + bag.blue = game.blue; + } + bag + }) + }) + .map(|bag| bag.red * bag.green * bag.blue) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green + Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue + Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red + Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red + Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 2286); + Ok(()) + } +} diff --git a/input/day_02 b/input/day_02 new file mode 100644 index 0000000..ababd44 --- /dev/null +++ b/input/day_02 @@ -0,0 +1,100 @@ +Game 1: 2 green, 6 blue, 7 red; 12 green, 6 blue, 3 red; 5 red, 18 green, 4 blue +Game 2: 10 green, 4 red; 2 red; 12 green, 11 red, 1 blue; 1 blue, 11 red, 5 green; 10 red, 9 green, 1 blue +Game 3: 3 green; 15 red, 7 blue, 1 green; 3 red, 6 blue, 1 green; 14 blue, 13 red, 2 green; 1 green, 6 blue, 6 red; 16 red, 13 blue, 2 green +Game 4: 5 blue; 8 blue, 7 red; 9 blue, 5 red, 4 green; 4 red, 1 green; 8 red, 6 blue; 2 blue, 4 red, 3 green +Game 5: 3 blue, 4 red, 10 green; 13 green, 8 blue, 2 red; 2 red, 4 green, 6 blue; 3 blue, 5 green, 2 red; 4 red, 13 blue, 8 green; 9 green, 2 red +Game 6: 3 red, 2 blue; 6 green, 13 blue; 11 blue, 1 red; 4 green, 3 red, 5 blue +Game 7: 14 green, 2 red, 5 blue; 4 blue, 8 green; 2 red, 2 green, 9 blue +Game 8: 10 green, 6 blue; 3 green, 4 red; 7 blue, 7 red, 5 green; 1 red, 11 green; 3 blue, 2 red, 11 green +Game 9: 4 blue, 1 green, 6 red; 5 red, 5 blue; 1 blue, 7 red; 4 red, 8 blue; 6 red +Game 10: 3 green, 7 blue, 6 red; 1 red, 5 blue; 6 red; 1 red, 5 green, 6 blue; 5 red, 2 blue; 6 red, 4 blue, 6 green +Game 11: 8 red, 6 blue, 6 green; 5 blue, 2 red, 5 green; 4 blue, 5 green; 4 blue, 3 red, 8 green +Game 12: 5 green, 1 red, 7 blue; 1 red, 10 green; 6 red, 3 green, 7 blue; 5 red, 6 blue, 7 green; 3 blue, 4 green; 1 red, 3 blue, 3 green +Game 13: 11 red, 9 blue, 16 green; 1 red, 3 green, 3 blue; 14 green, 2 red, 7 blue +Game 14: 4 red, 8 blue, 12 green; 7 blue, 4 red, 2 green; 4 blue, 7 green, 6 red; 3 red, 11 blue, 12 green +Game 15: 6 red; 3 red, 5 blue; 3 red, 10 blue, 1 green; 2 green, 8 red, 2 blue +Game 16: 2 green, 4 red, 6 blue; 2 green, 16 blue, 2 red; 13 blue, 7 green, 3 red +Game 17: 5 blue; 1 blue; 2 red, 2 green, 4 blue; 6 blue, 4 green, 2 red +Game 18: 3 red; 7 green, 11 red, 6 blue; 6 red, 3 blue, 12 green; 5 red, 3 blue; 7 green, 8 red, 9 blue +Game 19: 2 green, 13 blue; 11 blue, 16 red, 7 green; 13 blue, 10 green, 8 red; 18 red, 1 green, 14 blue +Game 20: 10 red, 6 green; 1 blue, 6 red, 15 green; 15 green, 9 red, 3 blue; 3 blue, 11 red, 9 green; 2 blue, 5 red, 10 green +Game 21: 1 green, 5 red, 8 blue; 4 red, 6 green, 7 blue; 13 blue, 10 green, 6 red; 9 blue, 2 red, 14 green; 16 green, 10 blue, 1 red; 9 blue, 3 red, 11 green +Game 22: 4 blue; 1 green, 1 red, 16 blue; 15 blue, 1 red +Game 23: 2 blue, 6 green; 15 green, 2 blue, 10 red; 1 green, 6 blue, 6 red; 8 red, 1 green; 4 green, 3 blue, 10 red; 7 red, 3 blue, 20 green +Game 24: 1 red, 13 blue, 8 green; 3 blue, 7 red, 8 green; 3 red, 11 green, 12 blue; 7 green, 6 blue, 2 red; 11 blue, 5 red, 10 green; 13 blue, 3 green, 8 red +Game 25: 4 red, 6 green, 8 blue; 4 red, 10 blue, 9 green; 5 red, 2 blue, 3 green; 2 green, 9 red, 4 blue +Game 26: 14 red, 4 green, 5 blue; 3 blue, 2 red, 3 green; 1 red, 9 blue, 1 green; 5 green, 15 red, 8 blue; 5 green, 6 red, 6 blue +Game 27: 1 blue, 1 green, 9 red; 4 green, 1 blue, 1 red; 4 green, 4 red; 5 green, 4 red; 1 green, 1 blue, 8 red; 9 red +Game 28: 11 red, 3 blue, 19 green; 3 green, 13 red, 5 blue; 8 blue, 16 red, 18 green; 13 red, 5 blue, 7 green; 8 red, 5 green, 4 blue; 16 green, 1 blue, 15 red +Game 29: 12 red, 15 blue, 1 green; 3 green, 10 red, 5 blue; 4 green, 5 blue, 17 red; 11 red, 2 blue; 6 blue, 3 green, 4 red; 1 blue, 13 red +Game 30: 2 blue, 2 green, 3 red; 1 green, 4 red; 1 red, 2 blue; 1 green, 1 blue, 2 red +Game 31: 3 green, 5 blue, 4 red; 3 red, 1 green; 3 blue, 4 red, 3 green; 8 blue, 8 red; 6 blue; 1 green, 6 blue, 1 red +Game 32: 3 blue, 12 green, 2 red; 13 red, 12 green, 4 blue; 9 green, 1 red; 10 red; 12 green, 9 red; 15 red, 3 blue, 10 green +Game 33: 1 green, 8 blue; 3 red, 7 green; 4 red, 3 green; 5 red, 9 green +Game 34: 12 blue, 3 red, 2 green; 12 blue, 12 green, 2 red; 10 blue, 8 green; 3 red, 3 green, 9 blue +Game 35: 4 red, 4 blue, 6 green; 3 blue, 8 green, 2 red; 5 green, 2 red, 2 blue +Game 36: 12 blue, 4 red, 5 green; 18 blue, 2 red, 5 green; 2 green, 2 red; 3 red, 9 green, 13 blue; 2 green, 12 blue, 1 red +Game 37: 8 red, 3 blue; 6 blue, 5 red; 9 red, 10 blue, 3 green; 4 green, 10 red +Game 38: 1 green, 3 blue, 7 red; 6 blue, 6 green, 9 red; 5 green +Game 39: 7 red, 1 blue, 1 green; 5 red, 2 blue, 3 green; 9 red, 9 blue; 5 red, 3 green, 1 blue; 1 green, 9 red, 1 blue +Game 40: 1 blue, 1 green, 3 red; 3 red, 2 green, 2 blue; 5 red, 1 blue, 8 green; 2 green, 4 red, 2 blue; 13 red, 2 green +Game 41: 17 blue, 5 red, 3 green; 10 green, 4 red, 5 blue; 3 red, 17 blue, 1 green; 11 blue, 10 red, 3 green +Game 42: 8 blue, 2 green, 7 red; 4 blue, 1 red; 9 red, 2 green, 6 blue; 4 blue, 9 red; 1 green, 9 blue, 8 red; 6 red, 1 green +Game 43: 1 green, 6 blue, 14 red; 7 green, 1 blue, 8 red; 3 green, 16 red, 7 blue +Game 44: 5 red, 5 blue; 1 green, 8 red, 9 blue; 6 red, 3 blue +Game 45: 4 green, 17 red, 14 blue; 14 red, 2 green, 2 blue; 9 blue, 1 green, 8 red +Game 46: 1 blue, 10 red, 7 green; 3 red, 4 blue; 2 blue, 1 green, 1 red +Game 47: 8 red, 2 green, 13 blue; 10 red, 9 blue; 4 green, 15 blue, 2 red; 1 blue, 1 green, 16 red +Game 48: 9 red, 8 green, 1 blue; 10 green, 5 red; 9 red, 3 green, 1 blue; 8 red, 1 green +Game 49: 11 blue, 18 green, 1 red; 13 red, 9 blue; 17 green, 18 red, 6 blue +Game 50: 2 green, 10 red, 6 blue; 6 red, 6 blue, 7 green; 7 red, 5 green, 3 blue; 5 green, 2 red, 1 blue +Game 51: 1 blue, 9 green, 7 red; 2 blue, 11 red, 10 green; 1 blue, 9 green, 12 red; 10 red, 5 green, 3 blue; 9 green, 3 blue, 14 red +Game 52: 4 green; 6 blue; 4 green, 7 blue; 1 blue; 1 red, 8 green, 1 blue +Game 53: 6 green, 3 blue, 13 red; 3 blue, 2 green, 16 red; 13 red, 9 green, 2 blue; 1 red, 1 blue, 7 green +Game 54: 1 green, 4 blue, 10 red; 2 red, 6 blue; 2 blue, 5 red; 3 blue, 7 red, 1 green +Game 55: 17 red; 18 red, 3 blue, 4 green; 5 blue, 14 red, 1 green; 4 green, 5 blue, 4 red; 2 green, 18 red +Game 56: 14 green, 2 red, 18 blue; 8 green, 12 blue, 2 red; 1 red, 13 blue, 12 green +Game 57: 2 green, 4 blue, 12 red; 1 green, 3 red, 4 blue; 4 green, 2 red, 3 blue; 3 green, 6 red; 3 red, 3 blue, 3 green +Game 58: 2 blue, 5 red, 2 green; 6 red, 6 green; 8 red, 11 green, 2 blue; 9 green, 1 blue, 11 red; 2 blue, 17 green, 11 red; 7 red, 8 green, 2 blue +Game 59: 4 blue, 1 red, 16 green; 2 blue, 5 green, 1 red; 2 green, 6 blue; 3 blue, 10 green; 14 green, 6 blue; 1 red, 11 green +Game 60: 4 red, 1 blue, 5 green; 2 green, 6 blue; 1 red, 17 green; 2 red, 11 blue, 14 green; 2 red, 8 blue, 14 green +Game 61: 6 red, 7 blue, 1 green; 2 red, 10 green, 16 blue; 14 blue, 10 green, 7 red; 12 red, 15 blue, 3 green; 5 blue, 1 red +Game 62: 9 blue, 4 red; 1 green, 2 blue, 4 red; 8 red, 9 blue; 6 red, 15 blue; 7 blue, 10 red +Game 63: 8 green, 2 red, 4 blue; 2 red, 1 green, 3 blue; 1 red, 3 green; 2 blue, 9 green +Game 64: 4 green, 13 red; 2 red, 6 green, 2 blue; 4 red, 6 green, 2 blue; 6 red, 2 blue; 5 red, 9 green; 3 red, 1 blue, 10 green +Game 65: 8 blue, 3 red, 3 green; 6 red, 8 green; 6 blue, 7 green, 7 red +Game 66: 6 red, 2 blue, 2 green; 15 green, 11 red, 1 blue; 1 red, 2 blue, 10 green; 2 blue, 17 green; 12 green, 2 blue, 4 red; 16 green, 10 red, 3 blue +Game 67: 1 green, 2 red, 12 blue; 2 red, 3 blue, 3 green; 8 blue, 4 green, 1 red; 12 blue, 2 red, 4 green; 2 red, 10 blue +Game 68: 6 red, 4 green, 16 blue; 9 red, 7 green, 11 blue; 7 blue, 16 green, 15 red; 2 red, 2 green; 16 red; 13 red, 15 green, 2 blue +Game 69: 14 red, 3 blue, 14 green; 4 blue, 18 green, 2 red; 4 green, 9 blue, 7 red; 15 green, 9 blue, 10 red +Game 70: 2 green, 5 red, 12 blue; 3 green, 5 red, 4 blue; 5 blue, 4 red; 6 red, 11 blue; 5 red, 2 blue; 5 blue, 1 green, 4 red +Game 71: 3 green, 3 red; 6 green, 11 blue, 2 red; 6 red, 4 blue +Game 72: 9 blue, 4 green, 4 red; 5 blue, 5 red, 3 green; 9 blue, 11 green, 3 red +Game 73: 1 blue, 13 red; 12 red; 5 red, 1 green, 6 blue; 5 blue, 7 red; 8 red, 9 blue +Game 74: 7 green, 17 red, 3 blue; 15 red, 2 green; 5 red, 3 blue, 1 green; 19 red, 1 blue; 3 red, 1 blue, 6 green; 7 red, 3 blue, 1 green +Game 75: 11 blue, 9 green, 4 red; 5 green, 2 red, 16 blue; 13 blue, 2 red; 3 red, 18 blue, 1 green +Game 76: 5 green, 13 red, 10 blue; 5 red, 11 green; 1 red, 5 green, 8 blue; 4 red, 14 green; 7 blue, 12 green, 2 red +Game 77: 5 blue, 2 green, 3 red; 2 red; 1 green, 6 blue; 5 blue, 1 red +Game 78: 9 red, 7 green; 10 red, 2 blue, 6 green; 13 red, 3 blue, 15 green; 9 blue, 10 green, 2 red; 1 blue, 4 red, 12 green +Game 79: 4 red, 3 green; 3 blue, 10 green, 4 red; 1 red, 12 green, 7 blue; 5 blue, 3 green, 6 red; 10 green, 1 blue, 5 red; 5 green, 5 red +Game 80: 3 blue, 18 green; 5 blue, 11 green, 3 red; 2 blue, 13 green, 7 red; 4 red, 1 blue, 8 green +Game 81: 9 green, 18 blue, 10 red; 6 red, 5 green, 13 blue; 8 red, 4 blue, 7 green +Game 82: 9 green, 2 red, 2 blue; 7 green, 2 red, 1 blue; 2 green, 2 red, 2 blue; 2 red, 14 green, 1 blue +Game 83: 10 red, 7 green, 3 blue; 4 green, 12 red, 12 blue; 13 blue, 1 green, 8 red +Game 84: 10 green, 3 blue; 8 green, 2 red; 3 blue, 5 green; 3 blue, 1 green; 5 blue, 7 green, 1 red; 8 green, 5 blue, 2 red +Game 85: 6 blue, 4 green, 5 red; 11 green, 1 blue, 13 red; 11 green, 1 red; 6 green, 2 red; 1 blue, 5 green, 4 red +Game 86: 1 blue, 10 red; 2 blue, 5 red; 1 red, 2 blue, 2 green +Game 87: 11 green, 13 red, 6 blue; 8 blue, 2 red, 6 green; 10 blue, 11 red, 1 green; 16 green, 16 blue, 6 red; 6 green, 12 red, 3 blue +Game 88: 15 green, 4 red, 2 blue; 6 red, 13 green, 4 blue; 3 green, 17 red, 4 blue; 4 blue, 14 green +Game 89: 6 green, 12 red, 7 blue; 3 blue, 6 green, 7 red; 6 green, 13 red, 5 blue; 6 red, 7 green +Game 90: 19 blue, 19 red; 6 blue, 12 red, 6 green; 2 green, 7 blue, 17 red; 3 green, 9 blue, 7 red; 8 red, 7 green, 18 blue +Game 91: 3 red, 1 green; 14 red, 2 blue, 2 green; 7 red, 3 blue; 1 blue, 6 red +Game 92: 2 green, 16 red, 15 blue; 3 green, 3 blue, 11 red; 7 blue, 6 red; 7 blue, 13 red +Game 93: 1 red, 1 blue, 9 green; 12 green, 3 red, 4 blue; 3 green, 3 red, 7 blue +Game 94: 12 green, 5 red, 9 blue; 3 blue, 3 green, 2 red; 5 green, 2 blue; 5 green, 7 red, 10 blue; 7 red, 10 blue, 10 green +Game 95: 3 blue, 15 red, 10 green; 3 blue, 16 red; 1 blue, 13 green, 6 red +Game 96: 11 blue, 2 green, 5 red; 2 green, 10 red, 15 blue; 11 blue, 19 red, 8 green +Game 97: 7 green, 2 red, 1 blue; 5 red, 3 blue, 12 green; 4 blue, 2 green, 3 red +Game 98: 2 green, 5 blue, 9 red; 4 green, 9 blue, 8 red; 7 green, 11 blue, 7 red +Game 99: 2 green, 15 blue, 2 red; 2 red, 6 green, 12 blue; 11 green, 18 blue, 2 red; 3 red, 9 blue, 3 green; 18 blue +Game 100: 13 red, 2 green; 15 red, 1 green; 4 green, 1 blue; 11 red, 5 green; 3 green, 8 red From f5c4cdb36a3b7bedf60f7d7758880f7ba07425b8 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 2 Dec 2023 19:02:56 +0100 Subject: [PATCH 04/15] chore: simplified day 2 --- day_02/src/lib.rs | 16 ++++++++-------- day_02/src/part_01.rs | 11 ++++------- day_02/src/part_02.rs | 16 +++++----------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/day_02/src/lib.rs b/day_02/src/lib.rs index a1f734f..27043c5 100644 --- a/day_02/src/lib.rs +++ b/day_02/src/lib.rs @@ -27,23 +27,23 @@ pub fn parse_input(input: &str) -> Input { .lines() .map(str::trim) .map(|string| { - string.split(": ").collect::>()[1] + string.split(": ").collect::>()[1] .split("; ") - .collect::>() + .collect::>() .iter() .map(|record| { - let games = record.split(", ").collect::>(); + let game = record.split(", ").collect::>(); let mut bag = Bag::new(); - games.iter().for_each(|list| { - let cubes = list.split(' ').next().unwrap().parse::().unwrap(); - if list.ends_with("red") { + game.iter().for_each(|part| { + let cubes = part.split(' ').next().unwrap().parse::().unwrap(); + if part.ends_with("red") { bag.red = cubes; } - if list.ends_with("green") { + if part.ends_with("green") { bag.green = cubes; } - if list.ends_with("blue") { + if part.ends_with("blue") { bag.blue = cubes; } }); diff --git a/day_02/src/part_01.rs b/day_02/src/part_01.rs index a9ba85c..0dc7292 100644 --- a/day_02/src/part_01.rs +++ b/day_02/src/part_01.rs @@ -4,15 +4,12 @@ use crate::Input; pub fn main(input: &Input) -> Result { Ok(input - .into_iter() + .iter() .enumerate() .filter(|(_, games)| { - let possible_games = games - .into_iter() - .filter(|game| game.red <= 12 && game.green <= 13 && game.blue <= 14) - .collect::>() - .len(); - possible_games == games.len() + !games + .iter() + .any(|game| game.red > 12 || game.green > 13 || game.blue > 14) }) .map(|(index, _)| u32::try_from(index + 1).unwrap()) .sum()) diff --git a/day_02/src/part_02.rs b/day_02/src/part_02.rs index 4d3e35d..f769360 100644 --- a/day_02/src/part_02.rs +++ b/day_02/src/part_02.rs @@ -4,18 +4,12 @@ use crate::{Bag, Input}; pub fn main(input: &Input) -> Result { Ok(input - .into_iter() + .iter() .map(|games| { - games.into_iter().fold(Bag::new(), |mut bag, game| { - if game.red > bag.red { - bag.red = game.red; - } - if game.green > bag.green { - bag.green = game.green; - } - if game.blue > bag.blue { - bag.blue = game.blue; - } + games.iter().fold(Bag::new(), |mut bag, game| { + bag.red = bag.red.max(game.red); + bag.green = bag.green.max(game.green); + bag.blue = bag.blue.max(game.blue); bag }) }) From a515c89a653fa9c8e7bc23f6f756fe8fe0963436 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 2 Dec 2023 19:06:59 +0100 Subject: [PATCH 05/15] feat: simplified once again --- day_02/src/lib.rs | 11 +++++++++++ day_02/src/part_02.rs | 11 ++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/day_02/src/lib.rs b/day_02/src/lib.rs index 27043c5..1fc1135 100644 --- a/day_02/src/lib.rs +++ b/day_02/src/lib.rs @@ -16,6 +16,17 @@ impl Bag { pub fn init(red: u32, green: u32, blue: u32) -> Self { Self { red, green, blue } } + + pub fn power(self) -> u32 { + self.red * self.green * self.blue + } + + pub fn max(mut self, other: &Bag) -> Bag { + self.red = self.red.max(other.red); + self.green = self.green.max(other.green); + self.blue = self.blue.max(other.blue); + self + } } pub type Input = Vec>; diff --git a/day_02/src/part_02.rs b/day_02/src/part_02.rs index f769360..e5a2ce3 100644 --- a/day_02/src/part_02.rs +++ b/day_02/src/part_02.rs @@ -5,15 +5,8 @@ use crate::{Bag, Input}; pub fn main(input: &Input) -> Result { Ok(input .iter() - .map(|games| { - games.iter().fold(Bag::new(), |mut bag, game| { - bag.red = bag.red.max(game.red); - bag.green = bag.green.max(game.green); - bag.blue = bag.blue.max(game.blue); - bag - }) - }) - .map(|bag| bag.red * bag.green * bag.blue) + .map(|games| games.iter().fold(Bag::new(), |bag, game| bag.max(game))) + .map(|bag| bag.power()) .sum()) } From 232a698c40b4fac09936605bbeb70c565a5e42b2 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 4 Dec 2023 10:51:16 +0100 Subject: [PATCH 06/15] feat: ugly solution for day 3 --- README.md | 2 +- day_01/src/lib.rs | 5 +- day_03/Cargo.toml | 11 ++++ day_03/src/lib.rs | 138 +++++++++++++++++++++++++++++++++++++++++ day_03/src/main.rs | 12 ++++ day_03/src/part_01.rs | 108 ++++++++++++++++++++++++++++++++ day_03/src/part_02.rs | 113 ++++++++++++++++++++++++++++++++++ input/day_03 | 140 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 524 insertions(+), 5 deletions(-) create mode 100644 day_03/Cargo.toml create mode 100644 day_03/src/lib.rs create mode 100644 day_03/src/main.rs create mode 100644 day_03/src/part_01.rs create mode 100644 day_03/src/part_02.rs create mode 100644 input/day_03 diff --git a/README.md b/README.md index 278d98e..5fcf3fb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - [Day 1](https://github.com/ankjevel/adventofcode/tree/2023/day_01) ⭐️ ⭐️ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2023/day_02) ⭐️ ⭐️ -- [Day 3](#) +- [Day 3](https://github.com/ankjevel/adventofcode/tree/2023/day_03) ⭐️ ⭐️ - [Day 4](#) - [Day 5](#) - [Day 6](#) diff --git a/day_01/src/lib.rs b/day_01/src/lib.rs index 7a2b29b..c6975c2 100644 --- a/day_01/src/lib.rs +++ b/day_01/src/lib.rs @@ -24,9 +24,6 @@ mod tests { #[test] fn it_parses_example() { - assert_eq!( - parse_input(&EXAMPLE_DATA), - vec!["1abc2".to_owned(), "pqr3stu8vwx".to_owned()] - ); + assert_eq!(parse_input(&EXAMPLE_DATA), vec!["1abc2", "pqr3stu8vwx"]); } } diff --git a/day_03/Cargo.toml b/day_03/Cargo.toml new file mode 100644 index 0000000..b73f0bf --- /dev/null +++ b/day_03/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_03" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_03/src/lib.rs b/day_03/src/lib.rs new file mode 100644 index 0000000..16fae7b --- /dev/null +++ b/day_03/src/lib.rs @@ -0,0 +1,138 @@ +use std::ops::RangeInclusive; + +pub mod part_01; +pub mod part_02; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Input { + rows: Vec, + max_x: usize, + max_y: usize, +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Row { + numbers: Vec<(RangeInclusive, u32)>, + symbols: Vec<(usize, char)>, +} + +fn parse(input: Vec) -> Input { + let max_x = input.first().unwrap().len(); + let max_y = input.len(); + + Input { + max_x, + max_y, + rows: input + .to_owned() + .iter() + .map(|line| { + let numbers: Vec<_> = line + .chars() + .into_iter() + .enumerate() + .fold(vec![], |mut indices_and_chars, (index, c)| { + if !c.is_digit(10) { + return indices_and_chars; + } + + let current = (index.to_owned(), c.to_owned()); + let last = indices_and_chars.last_mut(); + + if last.is_none() { + indices_and_chars.push(vec![current]); + return indices_and_chars; + } + + let last_mut = last.unwrap(); + if last_mut.last().unwrap().0 == index - 1 { + last_mut.push(current); + } else { + indices_and_chars.push(vec![current]); + } + + indices_and_chars + }) + .iter() + .map(|v| { + ( + v.first().unwrap().0..=v.last().unwrap().0, + v.iter() + .fold("".to_owned(), |str, (_, c)| { + String::from(str.to_owned() + &c.to_string()) + }) + .parse() + .unwrap(), + ) + }) + .collect(); + + let symbols = + line.chars() + .into_iter() + .enumerate() + .fold(vec![], |mut symbols, (index, c)| { + if !c.is_digit(10) && c != '.' { + symbols.push((index, c.to_owned())) + } + symbols + }); + + Row { symbols, numbers } + }) + .collect(), + } +} + +pub fn parse_input(input: &str) -> Input { + parse( + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|string| string.parse().unwrap()) + .collect(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + 467..114.. + ...#.+.58. + ...$.*.... + .664.598.. + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + Input { + rows: vec![ + Row { + numbers: vec![(0..=2, 467), (5..=7, 114)], + symbols: vec![] + }, + Row { + numbers: vec![(7..=8, 58)], + symbols: vec![(3, '#'), (5, '+')] + }, + Row { + numbers: vec![], + symbols: vec![(3, '$'), (5, '*')] + }, + Row { + numbers: vec![(1..=3, 664), (5..=7, 598)], + symbols: vec![] + } + ], + max_x: 10, + max_y: 4, + } + ); + } +} diff --git a/day_03/src/main.rs b/day_03/src/main.rs new file mode 100644 index 0000000..61dd442 --- /dev/null +++ b/day_03/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_03::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_03")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_03/src/part_01.rs b/day_03/src/part_01.rs new file mode 100644 index 0000000..c4e1094 --- /dev/null +++ b/day_03/src/part_01.rs @@ -0,0 +1,108 @@ +use std::{io::Result, ops::RangeInclusive}; + +use crate::{Input, Row}; + +#[derive(Debug, Clone, Eq, PartialEq)] +struct IteratedRow { + to_check: Vec>, + row: Row, +} + +fn around( + range: &RangeInclusive, + max_x: usize, + max_y: usize, + row: usize, +) -> Vec<(usize, usize)> { + let start = range.start().to_owned(); + let end = range.end().to_owned(); + + let start_x = if start != 0 { start - 1 } else { start }; + let end_x = if end != max_x { end + 1 } else { end }; + + let start_y = if row != 0 { row - 1 } else { row }; + let end_y = if row != max_y { row + 1 } else { row }; + + let mut values = vec![]; + for y in start_y..=end_y { + for x in start_x..=end_x { + if y == row && range.contains(&x) { + continue; + } + values.push((x.to_owned(), y.to_owned())); + } + } + + values +} + +pub fn main(input: &Input) -> Result { + Ok(input + .rows + .clone() + .into_iter() + .enumerate() + .map(|(index, row)| { + let to_check = row + .numbers + .iter() + .map(|(range, _)| around(range, input.max_x, input.max_y, index)) + .collect(); + IteratedRow { row, to_check } + }) + .fold(vec![], |mut matched: Vec, iterated_row| { + for (i, range) in iterated_row.to_check.into_iter().enumerate() { + let exists = range.into_iter().any(|(x, y)| { + let rows = input.rows.clone(); + let y_row = rows.get(y); + if y_row.is_none() { + return false; + } + + let y_row = y_row.unwrap(); + let x_numbers = y_row.numbers.clone().into_iter().find(|n| n.0.contains(&x)); + let x_symbols = y_row.symbols.clone().into_iter().find(|n| n.0 == x); + + if x_numbers.is_some() || x_symbols.is_some() { + return true; + } + + false + }); + + if exists { + matched.push(iterated_row.row.numbers.get(i).unwrap().1); + } + } + + matched + }) + .into_iter() + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 467..114.. + ...*...... + ..35..633. + ......#... + 617*...... + .....+.58. + ..592..... + ......755. + ...$.*.... + .664.598.. + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 4361); + Ok(()) + } +} diff --git a/day_03/src/part_02.rs b/day_03/src/part_02.rs new file mode 100644 index 0000000..712204b --- /dev/null +++ b/day_03/src/part_02.rs @@ -0,0 +1,113 @@ +use std::io::Result; + +use crate::Input; + +pub const DIRECTIONS: [(i32, i32); 8] = [ + (-1, -1), + (0, -1), + (1, -1), + (-1, 0), + (1, 0), + (-1, 1), + (0, 1), + (1, 1), +]; + +pub fn main(input: &Input) -> Result { + Ok(input + .rows + .clone() + .into_iter() + .enumerate() + .fold(vec![], |mut v: Vec<_>, (y, row)| { + row.symbols.iter().for_each(|n| { + if n.1 == '*' { + v.push((n.0, y)); + } + }); + v + }) + .into_iter() + .map(|(x, y)| { + DIRECTIONS + .iter() + .fold(vec![], |mut dirs: Vec<(usize, usize)>, (dx, dy)| { + let x = i32::try_from(x).unwrap(); + let y = i32::try_from(y).unwrap(); + let nx = x.to_owned() + dx.to_owned(); + let ny = y.to_owned() + dy.to_owned(); + + if nx == -1 || nx == i32::try_from(input.max_x).unwrap() { + return dirs; + } + + if ny == -1 || ny == i32::try_from(input.max_y).unwrap() { + return dirs; + } + + dirs.push((usize::try_from(nx).unwrap(), usize::try_from(ny).unwrap())); + dirs + }) + }) + .fold(vec![], |mut matches: Vec, positions| { + let m = positions + .clone() + .into_iter() + .fold(vec![], |mut m: Vec, (x, y)| { + let matched = input + .rows + .get(y) + .unwrap() + .numbers + .iter() + .find(|n| n.0.contains(&x)); + + if matched.is_none() { + return m; + } + + let number = matched.unwrap().1; + + if m.contains(&number) { + return m; + } + + m.push(number); + + m + }); + if m.len() == 2 { + matches.push(m.iter().product()); + } + + matches + }) + .iter() + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 467..114.. + ...*...... + ..35..633. + ......#... + 617*...... + .....+.58. + ..592..... + ......755. + ...$.*.... + .664.598.. + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 467835); + Ok(()) + } +} diff --git a/input/day_03 b/input/day_03 new file mode 100644 index 0000000..eb744d3 --- /dev/null +++ b/input/day_03 @@ -0,0 +1,140 @@ +416.........................559...............417...............785.......900.......284...........503...796....992.......................... +.........702*....772............378..569.........&.49..606...14*..............$.453*.........307....*......$.....-.................995...... +.....................458...856......+.........+....&..............680.......104.............%....516.................................*...... +...........822..174..*.....&...........711.746.......&............$....../.............656....#...........265=......634.*.............430... +..827.137..*...*....39................*..............856..............767........522......$..773....619..............*...287....501......... +..........726...511.............*.....320........476...............................*................%...899....72..731...........%....$..... +.....861..............232....223.933...............*.@........424*618.858.......................................$.......338.205........535.. +.......#.............-....................676...713...427.................-.......615.........126...................=..*.......*...&........ +....40..........996..............520.974.*..........#......*.566........907......................&...214...996*911.115.363..960..897........ +....+.............*....................$..172.....559...763.....*............554......*.............*....................................... +.......527...#.....90.+....66.................................890..............=...802.93.131..791...209......&........928......303....$.... +950.....*....773......105..............725........................................................%............886.......*........$.384..... +........383......741..............@..=....#.....179.18..%974..........624.......64.266.................701...........%..671.721.........942. +..................*.............914..548..........+.*.................*...........*...................*.........834.394.....*............... +.......502*80..960........................25........464.........831.846........25.........329..985...458.+.....&................377..659.... +..........................................*.....292...............*............/..................@......350........938............*....$... +...738..............428......+.......311...742.........236*631....816.&......+....86.........81.......................*.973*341.266......... +......*.....673......*......614.........*.......689.48.................450...816.....754...........258..@585.......154..............@....... +...231.....#.........681...........855...775..........*.....=..257...................................*.............................469..=682 +........#...................807...&.............418.19.....259....*459..906...185.356.......778.......230....................556............ +.......924.........459......*..............804.=.........................*...*...........&....*................=........836..+......618..... +.....=............#.......900........+...........702..383........%..*...250..503.......637.808.......97/....@...370.......&........#.....710 +..988........685............../663.273...........*...@..........16.251...........$..............*..........964.......658.....537........*... +...............*......171*......................714.....543............737.....372.............941.............113..*....=.......853....733. +............470..161......508.....56...170.............$...................389.....544.....208.....98.........%.....617...884......-..@..... +.766.591............../.........-......*.......210.........618......*874....-.........#...*....129.=..194......../...................992.... +....../....170@..140+..753....918....467.854....*................989..........979...............*.............-.907...276...931*618......... +..700............................................801....929..859.....#..........*.............594.493......981..........*................... +....+............173..............................................251........629..........970........*................136.........388...*... +......721.......$......911....766..................541.234....=...........47......614....@.....*217..885.680.742....................#..84... +.377*....*81.............*.......&...937...........+...*....155..@......*............*......648............&...*.....&401......493.......... +.....1.................533............*..399...........861......951..709.331..126.....876.....................289.........@.....*.......*... +..........107....452-......594.888.431....*..+660...........79................................$......275*876.......978.....594..400...39.... +.........+................*....*.........925..........$.......*........599.......228..........60....................+....#.................. +...283...................177...708...........642....661........294........*............................288*793.867.....482.479.769....73.... +...........617..855*...&.............605................783.91.........847................&...../...............*..#..........*.......+..... +.672..303+...*........967...............*453.....884@......*...208.949........*..........986.....249.....352..818.596.56.................... +...*..........88...............569.....................975.....*.....*..638....619.......................+.............*.......*.......462.. +....500.....=...................*.................*205..*..-...228.508....&........................395..............845.....683.365....*.... +........197.672.841..............214.=...847...912.....24.349...................%....584.257............152................................. +.626....-..........*..447............714....&.....................463....287...360........*............*...............*.................717 +..........53......334...*........................*167......................*.......................554..246...........295.396......./....... +.......24..............92...........897.......220...........................930..703...414.....711..*.........723.........*....117...146.... +.......&...........548.....324.21......................622.........820.404...............*....../..139......&......*.......578...*.......... +.....*......204......*..........*.............226...........663.......*......555.992.....502...........631..49.#....366........677.......... +..958.350.....*......44...694...449..-39........*...........-...786.........*......*............................140......................... +...............477.........*...................815..............*..........815.................521....................&...273........103.... +.........828............781....464..................276......646....665-............924..........*.......955........759.........934...#..... +........*..........*..........*..........15.193.......*.....................&.................808....347*......963........*....+............ +.........810.....360........638...434.....*....*751....812.....573...........85.........+.........95.......892...*...153...220..........187. +....714*....................................................80*....162=.................214...........842...&...39.......................... +........265.........................51.......285$.......................586.......=.................................*.....*..............948 +..989..........22.=......374.......%....................142.......736..@.....507.636.797.....273.........872.....567.978..334.....382...*... +..........@.......68..=......................116.130.......%.250../......-...=........*.........*486.415..........................*....813.. +.......505............61..140..........435..........*691......*........852..........571....408..............12......80.......228...109...... +.........................*.........207...*..24................402.=...........................@....................../...162................ +....285.................672.552........492....$./..................3..620.391...............................179............*................ +...*..........159...428.......*.................390.111.........$.....*...*..................348.355.481.......&.210..99...41............... +...52......12..@..........808.810.897...................663..280....57..............476.............*............*.....*........208=........ +..........*...............*.........@.286..................*...........................*..........................905.296..............148.. +.......298......119.....172.................622$..637................#......342.......679.111......-968..104*478............#.......*....... +.../.............*........../......................*..............658..........*718..........*136.....................503.899....889.498.... +....691........341.262..36.549...........386........437.............................662...........848............#......*................... +.......................*..........936...*...............................-...........*......516....%......358....707..535...........841...... +......$..............639............*..798.../..67%............137...716.......313.247......................-............@.....371.......... +....433.677..605.267................1.......930........478........*........565*................................869.......372..@....228...... +...........@..*..$...794.........................74.......#./......833.348.......................................+................*......... +......865...............*.........................*..........839.....................=......................................916..84.@....... +.................-....451........541@......468..684....18............759.............499................124.....426*.........*......882..... +...68*...........614........509...............*.........&...956*308.%........&....36...........480+....../..........917....32....#.......... +......363..377.............*..........441.....418..........................279.....*.................139..........................944....... +........................412............*..........%....920*585....526*............931.346&.807..840.....*.626-...#....................923... +.....283.....924...+..................628.......33....................908..766..............*........336..........446...........191......... +...............*.249........@264..35...................502.791.#...........*......=............126....................957....71....*.768.... +.........144&.36.....216.........*...........................&.730..........201..581.704.........$......715.............=......*.......%.... +...349..............*....598...949.........189....981.....#.............524...................*............=................440...847....... +.................967....#..........999*6..%.........-......604............&.189.626...#774.159.647....................168.........../...329. +..............................................481.....*........................*.................................747..%.................#... +..........245...878.......495....57....841.........351.517..........-297........................343.599/........&.......360..........-...... +430........&..............&.....*...&.....*885...........................9....392.......93..336*...........................*321.....86...... +....469......#.................999..796..................899.........250*........$.......*........247............................+.......328 +......*...696...615..300..603..............................*....232...................402...501......*.......77.271.....@.........146....... +......170......%....$.........*..991..........782.49.......128.............................*........161........*.....648......123........... +..........646................901..*.............*......134...........517..2..287........513...............................644...*........... +...................999*620.........541.379.....488..18*.......................*...............208....931.338..%143...........*...19......... +...+.......938...........................*...%.............&........626......268.418......841*..........*...............820.395..........913 +.723...834*..........+............162.667.....84............803.804...*............*................399........172..530*.................... +.....................596............*............................./.287............852....623....@.*..............@........168...965..*17... +.....=488...................758....607.....&......53%......................105.683......-...*.860..244.....&....$....232.................... +.............................*..........333....................197........+....*......334.954.............686...464...............544....... +.139=..............754...993.677..../.......933..........391....*....357*.....952......................@..........................*......... +..........831............../.........94.600................+.121.........183........18..686.....665*..823...........575...........68........ +141...800....*....692...........254@.....*...819-.....................................*..@...................17.......*...815...........686. +........%.602........%.................186..........562.93....774.....................11....948....912.........*328..243.*.................. +.......................................................*..........607....280..................&....+.....................548.&897.100@...955 +......504.....605...........889@.............975..664....$.....#............*....141.................378%.........812....................... +.....=...........*.....730...............%...............899..480.....*....375.....-.444.........47*.........@63...........828.....468..462. +.......607.../...23....=......=........383.............................522............*.............433..360..................*372...*...... +........*...975.............428...................592*......@258...114......%......431...................*......+........658.......560.163.. +........16........................329.......315.......973............*...333....@.......$....782..18...58....533........*...............*... +..327.............563...889.......*...........*..595...............913........671..354..937...%..*...&...........&.787...975.......676...247 +.....+..&...401..*..............713........=.757...#.873*47...#465..................................440..588/..859.%..................*..... +.665....133...%..196....907..............348..................................162........305...................................163.-........ +....*....................*...........170.........222.804....784..............*...................................................=.742...... +..239..338.....27-.437..543.........*....609......*..*......-...............582..-...........343*560....852.../....636..263................. +..........*900.......*............17...........948.....597.....=....922...........682./255................@..35.......*.......211-..#....... +.......*...........686........984..........78......896*......13........+....................985...&....................278.........876.835.. +....630.82....$851.....905.......*..281...*...............................352..........*746........215.877....845..........471.852.....*.... +.........................*.....757.*......625...........924......878..........912...............-.........*../.....538%......+.........871.. +..954*712..977.....-..762..........236.........527*674.*.........*........811*.....228....&.....5........739...483.......................... +..........*.......470..................................707....363.................*......525.......303..........*........463....537......... +...460...947...............130.....757./96........*.................=...529....+..466........527................65..%648....=.....*......... +.....*......................$....../..............968..............479.&....630............................598.................952.......... +.......&..........793...................................514...............-...................311..........=....83......#.............124... +.975....399.......*...-.......*814...663..................*....*...........381../...............*.............@.........284............*.... +...*............367....198..........@.....992.....716*529....96.................729.329.688...%.322.-......67.79......-.......335....997.... +488............................632........*...../......................387.............*....225......491.............391..750...%........... +.......861.527......778.165.......*........187..916.......845....-....*.......873.................................@......................... +..545.....*.....$.....*..*......627............................736.376..845...*....594........+.......171.........292...........*750..-..... +.................407.403............+...342.................+..........*.......897..........400.......$......................910.......350.. +...........................40..284.66...*......494..........255.......653..............866.................942=..................*52........ +....&........827.......296...*........$.401......*..............892*...................$.............1.164.......343..........485........... +.930............*.........*..944...308...........763....../.........62.....113....=...........421........*..........................580+.... +........50*.....934......705............................999..................*....541...............847-.950..............*131.............. +...........209....................................&404...............276..242.............723.652................873...313..............24.. +......*........606....550#.....2./........@..................899.......*.....................*..........235..112...........704.....337...... +.......837....*...............*...83...993.....*416.........-....580%..535......../.....-204...............#..*.....93+....*...723./........ +..............168.....753..593..............504......./........................488..............=.............458.......872...*............. +...#85...................&......911/...................880........315..872..........=....494..349....466..428..................40........... +........939........648......................*................227...*......*.......924...*...........*.....*................................. +918*......&.@........*...902..269..834....87.826.........../...*...919.118...817......109..........933..643...........&..............-...... +....949......883...111..%......*...%..................710.464.943.........../...................................842....305.....469..289..... +..........................%...974......*407..168.647...*..............617.......498/.....848...........@99........*...........+............. +..........176...........120.........469........&...#....997......464.......274.............*......./............477.417../.......738........ +.........*.......964.........291..........................................-.............2..29...272..465...............-..819........718*265 +....298...747.......#.......*.....+745...........460...741*762.275...491.......+.735*34.*............-....*841......+............*.......... +...*........................593..............298...#............%....$......225.........943........................23.....702.601.616....... +..889................695........654..750.....*.............637........./...............................780....*726....233...*............... +..................../.................*.....453.....642....*.........828......@...94...........152/...*....790.......*.....445......../..... +...........................51.......681........................271..........719.......................964......399..426...............456... From bada40def9ee321a730b8137c9e30b6ac86423ba Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 4 Dec 2023 11:33:37 +0100 Subject: [PATCH 07/15] feat: completed part 1 --- README.md | 2 +- day_04/Cargo.toml | 11 +++ day_04/src/lib.rs | 52 ++++++++++ day_04/src/main.rs | 11 +++ day_04/src/part_01.rs | 37 +++++++ input/day_04 | 218 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 day_04/Cargo.toml create mode 100644 day_04/src/lib.rs create mode 100644 day_04/src/main.rs create mode 100644 day_04/src/part_01.rs create mode 100644 input/day_04 diff --git a/README.md b/README.md index 5fcf3fb..46e3242 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [Day 1](https://github.com/ankjevel/adventofcode/tree/2023/day_01) ⭐️ ⭐️ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2023/day_02) ⭐️ ⭐️ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2023/day_03) ⭐️ ⭐️ -- [Day 4](#) +- [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️ - [Day 5](#) - [Day 6](#) - [Day 7](#) diff --git a/day_04/Cargo.toml b/day_04/Cargo.toml new file mode 100644 index 0000000..437c2ae --- /dev/null +++ b/day_04/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_04" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_04/src/lib.rs b/day_04/src/lib.rs new file mode 100644 index 0000000..ac0326b --- /dev/null +++ b/day_04/src/lib.rs @@ -0,0 +1,52 @@ +pub mod part_01; + +pub type Input = Vec<(Vec, Vec)>; + +fn numbers(input: &str) -> Vec { + input + .split(' ') + .filter(|string| !string.trim().is_empty()) + .map(|string| string.trim().parse::<_>().unwrap()) + .collect() +} + +pub fn parse_input(input: &str) -> Input { + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|string| { + let card = string.split(": ").collect::>()[1]; + let split = card.split(" | ").collect::>(); + (numbers(split[0]), numbers(split[1])) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 + Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![ + ( + vec![41, 48, 83, 86, 17], // + vec![83, 86, 6, 31, 17, 9, 48, 53] + ), + ( + vec![13, 32, 20, 16, 61], // + vec![61, 30, 68, 82, 17, 32, 24, 19] + ) + ] + ); + } +} diff --git a/day_04/src/main.rs b/day_04/src/main.rs new file mode 100644 index 0000000..ed0cf2f --- /dev/null +++ b/day_04/src/main.rs @@ -0,0 +1,11 @@ +use std::io::Result; + +use day_04::{parse_input, part_01::main as part_01}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_04")); + + println!("part_01: {:?}", part_01(&input)?); + + Ok(()) +} diff --git a/day_04/src/part_01.rs b/day_04/src/part_01.rs new file mode 100644 index 0000000..e631545 --- /dev/null +++ b/day_04/src/part_01.rs @@ -0,0 +1,37 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .iter() + .map(|(winning_numbers, numbers)| { + winning_numbers + .iter() + .filter(|n| numbers.contains(n)) + .fold(0, |sum, _| if sum == 0 { 1 } else { sum * 2 }) + }) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 + Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 + Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 + Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 + Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 + Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 13); + Ok(()) + } +} diff --git a/input/day_04 b/input/day_04 new file mode 100644 index 0000000..4db3058 --- /dev/null +++ b/input/day_04 @@ -0,0 +1,218 @@ +Card 1: 9 32 7 82 10 36 31 12 85 95 | 7 69 23 9 32 22 47 10 95 14 24 71 57 12 31 59 36 68 2 82 38 80 85 21 92 +Card 2: 16 35 95 22 59 82 76 60 19 88 | 63 91 16 35 26 82 95 51 53 60 94 59 56 73 28 76 12 44 22 62 8 7 19 38 88 +Card 3: 1 88 48 52 70 19 11 78 94 28 | 19 92 70 18 34 78 83 8 82 87 3 97 66 31 63 17 69 4 75 94 52 54 77 24 45 +Card 4: 72 14 2 92 65 62 16 28 55 91 | 73 8 35 4 9 86 83 51 47 53 3 7 15 52 96 54 49 88 85 30 6 59 81 33 99 +Card 5: 60 61 24 41 30 77 94 50 38 75 | 47 93 82 98 96 5 9 53 17 6 10 21 2 91 80 4 14 71 29 69 62 1 7 87 88 +Card 6: 21 4 16 41 58 68 66 22 64 94 | 72 69 60 62 34 21 75 66 38 25 63 17 81 23 71 73 78 64 89 24 26 56 68 79 51 +Card 7: 10 90 4 56 14 26 7 23 57 19 | 62 10 60 51 46 34 23 49 44 8 53 50 2 56 21 81 69 89 87 91 16 63 38 36 22 +Card 8: 36 27 38 47 16 57 96 31 43 76 | 62 99 87 68 76 44 49 63 47 16 58 81 84 48 45 88 64 34 54 42 72 24 26 12 50 +Card 9: 92 9 35 2 26 97 79 88 43 45 | 89 96 62 1 39 98 72 66 28 78 64 68 42 63 15 25 22 60 31 91 69 94 21 81 29 +Card 10: 40 17 3 9 47 23 82 90 13 87 | 51 2 36 85 34 27 11 53 71 38 8 57 58 17 24 20 54 23 31 37 35 70 9 83 99 +Card 11: 2 21 9 53 45 19 91 74 23 25 | 29 26 91 39 58 18 96 53 31 63 46 21 32 54 22 66 17 25 94 74 4 41 77 35 23 +Card 12: 13 84 68 67 99 97 73 49 86 65 | 97 49 35 40 42 80 45 11 94 53 99 29 72 73 23 77 62 3 38 90 83 68 59 20 95 +Card 13: 70 9 7 11 95 46 69 91 22 71 | 30 56 31 90 91 69 17 71 25 46 66 9 62 89 70 98 36 57 45 94 21 40 75 95 24 +Card 14: 54 37 89 14 18 1 60 34 7 80 | 83 98 11 85 50 35 61 29 18 67 33 63 32 92 68 65 95 56 74 96 27 73 81 47 15 +Card 15: 25 82 90 66 50 40 15 43 32 62 | 49 77 96 36 89 70 50 29 26 1 20 71 37 30 9 91 7 52 5 82 51 55 72 38 12 +Card 16: 26 46 79 29 52 10 87 75 45 49 | 52 25 2 42 73 44 58 85 10 37 4 17 8 86 95 65 35 31 16 12 99 51 70 80 45 +Card 17: 17 96 64 38 73 11 32 24 25 65 | 83 37 25 81 59 18 4 15 98 87 2 1 3 60 5 53 67 46 6 20 79 84 95 50 62 +Card 18: 48 50 90 87 40 71 59 61 30 85 | 85 19 13 39 76 55 72 29 5 84 22 91 67 63 74 73 61 90 62 42 30 9 69 87 48 +Card 19: 4 99 93 55 22 37 41 75 32 79 | 95 7 65 50 40 49 42 64 89 1 59 33 9 53 20 44 23 98 60 30 39 12 66 84 45 +Card 20: 14 34 73 91 30 48 35 44 50 8 | 48 33 35 40 65 29 9 42 18 79 43 89 92 68 75 97 87 69 85 80 98 14 6 25 76 +Card 21: 33 15 83 71 94 61 87 54 36 72 | 56 52 83 46 32 99 36 67 12 47 38 13 14 2 7 26 20 63 90 40 37 6 84 42 92 +Card 22: 66 45 79 71 92 57 47 43 62 9 | 45 89 9 5 53 84 80 19 77 61 68 40 98 23 56 36 6 60 93 1 11 49 71 52 29 +Card 23: 29 53 13 4 76 62 25 94 82 98 | 24 45 85 59 41 11 13 78 54 67 74 21 50 75 16 37 63 86 14 26 92 31 57 33 40 +Card 24: 16 66 90 39 24 92 53 76 52 31 | 21 7 57 65 58 2 10 61 35 73 68 95 84 43 12 18 76 71 83 15 82 88 30 55 5 +Card 25: 9 45 17 42 64 93 54 33 77 62 | 35 81 82 18 47 92 2 20 95 1 6 58 40 73 43 37 15 48 23 75 26 13 50 63 79 +Card 26: 1 99 92 73 57 29 18 97 34 5 | 82 89 61 3 95 39 14 98 15 87 45 48 49 60 22 28 65 16 9 58 4 53 38 56 51 +Card 27: 87 45 35 93 62 1 66 91 6 3 | 51 60 96 84 35 91 6 28 32 40 3 57 29 90 73 12 81 89 31 69 97 66 70 36 18 +Card 28: 72 89 51 45 10 21 84 23 6 94 | 26 45 23 13 68 51 89 66 34 99 83 70 44 56 72 57 73 94 30 84 61 80 85 29 6 +Card 29: 58 80 96 47 13 44 71 38 55 54 | 58 35 3 61 81 96 71 44 43 14 36 54 20 69 63 13 75 28 80 18 73 11 38 55 47 +Card 30: 44 17 81 16 13 65 85 60 34 46 | 60 71 72 13 10 51 65 56 34 59 80 75 18 64 35 92 58 50 97 46 81 37 44 16 17 +Card 31: 84 55 99 95 42 83 26 33 43 87 | 78 59 45 26 28 94 63 17 2 73 38 84 96 34 99 19 75 65 8 92 58 20 11 82 57 +Card 32: 57 67 49 76 15 84 27 39 24 14 | 20 15 21 14 67 97 76 19 17 38 23 41 24 32 27 57 26 81 99 49 34 96 9 84 39 +Card 33: 96 76 23 95 5 37 8 62 65 63 | 59 23 67 51 40 68 70 38 8 36 41 53 62 65 96 76 66 57 37 5 46 34 95 19 69 +Card 34: 52 13 90 81 94 62 91 40 53 42 | 82 90 35 13 55 95 93 74 44 70 89 87 62 5 80 22 53 94 21 77 54 20 18 52 40 +Card 35: 1 75 37 64 17 12 57 18 58 3 | 65 74 89 12 72 64 93 23 51 73 44 80 21 7 35 87 42 82 25 53 71 38 79 43 61 +Card 36: 15 49 18 30 20 90 69 3 60 59 | 14 63 53 86 5 62 69 52 21 9 18 48 13 34 2 67 60 55 10 98 15 81 88 83 94 +Card 37: 48 39 43 81 40 59 70 22 62 61 | 48 14 79 26 12 56 3 89 66 17 87 9 43 27 73 23 47 1 18 97 96 65 59 42 6 +Card 38: 35 94 15 31 22 55 8 2 11 45 | 78 53 41 13 37 64 36 90 26 97 16 99 57 54 20 59 82 24 58 81 14 74 95 93 79 +Card 39: 74 25 37 8 53 36 86 68 91 79 | 48 45 24 80 20 49 86 42 47 28 17 22 93 38 35 34 85 21 18 14 97 61 27 41 88 +Card 40: 41 16 26 14 79 27 62 55 80 99 | 52 83 5 38 69 11 89 6 35 58 4 74 99 97 63 30 90 33 46 60 95 59 22 76 40 +Card 41: 57 77 60 66 35 4 13 89 14 40 | 78 4 97 58 9 65 94 44 89 95 80 7 47 59 37 26 19 76 91 18 45 6 56 3 90 +Card 42: 83 19 31 59 78 54 22 16 44 88 | 53 84 90 39 56 29 44 55 58 13 72 80 49 6 28 97 25 18 99 89 40 43 10 33 71 +Card 43: 81 94 73 42 85 45 28 61 88 19 | 84 86 36 44 51 40 82 72 39 57 13 53 87 83 38 76 63 35 47 15 2 95 79 26 1 +Card 44: 78 28 92 59 51 37 61 55 58 98 | 49 6 30 89 61 98 58 78 31 28 48 92 60 56 59 51 29 14 37 63 38 55 24 42 21 +Card 45: 23 59 74 5 81 53 3 93 54 68 | 68 15 38 45 66 61 87 54 3 81 74 43 5 25 59 23 93 78 44 72 53 52 89 97 48 +Card 46: 79 17 74 72 84 44 3 59 66 76 | 3 44 95 84 66 79 62 97 63 48 76 10 75 72 6 17 74 94 80 59 30 12 20 83 87 +Card 47: 98 18 11 52 66 14 68 89 80 19 | 66 67 23 52 80 89 95 44 60 11 55 34 18 59 19 53 98 82 14 38 63 65 90 68 77 +Card 48: 84 64 87 81 54 61 31 57 53 79 | 99 74 9 45 54 14 96 38 7 82 4 17 43 69 62 83 66 81 41 86 55 75 30 10 21 +Card 49: 95 4 89 38 67 71 70 75 35 39 | 39 71 22 80 75 43 35 89 17 38 4 29 7 69 94 63 85 6 5 95 70 58 67 50 68 +Card 50: 18 59 9 34 83 68 80 21 61 78 | 10 96 19 11 74 15 26 8 65 56 35 79 32 44 55 36 94 95 53 86 3 27 88 67 85 +Card 51: 17 79 69 92 39 51 46 81 14 21 | 49 92 48 29 47 74 76 81 69 85 86 98 73 43 24 45 21 84 25 10 17 44 1 31 28 +Card 52: 39 31 1 21 7 27 85 59 35 67 | 84 29 91 89 70 5 94 23 21 65 71 12 26 41 15 11 61 73 14 52 76 9 16 24 75 +Card 53: 93 21 1 18 54 87 35 98 9 48 | 44 30 79 82 23 43 51 46 34 35 9 75 12 60 77 36 27 86 32 47 85 98 87 1 13 +Card 54: 51 95 69 73 66 61 9 28 75 24 | 42 28 87 69 24 19 56 98 57 66 95 73 9 51 61 50 64 75 76 27 86 1 29 30 79 +Card 55: 73 99 56 8 10 97 26 64 52 2 | 79 47 80 89 5 53 37 63 41 48 12 96 1 25 84 70 9 76 58 75 72 31 20 17 46 +Card 56: 71 43 8 50 95 1 2 77 45 81 | 43 85 24 59 46 15 36 81 3 50 9 45 95 77 71 12 65 1 60 2 8 5 7 20 88 +Card 57: 10 76 62 41 58 67 80 39 9 21 | 58 10 42 60 37 27 51 1 25 18 56 23 67 88 62 50 87 80 9 54 41 81 26 21 7 +Card 58: 41 80 74 71 10 63 58 26 38 21 | 63 66 80 65 12 68 72 10 20 91 88 44 11 70 36 71 3 61 74 7 21 41 67 43 38 +Card 59: 41 60 82 94 90 7 76 69 28 95 | 35 69 77 9 81 65 11 90 19 22 36 75 12 52 98 55 92 79 14 67 47 83 39 74 94 +Card 60: 44 82 36 55 94 72 99 70 6 50 | 35 29 95 62 97 11 33 92 19 91 10 54 56 53 98 1 39 16 64 52 73 66 30 4 43 +Card 61: 55 19 82 18 24 91 99 36 54 98 | 68 58 99 51 79 1 7 50 4 28 59 8 19 14 10 40 57 34 60 82 88 47 52 80 54 +Card 62: 14 20 49 2 94 59 33 72 86 34 | 35 33 54 94 5 14 7 6 60 41 40 38 20 26 72 22 49 13 83 98 36 3 51 82 74 +Card 63: 73 78 8 64 52 7 86 30 98 27 | 95 4 48 24 45 72 60 31 25 18 59 90 51 14 50 65 2 16 3 33 94 11 93 6 32 +Card 64: 26 71 76 85 70 80 81 61 58 77 | 87 10 24 38 4 40 42 96 47 63 99 7 20 68 78 66 53 12 46 90 75 59 60 88 5 +Card 65: 26 37 35 44 45 10 13 85 6 8 | 39 80 93 79 60 89 58 96 44 85 59 94 24 23 48 86 22 42 6 5 25 57 43 90 21 +Card 66: 62 45 67 18 36 84 66 90 98 95 | 1 71 68 54 76 48 42 35 19 94 39 69 80 17 41 20 93 59 29 30 15 37 14 12 88 +Card 67: 33 10 5 94 72 86 84 80 19 85 | 76 26 61 3 93 17 74 42 23 16 18 2 14 46 1 38 72 13 28 29 91 21 12 7 48 +Card 68: 70 73 2 79 88 36 35 13 16 6 | 75 47 30 7 89 78 39 69 96 91 19 83 61 4 10 77 81 44 90 50 29 38 32 53 60 +Card 69: 60 20 40 38 6 49 5 85 84 80 | 38 12 59 94 27 15 85 17 65 69 82 36 7 43 47 99 5 87 61 77 4 83 81 90 40 +Card 70: 85 96 73 11 22 33 9 61 37 42 | 27 70 96 74 9 75 60 88 22 86 13 63 37 80 83 73 42 85 17 33 35 82 61 65 11 +Card 71: 41 47 37 52 73 97 77 69 5 45 | 47 95 78 41 74 54 5 88 63 87 34 18 37 85 26 73 52 50 28 77 97 86 1 69 45 +Card 72: 90 88 85 71 5 59 65 52 30 13 | 5 29 68 62 24 12 90 30 85 18 76 25 40 60 77 14 19 33 61 88 70 17 95 73 2 +Card 73: 44 98 60 49 51 9 21 88 87 70 | 63 3 46 65 49 70 44 7 74 88 35 45 17 24 51 60 25 9 98 87 21 10 81 83 61 +Card 74: 31 23 80 11 13 72 63 57 29 70 | 57 28 70 45 3 94 5 80 14 11 2 29 20 47 72 63 10 31 66 19 79 38 74 13 23 +Card 75: 82 54 62 8 30 7 88 20 71 85 | 83 65 74 44 79 3 29 64 51 35 98 56 41 22 27 13 26 34 39 59 24 4 33 14 63 +Card 76: 16 24 87 73 17 71 69 26 4 76 | 59 71 32 3 56 64 24 41 66 26 12 36 87 65 73 4 75 16 8 17 57 69 49 46 76 +Card 77: 98 57 96 56 39 12 69 37 54 65 | 21 48 87 97 29 27 56 31 18 88 7 79 70 35 64 38 91 15 14 43 3 84 20 85 32 +Card 78: 5 62 37 51 73 78 18 64 42 48 | 35 82 85 62 23 66 3 14 19 64 79 18 93 53 69 58 96 63 90 24 41 65 94 40 95 +Card 79: 8 73 46 15 99 29 96 34 59 75 | 48 65 57 99 63 33 37 73 79 28 72 56 34 3 25 75 70 95 58 27 43 30 54 8 87 +Card 80: 91 81 96 55 44 82 31 23 11 74 | 24 51 96 77 40 28 56 44 54 89 78 38 76 74 17 92 3 23 36 63 80 65 55 7 11 +Card 81: 81 77 30 26 93 28 97 10 84 88 | 57 28 36 33 54 11 96 58 18 99 30 1 5 79 12 24 56 93 25 78 10 40 76 84 81 +Card 82: 31 67 3 90 28 76 55 6 29 26 | 75 34 82 73 38 17 67 91 86 40 43 45 42 60 37 63 55 87 93 84 58 78 80 20 11 +Card 83: 8 97 43 88 62 34 68 50 82 71 | 8 68 2 96 85 36 10 14 35 32 73 16 26 29 67 60 37 89 52 98 74 22 78 1 59 +Card 84: 77 97 5 33 12 73 90 57 31 19 | 97 46 48 57 4 10 94 85 59 31 17 60 81 49 62 58 25 8 79 78 50 36 55 51 32 +Card 85: 74 85 97 19 76 99 21 47 20 50 | 50 69 72 39 45 26 13 7 92 21 63 58 84 9 94 53 43 81 89 49 62 32 70 82 10 +Card 86: 81 96 82 76 97 77 40 3 68 98 | 4 26 69 88 43 15 78 64 79 92 62 30 49 89 37 59 95 63 58 98 75 99 80 51 6 +Card 87: 80 73 51 20 41 67 31 66 97 27 | 16 59 75 86 24 83 95 44 85 13 11 77 70 14 2 39 88 89 93 55 52 6 53 94 98 +Card 88: 18 38 66 87 56 25 46 63 37 15 | 86 22 17 96 10 49 80 21 15 77 66 3 20 95 36 87 16 74 46 26 79 76 38 43 6 +Card 89: 51 55 41 3 20 60 99 70 23 43 | 51 81 99 32 50 96 41 53 80 76 43 3 83 65 20 46 70 60 38 35 57 55 7 74 23 +Card 90: 94 19 23 81 44 15 74 73 22 71 | 83 66 54 43 23 55 69 81 85 71 62 96 19 86 78 22 15 58 94 74 44 73 57 17 8 +Card 91: 72 77 21 83 82 75 1 56 99 43 | 42 77 56 90 68 96 39 72 83 97 21 17 66 70 49 43 69 63 82 75 47 99 87 11 1 +Card 92: 62 58 71 78 38 80 52 94 48 92 | 11 7 56 4 77 59 62 49 14 94 52 80 92 64 71 58 13 1 48 12 78 9 17 38 35 +Card 93: 98 45 63 46 48 97 91 29 90 15 | 95 31 41 15 43 80 24 44 75 83 30 87 98 12 27 36 74 16 86 35 33 85 54 94 78 +Card 94: 27 60 36 26 76 65 86 89 10 54 | 80 78 95 36 88 82 6 46 73 58 22 40 12 50 76 14 65 26 70 60 54 17 27 89 93 +Card 95: 18 62 66 98 24 16 80 58 53 97 | 93 9 15 61 51 19 81 8 21 36 6 71 80 13 52 87 5 37 86 75 68 60 97 54 10 +Card 96: 6 55 40 79 3 67 13 96 91 34 | 54 13 63 17 52 72 7 81 82 69 6 91 90 57 14 2 8 74 75 40 96 21 3 38 55 +Card 97: 40 33 3 59 8 88 99 14 41 74 | 99 48 41 35 69 64 18 50 3 96 40 8 36 63 17 90 59 33 49 26 88 93 74 27 14 +Card 98: 63 44 52 11 32 46 62 19 30 6 | 71 51 48 70 82 44 8 60 92 21 77 62 53 95 31 73 80 96 55 34 86 97 76 88 6 +Card 99: 66 46 22 44 94 50 68 59 25 75 | 40 46 56 32 64 89 80 22 60 87 77 9 59 25 75 38 44 48 8 50 94 68 66 72 20 +Card 100: 15 8 13 93 80 58 66 10 76 32 | 58 68 34 54 53 79 69 18 71 33 66 13 92 77 93 40 80 94 76 75 15 10 83 8 70 +Card 101: 22 25 48 97 63 81 7 84 60 43 | 58 91 10 85 59 76 16 36 96 39 42 50 72 34 86 61 67 11 79 27 83 73 98 57 70 +Card 102: 1 35 10 28 59 2 74 45 25 13 | 47 56 90 19 20 95 64 72 88 28 27 18 31 7 55 61 48 2 10 81 70 25 96 73 74 +Card 103: 28 5 31 23 76 83 25 94 35 18 | 10 85 72 18 23 60 17 50 30 76 35 83 8 42 65 4 34 84 28 39 5 98 25 94 92 +Card 104: 32 45 61 94 5 44 3 15 77 87 | 37 15 84 94 19 33 11 51 1 81 16 61 87 68 74 97 62 88 8 98 45 44 92 70 83 +Card 105: 32 89 96 15 67 83 9 64 60 44 | 76 38 75 54 44 67 3 88 89 46 15 40 96 19 7 36 55 64 1 83 78 72 31 18 21 +Card 106: 78 7 12 67 54 29 76 66 17 35 | 53 19 78 86 36 33 84 63 16 43 64 46 31 58 66 11 39 38 76 14 49 94 47 67 56 +Card 107: 95 91 53 27 12 51 29 1 36 9 | 43 70 74 89 52 12 82 97 96 4 45 37 14 68 15 58 63 51 59 34 50 81 3 6 83 +Card 108: 68 79 91 36 9 55 21 81 2 17 | 26 21 63 91 27 45 73 92 29 24 74 57 43 61 4 82 20 50 84 88 54 89 81 8 15 +Card 109: 68 37 88 22 26 53 67 43 62 35 | 24 19 46 44 49 7 17 51 21 81 96 30 59 3 72 99 71 58 22 28 60 52 43 65 86 +Card 110: 95 66 14 29 71 44 35 73 15 63 | 2 87 82 39 55 92 33 98 51 20 16 89 24 12 64 8 85 41 37 77 42 45 22 48 52 +Card 111: 83 21 90 50 91 82 73 19 41 25 | 47 74 77 86 84 33 3 99 30 93 37 98 42 59 24 12 69 36 61 68 55 17 32 53 18 +Card 112: 72 29 25 73 15 93 4 36 78 56 | 35 80 62 12 88 59 30 17 51 76 70 64 21 42 79 31 96 83 63 57 1 68 52 53 75 +Card 113: 39 52 17 98 34 9 72 53 47 51 | 70 72 17 48 83 98 64 9 15 94 57 68 87 14 27 55 40 38 53 8 47 51 16 28 67 +Card 114: 18 1 14 61 17 28 24 34 63 5 | 62 64 68 67 8 88 18 73 28 17 14 34 65 95 47 1 31 5 24 99 61 89 63 42 80 +Card 115: 31 76 94 50 65 52 21 53 5 43 | 93 38 97 35 2 82 1 45 65 50 43 59 21 27 94 5 31 53 61 52 10 76 99 44 47 +Card 116: 2 13 64 10 14 29 33 55 19 6 | 91 68 22 47 5 65 23 64 20 18 70 21 45 1 42 31 59 17 61 58 30 73 81 14 2 +Card 117: 52 22 10 59 25 80 48 28 99 82 | 93 52 25 19 94 80 85 28 46 90 74 65 48 99 22 66 87 49 83 57 3 10 59 78 67 +Card 118: 22 98 56 42 95 5 62 50 26 71 | 42 50 56 98 41 92 71 69 60 22 59 64 62 27 30 26 21 87 2 51 40 89 24 95 5 +Card 119: 25 61 90 69 15 3 33 14 59 21 | 61 30 25 44 18 90 74 64 33 73 51 3 14 60 45 82 23 69 46 59 27 21 49 15 7 +Card 120: 99 83 2 21 85 47 45 34 58 31 | 18 34 47 91 58 97 40 60 71 85 24 45 83 21 10 53 56 99 32 38 77 31 2 79 23 +Card 121: 91 63 88 9 66 25 48 94 44 51 | 42 24 19 83 43 9 25 28 18 88 91 35 63 69 66 55 3 14 54 75 80 51 73 94 44 +Card 122: 4 32 37 25 8 13 3 67 39 5 | 94 99 11 5 93 60 29 37 45 28 3 8 32 66 98 25 36 97 34 96 80 67 63 71 31 +Card 123: 93 89 1 53 15 98 21 26 82 42 | 17 25 40 63 15 21 28 42 30 46 13 53 81 74 93 85 98 57 39 65 60 38 12 36 92 +Card 124: 14 12 90 59 56 54 94 80 51 63 | 5 7 14 33 86 94 34 3 80 25 40 56 90 65 63 59 71 67 54 12 82 66 46 51 44 +Card 125: 52 37 74 28 4 96 92 40 3 64 | 23 44 64 71 22 34 35 17 10 74 93 37 40 42 52 96 53 91 92 70 28 4 75 3 12 +Card 126: 40 70 12 23 61 99 47 96 77 24 | 58 32 96 66 44 21 83 84 43 11 94 13 99 62 87 1 31 10 71 53 39 14 95 97 56 +Card 127: 9 40 39 4 19 3 25 96 5 95 | 86 9 31 25 14 19 40 55 5 84 66 4 28 3 88 95 27 1 34 52 97 29 12 96 15 +Card 128: 56 35 9 95 93 66 38 85 39 65 | 92 14 12 79 86 95 66 1 38 93 6 94 77 2 34 62 56 87 46 39 65 72 40 10 45 +Card 129: 77 19 10 24 25 68 67 5 38 29 | 73 59 50 39 37 24 58 56 70 72 74 3 20 66 26 92 71 29 83 15 96 79 2 28 27 +Card 130: 19 42 20 87 76 41 83 47 99 51 | 85 27 63 1 55 9 49 28 25 14 76 51 36 34 53 57 73 12 58 66 78 16 22 84 26 +Card 131: 33 82 49 90 74 24 53 48 12 51 | 48 29 52 12 31 99 81 33 80 76 37 32 96 28 8 51 79 56 26 62 53 10 15 42 58 +Card 132: 51 90 31 49 77 64 20 76 91 45 | 77 93 27 16 45 53 57 84 63 42 25 44 4 64 71 8 70 66 95 34 23 85 35 31 87 +Card 133: 76 84 3 16 25 79 35 8 50 60 | 32 97 7 51 49 12 37 54 59 60 27 87 40 64 38 78 8 53 43 39 81 66 68 44 18 +Card 134: 77 36 47 40 44 14 94 91 39 5 | 95 25 45 99 59 27 64 12 15 9 22 4 50 62 73 39 34 6 8 72 56 96 89 90 35 +Card 135: 33 49 25 71 39 75 30 16 46 55 | 94 82 64 10 16 73 79 68 4 12 1 87 6 53 34 98 66 99 78 59 58 2 36 77 52 +Card 136: 53 22 58 94 49 25 6 64 69 73 | 42 23 3 76 77 83 8 1 89 2 33 78 46 12 34 95 26 96 41 93 97 35 5 82 55 +Card 137: 64 25 86 72 6 39 48 95 73 62 | 52 4 29 18 11 84 79 87 19 32 96 71 61 35 17 15 44 27 68 70 98 45 22 51 85 +Card 138: 60 30 89 73 76 11 23 90 86 80 | 11 89 23 86 66 5 80 13 67 73 59 96 90 76 49 20 84 30 82 29 72 87 63 60 92 +Card 139: 82 6 17 76 72 5 70 45 90 7 | 12 50 25 41 9 16 29 20 63 70 28 10 53 90 76 5 7 82 17 15 45 93 72 97 6 +Card 140: 3 88 95 62 81 35 92 16 21 87 | 92 42 18 35 21 12 30 25 27 17 87 71 81 95 88 40 16 29 69 70 14 62 3 2 31 +Card 141: 2 36 5 53 22 30 40 94 84 52 | 86 67 38 32 90 33 18 26 13 2 96 49 25 74 83 31 54 42 52 36 97 55 63 44 47 +Card 142: 84 77 81 89 56 61 39 2 22 7 | 79 40 62 91 39 20 13 80 81 63 7 22 84 77 6 5 2 42 89 61 53 52 56 75 88 +Card 143: 90 86 6 75 67 76 18 41 36 55 | 25 7 30 21 40 65 47 42 77 34 53 60 97 10 49 39 45 57 8 94 83 93 32 80 98 +Card 144: 26 5 82 70 75 88 53 47 29 93 | 23 40 49 74 62 93 60 75 86 98 53 45 47 29 97 67 8 88 4 5 26 37 71 82 70 +Card 145: 8 62 96 13 21 82 42 54 41 61 | 87 13 23 96 29 27 6 14 53 75 5 24 31 60 90 12 82 51 64 65 70 58 9 74 47 +Card 146: 57 20 68 46 52 36 49 48 14 34 | 52 45 92 33 49 57 61 54 44 18 47 59 46 34 63 32 65 53 50 14 20 80 38 42 15 +Card 147: 47 86 79 34 64 91 57 21 1 89 | 14 79 31 91 44 11 89 75 40 97 81 92 63 73 21 49 93 86 64 22 35 8 13 99 57 +Card 148: 83 62 84 25 96 13 30 99 24 82 | 64 2 65 63 60 98 91 76 81 94 23 22 88 85 28 92 15 38 35 72 52 32 47 69 31 +Card 149: 62 32 13 59 96 78 11 73 34 52 | 89 73 13 1 52 24 71 83 53 97 62 85 78 20 17 11 59 96 74 19 32 29 34 43 9 +Card 150: 76 14 58 69 8 21 49 60 29 6 | 29 86 82 88 3 91 72 71 55 57 51 95 9 61 12 79 23 33 19 20 50 37 62 30 4 +Card 151: 68 93 22 66 81 77 16 75 47 34 | 47 76 34 52 25 68 9 26 29 66 37 57 10 54 99 46 77 95 81 93 53 13 97 22 7 +Card 152: 2 9 24 70 11 42 44 98 79 27 | 18 72 91 78 44 77 35 17 79 21 9 5 4 98 43 54 70 42 2 6 76 11 24 68 27 +Card 153: 36 28 10 9 69 25 87 50 77 11 | 7 54 25 40 85 33 15 20 87 71 96 99 77 53 94 9 67 28 69 32 26 18 63 29 43 +Card 154: 41 15 97 7 50 92 9 66 20 6 | 91 53 3 15 66 92 72 67 85 9 20 1 28 32 14 95 81 34 79 8 7 59 82 52 6 +Card 155: 8 1 15 14 44 81 89 37 55 43 | 76 62 83 79 37 24 56 30 34 58 45 64 23 41 84 71 14 22 60 27 18 9 42 54 85 +Card 156: 1 44 85 92 7 35 52 50 72 26 | 62 69 85 60 80 73 94 37 12 63 99 70 17 51 23 8 95 29 32 55 47 41 6 36 25 +Card 157: 12 49 19 14 28 64 17 76 34 8 | 33 87 95 61 84 55 86 19 92 44 3 42 24 96 94 36 38 13 10 51 15 81 27 75 67 +Card 158: 29 88 66 19 41 57 52 30 46 47 | 16 1 36 91 8 70 27 3 38 32 89 84 90 31 88 49 60 22 18 9 62 67 47 7 34 +Card 159: 99 87 7 36 67 23 14 92 52 82 | 93 41 76 15 8 63 50 40 30 85 92 48 1 99 87 53 46 70 34 5 44 79 16 35 17 +Card 160: 40 35 56 38 93 47 42 72 80 79 | 77 90 83 4 65 61 27 41 21 13 25 68 34 11 84 40 94 42 69 64 14 70 58 97 63 +Card 161: 57 54 92 91 51 37 93 55 59 41 | 2 78 35 85 23 41 74 33 8 21 72 94 63 90 95 64 71 12 65 27 38 18 19 9 60 +Card 162: 51 37 4 45 15 59 71 23 61 77 | 72 81 19 35 53 60 11 93 54 2 31 70 40 28 57 63 6 46 89 96 30 36 12 20 29 +Card 163: 52 53 7 23 29 89 86 43 97 77 | 79 18 34 45 80 61 30 41 68 7 52 89 8 49 29 86 67 17 81 98 97 78 26 48 70 +Card 164: 98 61 60 4 5 28 70 37 41 10 | 22 58 68 41 98 53 30 99 60 61 17 12 4 25 49 10 70 92 5 83 36 28 13 37 56 +Card 165: 16 12 15 73 22 31 20 63 42 95 | 26 53 40 17 97 15 8 25 78 46 44 45 54 39 61 23 51 11 90 95 2 35 68 24 18 +Card 166: 14 89 75 8 90 29 18 27 64 19 | 74 76 7 75 23 72 45 54 59 90 80 44 29 27 20 64 12 89 14 52 79 8 83 11 15 +Card 167: 16 78 33 67 66 63 69 59 36 94 | 89 70 42 47 92 34 14 69 37 83 19 25 57 36 3 44 77 26 73 85 18 59 13 65 76 +Card 168: 33 76 58 94 34 84 79 69 60 2 | 49 54 80 51 68 84 71 17 44 12 82 69 4 88 76 8 98 34 93 5 52 95 13 75 39 +Card 169: 2 63 48 44 25 75 51 36 29 52 | 24 84 56 37 17 60 74 36 77 48 62 32 16 2 80 41 15 35 88 72 30 58 20 49 12 +Card 170: 98 14 67 30 5 15 89 28 74 12 | 99 88 7 35 19 27 81 50 70 97 98 61 2 67 92 46 75 30 53 4 59 1 74 26 45 +Card 171: 4 26 44 61 5 75 76 20 56 33 | 34 97 62 70 83 35 23 78 13 3 86 58 56 65 72 59 76 20 39 32 36 71 33 30 46 +Card 172: 47 15 51 8 77 74 7 41 30 35 | 8 30 45 15 99 80 75 52 74 28 49 81 16 2 66 62 32 10 69 65 92 77 38 73 13 +Card 173: 65 57 60 74 69 55 21 59 10 50 | 32 28 78 16 17 79 67 76 35 66 9 63 80 58 62 20 89 88 92 26 64 10 12 97 3 +Card 174: 40 66 82 30 61 27 78 54 8 48 | 73 45 28 49 52 91 25 20 55 34 33 7 23 4 5 56 74 44 9 75 2 24 71 3 70 +Card 175: 49 42 2 59 41 38 32 83 89 23 | 25 57 90 46 99 54 67 40 73 87 9 27 82 58 53 63 22 7 13 37 52 68 15 81 88 +Card 176: 22 81 29 71 28 4 94 32 19 98 | 53 47 84 63 45 73 7 79 52 31 75 49 55 21 96 38 23 58 1 15 40 83 2 90 41 +Card 177: 44 43 42 73 56 74 64 61 29 89 | 91 15 96 77 65 23 84 20 94 45 95 6 33 13 63 50 53 18 12 24 41 47 54 9 14 +Card 178: 12 16 77 20 89 41 55 94 13 50 | 78 36 67 75 55 12 20 28 94 63 45 81 53 26 43 41 16 89 50 68 77 22 32 62 13 +Card 179: 41 64 96 46 85 5 11 79 89 51 | 98 62 92 55 49 93 90 91 41 64 94 17 48 46 70 31 51 74 34 33 75 12 28 35 84 +Card 180: 27 13 89 84 45 16 77 86 72 83 | 55 89 12 83 5 37 38 53 98 77 16 27 22 78 45 87 35 64 68 93 41 84 13 34 88 +Card 181: 79 31 12 61 49 11 68 56 78 54 | 58 23 21 59 62 72 69 17 12 34 68 87 26 98 67 16 1 3 11 38 31 78 89 27 91 +Card 182: 52 55 4 31 57 5 23 66 78 68 | 52 31 87 50 98 83 66 4 26 25 68 63 14 70 54 29 7 22 85 42 17 27 67 32 5 +Card 183: 71 9 31 84 59 32 74 26 85 36 | 5 9 58 26 50 74 80 37 59 16 44 27 98 11 21 48 43 77 57 62 33 86 24 28 63 +Card 184: 47 16 69 11 99 34 79 65 49 9 | 92 97 64 83 49 73 17 36 29 46 1 15 78 25 58 81 51 23 84 39 60 91 67 3 56 +Card 185: 84 21 9 64 45 19 51 90 91 76 | 91 83 76 26 8 6 21 70 32 42 11 51 19 39 9 69 4 67 82 49 61 68 64 45 57 +Card 186: 69 5 36 53 16 65 64 32 62 50 | 8 4 2 92 18 13 33 9 42 78 59 25 68 79 19 28 38 32 16 36 71 69 65 63 17 +Card 187: 37 58 19 57 26 21 55 92 68 50 | 95 61 58 35 6 55 31 45 76 23 22 96 18 30 86 15 94 70 53 13 80 26 68 87 36 +Card 188: 46 30 47 87 12 68 84 51 90 42 | 71 61 95 42 60 14 22 84 83 48 23 20 27 69 47 18 85 94 96 35 66 93 87 5 50 +Card 189: 36 31 8 73 83 14 39 90 67 74 | 89 21 37 74 36 19 8 14 53 32 17 39 23 96 95 38 46 78 90 3 35 52 26 56 84 +Card 190: 41 75 39 18 97 85 30 24 83 40 | 81 95 22 33 97 40 98 48 30 47 37 39 75 78 46 27 59 85 82 41 6 56 24 12 29 +Card 191: 39 29 11 97 41 63 57 87 10 53 | 14 68 22 52 75 35 97 42 13 38 54 23 24 3 71 20 4 43 86 56 34 70 51 94 47 +Card 192: 18 88 98 45 41 71 32 87 29 99 | 99 85 91 7 21 86 5 12 87 35 95 94 65 57 17 58 39 78 22 49 32 2 8 19 97 +Card 193: 5 31 29 36 40 65 95 74 9 69 | 79 64 74 44 9 95 73 65 70 18 48 58 33 22 88 81 26 57 67 46 42 11 30 5 15 +Card 194: 45 87 50 51 64 69 13 83 40 52 | 54 82 76 85 32 61 70 81 44 33 9 77 99 23 39 60 19 35 31 52 65 78 79 34 95 +Card 195: 48 35 73 17 39 44 91 70 45 66 | 76 3 23 30 15 20 22 40 25 8 90 75 24 42 21 46 10 7 77 72 29 64 55 49 28 +Card 196: 45 95 66 40 83 68 96 84 15 89 | 51 86 89 90 70 88 36 53 14 18 22 74 46 17 56 1 9 65 87 24 30 32 38 66 92 +Card 197: 92 25 8 82 33 4 52 55 95 83 | 75 50 53 28 14 76 61 65 90 29 81 39 27 33 15 56 24 97 80 40 70 13 21 78 37 +Card 198: 30 94 70 37 58 66 8 78 71 18 | 97 64 16 13 77 57 87 74 54 92 1 52 35 23 82 9 72 22 80 27 75 10 15 20 43 +Card 199: 20 30 64 54 88 32 16 59 43 48 | 88 2 11 27 21 52 68 99 75 80 84 62 23 37 56 82 89 8 1 24 48 98 7 15 72 +Card 200: 86 18 84 48 80 11 30 72 1 88 | 16 48 55 32 41 51 18 2 76 72 81 84 58 22 88 27 11 89 8 66 30 53 93 20 19 +Card 201: 35 12 66 89 38 67 14 64 51 7 | 6 88 40 49 86 96 47 67 70 71 23 7 32 42 12 51 30 81 20 74 28 34 35 14 15 +Card 202: 89 13 72 16 67 26 37 46 66 33 | 56 95 63 93 67 97 44 74 83 60 36 78 30 45 87 24 90 70 99 88 80 48 82 68 54 +Card 203: 94 55 82 24 41 48 28 14 42 80 | 28 22 78 14 80 41 42 58 48 13 44 57 61 55 15 47 53 11 21 19 29 82 63 65 94 +Card 204: 54 66 41 71 2 91 73 85 78 69 | 24 82 41 38 45 91 4 68 77 18 54 78 85 73 71 2 69 44 76 80 63 89 9 34 66 +Card 205: 76 2 81 21 16 64 10 6 30 45 | 72 38 8 84 64 21 81 45 69 3 22 5 60 23 18 63 74 9 6 29 82 4 46 30 16 +Card 206: 62 17 63 99 1 31 48 89 22 53 | 28 74 78 47 48 97 73 31 68 23 42 14 2 99 49 37 39 8 35 64 66 92 44 54 11 +Card 207: 69 76 21 35 49 91 77 75 72 53 | 88 57 58 16 91 84 21 99 53 70 19 29 3 56 55 22 49 41 1 72 10 35 9 20 60 +Card 208: 33 97 67 76 69 63 13 41 54 21 | 84 69 9 11 21 50 12 45 97 67 54 42 18 63 62 81 96 33 31 95 26 48 17 68 76 +Card 209: 66 35 6 71 82 16 14 97 68 50 | 58 50 82 71 97 90 96 49 16 84 91 21 98 63 76 31 65 51 1 80 52 30 47 93 33 +Card 210: 56 53 62 97 78 68 27 16 50 10 | 48 71 80 87 8 39 22 9 53 58 54 1 11 14 96 6 41 65 37 34 98 17 85 24 20 +Card 211: 52 6 85 59 80 96 77 26 65 36 | 38 77 86 29 98 27 58 51 8 75 87 50 63 2 82 1 41 92 97 53 33 14 16 93 49 +Card 212: 82 69 16 77 49 12 19 29 30 31 | 42 81 22 94 32 78 61 29 11 1 64 87 19 57 88 82 24 8 7 46 5 28 30 77 26 +Card 213: 71 48 25 24 37 40 77 88 44 74 | 51 92 62 34 33 93 54 78 6 25 10 96 70 63 81 82 85 20 48 12 99 40 86 8 58 +Card 214: 12 43 97 95 19 39 98 13 41 93 | 69 89 10 36 50 20 51 33 67 88 73 59 81 29 17 34 85 28 92 55 5 63 79 72 52 +Card 215: 21 31 62 69 74 97 40 45 20 35 | 25 18 1 52 86 84 68 44 15 47 91 99 57 87 98 17 66 56 73 42 33 93 30 8 95 +Card 216: 94 71 33 51 86 70 60 78 12 17 | 98 68 80 56 47 53 41 55 70 37 46 43 8 22 74 48 62 9 10 65 7 60 39 29 97 +Card 217: 70 98 21 38 77 68 67 39 45 72 | 63 57 37 21 94 64 8 96 69 80 84 25 71 26 83 99 81 31 48 42 41 73 54 60 22 +Card 218: 92 44 79 17 16 34 55 78 19 9 | 52 39 85 98 93 46 21 91 20 45 1 89 66 27 4 88 99 41 86 72 38 40 84 81 69 From 23109074a06e1d444a012a05bef4a9aeef9abc16 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 4 Dec 2023 12:47:38 +0100 Subject: [PATCH 08/15] feat: completed part 2 --- README.md | 2 +- day_04/src/lib.rs | 1 + day_04/src/main.rs | 3 ++- day_04/src/part_02.rs | 56 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 day_04/src/part_02.rs diff --git a/README.md b/README.md index 46e3242..822084e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [Day 1](https://github.com/ankjevel/adventofcode/tree/2023/day_01) ⭐️ ⭐️ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2023/day_02) ⭐️ ⭐️ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2023/day_03) ⭐️ ⭐️ -- [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️ +- [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️ ⭐️ - [Day 5](#) - [Day 6](#) - [Day 7](#) diff --git a/day_04/src/lib.rs b/day_04/src/lib.rs index ac0326b..71a28ec 100644 --- a/day_04/src/lib.rs +++ b/day_04/src/lib.rs @@ -1,4 +1,5 @@ pub mod part_01; +pub mod part_02; pub type Input = Vec<(Vec, Vec)>; diff --git a/day_04/src/main.rs b/day_04/src/main.rs index ed0cf2f..6286c1f 100644 --- a/day_04/src/main.rs +++ b/day_04/src/main.rs @@ -1,11 +1,12 @@ use std::io::Result; -use day_04::{parse_input, part_01::main as part_01}; +use day_04::{parse_input, part_01::main as part_01, part_02::main as part_02}; fn main() -> Result<()> { let input = parse_input(include_str!("../../input/day_04")); println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); Ok(()) } diff --git a/day_04/src/part_02.rs b/day_04/src/part_02.rs new file mode 100644 index 0000000..af37423 --- /dev/null +++ b/day_04/src/part_02.rs @@ -0,0 +1,56 @@ +use std::{collections::BTreeMap, io::Result}; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut cards = BTreeMap::from_iter(input.iter().enumerate().map(|n| (n.0 + 1, 1u128))); + + input + .iter() + .enumerate() + .for_each(|(index, (winning_numbers, numbers))| { + let mut i = index + 1; + let mut next = || -> usize { + i = i + 1; + i + }; + + for card in winning_numbers + .iter() + .filter(|n| numbers.contains(n)) + .map(|_| next()) + { + let current_for_card = cards.get(&(index + 1)).unwrap_or(&0).to_owned(); + let to_add = if current_for_card > 1 { + current_for_card + } else { + 1 + }; + cards.entry(card).and_modify(|previous| *previous += to_add); + } + }); + + Ok(cards.values().sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 + Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 + Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 + Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 + Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 + Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 30); + Ok(()) + } +} From 8d73ad2e075f2666c1153040e29e6b28e838f03e Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 4 Dec 2023 12:52:17 +0100 Subject: [PATCH 09/15] chore: simplified --- day_04/src/part_02.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/day_04/src/part_02.rs b/day_04/src/part_02.rs index af37423..63fc1e0 100644 --- a/day_04/src/part_02.rs +++ b/day_04/src/part_02.rs @@ -3,30 +3,21 @@ use std::{collections::BTreeMap, io::Result}; use crate::Input; pub fn main(input: &Input) -> Result { - let mut cards = BTreeMap::from_iter(input.iter().enumerate().map(|n| (n.0 + 1, 1u128))); + let mut cards = BTreeMap::from_iter(input.iter().enumerate().map(|n| (n.0, 1))); input .iter() .enumerate() .for_each(|(index, (winning_numbers, numbers))| { - let mut i = index + 1; - let mut next = || -> usize { - i = i + 1; - i - }; - - for card in winning_numbers + let to_add = cards.get(&(index)).unwrap_or(&1).to_owned(); + for (card, _) in winning_numbers .iter() .filter(|n| numbers.contains(n)) - .map(|_| next()) + .enumerate() { - let current_for_card = cards.get(&(index + 1)).unwrap_or(&0).to_owned(); - let to_add = if current_for_card > 1 { - current_for_card - } else { - 1 - }; - cards.entry(card).and_modify(|previous| *previous += to_add); + cards + .entry(card + index + 1) + .and_modify(|previous| *previous += to_add); } }); From 7df6afdbae9078f0c199e6cc0cd810093a388e11 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Tue, 5 Dec 2023 10:00:21 +0100 Subject: [PATCH 10/15] feat: completed day 5 (dirty!) --- README.md | 2 +- day_05/Cargo.toml | 11 +++ day_05/src/lib.rs | 129 ++++++++++++++++++++++++++ day_05/src/main.rs | 12 +++ day_05/src/part_01.rs | 28 ++++++ day_05/src/part_02.rs | 79 ++++++++++++++++ input/day_05 | 211 ++++++++++++++++++++++++++++++++++++++++++ input/day_05_example | 33 +++++++ 8 files changed, 504 insertions(+), 1 deletion(-) create mode 100644 day_05/Cargo.toml create mode 100644 day_05/src/lib.rs create mode 100644 day_05/src/main.rs create mode 100644 day_05/src/part_01.rs create mode 100644 day_05/src/part_02.rs create mode 100644 input/day_05 create mode 100644 input/day_05_example diff --git a/README.md b/README.md index 822084e..4e85fae 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2023/day_02) ⭐️ ⭐️ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2023/day_03) ⭐️ ⭐️ - [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️ ⭐️ -- [Day 5](#) +- [Day 5](https://github.com/ankjevel/adventofcode/tree/2023/day_05) ⭐️ ⭐️ - [Day 6](#) - [Day 7](#) - [Day 8](#) diff --git a/day_05/Cargo.toml b/day_05/Cargo.toml new file mode 100644 index 0000000..2f6f9a5 --- /dev/null +++ b/day_05/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_05" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_05/src/lib.rs b/day_05/src/lib.rs new file mode 100644 index 0000000..ee44d80 --- /dev/null +++ b/day_05/src/lib.rs @@ -0,0 +1,129 @@ +use std::ops::Range; + +pub mod part_01; +pub mod part_02; + +pub type Integer = u128; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Input { + seeds: Vec, + maps: Vec)>>, +} + +impl<'a> Input { + pub fn get_location(input: &'a Input, seed: Integer) -> Integer { + let mut current_index = seed.to_owned(); + let mut iterator = input.maps.to_owned().into_iter(); + let mut result = 0; + + while let Some(next) = iterator.next() { + let next_index = next.into_iter().find( + |(_destination_range_start, _source_range_start, source_range)| { + source_range.contains(¤t_index) + }, + ); + + current_index = match next_index { + Some((destination_range_start, source_range_start, _source_range)) => { + destination_range_start + (current_index - source_range_start) + } + _ => current_index, + }; + + result = current_index.to_owned(); + } + + result + } +} + +pub fn parse_input(input: &str) -> Input { + let mut last_key = "".to_owned(); + let mut current_array = vec![]; + let mut seeds = vec![]; + let mut maps = vec![]; + let map_identifier = " map:"; + + let numbers = |key: &str| -> Vec<_> { + key.split(' ') + .filter(|string| !string.trim().is_empty()) + .map(|string| string.trim().parse::<_>().unwrap()) + .collect() + }; + + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|string| string.to_owned()) + .for_each(|string| { + if string.is_empty() { + if last_key.is_empty() || current_array.is_empty() { + return; + } + maps.push(current_array.to_owned()); + current_array.clear(); + return; + } + + let is_map_start = string.contains(map_identifier); + let is_start = string.contains(": ") && !string.contains(map_identifier); + + if is_start { + seeds = numbers(string.split(": ").collect::>()[1]); + return; + } + + if is_map_start { + current_array.clear(); + last_key = string.split(map_identifier).collect::>()[0].to_owned(); + return; + } + + let numbers = numbers(&string); + let (destination_range_start, source_range_start, range_length) = + (numbers[0], numbers[1], numbers[2]); + let source_range = source_range_start..(source_range_start + range_length); + current_array.push((destination_range_start, source_range_start, source_range)); + }); + + if !current_array.is_empty() { + maps.push(current_array.to_owned()); + } + + Input { seeds, maps } +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + seeds: 79 14 55 13 + + seed-to-soil map: + 50 98 2 + 52 50 48 + + soil-to-fertilizer map: + 0 15 37 + 37 52 2 + 39 0 15 + "; + + #[test] + fn it_parses_input() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + Input { + seeds: vec![79, 14, 55, 13], + maps: vec![ + vec![(50, 98, 98..100), (52, 50, 50..98)], + vec![(0, 15, 15..52), (37, 52, 52..54), (39, 0, 0..15)] + ] + } + ); + } +} diff --git a/day_05/src/main.rs b/day_05/src/main.rs new file mode 100644 index 0000000..3e6c10d --- /dev/null +++ b/day_05/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_05::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_05")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_05/src/part_01.rs b/day_05/src/part_01.rs new file mode 100644 index 0000000..dc7a1ed --- /dev/null +++ b/day_05/src/part_01.rs @@ -0,0 +1,28 @@ +use std::io::Result; + +use crate::{Input, Integer}; + +pub fn main(input: &Input) -> Result { + Ok(input + .seeds + .clone() + .into_iter() + .map(|seed| Input::get_location(&input, seed)) + .min() + .unwrap()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_05_example"); + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 35); + Ok(()) + } +} diff --git a/day_05/src/part_02.rs b/day_05/src/part_02.rs new file mode 100644 index 0000000..5d416c8 --- /dev/null +++ b/day_05/src/part_02.rs @@ -0,0 +1,79 @@ +use std::{io::Result, sync::mpsc, thread}; + +use crate::{Input, Integer}; + +pub fn main(input: &Input) -> Result { + let (tx, rx) = mpsc::channel::<_>(); + + let input = input.clone(); + let seeds = input.seeds.to_owned(); + + Ok(thread::scope(move |s| -> _ { + let chunks = seeds.chunks(2); + + let to_check = chunks.fold(vec![], |mut chunks, chunk| { + let (start, length) = (chunk[0], chunk[1]); + chunks.extend(Vec::from_iter((start..(start + length)).into_iter())); + chunks + }); + + let ranged_chunks = to_check.chunks(100); + let ranged_chunks_max = ranged_chunks.len(); + let mut i = 0; + + for chunks in ranged_chunks { + let chunks = chunks.to_owned(); + let input = input.to_owned(); + let handle = s.spawn(move || -> _ { + let mut min = Integer::MAX; + let value = chunks.clone(); + let input = input.to_owned(); + for seed in value { + let location = Input::get_location(&input, seed); + if location < min { + min = location; + } + } + min + }); + + let result = handle.join().unwrap(); + let _ = tx.send(result); + + i = i + 1; + if i == ranged_chunks_max { + let _ = tx.send(0); // I needed to break on something... + } + } + + let handle = s.spawn(move || -> _ { + let mut min = Integer::MAX; + while let Ok(min_from_message) = rx.recv() { + if min_from_message == 0 { + break; + } + + if min_from_message < min { + min = min_from_message; + } + } + min + }); + + handle.join().unwrap() + })) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::parse_input; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_05_example"); + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 46); + Ok(()) + } +} diff --git a/input/day_05 b/input/day_05 new file mode 100644 index 0000000..4a2a7c8 --- /dev/null +++ b/input/day_05 @@ -0,0 +1,211 @@ +seeds: 630335678 71155519 260178142 125005421 1548082684 519777283 4104586697 30692976 1018893962 410959790 3570781652 45062110 74139777 106006724 3262608046 213460151 3022784256 121993130 2138898608 36769984 + +seed-to-soil map: +2977255263 3423361099 161177662 +3464809483 1524036300 40280620 +1278969303 2583891002 282823382 +3766263020 1796922321 171061976 +411885923 23002578 152894367 +564780290 442452799 75000259 +2421385924 1454220354 69815946 +3348169880 3014668733 58677303 +903828313 1975611534 37514769 +3406847183 1396258054 57962300 +4043490501 3171884304 251476795 +941343082 2866714384 147954349 +1089297431 1206586182 189671872 +2891116902 3584538761 18778869 +0 517453058 122327491 +2491201870 932395829 274190353 +388883345 0 23002578 +3944952233 3073346036 98538268 +3505090103 671222912 261172917 +2073455492 2013126303 347930432 +2909895771 2361056735 67359492 +1561792685 3603317630 511662807 +2765392223 2458166323 125724679 +3168183021 4114980437 179986859 +3138432925 2428416227 29750096 +122327491 175896945 266555854 +671222912 1564316920 232605401 +3937324996 1967984297 7627237 + +soil-to-fertilizer map: +895998030 0 382128379 +2851625320 2664267363 205943350 +2518444693 3961786669 333180627 +1879667741 2025490411 638776952 +0 1243838521 558555556 +3280896340 2870210713 1014070956 +558555556 906396047 337442474 +3057568670 3884281669 77505000 +3135073670 1879667741 145822670 +1278126409 382128379 524267668 + +fertilizer-to-water map: +0 1845976330 336090970 +3299138007 3322545218 12048535 +336090970 0 11457152 +1280501317 1371665084 474311246 +2583893821 3334593753 715244186 +3311186542 2468197905 738651397 +2468197905 3206849302 115695916 +347548122 11457152 932953195 +1754812563 944410347 427254737 + +water-to-light map: +1121222108 519789808 4326619 +1125548727 524116427 429792955 +1052043895 3930896885 69178213 +3210593080 0 36442681 +1669405426 2787769857 138341045 +1919839172 3142586910 277606697 +2197445869 2466152271 321617586 +1555341682 3816833141 114063744 +3431283943 3092543143 50043767 +3481327710 1975836414 28620233 +136025352 1371812880 335069822 +0 3420193607 136025352 +2600375975 3610804829 206028312 +3247035761 36442681 184248182 +3676380184 1048117966 323694914 +2519063455 966805446 81312520 +483991238 1706882702 268953712 +3509947943 2926110902 166432241 +1862332341 2408645440 57506831 +752944950 220690863 299098945 +2806404287 2004456647 404188793 +1807746471 3556218959 54585870 +471095174 953909382 12896064 + +light-to-temperature map: +3941111261 382813357 83783792 +4083751028 2792620142 62769876 +2924924808 517646744 141124785 +10073304 296361721 86451636 +2112077648 3356571260 325360811 +2097723771 930487406 14353877 +1038821361 2233157447 330985253 +1604981575 0 157737476 +4232208439 2231398376 1759071 +3126943010 2564142700 228477442 +3355420452 3681932071 528033316 +3066049593 1302213021 60893417 +2893234140 1091417457 31690668 +4146520904 1005729922 85687535 +764412615 658771529 271715877 +4024895053 4212658256 21309254 +1601614929 3144601228 3366646 +2813498518 3276835638 79735622 +0 944841283 10073304 +3883453768 1363106438 57657493 +1036128492 4209965387 2692869 +1762719051 1160654846 141558175 +2437438459 157737476 138624245 +96524940 1420763931 667887675 +2576062704 3147967874 128867764 +1904277226 2855390018 50699775 +1420621949 2906089793 129943385 +1550565334 466597149 51049595 +2704930468 3036033178 108568050 +4046204307 1123108125 37546721 +1369806614 954914587 50815335 +1954977001 2088651606 142746770 + +temperature-to-humidity map: +3744493855 2753433800 53429527 +3926657179 2806863327 207882975 +567844723 1829271702 6392959 +3797923382 3046866321 128733797 +1711260618 465872733 110275892 +2947786208 2530091374 223342426 +2371290430 3335177849 39675908 +1900678095 703125986 238513863 +1521940365 941639849 16040471 +979702084 962957535 519048585 +2678536664 3987414423 22824189 +316276095 2006380474 251568628 +574237682 576148625 7838036 +2512589774 2369848229 129456424 +1821536510 386731148 79141585 +659847979 2257949102 32337475 +1498750669 0 23189696 +2139191958 1482006120 151094619 +2410966338 2361085516 8762713 +248695366 629624702 67580729 +1705983403 957680320 5277215 +3436386005 3680199681 275987831 +896869843 23189696 23822140 +1540019357 265437244 43521643 +3171128634 4029709925 265257371 +700698880 1633100739 196170963 +2659065351 4010238612 19471313 +1660345362 583986661 45638041 +2642046198 3175600118 17019153 +4134540154 3966392426 21021997 +145503080 1903188188 103192286 +4155562151 3467714480 139405145 +920691983 1844178087 59010101 +2805227630 3192619271 142558578 +692185454 1835664661 8513426 +2732147574 3607119625 73080056 +3712373836 3014746302 32120019 +2361085516 3956187512 10204914 +2701360853 2499304653 30786721 +582075718 308958887 77772261 +1583541000 188632882 76804362 +3882034 47011836 141621046 +0 699243952 3882034 +1537980836 697205431 2038521 +2419729051 3374853757 92860723 + +humidity-to-location map: +3880387060 2052152805 97611299 +2442736538 3295723734 10591308 +3014234548 3058886861 44150293 +2722522139 3413370195 153277538 +2877652345 3226748198 68975536 +678696757 79205913 5515453 +3758528684 3103037154 121858376 +3648288667 2533118408 110240017 +3457871155 4266074310 28892986 +2176930761 3905620500 135283057 +2312213818 2369019482 56130623 +2875799677 3224895530 1852668 +2052152805 3780842544 124777956 +2598433171 3306315042 56382802 +1279041455 278559111 48074772 +2964261570 2302916483 49972978 +344154771 1539624544 79809331 +1030322972 1619433875 248718483 +1905012367 1868152358 115533200 +105230362 326633883 51970437 +4085966662 2880778716 178108145 +684212210 1466450827 73173717 +919250672 396737705 108684083 +868993622 1215278638 50257050 +2962757902 2879275048 1503668 +1847630888 378604320 18133385 +3232700402 4040903557 225170753 +2575587736 3390524760 22845435 +3977998359 2425150105 107968303 +3058384841 3362697844 27826916 +789787709 0 79205913 +4264074807 2272023994 30892489 +3114006964 3594442940 118693438 +460824111 202771015 75788096 +423964102 1983685558 36860009 +2946627881 2352889461 16130021 +157200799 505421788 186953972 +3486764141 2717750522 161524526 +1027934755 692375760 2388217 +2453327846 2149764104 122259890 +2368344441 2643358425 74392097 +0 1407620238 58830589 +2654815973 3713136378 67706166 +1865764273 117123148 39248094 +3086211757 3566647733 27795207 +58830589 156371242 46399773 +536612207 1265535688 142084550 +757385927 84721366 32401782 +1327116227 694763977 520514661 diff --git a/input/day_05_example b/input/day_05_example new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/input/day_05_example @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 From ec89334015618a5ff12d16c026c73c1641d26341 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Tue, 5 Dec 2023 20:47:47 +0100 Subject: [PATCH 11/15] feat: not optimized, just quicker --- day_05/src/part_02.rs | 43 ++++++++----------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/day_05/src/part_02.rs b/day_05/src/part_02.rs index 5d416c8..db534b4 100644 --- a/day_05/src/part_02.rs +++ b/day_05/src/part_02.rs @@ -1,10 +1,8 @@ -use std::{io::Result, sync::mpsc, thread}; +use std::{io::Result, thread}; use crate::{Input, Integer}; pub fn main(input: &Input) -> Result { - let (tx, rx) = mpsc::channel::<_>(); - let input = input.clone(); let seeds = input.seeds.to_owned(); @@ -17,50 +15,25 @@ pub fn main(input: &Input) -> Result { chunks }); - let ranged_chunks = to_check.chunks(100); - let ranged_chunks_max = ranged_chunks.len(); - let mut i = 0; + let ranged_chunks = to_check.chunks(100_000); + let mut threads = vec![]; for chunks in ranged_chunks { let chunks = chunks.to_owned(); let input = input.to_owned(); - let handle = s.spawn(move || -> _ { + threads.push(s.spawn(move || -> _ { let mut min = Integer::MAX; let value = chunks.clone(); let input = input.to_owned(); for seed in value { - let location = Input::get_location(&input, seed); - if location < min { - min = location; - } + min = min.min(Input::get_location(&input, seed)); } min - }); - - let result = handle.join().unwrap(); - let _ = tx.send(result); - - i = i + 1; - if i == ranged_chunks_max { - let _ = tx.send(0); // I needed to break on something... - } + })); } - let handle = s.spawn(move || -> _ { - let mut min = Integer::MAX; - while let Ok(min_from_message) = rx.recv() { - if min_from_message == 0 { - break; - } - - if min_from_message < min { - min = min_from_message; - } - } - min - }); - - handle.join().unwrap() + let threads = threads.into_iter().map(|t| t.join().unwrap()); + threads.min().unwrap() })) } From 0631de94ca1792c4c2cc4ff90169105880aa0d63 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Wed, 6 Dec 2023 12:52:44 +0100 Subject: [PATCH 12/15] feat: completed day 6 --- README.md | 2 +- day_06/Cargo.toml | 11 ++++++++++ day_06/src/lib.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ day_06/src/main.rs | 12 +++++++++++ day_06/src/part_01.rs | 36 ++++++++++++++++++++++++++++++++ day_06/src/part_02.rs | 46 +++++++++++++++++++++++++++++++++++++++++ input/day_06 | 2 ++ input/day_06_example | 2 ++ 8 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 day_06/Cargo.toml create mode 100644 day_06/src/lib.rs create mode 100644 day_06/src/main.rs create mode 100644 day_06/src/part_01.rs create mode 100644 day_06/src/part_02.rs create mode 100644 input/day_06 create mode 100644 input/day_06_example diff --git a/README.md b/README.md index 4e85fae..1f93cd0 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2023/day_03) ⭐️ ⭐️ - [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️ ⭐️ - [Day 5](https://github.com/ankjevel/adventofcode/tree/2023/day_05) ⭐️ ⭐️ -- [Day 6](#) +- [Day 6](https://github.com/ankjevel/adventofcode/tree/2023/day_06) ⭐️ ⭐️ - [Day 7](#) - [Day 8](#) - [Day 9](#) diff --git a/day_06/Cargo.toml b/day_06/Cargo.toml new file mode 100644 index 0000000..209de00 --- /dev/null +++ b/day_06/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_06" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_06/src/lib.rs b/day_06/src/lib.rs new file mode 100644 index 0000000..a341004 --- /dev/null +++ b/day_06/src/lib.rs @@ -0,0 +1,48 @@ +pub mod part_01; +pub mod part_02; + +pub type Integer = u128; + +pub type Input = Vec<(Integer, Integer)>; + +pub fn parse_input(input: &str) -> Input { + let parse_numbers = |s: &str| -> Vec { + s.split(':').collect::>()[1] + .trim() + .to_owned() + .split(' ') + .filter(|s| !s.is_empty()) + .map(|string| string.parse().unwrap()) + .collect() + }; + + let lines = input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .filter(|line| !line.is_empty()) + .collect::>(); + + let (times, distances) = ( + parse_numbers(lines.get(0).unwrap()), + parse_numbers(lines.get(1).unwrap()), + ); + + times.into_iter().zip(distances.into_iter()).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_06_example"); + + #[test] + fn it_parses_input() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![(7, 9), (15, 40), (30, 200)] + ); + } +} diff --git a/day_06/src/main.rs b/day_06/src/main.rs new file mode 100644 index 0000000..1209313 --- /dev/null +++ b/day_06/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_06::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_06")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_06/src/part_01.rs b/day_06/src/part_01.rs new file mode 100644 index 0000000..a6ee9c8 --- /dev/null +++ b/day_06/src/part_01.rs @@ -0,0 +1,36 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .to_owned() + .into_iter() + .map(|(time, distance)| { + (0..=time) + .into_iter() + .filter(|warmup| { + let runtime = time - warmup; + let total_time = runtime * warmup; + total_time > distance + }) + .collect::>() + .len() + }) + .product()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_06_example"); + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 288); + Ok(()) + } +} diff --git a/day_06/src/part_02.rs b/day_06/src/part_02.rs new file mode 100644 index 0000000..fbfd201 --- /dev/null +++ b/day_06/src/part_02.rs @@ -0,0 +1,46 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let (time, distance) = input.to_owned().into_iter().fold( + ("".to_owned(), "".to_owned()), + |mut n, (time, distance)| { + n.0 = format!("{}{}", n.0, time); + n.1 = format!("{}{}", n.1, distance); + n + }, + ); + + let (time, distance) = ( + time.parse::().unwrap(), + distance.parse::().unwrap(), + ); + + let n_times = (0..=time) + .into_iter() + .filter(|warmup| { + let runtime = time - warmup; + let total_time = runtime * warmup; + total_time > distance + }) + .collect::>() + .len(); + + Ok(n_times) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_06_example"); + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 71503); + Ok(()) + } +} diff --git a/input/day_06 b/input/day_06 new file mode 100644 index 0000000..f0187ef --- /dev/null +++ b/input/day_06 @@ -0,0 +1,2 @@ +Time: 58 99 64 69 +Distance: 478 2232 1019 1071 diff --git a/input/day_06_example b/input/day_06_example new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/input/day_06_example @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 From 60b2ce651b9a8b60d39031b8165d2cd2a81c67d0 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Thu, 7 Dec 2023 15:52:41 +0100 Subject: [PATCH 13/15] feat: completed day 7 --- README.md | 2 +- day_07/Cargo.toml | 11 + day_07/src/hand.rs | 263 +++++++++++ day_07/src/lib.rs | 51 +++ day_07/src/main.rs | 12 + day_07/src/part_01.rs | 22 + day_07/src/part_02.rs | 22 + input/day_07 | 1000 +++++++++++++++++++++++++++++++++++++++++ input/day_07_example | 5 + 9 files changed, 1387 insertions(+), 1 deletion(-) create mode 100644 day_07/Cargo.toml create mode 100644 day_07/src/hand.rs create mode 100644 day_07/src/lib.rs create mode 100644 day_07/src/main.rs create mode 100644 day_07/src/part_01.rs create mode 100644 day_07/src/part_02.rs create mode 100644 input/day_07 create mode 100644 input/day_07_example diff --git a/README.md b/README.md index 1f93cd0..05c0244 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ - [Day 4](https://github.com/ankjevel/adventofcode/tree/2023/day_04) ⭐️ ⭐️ - [Day 5](https://github.com/ankjevel/adventofcode/tree/2023/day_05) ⭐️ ⭐️ - [Day 6](https://github.com/ankjevel/adventofcode/tree/2023/day_06) ⭐️ ⭐️ -- [Day 7](#) +- [Day 7](https://github.com/ankjevel/adventofcode/tree/2023/day_07) ⭐️ ⭐️ - [Day 8](#) - [Day 9](#) - [Day 10](#) diff --git a/day_07/Cargo.toml b/day_07/Cargo.toml new file mode 100644 index 0000000..f2d1844 --- /dev/null +++ b/day_07/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_07" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_07/src/hand.rs b/day_07/src/hand.rs new file mode 100644 index 0000000..1536033 --- /dev/null +++ b/day_07/src/hand.rs @@ -0,0 +1,263 @@ +use std::{ + cmp::Ordering::{self, *}, + collections::{BTreeMap, BTreeSet}, +}; + +use crate::Hand; + +#[derive(Debug, Clone, Eq, PartialEq)] +enum HandOrder { + FiveOfAKind = 0, + FourOfAKind = 1, + FullHouse = 2, + ThreeOfAKind = 3, + TwoPair = 4, + OnePair = 5, + HighCard = 6, +} + +use HandOrder::*; + +impl HandOrder { + fn char_to_card(c: char, joker_rule: bool) -> u8 { + let joker_or_knight = if joker_rule { 1 } else { 11 }; + match c { + 'A' => 14, + 'K' => 13, + 'Q' => 12, + 'J' => joker_or_knight, + 'T' => 10, + n => n.to_digit(10).unwrap() as u8, + } + } + + fn from_chars(hand: &Hand, joker_rule: bool) -> Vec { + hand.to_owned() + .into_iter() + .map(|c| Self::char_to_card(c, joker_rule)) + .collect::>() + } + + fn to_map(hand: Vec) -> BTreeMap { + hand.into_iter().fold(BTreeMap::new(), |mut map, card| { + match map.get_mut(&card) { + Some(v) => *v += 1, + _ => { + map.insert(card, 1); + } + }; + map + }) + } + + pub fn from_hand(hand: &Hand) -> Self { + let unique = BTreeSet::from_iter(hand.to_owned().into_iter()); + if unique.len() == 1 { + return FiveOfAKind; + } + + let map = Self::to_map( + hand.to_owned() + .into_iter() + .map(|c| Self::char_to_card(c, false)) + .collect(), + ); + + if map.values().any(|v| v == &4) { + return FourOfAKind; + } + + if map.values().any(|v| v == &3) && map.values().any(|v| v == &2) { + return FullHouse; + } + + if map.values().any(|v| v == &3) { + return ThreeOfAKind; + } + + if map.values().filter(|v| v == &&2).collect::>().len() == 2 { + return TwoPair; + } + + if map.values().any(|v| v == &2) { + return OnePair; + } + + HighCard + } + + pub fn from_hand_with_joker_rule(hand: &Hand) -> Self { + let mut jokers = 0; + let map = Self::to_map( + hand.to_owned() + .into_iter() + .filter(|c| { + if c == &'J' { + jokers += 1; + return false; + } + true + }) + .map(|c| Self::char_to_card(c, true)) + .collect(), + ); + + if jokers == 5 || map.values().any(|v| (*v as u8 + jokers) == 5) { + return FiveOfAKind; + } + + if map.values().any(|v| (*v as u8 + jokers) >= 4) { + return FourOfAKind; + } + + let threes = map.values().filter(|v| v == &&3).collect::>(); + let twos = map.values().filter(|v| v == &&2).collect::>(); + + if !threes.is_empty() && !twos.is_empty() { + return FullHouse; + } + + if threes.is_empty() && twos.len() == 2 && jokers == 1 { + return FullHouse; + } + + if jokers == 2 { + return ThreeOfAKind; + } + + if !threes.is_empty() || (!twos.is_empty() && jokers >= 1) { + return ThreeOfAKind; + } + + if map.values().filter(|v| v == &&2).collect::>().len() == 2 { + return TwoPair; + } + + if map.values().any(|v| (*v as u8 + jokers) >= 2) { + return OnePair; + } + + HighCard + } +} + +fn rank(a_rank: HandOrder, b_rank: HandOrder, a: &Hand, b: &Hand, joker_rule: bool) -> Ordering { + let to_vec = HandOrder::from_chars; + match (b_rank as u8).cmp(&(a_rank as u8)) { + Equal => { + let a = to_vec(a, joker_rule); + let b = to_vec(b, joker_rule); + for (i, c) in a.into_iter().enumerate() { + match c.cmp(b.get(i).unwrap()) { + Equal => continue, + order => return order, + } + } + Equal + } + order => order, + } +} + +pub fn sort_hand(a: &Hand, b: &Hand) -> Ordering { + let parse = HandOrder::from_hand; + rank(parse(a), parse(b), a, b, false) +} + +pub fn sort_hand_with_joker_rule(a: &Hand, b: &Hand) -> Ordering { + let parse = HandOrder::from_hand_with_joker_rule; + rank(parse(a), parse(b), a, b, true) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn c(input: &str) -> Vec { + input.chars().collect() + } + + #[test] + fn it_can_parse_hands() { + let f = HandOrder::from_hand; + let j = HandOrder::from_hand_with_joker_rule; + assert_eq!(f(&c("22222")), FiveOfAKind); + assert_eq!(j(&c("22222")), FiveOfAKind); + assert_eq!(f(&c("22223")), FourOfAKind); + assert_eq!(j(&c("22223")), FourOfAKind); + assert_eq!(f(&c("22233")), FullHouse); + assert_eq!(j(&c("22233")), FullHouse); + assert_eq!(f(&c("22234")), ThreeOfAKind); + assert_eq!(j(&c("22234")), ThreeOfAKind); + assert_eq!(f(&c("22334")), TwoPair); + assert_eq!(j(&c("22334")), TwoPair); + assert_eq!(f(&c("22345")), OnePair); + assert_eq!(j(&c("22345")), OnePair); + assert_eq!(f(&c("23457")), HighCard); + assert_eq!(j(&c("23457")), HighCard); + } + + #[test] + fn it_can_parse_hands_with_joker_rule() { + let f = HandOrder::from_hand_with_joker_rule; + assert_eq!(f(&c("JJJJJ")), FiveOfAKind); + assert_eq!(f(&c("22JJJ")), FiveOfAKind); + assert_eq!(f(&c("222JJ")), FiveOfAKind); + assert_eq!(f(&c("2222J")), FiveOfAKind); + assert_eq!(f(&c("222J3")), FourOfAKind); + assert_eq!(f(&c("22JJ3")), FourOfAKind); + assert_eq!(f(&c("2JJJ3")), FourOfAKind); + assert_eq!(f(&c("22J33")), FullHouse); + assert_eq!(f(&c("22J34")), ThreeOfAKind); + assert_eq!(f(&c("2345J")), OnePair); + assert_eq!(f(&c("23457")), HighCard); + } + + #[test] + fn it_orders_equal_hands() { + let f = sort_hand; + assert_eq!(f(&c("22222"), &c("22222")), Equal); + assert_eq!(f(&c("22223"), &c("22224")), Less); + assert_eq!(f(&c("22224"), &c("22223")), Greater); + assert_eq!(f(&c("22233"), &c("22244")), Less); + assert_eq!(f(&c("22244"), &c("22233")), Greater); + assert_eq!(f(&c("22333"), &c("22444")), Less); + assert_eq!(f(&c("22444"), &c("22333")), Greater); + assert_eq!(f(&c("23333"), &c("24444")), Less); + assert_eq!(f(&c("24444"), &c("23333")), Greater); + } + + #[test] + fn it_orders_equal_hands_using_joker_rule() { + let f = sort_hand_with_joker_rule; + assert_eq!(f(&c("J3333"), &c("22222")), Less); + assert_eq!(f(&c("J2222"), &c("J2222")), Equal); + assert_eq!(f(&c("222J4"), &c("22223")), Less); + assert_eq!(f(&c("222J3"), &c("222J3")), Equal); + assert_eq!(f(&c("2J444"), &c("23333")), Less); + assert_eq!(f(&c("2J444"), &c("2J444")), Equal); + assert_eq!(f(&c("JKKK2"), &c("QQQQ2")), Less); + } + + #[test] + fn it_orders() { + let map = vec![ + c("22222"), // FiveOfAKind + c("23333"), // FourOfAKind + c("22333"), // FullHouse + c("22234"), // ThreeOfAKind + c("22334"), // TwoPair + c("22345"), // OnePair + c("23457"), // HighCard + ]; + + let inputs = map.clone(); + + for (i, input) in inputs.into_iter().enumerate() { + for (j, compare) in map.clone().into_iter().enumerate() { + assert_eq!(sort_hand(&input, &compare), j.cmp(&i)); + assert_eq!(sort_hand_with_joker_rule(&input, &compare), j.cmp(&i)); + } + } + } +} diff --git a/day_07/src/lib.rs b/day_07/src/lib.rs new file mode 100644 index 0000000..2690816 --- /dev/null +++ b/day_07/src/lib.rs @@ -0,0 +1,51 @@ +use std::cmp::Ordering; + +pub mod hand; +pub mod part_01; +pub mod part_02; + +pub type Hand = Vec; +type Input = Vec<(Hand, u128)>; + +pub fn parse_input(input: &str) -> Input { + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|line| { + let line_split = line.split(' ').collect::>(); + let cards = line_split[0].chars().collect(); + let bid = line_split[1].parse().unwrap(); + (cards, bid) + }) + .collect() +} + +pub fn sum(input: &Input, sort: Box Ordering>) -> u128 { + let mut input = input.to_owned(); + input.sort_by(|(a_cards, _), (b_cards, _)| sort(&a_cards, &b_cards)); + let iter = input.into_iter().enumerate(); + iter.map(|(i, (_, bid))| bid * ((i as u128) + 1)).sum() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + 32T3K 765 + T55J5 684 + "; + + #[test] + fn it_parses_input() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![ + (vec!['3', '2', 'T', '3', 'K'], 765), + (vec!['T', '5', '5', 'J', '5'], 684) + ] + ); + } +} diff --git a/day_07/src/main.rs b/day_07/src/main.rs new file mode 100644 index 0000000..6530793 --- /dev/null +++ b/day_07/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_07::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_07")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_07/src/part_01.rs b/day_07/src/part_01.rs new file mode 100644 index 0000000..9fd724c --- /dev/null +++ b/day_07/src/part_01.rs @@ -0,0 +1,22 @@ +use std::io::Result; + +use crate::{hand::sort_hand, sum, Input}; + +pub fn main(input: &Input) -> Result { + Ok(sum(input, Box::new(sort_hand))) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_07_example"); + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 6440); + Ok(()) + } +} diff --git a/day_07/src/part_02.rs b/day_07/src/part_02.rs new file mode 100644 index 0000000..b28a172 --- /dev/null +++ b/day_07/src/part_02.rs @@ -0,0 +1,22 @@ +use std::io::Result; + +use crate::{hand::sort_hand_with_joker_rule, sum, Input}; + +pub fn main(input: &Input) -> Result { + Ok(sum(input, Box::new(sort_hand_with_joker_rule))) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = include_str!("../../input/day_07_example"); + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 5905); + Ok(()) + } +} diff --git a/input/day_07 b/input/day_07 new file mode 100644 index 0000000..c9507dc --- /dev/null +++ b/input/day_07 @@ -0,0 +1,1000 @@ +32T3J 893 +A9942 54 +J57Q8 571 +779TK 931 +69696 457 +Q55Q5 478 +99399 735 +TA756 782 +QQQKQ 838 +QTJJA 995 +J7T7T 8 +22792 790 +588K8 376 +J77KT 191 +72T29 575 +58585 196 +58535 394 +263K5 566 +ATAAA 352 +7K7AK 477 +A3829 14 +22349 954 +KT647 244 +84848 208 +82A8A 77 +3A383 698 +97788 820 +QQ4Q7 991 +67Q66 837 +73QQ7 879 +Q4Q5Q 69 +Q5Q33 767 +585TT 52 +5K859 943 +238A3 170 +48887 668 +QAQQQ 591 +A8755 666 +4AK62 157 +4A9J4 350 +TJAAA 280 +AT9T9 592 +7Q497 390 +56748 681 +9425A 414 +5357J 737 +5QQQQ 597 +383QT 583 +TQTJT 81 +T36AQ 441 +899A9 485 +AKA22 667 +73K73 676 +782AJ 361 +TKKTK 608 +8J8JJ 317 +2J222 579 +TJ224 259 +6AK5Q 484 +Q5T9K 618 +JAKJ7 687 +KT9J2 348 +7775J 653 +9297A 798 +A72AA 642 +49749 794 +22282 733 +22Q8Q 810 +QK456 308 +853K9 568 +99A88 573 +88A8Q 20 +8Q47T 855 +39548 184 +7J478 92 +75755 710 +986T5 407 +8837K 999 +64J7A 345 +444K4 364 +39475 775 +J332A 413 +77JTJ 921 +47272 154 +K2T3A 318 +222A2 211 +7J695 935 +344QQ 683 +36Q26 728 +7QJ56 543 +25QTJ 555 +8Q88Q 928 +T5KTT 559 +A8T96 365 +44446 319 +A6A3J 880 +43KK3 948 +9J244 882 +6A66A 804 +88868 117 +J86T7 140 +969J9 18 +J2387 742 +79Q9Q 766 +83888 194 +43556 16 +8AA88 233 +Q2895 65 +QQ879 90 +TTT99 729 +64633 548 +A8K7A 884 +68A9A 429 +85A33 635 +J2A22 876 +49AAA 906 +25222 625 +TA33K 454 +99499 759 +78T4K 753 +76QQJ 89 +34TTT 445 +6K4J5 100 +4TQ82 114 +T7T75 28 +TJJTT 490 +4KKK6 716 +KKK5J 119 +642TJ 781 +227AJ 189 +89388 590 +865J3 553 +29962 997 +43Q7Q 736 +AJ792 351 +4Q4J6 572 +A6JA6 652 +74477 799 +KA889 593 +JJ56K 556 +AA787 867 +JAAJJ 35 +J4625 640 +52829 295 +97772 192 +Q79Q7 474 +8A2AJ 748 +525TJ 278 +69639 360 +T222T 58 +777J7 832 +QQJQQ 245 +66A63 725 +T7222 594 +955TT 430 +3373Q 703 +33KJ3 173 +9Q8TQ 122 +9KKK7 27 +88828 632 +AQTAT 524 +955J3 420 +77472 76 +T535K 585 +6K677 107 +TAJAT 85 +J64T5 646 +83QJ8 609 +88KKK 195 +TT8J8 897 +QTQTA 243 +78267 202 +3AKJK 32 +T8664 246 +84KK8 821 +JQQQT 178 +85Q88 288 +A93K8 23 +J2J24 582 +62622 498 +97A34 953 +6JK2J 149 +53322 788 +79A83 898 +73429 491 +33QTQ 201 +A9999 160 +2T26K 866 +JTJ39 770 +Q422T 881 +35333 833 +J4262 795 +6QJ8Q 289 +J69J9 908 +3433K 565 +226Q2 606 +JAKKK 786 +6Q22Q 258 +4454Q 421 +QAQ33 287 +KK68K 648 +3T4K2 3 +78526 489 +K66KK 980 +J99J9 56 +AA3JA 705 +A486T 33 +K4999 144 +6QA29 950 +69999 892 +KAKAK 480 +A5A23 675 +T5T6T 458 +TT5A5 774 +T7857 465 +A79QQ 532 +6T66T 388 +5556T 717 +4949A 695 +27227 650 +QA8J5 545 +44949 341 +76JA2 104 +J568K 511 +TK7A2 239 +63AK2 432 +67876 419 +J2J99 519 +JAAJA 680 +8TA7T 281 +85249 162 +89Q99 367 +6K664 526 +66J96 273 +QJ995 422 +K2KK2 521 +KJKKJ 322 +22A8A 584 +96A47 925 +9QJ52 50 +29J52 515 +4Q6J6 124 +2522Q 993 +7J922 596 +TT9T2 875 +49484 677 +A5KKA 500 +J9Q9Q 701 +TK228 577 +42224 911 +66K96 126 +4T7K2 241 +3Q267 706 +8J22K 111 +TTT55 481 +6555K 901 +55557 152 +859JJ 933 +Q44AA 408 +27J88 974 +44J44 947 +A42K8 777 +QQQ6Q 49 +AJ529 418 +TATTT 93 +A5AAA 562 +TTTT6 504 +AAA78 496 +T549J 7 +Q332Q 276 +T4434 382 +JTJTA 123 +A7A9A 678 +44JJ4 406 +T934K 130 +444TQ 358 +J64J4 917 +9Q9QA 981 +J33J3 549 +TK68J 29 +Q7956 186 +A5TAT 400 +KTKT3 529 +J7887 707 +6KA6K 383 +Q4Q6A 976 +76565 870 +8T9J8 578 +KAJKA 333 +AK4J9 926 +KQQJQ 190 +88588 914 +45T4J 621 +7Q777 796 +Q8Q3Q 803 +J7755 449 +65K74 626 +8787Q 847 +QJJ9J 904 +Q8Q8Q 227 +Q9J5K 567 +K2K9K 369 +T7T7T 145 +K3366 731 +AK3T7 79 +TQ328 397 +JQJJQ 297 +77799 886 +35J6J 745 +K6T78 890 +88AA5 638 +J3Q38 301 +36T74 392 +666QA 814 +43JQ8 613 +J7J79 570 +KQK55 768 +TT5TT 61 +49TA5 109 +2QQ2Q 336 +9J9AQ 561 +J8866 512 +A2JAQ 309 +KKTTT 518 +6J692 826 +8Q2A2 403 +9J899 990 +9966A 887 +55J25 620 +4TT66 125 +62JK8 68 +44766 342 +2A475 513 +KJKK4 385 +324J8 569 +66636 785 +438KQ 143 +54855 749 +93929 337 +58886 853 +9AT99 266 +59535 547 +99599 451 +7367Q 335 +534A9 409 +AAJQQ 758 +68K54 909 +JK259 859 +A3535 938 +J6446 969 +97999 659 +446A6 851 +48T9T 848 +K5T7A 412 +8J696 517 +JT664 306 +TQ6Q2 416 +66667 60 +T89AQ 235 +54J34 824 +64969 141 +73JKJ 427 +AAT3K 249 +T7T77 891 +8JA88 861 +74T57 514 +69A9K 937 +JJK7K 651 +AAQQQ 827 +622TJ 142 +72882 822 +44595 71 +KKKJ8 257 +6656T 151 +64725 78 +J65AA 193 +68744 920 +63943 399 +9KQQ9 685 +4K2A9 240 +A236A 282 +77575 442 +648JJ 463 +55655 520 +99K99 973 +9J9AA 699 +72K22 460 +JK47A 817 +3Q3Q3 713 +J4628 44 +Q88AQ 986 +J6668 375 +T85T8 988 +4T44T 506 +K9989 311 +48888 368 +7753K 952 +88666 761 +J8AJA 136 +74494 175 +KK373 657 +4KT4T 712 +9339J 99 +K52T7 47 +JJ8QQ 42 +32222 210 +62626 212 +65552 213 +47A82 260 +T4J6A 41 +345J3 82 +4799J 924 +JT5K9 181 +8JJ88 791 +T4672 476 +A675K 204 +7A499 354 +848JK 377 +3863K 787 +4929Q 183 +KKK78 872 +65KK9 958 +94355 434 +37T7Q 523 +338T6 447 +52JA5 39 +JAAKA 326 +8KKQ8 715 +5T333 533 +44T2J 131 +233K3 831 +935T6 689 +A64K8 929 +T7699 630 +2QJ66 669 +A2KJJ 922 +AT992 588 +93QTQ 251 +66668 440 +7KJTT 393 +8T674 269 +8AA32 324 +97AJ9 692 +3J335 563 +2A3K7 229 +QK7KQ 772 +QQAA9 834 +73232 916 +296Q7 464 +65J65 272 +KKK55 721 +66333 223 +QQQTA 663 +9AKAA 604 +888J8 261 +64646 605 +636J6 452 +6J665 873 +94TTT 507 +KT9K9 616 +K2K53 426 +4T944 751 +43423 809 +889T8 509 +3AKQJ 655 +QQQ98 146 +9Q444 359 +993J4 59 +22K26 75 +T58K2 118 +5T55Q 808 +6A4Q4 492 +T4JT9 516 +TJ8A5 552 +3AQ59 4 +297K3 915 +7Q288 815 +44KJK 726 +8J496 531 +88A3A 979 +2AQ76 21 +K5K8K 248 +2373Q 603 +Q5666 660 +Q2424 472 +J2422 828 +TT7TT 304 +KJKQ7 960 +2AT68 24 +T66Q6 899 +A9852 225 +TT2TT 852 +75975 720 +73AT3 671 +J7773 155 +52225 15 +ATJKT 299 +6KKKJ 168 +667J4 732 +44J47 188 +77Q78 43 +Q63K8 741 +4T4A4 939 +Q7Q77 905 +KKK9K 294 +KAJ4K 330 +977TJ 842 +56JKQ 300 +67K5T 26 +J944K 468 +Q3Q3Q 187 +777AQ 975 +42Q7J 264 +J5KQ5 994 +9J42A 670 +8A4QA 525 +7TTT6 877 +K8KKK 497 +67T62 483 +6222A 197 +Q4584 381 +5595J 714 +3T6T6 987 +QQTQT 64 +KKTKK 495 +QKAA9 863 +KKKKQ 200 +3QAJ8 989 +222T5 797 +T9Q73 595 +TKK7T 945 +76767 871 +3837K 674 +99Q96 40 +JJ296 762 +KJ3KQ 98 +Q73A7 747 +5Q757 105 +447JT 373 +28298 356 +KK3KT 755 +266TT 992 +Q5JQ5 840 +KK59Q 538 +4A94Q 113 +T4TTT 268 +AAAJA 158 +T3T88 252 +JKKKK 690 +2339T 153 +QQ9Q9 718 +AA2AA 586 +666AK 602 +8Q55Q 743 +J4J4K 740 +9AK2J 600 +TTTT9 256 +94989 128 +K3KKK 387 +6K6TK 540 +3Q333 72 +QKQKQ 346 +Q7A8K 1 +2T222 679 +6AJJJ 378 +45A55 316 +TTT44 622 +A42K3 315 +49QTK 711 +4562Q 462 +58K27 818 +6Q8A8 539 +KJQQK 293 +Q28TK 946 +K8J49 220 +3333T 17 +T774K 48 +883J2 94 +5QQQ5 433 +T9325 971 +55855 998 +55K5K 96 +3T32K 167 +92229 226 +33KJ7 972 +Q7A3T 868 +6JQ6Q 177 +Q2Q55 756 +68K6A 581 +5555A 286 +85QQ6 185 +TTTTJ 934 +2JJ64 91 +46QQ6 254 +626J8 30 +43444 471 +TKQAK 550 +8KK5J 277 +A46K4 371 +48A8A 587 +88842 541 +JK9J9 405 +T225T 944 +TTTAJ 349 +J7629 320 +2K44K 843 +59857 754 +KKJ54 849 +T85QJ 36 +8T888 574 +99333 968 +44258 907 +9A37J 355 +QAAA2 53 +TT3TT 805 +6T696 878 +74A23 482 +6KJTT 784 +42QAT 488 +J874J 792 +2Q8JK 654 +89K87 967 +QQQQT 615 +A4A49 475 +QQJJK 885 +JAAA8 508 +AKA4K 384 +82T88 12 +99A55 658 +33338 614 +A93JA 214 +K386T 978 +4443J 428 +3KTT3 910 +22T62 888 +82637 487 +3T344 850 +J4424 874 +J734Q 302 +KJ4Q8 395 +JA9J5 636 +2AT5J 263 +9AK4Q 9 +KTTTT 174 +336J6 896 +JJJ8J 836 +Q7K25 530 +52AQT 672 +A6886 179 +6JJ66 148 +QJJQQ 165 +85685 291 +7QQJ7 31 +7KK87 171 +45534 112 +K55K2 106 +6A666 764 +38AA3 956 +KKAJT 940 +55578 846 +333J3 984 +3Q3J5 313 +8946K 13 +93J43 236 +8T9A3 869 +4K48K 823 +J222J 325 +TTJ99 279 +664Q6 628 +6QQQ6 87 +QJQQ8 2 +2TT2T 132 +65Q5K 607 +3K3KK 637 +777JJ 224 +9T84J 912 +99JA9 461 +5K332 923 +A57Q4 839 +46J59 844 +3768T 793 +JQ966 389 +KKK7A 723 +J4QKJ 645 +TQJ7A 110 +QJ2Q6 647 +22894 665 +22363 502 +KAKKK 682 +5KKKA 265 +K35K5 734 +Q6666 470 +T24TT 996 +8JQQT 163 +A6934 232 +98AA4 343 +KJJ77 332 +5TQ5J 697 +38Q89 627 +QQJ56 247 +T28T2 486 +55JQ5 551 +836Q9 629 +A88Q7 6 +52555 62 +84548 230 +9KKK9 37 +AA2AJ 789 +Q2222 854 +77K7K 900 +T8QT6 505 +95393 292 +783JQ 902 +8888A 97 +99779 86 +KKJJJ 961 +87558 951 +58T32 238 +QJ29J 528 +A7KJ3 323 +5T38K 860 +AKKK8 121 +J5589 366 +J99JJ 580 +J7QJK 829 +K7847 180 +44TQQ 825 +76266 760 +23Q6T 895 +73Q87 234 +A6282 328 +QQ9Q2 417 +J5224 599 +T66TT 215 +35555 862 +77787 536 +7TQ2J 228 +T5JTQ 431 +K24Q9 816 +53KKK 410 +Q666Q 402 +6K67K 334 +T8TTT 962 +66664 147 +AKAKA 446 +J844A 773 +2JQQ2 510 +92Q99 73 +44Q4Q 129 +42K9K 479 +66Q34 290 +33322 783 +A6AAA 401 +2JQJ4 927 +J5558 218 +3Q343 303 +995J5 779 +TJJTJ 70 +557K5 466 +86656 134 +3AQQK 275 +65J3A 339 +A584T 558 +A9A99 644 +J5J74 641 +3A9JK 127 +67KQJ 469 +Q3QKK 631 +88A7J 830 +JJK67 115 +47AKQ 231 +77677 456 +2T3T6 25 +K24TQ 19 +AQ3AA 46 +92289 57 +66565 95 +345JQ 894 +T7JA8 250 +QQJ88 274 +2QJ2A 253 +77737 913 +KK22J 271 +J66JJ 270 +K9QJ9 380 +75KK7 172 +QQAJQ 769 +33J88 22 +AJ88A 780 +T98Q8 473 +88KK8 363 +36T97 216 +777KA 771 +QT77K 883 +K5KT5 738 +73QJ9 396 +7A59J 296 +J7876 493 +J977A 467 +999J9 903 +6KA95 802 +QA3KK 298 +88558 448 +JA8KT 88 +3A73J 307 +288QA 966 +9Q563 845 +JTAA8 576 +8AJ37 746 +5AQ28 435 +8473Q 964 +JAA5T 423 +QTQTT 918 +44547 284 +66QJ5 776 +65556 340 +3AA33 221 +7975J 398 +54Q94 858 +5666K 83 +2A3J6 169 +5J554 763 +AQ9A4 865 +5468J 812 +QQ7QQ 598 +Q9999 807 +Q2QQQ 459 +46AAA 34 +A788A 702 +JTTK8 453 +5AJ7T 164 +A38K6 159 +QJ5QQ 750 +J54KT 116 +JTKKK 688 +499KK 63 +A5538 404 +6TAA3 242 +Q9J43 379 +6A2JQ 535 +Q8882 643 +535J3 686 +666J6 813 +93963 857 +5TA42 353 +99J53 205 +JQ7K3 222 +92A66 1000 +K5QT5 634 +5AA6A 806 +KKTAK 206 +5242T 708 +26K26 344 +JK8T8 841 +39A92 357 +726J7 719 +33QT7 5 +Q3J6T 554 +9K69T 66 +JJJJJ 503 +866AA 601 +K96A4 267 +T222J 45 +AJQAA 744 +7K7J7 370 +JJJ34 499 +77TTK 161 +98T35 347 +3J8J3 949 +92J2T 941 +2JJJ2 957 +87QQ2 557 +67646 649 +T5T99 166 +6Q26Q 765 +4J538 437 +TAQ67 103 +8TT8T 662 +7A77A 120 +5555J 176 +449J9 709 +JK98Q 811 +K8JKJ 930 +25AA2 752 +222QQ 321 +22K22 386 +76J55 362 +93577 656 +555JJ 589 +42Q54 684 +Q4QQQ 305 +3993A 391 +3JQ2K 544 +844J4 285 +AA222 314 +38467 970 +4Q6T8 331 +J4Q76 101 +33J64 450 +7A326 137 +9977K 10 +9JJK2 564 +2JKA8 739 +9QA72 694 +3K6KJ 527 +J6Q7J 444 +67AJQ 135 +99898 411 +79823 936 +5K643 237 +J62J6 696 +26T26 522 +K8AK3 415 +35553 864 +J23T2 374 +5555K 800 +JT266 977 +77772 639 +8822K 610 +KJQ9Q 835 +A9A9A 338 +J36T6 424 +2586Q 494 +73337 74 +A8AAA 438 +K66J6 199 +87J58 310 +5T4KT 133 +28768 329 +AA3A3 11 +77J27 80 +44442 691 +525KA 198 +86T5A 436 +5K55J 84 +33343 624 +3A733 965 +83523 67 +66896 982 +63JQ4 963 +QQT88 38 +27J42 150 +828T5 439 +3444A 955 +QTQQ8 55 +K839J 217 +64697 156 +43393 546 +K7777 51 +2QKJ2 704 +2A2AA 312 +AA5T3 534 +55544 139 +6572K 255 +6KJK6 425 +T987K 778 +3TA5Q 611 +QTT55 219 +86TTK 664 +TT344 207 +77577 327 +KJK3K 262 +J68J5 730 +43555 727 +TTK2A 537 +K58QA 919 +KAQ97 102 +A444J 443 +4J969 889 +9TTT3 283 +AT399 983 +KKJ98 661 +K22J2 501 +467J7 673 +QJTQJ 985 +787TK 932 +556A5 617 +6JT4T 372 +97343 724 +Q693Q 942 +999TJ 182 +TT9JQ 856 +88Q42 757 +633J3 560 +Q6295 722 +KK6J8 959 +QTTA8 693 +A8688 542 +82434 819 +6T6A9 623 +78J88 138 +83Q34 209 +J8847 619 +6QJ22 633 +59995 203 +99K7K 801 +A3343 455 +84982 108 +T42JT 700 +38383 612 diff --git a/input/day_07_example b/input/day_07_example new file mode 100644 index 0000000..e3500c3 --- /dev/null +++ b/input/day_07_example @@ -0,0 +1,5 @@ +32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483 From 208a29b7e4db96b8c52da1e17a8aa15d1664cdfe Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sun, 10 Dec 2023 08:39:57 +0100 Subject: [PATCH 14/15] feat: completed day 8 (naive approach) --- README.md | 2 +- day_08/Cargo.toml | 11 + day_08/src/lib.rs | 105 +++++++ day_08/src/main.rs | 12 + day_08/src/part_01.rs | 61 ++++ day_08/src/part_02.rs | 84 +++++ input/day_08 | 716 ++++++++++++++++++++++++++++++++++++++++++ input/day_08_example | 9 + 8 files changed, 999 insertions(+), 1 deletion(-) create mode 100644 day_08/Cargo.toml create mode 100644 day_08/src/lib.rs create mode 100644 day_08/src/main.rs create mode 100644 day_08/src/part_01.rs create mode 100644 day_08/src/part_02.rs create mode 100644 input/day_08 create mode 100644 input/day_08_example diff --git a/README.md b/README.md index 05c0244..55325de 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - [Day 5](https://github.com/ankjevel/adventofcode/tree/2023/day_05) ⭐️ ⭐️ - [Day 6](https://github.com/ankjevel/adventofcode/tree/2023/day_06) ⭐️ ⭐️ - [Day 7](https://github.com/ankjevel/adventofcode/tree/2023/day_07) ⭐️ ⭐️ -- [Day 8](#) +- [Day 8](https://github.com/ankjevel/adventofcode/tree/2023/day_08) ⭐️ ⭐️ - [Day 9](#) - [Day 10](#) - [Day 11](#) diff --git a/day_08/Cargo.toml b/day_08/Cargo.toml new file mode 100644 index 0000000..ed7dc40 --- /dev/null +++ b/day_08/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "day_08" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_08/src/lib.rs b/day_08/src/lib.rs new file mode 100644 index 0000000..d79b743 --- /dev/null +++ b/day_08/src/lib.rs @@ -0,0 +1,105 @@ +use std::{cell::RefCell, collections::HashMap}; + +pub mod part_01; +pub mod part_02; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub enum Direction { + Left = 0, + Right, +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Input { + moves: Vec, + nodes: RefCell>, + indices: HashMap, +} + +pub fn parse_input(input: &str) -> Input { + let rows = input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .filter(|s| !s.is_empty()) + .map(|s| s.to_owned()) + .collect::>(); + + let header = rows.get(0).unwrap(); + + let moves = header + .chars() + .map(|c| match c { + 'L' => Direction::Left, + 'R' => Direction::Right, + _ => panic!("wrong direction?! {c}"), + }) + .collect::>(); + + let nodes = rows + .into_iter() + .skip(1) + .map(|row| { + let split = row.split('=').collect::>(); + let index = split[0].trim().to_owned(); + let nodes = split[1].trim().to_owned(); + let nodes = nodes.replace('(', ""); + let nodes = nodes.replace(')', ""); + + let nodes = nodes.split(", ").collect::>(); + let left = nodes[0].to_owned(); + let right = nodes[1].to_owned(); + (index, (left, right)) + }) + .collect::>(); + + let indices = HashMap::from_iter( + nodes + .clone() + .into_iter() + .enumerate() + .map(|(index, n)| (n.0.to_owned(), index)), + ); + + Input { + moves, + nodes: RefCell::new(nodes), + indices, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use Direction::*; + + const EXAMPLE_DATA: &'static str = " + LLR + + AAA = (BBB, BBB) + BBB = (AAA, ZZZ) + ZZZ = (ZZZ, ZZZ) + "; + + #[test] + fn it_parses_input() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + Input { + moves: vec![Left, Left, Right], + nodes: RefCell::new(vec![ + ("AAA".to_owned(), ("BBB".to_owned(), "BBB".to_owned())), + ("BBB".to_owned(), ("AAA".to_owned(), "ZZZ".to_owned())), + ("ZZZ".to_owned(), ("ZZZ".to_owned(), "ZZZ".to_owned())), + ]), + indices: HashMap::from([ + ("AAA".to_owned(), 0), + ("BBB".to_owned(), 1), + ("ZZZ".to_owned(), 2), + ]), + } + ); + } +} diff --git a/day_08/src/main.rs b/day_08/src/main.rs new file mode 100644 index 0000000..ffd55f5 --- /dev/null +++ b/day_08/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_08::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_08")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_08/src/part_01.rs b/day_08/src/part_01.rs new file mode 100644 index 0000000..746418c --- /dev/null +++ b/day_08/src/part_01.rs @@ -0,0 +1,61 @@ +use std::io::Result; + +use crate::{Direction::*, Input}; + +pub fn main(input: &Input) -> Result { + let nodes = input.nodes.to_owned(); + let index_of_aaa = *input.indices.get("AAA").unwrap(); + let mut current = nodes.borrow().get(index_of_aaa).unwrap().to_owned(); + + let mut steps = 0; + 'main: loop { + for direction in input.moves.clone() { + steps += 1; + + let next_index = match direction { + Left => &*current.1 .0, + Right => &*current.1 .1, + }; + + let next_index = *input.indices.get(next_index).unwrap(); + let next = nodes.borrow().get(next_index).unwrap().to_owned(); + + if next.0 == "ZZZ" { + break 'main; + } + + current = next; + } + } + + Ok(steps) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + LLR + + AAA = (BBB, BBB) + BBB = (AAA, ZZZ) + ZZZ = (ZZZ, ZZZ) + "; + + const LONGER_EXAMPLE_DATA: &'static str = include_str!("../../input/day_08_example"); + + #[test] + fn it_gets_the_short_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 6); + Ok(()) + } + + #[test] + fn it_gets_the_longer_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&LONGER_EXAMPLE_DATA))?, 2); + Ok(()) + } +} diff --git a/day_08/src/part_02.rs b/day_08/src/part_02.rs new file mode 100644 index 0000000..742a194 --- /dev/null +++ b/day_08/src/part_02.rs @@ -0,0 +1,84 @@ +use std::{cell::RefCell, io::Result}; + +use crate::{Direction::*, Input}; + +pub fn main(input: &Input) -> Result { + let nodes = input.nodes.to_owned(); + let all_indices = input + .indices + .keys() + .into_iter() + .filter(|k| k.ends_with('A')) + .map(|k| *input.indices.get(k).unwrap()) + .collect::>(); + + let ends_to_find = all_indices.len(); + + let all_currents = RefCell::new( + all_indices + .clone() + .into_iter() + .map(|i| nodes.borrow().get(i).unwrap().to_owned()) + .collect::>(), + ); + + let mut steps = 0; + 'main: loop { + for direction in input.moves.clone() { + steps += 1; + + let found_ends = + all_currents + .borrow_mut() + .iter_mut() + .fold(0, |mut completed, current| { + let next_index = match direction { + Left => &*current.1 .0, + Right => &*current.1 .1, + }; + let next_index = *input.indices.get(next_index).unwrap(); + let next = nodes.borrow().get(next_index).unwrap().to_owned(); + + *current = next; + + if current.0.ends_with('Z') { + completed += 1; + } + + completed + }); + + if found_ends == ends_to_find { + break 'main; + } + } + } + + Ok(steps) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + LR + + 11A = (11B, XXX) + 11B = (XXX, 11Z) + 11Z = (11B, XXX) + 22A = (22B, XXX) + 22B = (22C, 22C) + 22C = (22Z, 22Z) + 22Z = (22B, 22B) + XXX = (XXX, XXX) + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 6); + Ok(()) + } +} diff --git a/input/day_08 b/input/day_08 new file mode 100644 index 0000000..d95e19b --- /dev/null +++ b/input/day_08 @@ -0,0 +1,716 @@ +LRRLRRLLRRRLRRLRLRRRLRRLRRRLRLLRRRLRRRLRLRRRLRRLRRLRLRLLLRRRLRRRLRRLRRLRLRRRLRRLLRRLRRLRLLRLRLRRLRLLRLRLRRRLRRLRLLRLRLLRRLRLRRLLLRLRRLRRRLLLRRLRLRRRLLRRLLLRRRLRRRLLLRRLLRLRRLRLRRLLLRLRRLLLLRRLLRRRLRRLRRLRLRLLRLRRRLLRRLLRRLRRLRRLRRLRLLRRLRRRLRLRLLLRRRLLRRRLRRLRRLLLLRRRR + +VRN = (CSM, GPD) +XDT = (QBK, PJR) +HVC = (MKM, TJS) +KRH = (BHN, PXB) +GTX = (VFD, TXR) +BQB = (MQV, PFQ) +TDV = (VSG, MJX) +VJM = (QHP, XMB) +KLG = (QLJ, HCV) +TSM = (JPG, DNP) +KHS = (QNR, DXJ) +FXM = (PHF, PHF) +RMV = (BMM, KHS) +QXL = (BKG, TLP) +MHS = (QXL, CFQ) +TBT = (TVT, BRD) +QXS = (GPP, RND) +XLL = (JHQ, LDV) +PBQ = (VXK, RJR) +FXB = (HMN, THX) +DPF = (GLX, GNC) +HVG = (HJF, SCH) +QMN = (DQJ, GMN) +MBS = (PFX, JHG) +VGL = (FHX, CCK) +QLV = (BLT, FDR) +MNS = (BDB, BMJ) +MMT = (TSM, SFR) +NRP = (FKB, QPH) +XFQ = (GTS, CCQ) +XFF = (HKG, NVL) +TXR = (VHQ, CKP) +VPL = (GHC, VMT) +SGC = (BGM, MJV) +SMS = (GJV, LSC) +BVC = (PRH, FJJ) +JHQ = (VJQ, XTX) +RJF = (GKK, NKX) +RJR = (JJQ, JTK) +VHQ = (JPS, JPS) +SFR = (JPG, DNP) +RDP = (GPD, CSM) +GGQ = (MVX, XBP) +XGK = (GBB, VPH) +XBH = (PTH, GSL) +MKG = (RBP, MGG) +FSG = (KFC, RMB) +NHC = (RQG, CVF) +SSV = (MVF, QFP) +RLP = (FXC, HCP) +FBZ = (FJF, CRJ) +HFL = (FKB, QPH) +NBM = (QBD, XGK) +NSP = (QKG, NDN) +TQF = (RMH, TDB) +RBB = (QTC, HPT) +CVF = (BSV, VTR) +XQG = (LFJ, JXD) +KXF = (CRJ, FJF) +MQC = (MQL, BSL) +RCC = (NHC, QLC) +TNT = (VKQ, NSX) +TVN = (NDN, QKG) +SCH = (FXB, NTM) +CVD = (XLJ, XDT) +QVF = (MQR, RDB) +GXS = (HCK, SHD) +KBD = (JHG, PFX) +KXV = (HVQ, QCJ) +MQR = (RNQ, SVC) +CBC = (SXB, BVC) +GKK = (HDR, LTF) +FRT = (CFQ, QXL) +VMT = (SQQ, DBB) +QPV = (NVQ, VVV) +XGD = (MPL, PMQ) +MNT = (JDD, NQC) +RGT = (QHD, SQP) +HXV = (RQD, RRX) +LGR = (FHX, CCK) +PHF = (CBV, TMC) +BTP = (JSC, QMN) +CXS = (SCH, HJF) +MQV = (FGJ, QPD) +HRZ = (CCX, BCF) +DDB = (DJK, CVD) +XNL = (QLJ, HCV) +GLX = (MGN, CFV) +QCJ = (NDK, XBR) +XBX = (CQM, VKD) +QPH = (XBH, CFG) +NSL = (LFJ, JXD) +DJQ = (VMN, STS) +RNV = (XFF, KSR) +JTR = (QLG, XRD) +VVV = (MDH, KHK) +TLR = (DMH, NXC) +GBH = (GLS, XLK) +LRN = (QRM, FMC) +BRM = (DCK, DSK) +GQS = (FXV, QPV) +XMT = (NHF, RXG) +KLR = (HFN, KDR) +SVH = (RMR, JLC) +QSG = (KQC, CMH) +VVX = (XGM, FNX) +BCF = (LNR, MTS) +RPJ = (RCC, CKG) +JJD = (NRP, HFL) +FGP = (MFS, RBB) +BJX = (BTD, KSC) +PQC = (VMN, STS) +JPG = (RQL, CSQ) +SLK = (JJC, BPJ) +JTK = (XHN, BFH) +HCV = (BRF, VPT) +VPH = (BVS, RRT) +MSH = (JQF, XFJ) +JHL = (QPF, QPF) +FGD = (MPC, TVB) +DCB = (JBC, XFB) +RKN = (TSL, VQG) +QBQ = (QLV, NRT) +MQX = (JSX, MCV) +XHX = (FNX, XGM) +DTL = (JDM, BDX) +LST = (JHL, CQN) +VGR = (HQV, HRZ) +CTQ = (XCF, TJK) +SQP = (HHB, NBM) +RRL = (TSL, VQG) +NNM = (DDS, XSF) +KHK = (HSG, NDF) +MGN = (VXT, VGV) +TLB = (DPK, MLV) +NMM = (TRR, XJH) +SJG = (XRJ, XBX) +GSC = (RRL, RKN) +KDR = (PSJ, TBM) +LRP = (SQP, QHD) +DKF = (MVR, QQB) +FHC = (BPT, BKC) +PLT = (JGG, GGQ) +XRD = (HMH, PBQ) +RHP = (JPD, XVL) +MXF = (MQR, RDB) +DML = (BRD, TVT) +CKP = (JPS, RBS) +BFK = (BCL, TLK) +KSR = (NVL, HKG) +XQF = (BMM, KHS) +BVJ = (CBL, PLC) +BCS = (RQF, GTB) +JCC = (VTT, VPQ) +BCL = (BNV, PDB) +HCK = (DDB, CBX) +SVG = (FGD, VQL) +BLT = (XNK, LHM) +MVQ = (LRG, NPB) +RND = (QCK, QRN) +HFR = (FBT, CLS) +KNK = (XDS, BCK) +XBB = (BRM, FDC) +XGM = (FFC, GJX) +PTN = (TQF, QKP) +RNQ = (GSC, XKH) +TPT = (CMK, KXT) +HPL = (CKB, QLR) +MVX = (QSG, TKJ) +QFP = (RLP, RSF) +GXF = (LNB, VNJ) +QHD = (NBM, HHB) +KSS = (LLH, PFK) +MFD = (XGJ, GGS) +JJQ = (XHN, BFH) +JDD = (HXV, FVH) +CFR = (FXM, FXM) +FDR = (XNK, LHM) +DHG = (BRS, TLR) +TDB = (SBS, XBB) +BVS = (GQH, BFS) +DGZ = (TMC, CBV) +HQD = (JSC, QMN) +TMZ = (XFB, JBC) +BRF = (MBF, KGJ) +RQG = (VTR, BSV) +CSQ = (FTS, RHR) +FFJ = (QCL, CTQ) +XKH = (RRL, RKN) +VJQ = (BTL, GQS) +NGK = (JGG, GGQ) +RQN = (GVG, SJN) +GRR = (RNV, FHF) +LSR = (VBL, SLK) +VQG = (QBV, NJV) +BHK = (BVJ, LJR) +BRS = (DMH, NXC) +NKX = (LTF, HDR) +QBD = (GBB, VPH) +RGQ = (CLM, FHK) +VXX = (KRH, VGN) +KRF = (VFQ, SMN) +XFR = (XGD, RMM) +JGC = (JGM, PFR) +SHD = (DDB, CBX) +QKG = (QTS, GXP) +CDL = (GLX, GNC) +RBP = (HKK, CMN) +CLN = (HDG, BHM) +XFX = (RNV, FHF) +AAA = (DXX, SVG) +FXC = (MMT, KLS) +PTF = (SKF, SDF) +QPF = (RQF, RQF) +DTB = (HQV, HQV) +RRF = (FNV, MKG) +MGG = (HKK, CMN) +NDF = (KMD, FFH) +DVL = (RKL, CLC) +JRQ = (BXV, BRB) +HVM = (DRT, SSV) +BHM = (KNK, BPP) +TDT = (DPS, VCP) +JPC = (DCH, FHD) +RSF = (FXC, HCP) +KSX = (BVJ, LJR) +NLT = (LTB, QBQ) +SBD = (NKX, GKK) +VQL = (MPC, TVB) +JXD = (MLK, LHN) +XNN = (TNT, LPQ) +HMH = (RJR, VXK) +JGG = (MVX, XBP) +PTP = (MVQ, NPS) +FQC = (XQF, RMV) +FXT = (TDH, JGD) +RGP = (CPN, KGF) +BTD = (NKN, NTQ) +BGM = (NRJ, CJN) +RMM = (PMQ, MPL) +DQJ = (DPF, CDL) +TVT = (TFJ, VJM) +TGL = (CGS, SJG) +NHF = (HVC, LVM) +NMK = (GRR, XFX) +QRN = (MMP, XSL) +FFH = (DJQ, PQC) +PDJ = (NQC, JDD) +CGS = (XBX, XRJ) +NTQ = (RXX, FLX) +CFB = (TVC, PTP) +GXK = (FLC, GXS) +RBS = (DCB, TMZ) +CFQ = (TLP, BKG) +QHP = (KVX, GSG) +HDJ = (RPH, QPK) +VXT = (GXF, JKM) +NCM = (NJL, TGL) +HFN = (TBM, PSJ) +JGF = (CFB, NMC) +JDM = (CLN, PHJ) +DJS = (RMR, JLC) +RDB = (RNQ, SVC) +RRX = (HQD, BTP) +HHS = (MXF, QVF) +MMP = (GTX, HKC) +JRK = (KKP, PTN) +TRR = (VGD, HGC) +JKM = (LNB, VNJ) +BRD = (TFJ, VJM) +TLK = (BNV, PDB) +QTS = (CHL, LSR) +BPP = (XDS, BCK) +RXX = (BKP, TLS) +VFD = (VHQ, VHQ) +BQF = (DHG, DQC) +XDL = (QVF, MXF) +ZZZ = (SVG, DXX) +GGB = (LTB, QBQ) +QTN = (JHL, CQN) +BVK = (BHQ, SSG) +VGB = (BXV, BRB) +QLR = (XFR, KNM) +QBK = (KNC, VJX) +BMJ = (JLK, TDV) +FDC = (DSK, DCK) +XDS = (SMS, TVR) +GBC = (NGK, PLT) +HPT = (XHQ, SXT) +FHK = (JCC, FJS) +KNM = (RMM, XGD) +GTB = (KLR, JVZ) +VCT = (PTN, KKP) +LDV = (XTX, VJQ) +KTK = (MQV, PFQ) +TVC = (MVQ, NPS) +BXV = (XLL, KDL) +BFA = (BCF, CCX) +CCF = (CLM, FHK) +JGS = (VCP, DPS) +RVK = (MKJ, QLH) +FNX = (FFC, GJX) +XBP = (QSG, TKJ) +LMJ = (FPH, JVL) +HKG = (VXX, RNX) +KMD = (DJQ, PQC) +BRB = (XLL, KDL) +NDK = (DJS, SVH) +BSV = (XVC, QXS) +TLS = (FSJ, MNS) +CRJ = (SGC, RNH) +BNV = (QSF, FSG) +SMN = (MNK, QGN) +JVV = (TRN, DVL) +JJC = (HBT, RPJ) +KSB = (XVL, JPD) +JJT = (TLB, DTJ) +TSL = (QBV, NJV) +GNV = (GCS, JLR) +HJF = (FXB, NTM) +VMN = (CFR, CFR) +GVG = (XDL, HHS) +LNB = (HMP, XMP) +RTL = (FXT, RCT) +XVC = (RND, GPP) +GNS = (MFS, RBB) +XTG = (LRM, DKQ) +JVZ = (KDR, HFN) +QQB = (XTG, GFG) +HMN = (FHC, FMN) +XLC = (FXT, RCT) +CKG = (QLC, NHC) +NJB = (DVL, TRN) +VCP = (LRF, HPL) +KQJ = (MXH, MXH) +GJV = (TPT, QNJ) +CCX = (LNR, MTS) +DTJ = (MLV, DPK) +HDX = (JSX, MCV) +XRJ = (VKD, CQM) +DMH = (RGP, KPR) +QGN = (VRF, QDL) +JSC = (DQJ, GMN) +BPT = (LLB, XNN) +CJN = (JJT, PGM) +XDV = (XVD, HJH) +JHG = (LLF, MLJ) +NKN = (FLX, RXX) +TGP = (XJH, TRR) +TLP = (XLC, RTL) +HQT = (SSN, BPS) +BKP = (MNS, FSJ) +VPT = (KGJ, MBF) +JSJ = (PFK, LLH) +CBX = (DJK, CVD) +TJK = (VGL, LGR) +THP = (TGL, NJL) +NSX = (SQL, GNV) +JQF = (VDM, JJD) +SDF = (JPC, KRV) +XNK = (HDJ, MQJ) +DSD = (SJN, GVG) +SBS = (FDC, BRM) +FPH = (NCM, THP) +LMR = (KQJ, BGV) +VKQ = (SQL, GNV) +FMC = (CPV, GBC) +HHB = (QBD, XGK) +HVQ = (NDK, XBR) +KXT = (JKG, NLS) +GJX = (TVP, JTR) +JLT = (MHM, MSH) +VNJ = (HMP, XMP) +TDH = (JGF, GGD) +VPQ = (HDX, MQX) +SKF = (JPC, KRV) +NXT = (RXG, NHF) +TRN = (CLC, RKL) +RRT = (BFS, GQH) +JFJ = (SXB, BVC) +KBR = (CGD, JGC) +PGG = (HFR, TJJ) +CMK = (JKG, NLS) +CBV = (FCX, KBR) +GMN = (DPF, CDL) +BTL = (QPV, FXV) +PFX = (MLJ, LLF) +DCT = (XLK, GLS) +PGM = (DTJ, TLB) +NVL = (VXX, RNX) +RPD = (TLK, BCL) +QDL = (FFJ, SFH) +JVL = (THP, NCM) +NVQ = (MDH, KHK) +TMC = (FCX, KBR) +LPQ = (NSX, VKQ) +RHR = (PPL, LCD) +JSX = (DHM, PMS) +DPK = (KBD, MBS) +LTF = (PGG, QPN) +BPS = (GXK, HCM) +HKC = (VFD, TXR) +PXB = (RPN, BQF) +FLX = (TLS, BKP) +PJR = (VJX, KNC) +PDB = (QSF, FSG) +NMC = (TVC, PTP) +MLP = (MSH, MHM) +PMQ = (KXV, QFJ) +FKB = (XBH, CFG) +JRV = (SMN, VFQ) +SQL = (GCS, JLR) +DXX = (FGD, VQL) +XMM = (GMG, LRN) +PFK = (HDL, XFQ) +XSL = (GTX, HKC) +CLC = (JRQ, VGB) +LMH = (KXF, FBZ) +NMX = (KSC, BTD) +GTJ = (KLG, XNL) +THX = (FMN, FHC) +RMH = (XBB, SBS) +SSG = (MFD, KFP) +QNR = (RBG, HNG) +DSK = (LMJ, FRP) +GQP = (FXM, XVT) +MFS = (QTC, HPT) +VHX = (VBD, RDD) +SQS = (GTJ, MSR) +LJR = (CBL, PLC) +QSF = (KFC, KFC) +DHM = (VFJ, DTL) +MDH = (NDF, HSG) +SSN = (HCM, GXK) +HCM = (FLC, GXS) +PQX = (FGG, BXR) +DQC = (BRS, TLR) +KRV = (DCH, FHD) +FNH = (SKF, SDF) +LFJ = (LHN, MLK) +MVR = (GFG, XTG) +HBT = (RCC, CKG) +VKD = (NMK, PFV) +TKJ = (KQC, CMH) +MRH = (RHV, ZZZ) +CKB = (XFR, KNM) +BMM = (DXJ, QNR) +KDL = (JHQ, LDV) +VFJ = (BDX, JDM) +PLC = (PND, GNP) +MNK = (QDL, VRF) +MXT = (RMV, XQF) +TJS = (PTF, FNH) +PHJ = (BHM, HDG) +FSJ = (BDB, BMJ) +CFV = (VGV, VXT) +XVD = (CBF, SQS) +TFJ = (QHP, XMB) +CPV = (PLT, NGK) +VTR = (XVC, QXS) +BKC = (XNN, LLB) +NPS = (NPB, LRG) +FXV = (NVQ, VVV) +FTS = (PPL, LCD) +HMP = (QMD, SNX) +VBD = (DTB, DTB) +VFQ = (QGN, MNK) +KFP = (GGS, XGJ) +BHN = (RPN, BQF) +PFV = (GRR, XFX) +QMD = (MGK, XDV) +MLK = (RPD, BFK) +GFG = (DKQ, LRM) +NLS = (BQB, KTK) +XLK = (HVG, CXS) +LSC = (QNJ, TPT) +FNV = (RBP, MGG) +HKK = (RCX, MQC) +GPD = (NSL, XQG) +RQL = (RHR, FTS) +HDB = (GHC, VMT) +HJH = (SQS, CBF) +QTC = (SXT, XHQ) +TRS = (BXR, FGG) +MTS = (DML, TBT) +RHV = (DXX, SVG) +XVL = (FGP, GNS) +GSG = (RRF, CSP) +VBL = (JJC, BPJ) +CCK = (RJF, SBD) +QPN = (TJJ, HFR) +PRH = (TGP, NMM) +PQT = (TRS, PQX) +QKP = (RMH, TDB) +XMP = (SNX, QMD) +QPD = (KRF, JRV) +RQD = (HQD, BTP) +XVT = (PHF, DGZ) +GXP = (CHL, LSR) +LHM = (MQJ, HDJ) +STS = (CFR, GQP) +FJJ = (NMM, TGP) +MPC = (CCF, RGQ) +MKM = (PTF, FNH) +QVH = (VBD, VBD) +XMB = (KVX, GSG) +CHL = (VBL, SLK) +XBR = (DJS, SVH) +KPR = (CPN, KGF) +MQL = (KSB, RHP) +CQM = (PFV, NMK) +XHQ = (HCR, HVM) +FFC = (JTR, TVP) +FMN = (BPT, BKC) +SNX = (MGK, XDV) +KNC = (QVJ, NNM) +DPS = (LRF, HPL) +GBB = (BVS, RRT) +BFH = (XMM, XQL) +GLS = (HVG, CXS) +RXG = (LVM, HVC) +MPL = (QFJ, KXV) +NXC = (KPR, RGP) +NJV = (TVN, NSP) +LLB = (LPQ, TNT) +SXT = (HVM, HCR) +LLH = (HDL, XFQ) +MLV = (KBD, MBS) +CBF = (GTJ, MSR) +JBC = (KTX, QQG) +FLC = (SHD, HCK) +FRP = (FPH, JVL) +BHQ = (MFD, KFP) +LNR = (DML, TBT) +CSP = (MKG, FNV) +GHC = (SQQ, DBB) +NRT = (BLT, FDR) +VTT = (HDX, MQX) +HSG = (FFH, KMD) +VGA = (CBV, TMC) +KLS = (TSM, SFR) +PFQ = (FGJ, QPD) +MKJ = (JLT, MLP) +MFX = (RVK, NSF) +QNJ = (KXT, CMK) +QBV = (NSP, TVN) +MSR = (KLG, XNL) +NSF = (QLH, MKJ) +DKQ = (JDR, DQQ) +PLH = (RVK, NSF) +JKG = (BQB, KTK) +LVM = (MKM, TJS) +QLJ = (VPT, BRF) +FVH = (RRX, RQD) +MJX = (DCT, GBH) +QLH = (MLP, JLT) +NPB = (XMT, NXT) +HDL = (CCQ, GTS) +JPS = (DCB, DCB) +CQN = (QPF, BCS) +GNP = (DSD, RQN) +JLK = (MJX, VSG) +PMS = (DTL, VFJ) +BFS = (JGS, TDT) +XJH = (VGD, HGC) +VRF = (SFH, FFJ) +BCK = (SMS, TVR) +DRT = (QFP, MVF) +PND = (DSD, RQN) +VSG = (GBH, DCT) +VGV = (JKM, GXF) +FHD = (MXT, FQC) +GPP = (QRN, QCK) +BDX = (CLN, PHJ) +LLF = (HQT, DCL) +DXA = (HFN, KDR) +MCV = (DHM, PMS) +NTM = (HMN, THX) +JLC = (RDP, VRN) +HQV = (BCF, CCX) +NDN = (QTS, GXP) +QVJ = (DDS, XSF) +MQJ = (RPH, QPK) +FGJ = (KRF, JRV) +LHN = (BFK, RPD) +MXH = (RHV, RHV) +HGC = (DNN, PQT) +MHM = (XFJ, JQF) +JGD = (JGF, GGD) +FCX = (JGC, CGD) +GCS = (LST, QTN) +RCX = (BSL, MQL) +XGJ = (VCT, JRK) +SXB = (FJJ, PRH) +LRG = (XMT, NXT) +XSF = (LRP, RGT) +DNP = (CSQ, RQL) +RKL = (VGB, JRQ) +KVX = (CSP, RRF) +JPD = (FGP, GNS) +GNC = (MGN, CFV) +PFR = (MHS, FRT) +DCK = (LMJ, FRP) +KKP = (TQF, QKP) +XQL = (GMG, LRN) +QCL = (TJK, XCF) +TVP = (XRD, QLG) +HDR = (QPN, PGG) +HLN = (BHQ, SSG) +GGS = (VCT, JRK) +SJN = (HHS, XDL) +DBB = (BHK, KSX) +QPK = (MFX, PLH) +MBF = (NLT, GGB) +BDB = (TDV, JLK) +KSC = (NKN, NTQ) +CPN = (HDB, VPL) +TVB = (RGQ, CCF) +VJX = (NNM, QVJ) +FBT = (JLL, LMR) +TVR = (GJV, LSC) +RNX = (KRH, VGN) +RPH = (MFX, PLH) +VJA = (JBC, XFB) +CLS = (JLL, LMR) +PSJ = (DKF, MSL) +QLG = (PBQ, HMH) +DXJ = (HNG, RBG) +XFB = (KTX, QQG) +TJJ = (FBT, CLS) +KFC = (JRF, JRF) +XFJ = (JJD, VDM) +RCT = (TDH, JGD) +RNH = (BGM, MJV) +VGD = (PQT, DNN) +KQC = (JFJ, CBC) +PPL = (JSJ, KSS) +HCR = (DRT, SSV) +XLJ = (PJR, QBK) +PTH = (QVH, VHX) +RQF = (KLR, KLR) +MSL = (MVR, QQB) +NRJ = (PGM, JJT) +JLL = (KQJ, KQJ) +DCL = (BPS, SSN) +GTS = (MNT, PDJ) +BXR = (NMX, BJX) +CBL = (PND, GNP) +VXK = (JJQ, JTK) +BSL = (RHP, KSB) +NJL = (SJG, CGS) +DQQ = (XHX, VVX) +SQQ = (BHK, KSX) +KTX = (BVK, HLN) +QCK = (XSL, MMP) +SVC = (GSC, XKH) +BGV = (MXH, MRH) +GMG = (QRM, FMC) +KGJ = (NLT, GGB) +LRF = (QLR, CKB) +CMN = (RCX, MQC) +CGD = (JGM, PFR) +LCD = (JSJ, KSS) +HDG = (BPP, KNK) +DDS = (LRP, RGT) +LRM = (DQQ, JDR) +CMH = (CBC, JFJ) +GQH = (TDT, JGS) +BKG = (RTL, XLC) +RMR = (VRN, RDP) +MGK = (XVD, HJH) +FJS = (VTT, VPQ) +SFH = (CTQ, QCL) +XHN = (XQL, XMM) +BPA = (CRJ, FJF) +XCF = (VGL, LGR) +TBM = (MSL, DKF) +JLR = (LST, QTN) +QFJ = (HVQ, QCJ) +CCQ = (MNT, PDJ) +VGN = (PXB, BHN) +LTB = (QLV, NRT) +DCH = (MXT, FQC) +CFG = (PTH, GSL) +JRF = (KXF, KXF) +QRM = (CPV, GBC) +XTX = (BTL, GQS) +FHX = (SBD, RJF) +FHF = (KSR, XFF) +CSM = (NSL, XQG) +HCP = (MMT, KLS) +DJK = (XDT, XLJ) +QLC = (CVF, RQG) +MLJ = (HQT, DCL) +CLM = (JCC, FJS) +NQC = (HXV, FVH) +RDD = (DTB, VGR) +RPN = (DQC, DHG) +GSL = (QVH, VHX) +FGG = (NMX, BJX) +BPJ = (RPJ, HBT) +RBG = (NJB, JVV) +FJF = (SGC, RNH) +GGD = (CFB, NMC) +QQG = (HLN, BVK) +MVF = (RLP, RSF) +JGM = (MHS, FRT) +RMB = (JRF, LMH) +VDM = (HFL, NRP) +HNG = (NJB, JVV) +MJV = (CJN, NRJ) +DNN = (PQX, TRS) +JDR = (XHX, VVX) +KGF = (HDB, VPL) diff --git a/input/day_08_example b/input/day_08_example new file mode 100644 index 0000000..9029a1b --- /dev/null +++ b/input/day_08_example @@ -0,0 +1,9 @@ +RL + +AAA = (BBB, CCC) +BBB = (DDD, EEE) +CCC = (ZZZ, GGG) +DDD = (DDD, DDD) +EEE = (EEE, EEE) +GGG = (GGG, GGG) +ZZZ = (ZZZ, ZZZ) From 9826f6cfb9e6be3945b0f298ab414591caf409f2 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 11 Dec 2023 07:28:14 +0100 Subject: [PATCH 15/15] fix: solve using LCM instead --- day_08/src/lib.rs | 43 +++++++++++++++++++++++++++++---- day_08/src/math.rs | 38 +++++++++++++++++++++++++++++ day_08/src/part_01.rs | 31 +++--------------------- day_08/src/part_02.rs | 56 +++++-------------------------------------- 4 files changed, 85 insertions(+), 83 deletions(-) create mode 100644 day_08/src/math.rs diff --git a/day_08/src/lib.rs b/day_08/src/lib.rs index d79b743..6c12674 100644 --- a/day_08/src/lib.rs +++ b/day_08/src/lib.rs @@ -1,5 +1,6 @@ -use std::{cell::RefCell, collections::HashMap}; +use std::collections::HashMap; +pub mod math; pub mod part_01; pub mod part_02; @@ -9,13 +10,45 @@ pub enum Direction { Right, } +pub type Integer = u64; + #[derive(Debug, Clone, Eq, PartialEq)] pub struct Input { moves: Vec, - nodes: RefCell>, + nodes: Vec<(String, (String, String))>, indices: HashMap, } +impl Input { + pub fn solve(input: &Input, start_index: usize) -> Integer { + let nodes = input.nodes.to_owned(); + let mut current = nodes.get(start_index).unwrap().to_owned(); + + let mut steps = 0; + 'main: loop { + for direction in input.moves.clone() { + steps += 1; + + let next_index = match direction { + Direction::Left => &*current.1 .0, + Direction::Right => &*current.1 .1, + }; + + let next_index = *input.indices.get(next_index).unwrap(); + let next = nodes.get(next_index).unwrap().to_owned(); + + if next.0.ends_with('Z') { + break 'main; + } + + current = next; + } + } + + steps + } +} + pub fn parse_input(input: &str) -> Input { let rows = input .trim_start() @@ -64,7 +97,7 @@ pub fn parse_input(input: &str) -> Input { Input { moves, - nodes: RefCell::new(nodes), + nodes, indices, } } @@ -89,11 +122,11 @@ mod tests { parse_input(&EXAMPLE_DATA), Input { moves: vec![Left, Left, Right], - nodes: RefCell::new(vec![ + nodes: vec![ ("AAA".to_owned(), ("BBB".to_owned(), "BBB".to_owned())), ("BBB".to_owned(), ("AAA".to_owned(), "ZZZ".to_owned())), ("ZZZ".to_owned(), ("ZZZ".to_owned(), "ZZZ".to_owned())), - ]), + ], indices: HashMap::from([ ("AAA".to_owned(), 0), ("BBB".to_owned(), 1), diff --git a/day_08/src/math.rs b/day_08/src/math.rs new file mode 100644 index 0000000..85d7387 --- /dev/null +++ b/day_08/src/math.rs @@ -0,0 +1,38 @@ +use std::{ + mem, + ops::{Div, Mul, Rem, Sub}, +}; + +pub fn lcm< + T: Mul + Sub + Div + Rem + Ord + Eq + Copy, +>( + a: T, + b: T, +) -> T { + a * b / gcd(a, b) +} + +pub fn gcd< + T: Mul + Sub + Div + Rem + Ord + Eq + Copy, +>( + a: T, + b: T, +) -> T { + let mut max = a; + let mut min = b; + + if min > max { + mem::swap(&mut max, &mut min); + } + + loop { + let result = max % min; + let zero = result - result; + if result == zero { + return min; + } + + max = min; + min = result; + } +} diff --git a/day_08/src/part_01.rs b/day_08/src/part_01.rs index 746418c..31a0d21 100644 --- a/day_08/src/part_01.rs +++ b/day_08/src/part_01.rs @@ -1,34 +1,9 @@ use std::io::Result; -use crate::{Direction::*, Input}; +use crate::{Input, Integer}; -pub fn main(input: &Input) -> Result { - let nodes = input.nodes.to_owned(); - let index_of_aaa = *input.indices.get("AAA").unwrap(); - let mut current = nodes.borrow().get(index_of_aaa).unwrap().to_owned(); - - let mut steps = 0; - 'main: loop { - for direction in input.moves.clone() { - steps += 1; - - let next_index = match direction { - Left => &*current.1 .0, - Right => &*current.1 .1, - }; - - let next_index = *input.indices.get(next_index).unwrap(); - let next = nodes.borrow().get(next_index).unwrap().to_owned(); - - if next.0 == "ZZZ" { - break 'main; - } - - current = next; - } - } - - Ok(steps) +pub fn main(input: &Input) -> Result { + Ok(Input::solve(input, *input.indices.get("AAA").unwrap())) } #[cfg(test)] diff --git a/day_08/src/part_02.rs b/day_08/src/part_02.rs index 742a194..82651cb 100644 --- a/day_08/src/part_02.rs +++ b/day_08/src/part_02.rs @@ -1,60 +1,16 @@ -use std::{cell::RefCell, io::Result}; +use std::io::Result; -use crate::{Direction::*, Input}; +use crate::{math::lcm, Input, Integer}; -pub fn main(input: &Input) -> Result { - let nodes = input.nodes.to_owned(); - let all_indices = input +pub fn main(input: &Input) -> Result { + Ok(input .indices .keys() .into_iter() .filter(|k| k.ends_with('A')) .map(|k| *input.indices.get(k).unwrap()) - .collect::>(); - - let ends_to_find = all_indices.len(); - - let all_currents = RefCell::new( - all_indices - .clone() - .into_iter() - .map(|i| nodes.borrow().get(i).unwrap().to_owned()) - .collect::>(), - ); - - let mut steps = 0; - 'main: loop { - for direction in input.moves.clone() { - steps += 1; - - let found_ends = - all_currents - .borrow_mut() - .iter_mut() - .fold(0, |mut completed, current| { - let next_index = match direction { - Left => &*current.1 .0, - Right => &*current.1 .1, - }; - let next_index = *input.indices.get(next_index).unwrap(); - let next = nodes.borrow().get(next_index).unwrap().to_owned(); - - *current = next; - - if current.0.ends_with('Z') { - completed += 1; - } - - completed - }); - - if found_ends == ends_to_find { - break 'main; - } - } - } - - Ok(steps) + .map(|start_index| Input::solve(input, start_index)) + .fold(1, |acc, n| lcm(acc, n))) } #[cfg(test)] pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy