diff --git a/Cargo.lock b/Cargo.lock index 9f94da0..5b3de76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,8 @@ version = 3 name = "advent_of_code_2023" version = "0.1.0" dependencies = [ + "env_logger", + "log", "regex", ] @@ -18,6 +20,76 @@ dependencies = [ "memchr", ] +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "env_logger" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "hermit-abi" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys 0.48.0", +] + +[[package]] +name = "libc" +version = "0.2.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" + +[[package]] +name = "linux-raw-sys" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + [[package]] name = "memchr" version = "2.6.4" @@ -52,3 +124,188 @@ name = "regex-syntax" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "rustix" +version = "0.38.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9470c4bf8246c8daf25f9598dca807fb6510347b1e1cfa55749113850c79d88a" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/Cargo.toml b/Cargo.toml index f2eab74..8f080f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +env_logger = "0.10.1" +log = "0.4.20" regex = "1.10.2" diff --git a/src/day1/mod.rs b/src/day1/mod.rs index 830c376..8927d8c 100644 --- a/src/day1/mod.rs +++ b/src/day1/mod.rs @@ -1,8 +1,4 @@ -#[allow(dead_code)] -pub enum Part { - One, - Two -} +use super::utils::Part; fn line_to_calibration_part1(line: &str) -> (Option, Option) { let a = line.chars().find(|c| c.is_ascii_digit()).expect("Could not find a valid number from the left").to_digit(10); @@ -53,7 +49,7 @@ fn line_to_calibration_part2(line: &str) -> (Option, Option) { } #[allow(dead_code)] -pub fn solve(lines: Vec, part: self::Part) -> u32 { +pub fn solve(lines: Vec, part: Part) -> u32 { let mut sum = 0; for line in lines.iter() { let (a, b) = match part { diff --git a/src/day2/mod.rs b/src/day2/mod.rs index d20b972..0f78b43 100644 --- a/src/day2/mod.rs +++ b/src/day2/mod.rs @@ -1,9 +1,6 @@ +use super::utils::Part; use regex::Regex; -pub enum Part { - One, - Two -} #[derive(Debug)] struct Game { diff --git a/src/day3/mod.rs b/src/day3/mod.rs index 7925145..621b7f0 100644 --- a/src/day3/mod.rs +++ b/src/day3/mod.rs @@ -1,5 +1,8 @@ +use super::utils::Part; + use regex::Regex; use std::ops::Range; +use log::debug; #[derive(Debug)] @@ -11,12 +14,6 @@ struct Match { gears: Vec } -#[allow(dead_code)] -pub enum Part { - One, - Two -} - #[derive(Debug, PartialEq)] struct Point { x: u32, @@ -57,7 +54,13 @@ pub fn solve(input: Vec, part: Part) -> u32 { for (i, line) in input.iter().enumerate() { for capture in regex.captures_iter(line) { if let Some(capture) = capture.get(0) { - matches.push(Match { value: capture.as_str().parse::().unwrap(), line: i, range: capture.range(), is_valid: false, gears: Vec::new() }); + matches.push(Match { + value: capture.as_str().parse::().unwrap(), + line: i, + range: capture.range(), + is_valid: false, + gears: Vec::new() + }); } } dataset.push(line.chars().collect()); @@ -69,24 +72,24 @@ pub fn solve(input: Vec, part: Part) -> u32 { let mut b1 = false; let mut b3 = false; - println!("Checking for {}", item.value); + debug!("Checking for {}", item.value); if item.line >= 1 { b1 = is_symbol(dataset.get(item.line-1).unwrap(), range.to_owned()); item.gears.append(&mut find_gears(dataset.get(item.line-1).unwrap(), range.to_owned(), item.line-1)); - println!("\tChecking line #{} - {}\t'{:?}'", item.line-1, b1, &dataset.get(item.line-1).unwrap()[range.to_owned()]); + debug!("\tChecking line #{} - {}\t'{:?}'", item.line-1, b1, &dataset.get(item.line-1).unwrap()[range.to_owned()]); } let b2 = is_symbol(dataset.get(item.line).unwrap(), range.to_owned()); item.gears.append(&mut find_gears(dataset.get(item.line).unwrap(), range.to_owned(), item.line)); - println!("\tChecking line #{} - {}\t'{:?}'", item.line, b2, &dataset.get(item.line).unwrap()[range.to_owned()]); + debug!("\tChecking line #{} - {}\t'{:?}'", item.line, b2, &dataset.get(item.line).unwrap()[range.to_owned()]); if item.line < dataset.len() - 1 { b3 = is_symbol(dataset.get(item.line+1).unwrap(), range.to_owned()); item.gears.append(&mut find_gears(dataset.get(item.line+1).unwrap(), range.to_owned(), item.line+1)); - println!("\tChecking line #{} - {}\t'{:?}'", item.line+1, b1, &dataset.get(item.line+1).unwrap()[range.to_owned()]); + debug!("\tChecking line #{} - {}\t'{:?}'", item.line+1, b1, &dataset.get(item.line+1).unwrap()[range.to_owned()]); } item.is_valid = b1 || b2 || b3; @@ -104,7 +107,7 @@ pub fn solve(input: Vec, part: Part) -> u32 { for gears_point in &item.gears { for other_match in &matches[(i+1)..] { if other_match.gears.contains(gears_point) { - println!("Match gear at {:?} -> {} * {}", gears_point, item.value, other_match.value); + debug!("Match gear at {:?} -> {} * {}", gears_point, item.value, other_match.value); sum_gears += item.value * other_match.value; } } diff --git a/src/main.rs b/src/main.rs index 20b97d9..7e276a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,16 @@ mod day1; mod day2; mod day3; mod utils; +use utils::Part; fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Debug) + .format_timestamp(None) + .init(); } @@ -19,8 +24,8 @@ mod tests { #[test] fn day1() { let day1_data_path = "./datasets/adventofcode.com_2023_day_1_input.txt"; - let day1_result_part1 = day1::solve(utils::lines_from_file(day1_data_path).expect("Could not load the dataset for day 1"), day1::Part::One); - let day1_result_part2 = day1::solve(utils::lines_from_file(day1_data_path).expect("Could not load the dataset for day 1"), day1::Part::Two); + let day1_result_part1 = day1::solve(utils::lines_from_file(day1_data_path).expect("Could not load the dataset for day 1"), Part::One); + let day1_result_part2 = day1::solve(utils::lines_from_file(day1_data_path).expect("Could not load the dataset for day 1"), Part::Two); assert_eq!(day1_result_part1, 54338); assert_eq!(day1_result_part2, 53389); @@ -29,8 +34,8 @@ mod tests { #[test] fn day2() { let data_path = "./datasets/adventofcode.com_2023_day_2_input.txt"; - let result = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"), day2::Part::One); - let result2 = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"), day2::Part::Two); + let result = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"), Part::One); + let result2 = day2::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for the day"),Part::Two); assert_eq!(result, 2795); @@ -40,8 +45,8 @@ mod tests { #[test] fn day3() { let data_path = "./datasets/adventofcode.com_2023_day_3_input.txt"; - let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), day3::Part::One); - let result2 = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), day3::Part::Two); + let result = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), Part::One); + let result2 = day3::solve(utils::lines_from_file(data_path).expect("Could not load the dataset for day 3"), Part::Two); assert_eq!(result, 522726); assert_eq!(result2, 81721933); diff --git a/src/utils.rs b/src/utils.rs index 34f0ad3..05b8ff3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -9,4 +9,9 @@ pub fn lines_from_file(filename: impl AsRef) -> Result> { let buf = BufReader::new(file); buf.lines().collect() +} + +pub enum Part { + One, + Two } \ No newline at end of file