diff --git a/Day2/P1/Cargo.lock b/Day2/P1/Cargo.lock new file mode 100644 index 0000000..055c97d --- /dev/null +++ b/Day2/P1/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "P1" +version = "0.1.0" diff --git a/Day2/P1/Cargo.toml b/Day2/P1/Cargo.toml new file mode 100644 index 0000000..c6065ec --- /dev/null +++ b/Day2/P1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "P1" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/Day2/P1/example b/Day2/P1/example new file mode 100644 index 0000000..f35aacd --- /dev/null +++ b/Day2/P1/example @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/Day2/P1/src/main.rs b/Day2/P1/src/main.rs new file mode 100644 index 0000000..0dcace3 --- /dev/null +++ b/Day2/P1/src/main.rs @@ -0,0 +1,31 @@ +use std::fs; + +fn main() { + println!("Day 2P1"); + let contents = fs::read_to_string("test").expect("input not found"); + let mut bad_count = 0; + for range in contents.split(",") { + let mut parts = range.split("-"); + + let first_id = parts.next().unwrap(); + let last_id = parts.next().unwrap().trim(); + + bad_count += check_range(first_id, last_id); + } + print!("Total {bad_count}"); +} + +fn check_range(first: &str, last: &str) -> u64 { + let first_int: u64 = first.parse().unwrap(); + let last_int: u64 = last.parse().unwrap(); + let mut count = 0; + for n in first_int..=last_int { + let input = n.to_string(); + let parts = input.split_at(input.len() / 2); + if parts.0 == parts.1 { + println!("{input} is invalid"); + count += n; + } + } + count +} diff --git a/Day2/P1/test b/Day2/P1/test new file mode 100644 index 0000000..bede0a4 --- /dev/null +++ b/Day2/P1/test @@ -0,0 +1 @@ +7777742220-7777814718,3201990-3447830,49-86,653243-683065,91-129,24-41,1-15,2678-4638,1407-2511,221-504,867867-942148,1167452509-1167622686,9957459726-9957683116,379068-535983,757-1242,955118-1088945,297342-362801,548256-566461,4926-10075,736811-799457,1093342-1130060,620410-651225,65610339-65732429,992946118-993033511,5848473-5907215,17190619-17315301,203488-286290,15631-36109,5858509282-5858695889,87824047-87984031,1313113913-1313147594,795745221-795825571,46303-100636,4743038-4844422 diff --git a/Day2/P2_Regex/Cargo.lock b/Day2/P2_Regex/Cargo.lock new file mode 100644 index 0000000..e7d33b8 --- /dev/null +++ b/Day2/P2_Regex/Cargo.lock @@ -0,0 +1,81 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "P1" +version = "0.1.0" +dependencies = [ + "fancy-regex", + "regex", +] + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + +[[package]] +name = "fancy-regex" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "998b056554fbe42e03ae0e152895cd1a7e1002aec800fdc6635d20270260c46f" +dependencies = [ + "bit-set", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" diff --git a/Day2/P2_Regex/Cargo.toml b/Day2/P2_Regex/Cargo.toml new file mode 100644 index 0000000..562bd0b --- /dev/null +++ b/Day2/P2_Regex/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "P2_Regex" +version = "0.1.0" +edition = "2024" + +[dependencies] +fancy-regex = "0.16.2" +regex = "1.12.2" diff --git a/Day2/P2_Regex/example b/Day2/P2_Regex/example new file mode 100644 index 0000000..f35aacd --- /dev/null +++ b/Day2/P2_Regex/example @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 diff --git a/Day2/P2_Regex/src/main.rs b/Day2/P2_Regex/src/main.rs new file mode 100644 index 0000000..c65c740 --- /dev/null +++ b/Day2/P2_Regex/src/main.rs @@ -0,0 +1,34 @@ +use std::{fs, sync::LazyLock}; + +use fancy_regex::Regex; + +static PATTERN: LazyLock = LazyLock::new(|| Regex::new(r"^(\d+)\1+$").unwrap()); + +fn main() { + println!("Day 2P2"); + let contents = fs::read_to_string("test").expect("input not found"); + let mut bad_count = 0; + for range in contents.split(",") { + let mut parts = range.split("-"); + + let first_id = parts.next().unwrap(); + let last_id = parts.next().unwrap().trim(); + + bad_count += check_range(first_id, last_id); + } + print!("Total {bad_count}"); +} + +fn check_range(first: &str, last: &str) -> u64 { + let first_int: u64 = first.parse().unwrap(); + let last_int: u64 = last.parse().unwrap(); + let mut count = 0; + for n in first_int..=last_int { + let input = n.to_string(); + let res = PATTERN.is_match(&input).unwrap(); + if res { + count += n; + } + } + count +} diff --git a/Day2/P2_Regex/test b/Day2/P2_Regex/test new file mode 100644 index 0000000..bede0a4 --- /dev/null +++ b/Day2/P2_Regex/test @@ -0,0 +1 @@ +7777742220-7777814718,3201990-3447830,49-86,653243-683065,91-129,24-41,1-15,2678-4638,1407-2511,221-504,867867-942148,1167452509-1167622686,9957459726-9957683116,379068-535983,757-1242,955118-1088945,297342-362801,548256-566461,4926-10075,736811-799457,1093342-1130060,620410-651225,65610339-65732429,992946118-993033511,5848473-5907215,17190619-17315301,203488-286290,15631-36109,5858509282-5858695889,87824047-87984031,1313113913-1313147594,795745221-795825571,46303-100636,4743038-4844422