done with day12, part1
This commit is contained in:
parent
5441906ea8
commit
2cd0dce596
4 changed files with 132 additions and 2 deletions
|
@ -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"
|
||||
ndarray = "0.15"
|
||||
petgraph = "0.6"
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
127
2022-aoc/src/d12.rs
Normal file
127
2022-aoc/src/d12.rs
Normal file
|
@ -0,0 +1,127 @@
|
|||
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
|
||||
use petgraph::{algo::dijkstra, prelude::NodeIndex, Graph};
|
||||
|
||||
type GRAPH = Graph<char, ()>;
|
||||
|
||||
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<char>]) -> 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);
|
||||
}
|
||||
}
|
|
@ -11,5 +11,6 @@ mod d8;
|
|||
mod d9;
|
||||
mod d10;
|
||||
mod d11;
|
||||
mod d12;
|
||||
|
||||
aoc_lib! { year = 2022 }
|
||||
|
|
Loading…
Reference in a new issue