advent-of-code/2015/day-09/dot.rs
Patrick Auernig 3c57921438 Refactor 2015 days 01 to 10
Split parts into separate files and remove some unused files
2021-12-08 01:05:17 +01:00

54 lines
1.2 KiB
Rust

mod common;
mod graph;
use std::env;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use common::{Graph, Result};
fn generate_dot(graph: &Graph, name: &str) -> Result<String> {
use std::fmt::Write;
let mut buf = String::new();
writeln!(buf, "strict graph {} {{", name)?;
for node in &graph.nodes {
for (outgoing, weight) in &node.borrow().outgoing {
writeln!(
buf,
"{} -- {} [ label = \"{}\" ];",
node.borrow().data,
outgoing.borrow().data,
weight
)?;
}
}
writeln!(buf, "}}")?;
Ok(buf)
}
fn main() -> Result<()> {
let path = PathBuf::from(env::args().nth(1).unwrap());
let graph = common::parse_file(&path)?;
let mut dot_path = path.clone();
dot_path.set_extension("dot");
let graph_name = dot_path.file_stem().unwrap().to_str().unwrap();
let dot = generate_dot(&graph, &graph_name)?;
let out_path = dot_path.file_name().unwrap();
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(out_path)?;
write!(file, "{}", dot)?;
Ok(())
}