From 9375c9b8060b2cc08a89f940011ca7fc0a19f90e Mon Sep 17 00:00:00 2001 From: mirsella Date: Mon, 11 Dec 2023 11:21:08 +0100 Subject: [PATCH] template: build.rs dont write file if no change made --- 2023/Cargo.toml | 2 +- 2023/day12/.gitignore | 19 +++++++++++++++++++ 2023/day12/Cargo.toml | 10 ++++++++++ 2023/day12/build.rs | 28 ++++++++++++++++++++++++++++ 2023/day12/src/main.rs | 24 ++++++++++++++++++++++++ template/build.rs | 5 ++++- 6 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 2023/day12/.gitignore create mode 100644 2023/day12/Cargo.toml create mode 100644 2023/day12/build.rs create mode 100644 2023/day12/src/main.rs diff --git a/2023/Cargo.toml b/2023/Cargo.toml index 7f9776f..dc0c5d5 100644 --- a/2023/Cargo.toml +++ b/2023/Cargo.toml @@ -13,7 +13,7 @@ members = [ "day9", "day10", "day11", - # "day12", + "day12", # "day13", # "day14", # "day15", diff --git a/2023/day12/.gitignore b/2023/day12/.gitignore new file mode 100644 index 0000000..e983cca --- /dev/null +++ b/2023/day12/.gitignore @@ -0,0 +1,19 @@ +input.txt +flamegraph.svg +perf.data* +### Rust +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + diff --git a/2023/day12/Cargo.toml b/2023/day12/Cargo.toml new file mode 100644 index 0000000..a172d25 --- /dev/null +++ b/2023/day12/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day12" +authors = ["mirsella "] +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + diff --git a/2023/day12/build.rs b/2023/day12/build.rs new file mode 100644 index 0000000..b6a19a0 --- /dev/null +++ b/2023/day12/build.rs @@ -0,0 +1,28 @@ +use std::fs::File; +use std::io::{self, Read}; +use std::path::PathBuf; +use std::{env, fs}; + +fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> { + let mut contents = String::new(); + File::open(file_path)?.read_to_string(&mut contents)?; + let new_contents = contents.replace(old, new); + if contents != new_contents { + println!("Updating {}", file_path.display()); + fs::write(file_path, new_contents)?; + } + Ok(()) +} + +fn main() -> io::Result<()> { + let pkg_name = env::var("CARGO_PKG_NAME").unwrap(); + replace_in_file( + &"../Cargo.toml".into(), + &format!("# \"{pkg_name}\""), + &format!("\"{pkg_name}\""), + )?; + + replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?; + + Ok(()) +} diff --git a/2023/day12/src/main.rs b/2023/day12/src/main.rs new file mode 100644 index 0000000..5ee7ff1 --- /dev/null +++ b/2023/day12/src/main.rs @@ -0,0 +1,24 @@ +fn part1(input: &str) -> usize { + todo!() +} +fn part2(input: &str) -> usize { + todo!() +} +fn main() { + let input = include_str!("../input.txt"); + println!("Part 1: {}", part1(input)); + println!("Part 2: {}", part2(input)); +} + +#[cfg(test)] +mod tests { + const INPUT: &str = ""; + #[test] + fn part1() { + assert_eq!(super::part1(INPUT), todo!()); + } + #[test] + fn part2() { + assert_eq!(super::part2(INPUT), todo!()); + } +} diff --git a/template/build.rs b/template/build.rs index 2792913..b6a19a0 100644 --- a/template/build.rs +++ b/template/build.rs @@ -7,7 +7,10 @@ fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> let mut contents = String::new(); File::open(file_path)?.read_to_string(&mut contents)?; let new_contents = contents.replace(old, new); - fs::write(file_path, new_contents)?; + if contents != new_contents { + println!("Updating {}", file_path.display()); + fs::write(file_path, new_contents)?; + } Ok(()) }