From 2cd0dce596acfb7510620b2d2a3c3542733db0b8 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 13 Dec 2022 13:14:42 -0800 Subject: [PATCH] done with day12, part1 --- 2022-aoc/Cargo.toml | 4 +- 2022-aoc/src/d11.rs | 2 + 2022-aoc/src/d12.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 2022-aoc/src/d12.rs diff --git a/2022-aoc/Cargo.toml b/2022-aoc/Cargo.toml index 53aaf75..ea7401a 100644 --- a/2022-aoc/Cargo.toml +++ b/2022-aoc/Cargo.toml @@ -9,6 +9,6 @@ edition = "2021" aoc-runner = "0.3" aoc-runner-derive = "0.3" lazy_static = "1.4.0" -#itertools = "0.10.5" regex = "1.7" -ndarray = "0.15" \ No newline at end of file +ndarray = "0.15" +petgraph = "0.6" diff --git a/2022-aoc/src/d11.rs b/2022-aoc/src/d11.rs index 8c9db06..7af07c9 100644 --- a/2022-aoc/src/d11.rs +++ b/2022-aoc/src/d11.rs @@ -144,6 +144,7 @@ fn part2(troop: &[Monkey]) -> u128 { troop.iter().take(2).map(|m| m.business).product() } +/* #[cfg(test)] mod test { use super::*; @@ -188,3 +189,4 @@ Monkey 3: assert_eq!(part2(&v), 2713310158); } } +*/ diff --git a/2022-aoc/src/d12.rs b/2022-aoc/src/d12.rs new file mode 100644 index 0000000..88f80f0 --- /dev/null +++ b/2022-aoc/src/d12.rs @@ -0,0 +1,127 @@ +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; +use petgraph::{algo::dijkstra, prelude::NodeIndex, Graph}; + +type GRAPH = Graph; + +type Pos = (usize, usize); + +#[aoc_generator(day12)] +fn parse_input(input: &str) -> (NodeIndex, NodeIndex, GRAPH) { + let mut out = Graph::default(); + let mut g = Vec::new(); + let mut s = NodeIndex::default(); + let mut e = NodeIndex::default(); + + for (ridx, line) in input.lines().enumerate() { + let row = Vec::from_iter(line.chars()); + g.push(row); + } + + let rows = g.len(); + let cols = g[0].len(); + let mut g2 = vec![vec![NodeIndex::default(); cols]; rows]; + for row in 0..rows { + for col in 0..cols { + let weight = g[row][col]; + let node = out.add_node(weight); + g2[row][col] = node; + if weight == 'S' { + s = node; + } + if weight == 'E' { + e = node; + } + } + } + + for row in 0..rows { + for col in 0..cols { + let node = g2[row][col]; + for neighbor in neighbors((row, col), &g).iter() { + let &(row, col) = neighbor; + let neighbor = g2[row][col]; + out.add_edge(node, neighbor, ()); + } + } + } + + (s, e, out) +} + +fn neighbors((ridx, cidx): Pos, graph: &[Vec]) -> Vec<(usize, usize)> { + let n = graph[ridx][cidx]; + let mut out = Vec::new(); + for (dr, dc) in [(-1, 0), (1, 0), (0, -1), (0, 1)] { + let row = (dr + ridx as i32) as usize; + let col = (dc + cidx as i32) as usize; + if let Some(r) = graph.get(row) { + if let Some(c) = r.get(col) { + if reachable(n, *c) { + out.push((row, col)); + } + } + } + } + + out +} + +fn reachable(a: char, b: char) -> bool { + let s = 'S' as i32; + let e = 'E' as i32; + let alit = 'a' as i32; + let zlit = 'z' as i32; + let a = a as i32; + let b = b as i32; + + if a == s && b == e { + return false; + } + if a == s { + return b - alit < 2; + } + + if b == s || a == e { + return true; + } + + if b == e { + return zlit - a < 2; + } + + (a - b).abs() < 2 || a > b +} + +#[aoc_run(day12, part1)] +fn part1((start, end, graph): &(NodeIndex, NodeIndex, GRAPH)) -> i32 { + let res = dijkstra(graph, *start, Some(*end), |_| 1); + res[end] +} + +#[aoc_run(day12, part2)] +fn part2(input: &(NodeIndex, NodeIndex, GRAPH)) -> u32 { + 0 +} + +#[cfg(test)] +mod test { + use super::*; + const INPUT: &str = "Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi"; + + #[test] + fn part1_test() { + let v = parse_input(INPUT); + + assert_eq!(part1(&v), 31); + } + + #[test] + fn part2_test() { + let v = parse_input(INPUT); + assert_eq!(part2(&v), 1); + } +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index aa6aaf3..769a7ff 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -11,5 +11,6 @@ mod d8; mod d9; mod d10; mod d11; +mod d12; aoc_lib! { year = 2022 }