diff --git a/Cargo.lock b/Cargo.lock index d926385..a21cb3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,8 +7,13 @@ name = "AdventOfCode2025" version = "0.1.0" dependencies = [ "day01", + "day02", ] [[package]] name = "day01" version = "0.1.0" + +[[package]] +name = "day02" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 06ea18a..3078720 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2024" [dependencies] day01 = {path = "src/day01"} +day02 = {path = "src/day02"} diff --git a/src/day02/1.txt b/src/day02/1.txt new file mode 100644 index 0000000..34b9d7b --- /dev/null +++ b/src/day02/1.txt @@ -0,0 +1 @@ +1090286-1131879,3259566-3404881,138124-175118,266204727-266361099,16765-24272,7657360692-7657593676,88857504-88926597,6869078-6903096,48444999-48532270,61427792-61580535,71-103,8077-10421,1920-2560,2-17,951-1259,34-50,28994-36978,1309-1822,9393918461-9393960770,89479-120899,834641-988077,5389718924-5389797353,34010076-34214499,5063-7100,607034-753348,19098586-19261191,125085556-125188689,39839-51927,3246-5037,174-260,439715-473176,187287-262190,348-535,58956-78301,4388160-4505757,512092-584994,13388753-13534387 \ No newline at end of file diff --git a/src/day02/Cargo.lock b/src/day02/Cargo.lock new file mode 100644 index 0000000..2762917 --- /dev/null +++ b/src/day02/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day02" +version = "0.1.0" diff --git a/src/day02/Cargo.toml b/src/day02/Cargo.toml new file mode 100644 index 0000000..3304419 --- /dev/null +++ b/src/day02/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day02" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/day02/input02.txt b/src/day02/input02.txt new file mode 100644 index 0000000..34b9d7b --- /dev/null +++ b/src/day02/input02.txt @@ -0,0 +1 @@ +1090286-1131879,3259566-3404881,138124-175118,266204727-266361099,16765-24272,7657360692-7657593676,88857504-88926597,6869078-6903096,48444999-48532270,61427792-61580535,71-103,8077-10421,1920-2560,2-17,951-1259,34-50,28994-36978,1309-1822,9393918461-9393960770,89479-120899,834641-988077,5389718924-5389797353,34010076-34214499,5063-7100,607034-753348,19098586-19261191,125085556-125188689,39839-51927,3246-5037,174-260,439715-473176,187287-262190,348-535,58956-78301,4388160-4505757,512092-584994,13388753-13534387 \ No newline at end of file diff --git a/src/day02/src/lib.rs b/src/day02/src/lib.rs new file mode 100644 index 0000000..f89298c --- /dev/null +++ b/src/day02/src/lib.rs @@ -0,0 +1,2 @@ +pub mod part02a; +pub mod part02b; \ No newline at end of file diff --git a/src/day02/src/part02a.rs b/src/day02/src/part02a.rs new file mode 100644 index 0000000..f9f597a --- /dev/null +++ b/src/day02/src/part02a.rs @@ -0,0 +1,24 @@ +use std::fs::File; +use std::io::Read; + +pub fn solve() -> u64 { + let mut solution: u64 = 0; + + let mut file: File = File::open("src/day02/input02.txt").unwrap(); + let mut file_content: String = String::new(); + file.read_to_string(&mut file_content).expect("Can't read file"); + + file_content.split(",").for_each(|id_range| { + match id_range.split_once("-") { + Some((a, b)) => a.parse::().expect("invalid range start")..b.parse::().expect("invalid range end"), + None => panic!("Invalid id range"), + }.for_each(|id| { + let id_len = id.ilog10() as usize + 1; + if id_len % 2 == 1 { return } + let half_pow = 10usize.pow(id_len as u32 / 2); + if id / half_pow == id % half_pow { solution += id as u64 } + }); + }); + + solution +} \ No newline at end of file diff --git a/src/day02/src/part02b.rs b/src/day02/src/part02b.rs new file mode 100644 index 0000000..6e3eb14 --- /dev/null +++ b/src/day02/src/part02b.rs @@ -0,0 +1,45 @@ +use std::fs::File; +use std::io::Read; + +pub fn solve() -> u64 { + let mut solution: u64 = 0; + + let mut file: File = File::open("src/day02/input02.txt").unwrap(); + let mut file_content: String = String::new(); + file.read_to_string(&mut file_content).expect("Can't read file"); + + file_content.split(",").for_each(|id_range| { + match id_range.split_once("-") { + Some((a, b)) => a.parse::().expect("invalid range start")..b.parse::().expect("invalid range end"), + None => panic!("Invalid id range"), + }.for_each(|id| { + let id_len = id.ilog10() as usize + 1; + + // try subdivide into 1..len/2 numbers + for j in 1..id_len/2 + 1 { + // no clean subdivision + if id_len % j != 0 {continue} + + // perform subdivision as map + let mut i = (j..id_len+1).step_by(j).map(|i| + (id / 10usize.pow((id_len - i) as u32)) % 10usize.pow(j as u32) + ); + + // check or equal parts + let all_equal = if let Some(first) = i.next() { + i.all(|v| v == first) + } else { + true + }; + + // solution + if all_equal { + solution += id as u64; + return; + } + } + }); + }); + + solution +} \ No newline at end of file diff --git a/src/day02/story02a.txt b/src/day02/story02a.txt new file mode 100644 index 0000000..3042aba --- /dev/null +++ b/src/day02/story02a.txt @@ -0,0 +1,37 @@ +--- Day 2: Gift Shop --- + +You get inside and take the elevator to its only other stop: the gift shop. "Thank you for visiting the North Pole!" gleefully exclaims a nearby sign. You aren't sure who is even allowed to visit the North Pole, but you know you can access the lobby through here, and from there you can access the rest of the North Pole base. + +As you make your way through the surprisingly extensive selection, one of the clerks recognizes you and asks for your help. + +As it turns out, one of the younger Elves was playing on a gift shop computer and managed to add a whole bunch of invalid product IDs to their gift shop database! Surely, it would be no trouble for you to identify the invalid product IDs for them, right? + +They've even checked most of the product ID ranges already; they only have a few product ID ranges (your puzzle input) that you'll need to check. For example: + +11-22,95-115,998-1012,1188511880-1188511890,222220-222224, +1698522-1698528,446443-446449,38593856-38593862,565653-565659, +824824821-824824827,2121212118-2121212124 + +(The ID ranges are wrapped here for legibility; in your input, they appear on a single long line.) + +The ranges are separated by commas (,); each range gives its first ID and last ID separated by a dash (-). + +Since the young Elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs. + +None of the numbers have leading zeroes; 0101 isn't an ID at all. (101 is a valid ID that you would ignore.) + +Your job is to find all of the invalid IDs that appear in the given ranges. In the above example: + + 11-22 has two invalid IDs, 11 and 22. + 95-115 has one invalid ID, 99. + 998-1012 has one invalid ID, 1010. + 1188511880-1188511890 has one invalid ID, 1188511885. + 222220-222224 has one invalid ID, 222222. + 1698522-1698528 contains no invalid IDs. + 446443-446449 has one invalid ID, 446446. + 38593856-38593862 has one invalid ID, 38593859. + The rest of the ranges contain no invalid IDs. + +Adding up all the invalid IDs in this example produces 1227775554. + +What do you get if you add up all of the invalid IDs? \ No newline at end of file diff --git a/src/day02/story02b.txt b/src/day02/story02b.txt new file mode 100644 index 0000000..e438843 --- /dev/null +++ b/src/day02/story02b.txt @@ -0,0 +1,23 @@ +--- Part Two --- + +The clerk quickly discovers that there are still invalid IDs in the ranges in your list. Maybe the young Elf was doing other silly patterns as well? + +Now, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So, 12341234 (1234 two times), 123123123 (123 three times), 1212121212 (12 five times), and 1111111 (1 seven times) are all invalid IDs. + +From the same example as before: + + 11-22 still has two invalid IDs, 11 and 22. + 95-115 now has two invalid IDs, 99 and 111. + 998-1012 now has two invalid IDs, 999 and 1010. + 1188511880-1188511890 still has one invalid ID, 1188511885. + 222220-222224 still has one invalid ID, 222222. + 1698522-1698528 still contains no invalid IDs. + 446443-446449 still has one invalid ID, 446446. + 38593856-38593862 still has one invalid ID, 38593859. + 565653-565659 now has one invalid ID, 565656. + 824824821-824824827 now has one invalid ID, 824824824. + 2121212118-2121212124 now has one invalid ID, 2121212121. + +Adding up all the invalid IDs in this example produces 4174379265. + +What do you get if you add up all of the invalid IDs using these new rules? \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3736dff..1b300c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,21 @@ use std::time::Instant; use day01::{part01a, part01b}; +use day02::{part02a, part02b}; fn main() { let exercises: Vec<(&str, fn () -> u64)> = vec![ ("day01:A", part01a::solve), ("day01:B", part01b::solve), + ("day02:A", part02a::solve), + ("day02:B", part02b::solve), ]; let start: Instant = Instant::now(); exercises.iter().for_each(|(day, func)| { let day_start: Instant = Instant::now(); - println!("{:7} => {:10} ({:6}us)", day, func(), day_start.elapsed().as_micros()); + println!("{:7} => {:15} ({:6}us)", day, func(), day_start.elapsed().as_micros()); }); - println!(" ----------"); - println!(" {:7}us", start.elapsed().as_micros()); + println!(" ----------"); + println!(" {:7}us", start.elapsed().as_micros()); } \ No newline at end of file