58 lines
1.7 KiB
Rust
58 lines
1.7 KiB
Rust
---cargo
|
|
[profile.dev]
|
|
opt-level = 3
|
|
|
|
[dependencies]
|
|
nalgebra = "0.34"
|
|
---
|
|
|
|
use nalgebra::DMatrix;
|
|
use std::collections::HashMap;
|
|
|
|
static CONTENT: &'static str = include_str!("./inputs/11_1.txt");
|
|
|
|
fn main() {
|
|
let parts: HashMap<[char; 3], (usize, Vec<[char; 3]>)> = CONTENT.lines().map(|l| l.trim()).map(|p| {
|
|
let (key, rest) = p.split_once(':').unwrap();
|
|
let key = key.trim();
|
|
let rest = rest.trim();
|
|
|
|
let key: [char; 3] = core::array::from_fn(|idx| key.chars().nth(idx).unwrap());
|
|
let rest: Vec<[char; 3]> = rest.split(' ').map(|p| {
|
|
core::array::from_fn(|idx| p.chars().nth(idx).unwrap())
|
|
}).collect();
|
|
|
|
(key, rest)
|
|
}).chain([(['o', 'u', 't'], Vec::new())]).enumerate().map(|(idx, (key, v))| (key, (idx, v))).collect();
|
|
|
|
println!("{:?}", parts);
|
|
|
|
let mut adj_matrix = DMatrix::from_element(parts.len(), parts.len(), 0u64);
|
|
|
|
for (src, (src_idx, targets)) in parts.iter() {
|
|
for dest_idx in targets.iter().map(|t| parts.get(t).map(|(i, _)| i).unwrap()) {
|
|
adj_matrix[(*src_idx, *dest_idx)] = 1;
|
|
}
|
|
}
|
|
|
|
println!("{}", adj_matrix);
|
|
|
|
let you_idx: usize = parts.get(&['y', 'o', 'u']).map(|(idx, _)| *idx).unwrap();
|
|
let out_idx: usize = parts.get(&['o', 'u', 't']).map(|(idx, _)| *idx).unwrap();
|
|
|
|
let mut total_routes = 0;
|
|
|
|
let mut connectivity_matrix = DMatrix::identity(parts.len(), parts.len());
|
|
for length in 1..=parts.len() {
|
|
println!("Length: {:?}", length);
|
|
connectivity_matrix *= &adj_matrix;
|
|
|
|
let routes: u64 = connectivity_matrix[(you_idx, out_idx)];
|
|
println!("Routes: {}", routes);
|
|
|
|
total_routes += routes;
|
|
}
|
|
|
|
println!("Total Routes: {}", total_routes);
|
|
}
|