Implement AoC 2025 #1
89
2025/07.rs
89
2025/07.rs
@@ -1,6 +1,14 @@
|
|||||||
---cargo
|
---cargo
|
||||||
|
[profile.dev]
|
||||||
|
opt-level = 3
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nalgebra = "0.34"
|
||||||
---
|
---
|
||||||
|
|
||||||
|
use nalgebra::DMatrix;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
static CONTENT: &'static str = include_str!("./inputs/07_1.txt");
|
static CONTENT: &'static str = include_str!("./inputs/07_1.txt");
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@@ -21,47 +29,72 @@ fn main() {
|
|||||||
}).collect::<Vec<_>>()
|
}).collect::<Vec<_>>()
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
println!("Grid: {:?}", grid);
|
let height = grid.len();
|
||||||
|
let width = grid[0].len();
|
||||||
|
|
||||||
// (x, y)
|
let nodes: Vec<(usize, usize)> = grid.iter().enumerate().flat_map(|(y, row)| {
|
||||||
let source = grid.iter().enumerate().flat_map(|(y, row)| core::iter::repeat(y).zip(row.iter().enumerate()).map(|(y, (x, c))| (y, x, c))).find_map(|(y, x, c)| (c == &Cell::Sourc).then_some((x, y))).unwrap();
|
core::iter::repeat(y).zip(row.iter().enumerate().filter(|(_, c)| c != &&Cell::Empty)).map(|(y, (x, c))| (x, y))
|
||||||
println!("Source {:?}", source);
|
}).chain((0..width).zip(core::iter::repeat(height))).collect();
|
||||||
|
println!("Nodes: {:?}", nodes.len());
|
||||||
|
let node_map: HashMap<(usize, usize), usize> = nodes.iter().copied().enumerate().map(|(idx, v)| (v, idx)).collect();
|
||||||
|
|
||||||
let mut splits = 0;
|
|
||||||
|
|
||||||
let mut queue: Vec<(usize, usize)> = vec![(source.0, source.1+1)];
|
let mut adj_matrix = DMatrix::from_element(nodes.len(), nodes.len(), 0);
|
||||||
while let Some((x, mut y)) = queue.pop() {
|
|
||||||
if grid[y][x] != Cell::Empty {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for (sx, sy) in nodes.iter().copied() {
|
||||||
|
match grid.get(sy).map(|r| &r[sx]) {
|
||||||
|
Some(Cell::Empty) | Some(Cell::TBeam) => unreachable!(),
|
||||||
|
Some(Cell::Sourc) => {
|
||||||
|
let x = sx;
|
||||||
|
let mut y = sy+1;
|
||||||
while grid.get(y).map(|r| &r[x]) == Some(&Cell::Empty) {
|
while grid.get(y).map(|r| &r[x]) == Some(&Cell::Empty) {
|
||||||
// println!("({}, {})", x, y);
|
|
||||||
|
|
||||||
grid[y][x] = Cell::TBeam;
|
|
||||||
y += 1;
|
y += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
match grid.get(y).map(|r| &r[x]) {
|
let s_idx = node_map.get(&(sx, sy)).unwrap();
|
||||||
None => {}
|
let t_idx = node_map.get(&(x, y)).unwrap();
|
||||||
Some(Cell::Empty) => unreachable!(),
|
|
||||||
Some(Cell::Sourc) => unreachable!(),
|
|
||||||
Some(Cell::Split) => {
|
|
||||||
// println!("Split");
|
|
||||||
splits += 1;
|
|
||||||
|
|
||||||
if let Some(nx) = x.checked_sub(1) {
|
adj_matrix[(*s_idx, *t_idx)] = 1;
|
||||||
queue.push((nx, y));
|
|
||||||
}
|
}
|
||||||
if x+1 < grid[y].len() {
|
Some(Cell::Split) => {
|
||||||
queue.push((x+1, y));
|
for x in [sx-1, sx+1] {
|
||||||
|
let mut y = sy+1;
|
||||||
|
while grid.get(y).map(|r| &r[x]) == Some(&Cell::Empty) {
|
||||||
|
y += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let s_idx = node_map.get(&(sx, sy)).unwrap();
|
||||||
|
let t_idx = node_map.get(&(x, y)).unwrap();
|
||||||
|
|
||||||
|
adj_matrix[(*s_idx, *t_idx)] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Cell::TBeam) => {
|
None => {}
|
||||||
println!("Beam");
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Splits: {:?}", splits);
|
let source = nodes.iter().copied().find(|(x, y)| matches!(grid.get(*y).map(|r| &r[*x]), Some(Cell::Sourc))).unwrap();
|
||||||
|
println!("Source: {:?}", source);
|
||||||
|
|
||||||
|
// println!("{}", adj_matrix);
|
||||||
|
|
||||||
|
let mut routes = 0;
|
||||||
|
let mut result_matrix = DMatrix::identity(nodes.len(), nodes.len());
|
||||||
|
for i in 1..height {
|
||||||
|
result_matrix *= &adj_matrix;
|
||||||
|
|
||||||
|
let versions = (0..width).map(|x| {
|
||||||
|
let s_idx = node_map.get(&source).unwrap();
|
||||||
|
let t_idx = node_map.get(&(x, height)).unwrap();
|
||||||
|
|
||||||
|
let routes = result_matrix[(*s_idx, *t_idx)];
|
||||||
|
|
||||||
|
routes
|
||||||
|
}).sum::<usize>();
|
||||||
|
println!("[{}] Routes {}", i, versions);
|
||||||
|
|
||||||
|
routes += versions;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Total Routes: {:?}", routes);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user