diff --git a/advent_of_code/2022/12/Cargo.lock b/advent_of_code/2022/12/Cargo.lock index 3fd4edd..f8c50a0 100644 --- a/advent_of_code/2022/12/Cargo.lock +++ b/advent_of_code/2022/12/Cargo.lock @@ -8,6 +8,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "either" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" + [[package]] name = "fixedbitset" version = "0.4.2" @@ -30,12 +36,30 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "integer-sqrt" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "276ec31bcb4a9ee45f58bec6f9ec700ae4cf4f4f8f2fa7e06cb406bd5ffdd770" +dependencies = [ + "num-traits", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "main" version = "0.1.0" dependencies = [ "nom", - "petgraph", + "pathfinding", ] [[package]] @@ -52,20 +76,95 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "nom" -version = "7.1.1" +version = "7.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" +checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c" dependencies = [ "memchr", "minimal-lexical", ] [[package]] -name = "petgraph" -version = "0.6.2" +name = "num-traits" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "pathfinding" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c66505cf9c582f34ae89f6433c16ccc05f88803a36adff59c812188021edee78" dependencies = [ "fixedbitset", "indexmap", + "integer-sqrt", + "itertools", + "num-traits", + "rustc-hash", + "thiserror", ] + +[[package]] +name = "proc-macro2" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" diff --git a/advent_of_code/2022/12/Cargo.toml b/advent_of_code/2022/12/Cargo.toml index d62e7ae..d601e89 100644 --- a/advent_of_code/2022/12/Cargo.toml +++ b/advent_of_code/2022/12/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] nom = "7.1.1" -petgraph = "0.6.2" +pathfinding = "4.2.0" diff --git a/advent_of_code/2022/12/src/main.rs b/advent_of_code/2022/12/src/main.rs index 7e59c58..f5ca6cc 100644 --- a/advent_of_code/2022/12/src/main.rs +++ b/advent_of_code/2022/12/src/main.rs @@ -1,13 +1,16 @@ use std::collections::HashMap; +use pathfinding::prelude::dijkstra; fn main() { - let test_input = include_str!("../test.txt") + let test_input = include_str!("../input.txt") .split_whitespace() .collect::>(); let mut start = (0, 0); let mut end = (0, 0); - let mut points : HashMap<(u32, u32), u32> = HashMap::new(); + // let mut max_y = 0; + // let mut max_x = 0; + let mut points : HashMap<(i32, i32), i32> = HashMap::new(); let mut y = 0; for line in test_input { @@ -23,9 +26,45 @@ fn main() { end = (x, y); } - points.insert((x, y), c as u32 - 'a' as u32); + points.insert((x, y), c as i32 - 'a' as i32); + + // max_x = x; x += 1; } + + // max_y = y; y += 1; } + + let r = dijkstra( + &start, + |&(x, y)| vec![((x-1, y), points.get(&(x-1, y)), (x, y)), + ((x+1, y), points.get(&(x+1, y)), (x, y)), + ((x, y+1), points.get(&(x, y+1)), (x, y)), + ((x, y-1), points.get(&(x, y-1)), (x, y)),] + .into_iter() + .filter(|&(_, n, c)| match n { + None => false, + Some(&height) => height <= *points.get(&c).unwrap() + 1, + }) + .map(|(coords, _, _)| (coords, 1)), + |&n| n == end); + + println!("Number of steps taken: {}", r.expect("No path found").1); + + // Testing the parsing + + // for y in 0..=max_y { + // for x in 0..=max_x { + // if (x, y) == start { + // print!("S"); + // } else if (x, y) == end { + // print!("E") + // } else { + // let c = from_u32((points.get(&(x, y)).unwrap() + 'a' as i32) as u32).unwrap(); + // print!("{c}"); + // } + // } + // println!(); + // } }