diff --git a/input/10-0a.in b/input/10-0a.in new file mode 100644 index 0000000..a305b9d --- /dev/null +++ b/input/10-0a.in @@ -0,0 +1,4 @@ +0123 +1234 +8765 +9876 diff --git a/input/10-0b.in b/input/10-0b.in new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/input/10-0b.in @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 diff --git a/input/10-0c.in b/input/10-0c.in new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/input/10-0c.in @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 diff --git a/input/10-1.in b/input/10-1.in new file mode 100644 index 0000000..d18cbd9 --- /dev/null +++ b/input/10-1.in @@ -0,0 +1,45 @@ +789032789543098712965101221019898103456454321 +470101678632105601876234034548781012765467100 +563210589789234512345549123697650101884398234 +454321430765432021236678998780143289991207965 +569834321898321130107654854012234676580312870 +578765010876980233498746763873401785676543541 +654321101945676542567832312964332694650145632 +765010210132156701010901203455963503765234701 +890124301231049878921001892396854412894339899 +981234456745030765432132701287760321023421078 +878965467896121076501245655332981212014322369 +690872356987232387431056986541274301015410450 +781081076578105498922347897890765412176704321 +632198985469216787011056785671893013089894565 +540345672354301017832345894789342104589723676 +891201451654302156901236893201230235678018985 +765432360783213443789107763196541343654321832 +654321078890198532876898632087832452789210101 +763011219912987691945678521087970961087123432 +892100308803676780032109431096981878996098542 +763965496734587654129818012345672345895467621 +354876785021096743016741001096789656760398930 +210787694103210892105432876585438769451207845 +321896543254387431012348965476323478300116596 +456910132123496545678256012365010565212323487 +787851401094563430789107101290126651054334678 +296542349887692321690678910987637892167210509 +101231056743781010541569010856546543098123418 +107645456652110127632438321658034012189014328 +298540365789054308971301434569122343276578789 +343231273210163210780210598778701656965489678 +850140984912378905698345699657892767874300569 +961001545805412344321010780346789876543211430 +873210656766901256543221271239654900612012321 +904589578927810187056102340128763211701012980 +012679865014321092167895434036650132890343478 +123456784325691233456996125675432147812334569 +456321092106780541245587098986789056903425876 +327867801256545650387654387129876541012510945 +010956910347838765495601296032989230109654354 +323843223498929678344760125141090121098701223 +456798114567012389413891434657890010567890110 +012387000345433678102110123766321023498989010 +323456781256921067221087432875435432103478321 +434321098987834554342196501989876301012565432 diff --git a/src/bin/10.rs b/src/bin/10.rs new file mode 100644 index 0000000..29ffdaa --- /dev/null +++ b/src/bin/10.rs @@ -0,0 +1,58 @@ +use std::collections::HashSet; + +fn count_paths(map: &[Vec], x: usize, y: usize) -> C +where + C: Extend<(usize, usize)> + + From<[(usize, usize); 1]> + + Default + + IntoIterator, +{ + [(1, 0), (0, 1), (-1, 0), (0, -1)] + .into_iter() + .filter_map(|(dx, dy)| { + Option::zip( + x.checked_add_signed(dx).filter(|nx| *nx < map[0].len()), + y.checked_add_signed(dy).filter(|&ny| ny < map.len()), + ) + }) + .filter(|&(nx, ny)| map[y][x] + 1 == map[ny][nx]) + .map(|(nx, ny)| { + if map[ny][nx] == 9 { + C::from([(nx, ny)]) + } else { + count_paths::(map, nx, ny) + } + }) + .reduce(|mut a, b| { + a.extend(b); + a + }) + .unwrap_or(C::default()) +} + +fn main() { + let text = std::fs::read_to_string("input/10-1.in").unwrap(); + + let map = text + .lines() + .map(|l| { + l.chars() + .map(|c| c.to_digit(10).unwrap() as u8) + .collect::>() + }) + .collect::>(); + + let mut sum1 = 0; + let mut sum2 = 0; + for (x, y, _) in map + .iter() + .enumerate() + .flat_map(|(y, l)| l.iter().enumerate().map(move |(x, c)| (x, y, c))) + .filter(|(_, _, c)| **c == 0) + { + sum1 += count_paths::>(&map, x, y).len(); + sum2 += count_paths::>(&map, x, y).len(); + } + + println!("{sum1}\n{sum2}") +}