diff options
| -rw-r--r-- | 2024/day10.rs | 103 | ||||
| -rw-r--r-- | 2024/in/day10.pzl | 47 | ||||
| -rw-r--r-- | 2024/in/day10.ref | 8 |
3 files changed, 158 insertions, 0 deletions
diff --git a/2024/day10.rs b/2024/day10.rs new file mode 100644 index 0000000..7c8576d --- /dev/null +++ b/2024/day10.rs @@ -0,0 +1,103 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_mut)] +use std::collections::HashSet; +use std::env; +use std::fs::File; +use std::io::Read; + +fn dfs(x0: i64, y0: i64, map: &Vec<Vec<i64>>, mut hs: HashSet<(i64, i64)>) -> HashSet<(i64, i64)> { + //println!("{} {}", x0, y0); + let h = map.len() as i64; + let w = map[0].len() as i64; + + let n = map[y0 as usize][x0 as usize]; + if n == 9 { + hs.insert((y0, x0)); + return hs; + } + + for (dx, dy) in [(0, 1), (0, -1), (1, 0), (-1, 0)] { + let x = x0 + dx as i64; + let y = y0 + dy as i64; + if (0 <= x) && (x < w) && (0 <= y) && (y < h) { + if map[y as usize][x as usize] == n + 1 { + hs = dfs(x, y, map, hs); + } + } + } + return hs; +} + +fn dfs2( + x0: i64, + y0: i64, + map: &Vec<Vec<i64>>, + mut path: Vec<(i64, i64)>, + mut hs: HashSet<Vec<(i64, i64)>>, +) -> HashSet<Vec<(i64, i64)>> { + //println!("{} {}", x0, y0); + let h = map.len() as i64; + let w = map[0].len() as i64; + + path.push((x0, y0)); + + let n = map[y0 as usize][x0 as usize]; + if n == 9 { + hs.insert(path); + return hs; + } + + for (dx, dy) in [(0, 1), (0, -1), (1, 0), (-1, 0)] { + let x = x0 + dx as i64; + let y = y0 + dy as i64; + if (0 <= x) && (x < w) && (0 <= y) && (y < h) { + if map[y as usize][x as usize] == n + 1 { + hs = dfs2(x, y, map, path.clone(), hs); + } + } + } + return hs; +} + +fn main() { + let args: Vec<String> = env::args().collect(); + let filename = if args.len() == 1 { + "in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl" + } else { + args[1].clone() + }; + let mut f = File::open(filename).expect("cannot open file"); + let mut content = String::new(); + f.read_to_string(&mut content).expect("cannot read file"); + let lines = content.trim_end().split("\n"); + + let mut trailheads: Vec<(i64, i64)> = Vec::new(); + let mut map: Vec<Vec<i64>> = Vec::new(); + for (y, line) in lines.enumerate() { + let mut l: Vec<i64> = Vec::new(); + for (x, c) in line.chars().enumerate() { + let n = c.to_digit(10).unwrap() as i64; + l.push(n); + if n == 0 { + trailheads.push((x as i64, y as i64)); + } + } + map.push(l); + } + + let mut res1 = 0; + let mut res2 = 0; + for (x0, y0) in trailheads { + let hs = dfs(x0, y0, &map, HashSet::new()); + res1 += hs.len(); + + let hs2 = dfs2(x0, y0, &map, Vec::new(), HashSet::new()); + res2 += hs2.len(); + } + + println!("res1: {}", res1); + println!("res2: {}", res2); + assert_eq!(res1, 607); + assert_eq!(res2, 1384); +} diff --git a/2024/in/day10.pzl b/2024/in/day10.pzl new file mode 100644 index 0000000..4463c59 --- /dev/null +++ b/2024/in/day10.pzl @@ -0,0 +1,47 @@ +23456734567892109810123432345698901208765434101 +10549825676783478454398521050787432019010125014 +01234918985490565569207649801456541023121076523 +10109401434301234478111056712365695434536789434 +23218321098210765329022346543876786985445890124 +94567036707601896011233487652985497876096787633 +85432145816548987120545694561016386987187896544 +76398901923439878921698763278987275496256102345 +65487432301656763212785612109216189345343201976 +54306511012345654100126703456705071234014567889 +67215301165454343201455896989814560124323498701 +78995432874763210112364307890123458065015678012 +97786929923892109101876216543260309876784589903 +87657818018901238012905487630871212105693210854 +56545307667652387643814896321962343454321018765 +45430212550743496556723765412455424569910659321 +30721403441812305401201234902398512378876743430 +21876548732903412390321903801287609410965812565 +10961239699876501987430814702108988501234901276 +25670012588701431016587723612987667602345672187 +34981203476102340127896654543096543211898983098 +43210112345216765432945012345125476540127454678 +10367895432345898741832101436034989032016565549 +23456976001898987650710654987654102121023476432 +21043987116787650101921745078943278930234189701 +30789783295490543217839830161258767876101089850 +49650694386321898346540123250319456543212786761 +58941545677012897656621012343401304789303695410 +67632432498008783210012303012511215610454584323 +52102341345129654105987454987620354328960178765 +43001650216734547894326567889431298798873239854 +78765781001889030765410128976530321667654390345 +69344392190972121256707034345323410501563781236 +55431283081569876301898943236516501432457670145 +46720894672430985432987650127407832345898545056 +37810765543421456701109871298398945496989430987 +23946540104012387890234567701280126787870121676 +10837231231010892110100108898965435456765014589 +76548101942101743021985239707876540367654323987 +87439432853232659132376545610012301298303410854 +90126501764345678987431496921678431001212543763 +78067876545874325876520387834599532198389432102 +69158965456985014901011276543787640067476501201 +54543012317876123452100345937876551258585432376 +23632152101231034563498487890903469349691678985 +16721043676541043674567096541212378438710521294 +07890124589832012987650123432101289321023430123 diff --git a/2024/in/day10.ref b/2024/in/day10.ref new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/2024/in/day10.ref @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 |
