diff --git a/README.md b/README.md index 8575bec..e108dc9 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Advent of Code 2025 ```bash -day01:A => 1150 ( 130us) -day01:B => 6738 ( 161us) -day02:A => 31839939622 ( 14191us) -day02:B => 41662374059 (135384us) -day03:A => 16812 ( 154us) -day03:B => 166345822896410 ( 356us) -day04:A => 1518 ( 304us) +day01:A => 1150 ( 137us) +day01:B => 6738 ( 170us) +day02:A => 31839939622 ( 13640us) +day02:B => 41662374059 (129163us) +day03:A => 16812 ( 169us) +day03:B => 166345822896410 ( 412us) +day04:A => 1518 ( 274us) day04:B => 0 ( 9us) ---------- - 150768us + 144047us ``` \ No newline at end of file diff --git a/src/day01/src/part01a.rs b/src/day01/src/part01a.rs index ed35cb0..951ec88 100644 --- a/src/day01/src/part01a.rs +++ b/src/day01/src/part01a.rs @@ -3,10 +3,11 @@ use std::io::Read; pub fn solve() -> u64 { let mut solution: u64 = 0; - + let mut file: File = File::open("src/day01/input01.txt").unwrap(); let mut file_content: String = String::new(); - file.read_to_string(&mut file_content).expect("Can't read file"); + file.read_to_string(&mut file_content) + .expect("Can't read file"); let mut dial: i32 = 50; file_content.split("\n").enumerate().for_each(|(_, line)| { @@ -23,4 +24,5 @@ pub fn solve() -> u64 { }); solution -} \ No newline at end of file +} + diff --git a/src/day01/src/part01b.rs b/src/day01/src/part01b.rs index c7ab4de..ec8d7bf 100644 --- a/src/day01/src/part01b.rs +++ b/src/day01/src/part01b.rs @@ -6,7 +6,8 @@ pub fn solve() -> u64 { let mut file: File = File::open("src/day01/input01.txt").unwrap(); let mut file_content: String = String::new(); - file.read_to_string(&mut file_content).expect("Can't read file"); + file.read_to_string(&mut file_content) + .expect("Can't read file"); let mut dial: i32 = 50; file_content.split("\n").enumerate().for_each(|(_, line)| { @@ -28,11 +29,12 @@ pub fn solve() -> u64 { solution += 1; dial -= 100; } - dial = (dial + 100000) % 100; + dial = (dial + 100000) % 100; if dial == 0 { solution += 1; } }); solution -} \ No newline at end of file +} + diff --git a/src/day02/src/part02a.rs b/src/day02/src/part02a.rs index f9f597a..7123b67 100644 --- a/src/day02/src/part02a.rs +++ b/src/day02/src/part02a.rs @@ -6,19 +6,29 @@ pub fn solve() -> u64 { 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.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"), + Some((a, b)) => { + a.parse::().expect("invalid range start") + ..b.parse::().expect("invalid range end") + } None => panic!("Invalid id range"), - }.for_each(|id| { + } + .for_each(|id| { let id_len = id.ilog10() as usize + 1; - if id_len % 2 == 1 { return } + 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 } + 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 index 6e3eb14..e1febee 100644 --- a/src/day02/src/part02b.rs +++ b/src/day02/src/part02b.rs @@ -6,24 +6,31 @@ pub fn solve() -> u64 { 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.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"), + Some((a, b)) => { + a.parse::().expect("invalid range start") + ..b.parse::().expect("invalid range end") + } None => panic!("Invalid id range"), - }.for_each(|id| { + } + .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 { + for j in 1..id_len / 2 + 1 { // no clean subdivision - if id_len % j != 0 {continue} + 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) - ); + 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() { @@ -42,4 +49,5 @@ pub fn solve() -> u64 { }); solution -} \ No newline at end of file +} + diff --git a/src/day03/Cargo.lock b/src/day03/Cargo.lock index 3ea7b86..169f416 100644 --- a/src/day03/Cargo.lock +++ b/src/day03/Cargo.lock @@ -5,3 +5,12 @@ version = 4 [[package]] name = "day03" version = "0.1.0" +dependencies = [ + "iter-first-max", +] + +[[package]] +name = "iter-first-max" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ace18d32276b4ce7d8261c06b376398a4ea82e4715a5b37e46fddbff41a617" diff --git a/src/day03/src/part03a.rs b/src/day03/src/part03a.rs index 3504887..fb4b6a1 100644 --- a/src/day03/src/part03a.rs +++ b/src/day03/src/part03a.rs @@ -1,23 +1,32 @@ +use iter_first_max::IterFirstMaxExt; use std::fs::File; use std::io::Read; -use iter_first_max::IterFirstMaxExt; pub fn solve() -> u64 { let mut solution: u64 = 0; let mut file: File = File::open("src/day03/input03.txt").unwrap(); let mut file_content: String = String::new(); - file.read_to_string(&mut file_content).expect("Can't read file"); + file.read_to_string(&mut file_content) + .expect("Can't read file"); file_content.lines().for_each(|battery_bank| { - let batteries = battery_bank.chars().map(|c| c as u8 - 0x30).collect::>(); - let (i, biggest_battery) = batteries.iter().enumerate().first_max_by_key(|(_, v)| *v).unwrap(); - solution += if i < battery_bank.len()-1 { - 10 * biggest_battery + batteries[i+1..].iter().max().unwrap() + let batteries = battery_bank + .chars() + .map(|c| c as u8 - 0x30) + .collect::>(); + let (i, biggest_battery) = batteries + .iter() + .enumerate() + .first_max_by_key(|(_, v)| *v) + .unwrap(); + solution += if i < battery_bank.len() - 1 { + 10 * biggest_battery + batteries[i + 1..].iter().max().unwrap() } else { 10 * batteries[..i].iter().max().unwrap() + biggest_battery } as u64; }); solution -} \ No newline at end of file +} + diff --git a/src/day03/src/part03b.rs b/src/day03/src/part03b.rs index fc177e9..9e191ed 100644 --- a/src/day03/src/part03b.rs +++ b/src/day03/src/part03b.rs @@ -1,6 +1,6 @@ +use iter_first_max::IterFirstMaxExt; use std::fs::File; use std::io::Read; -use iter_first_max::IterFirstMaxExt; fn get_n_max(vec: &Vec, n: usize) -> Vec { let mut n = n; @@ -14,10 +14,9 @@ fn get_n_max(vec: &Vec, n: usize) -> Vec { max.push(*biggest); if i > vec.len() - 1 - n { n -= vec.len() - i - 1; - max.append(&mut vec[i+1..].to_vec()); - } - else { - let mut found = get_n_max(&vec[i+1..].to_vec(), n); + max.append(&mut vec[i + 1..].to_vec()); + } else { + let mut found = get_n_max(&vec[i + 1..].to_vec(), n); n -= found.len(); max.append(&mut found); } @@ -39,14 +38,22 @@ pub fn solve() -> u64 { let mut file: File = File::open("src/day03/input03.txt").unwrap(); let mut file_content: String = String::new(); - file.read_to_string(&mut file_content).expect("Can't read file"); + file.read_to_string(&mut file_content) + .expect("Can't read file"); file_content.lines().for_each(|battery_bank| { - let batteries = battery_bank.chars().map(|c| c as u8 - 0x30).collect::>(); - get_n_max(&batteries, 12).iter().enumerate().for_each(|(i, battery)| { - solution += *battery as u64 * 10u64.pow(11 - i as u32); - }); + let batteries = battery_bank + .chars() + .map(|c| c as u8 - 0x30) + .collect::>(); + get_n_max(&batteries, 12) + .iter() + .enumerate() + .for_each(|(i, battery)| { + solution += *battery as u64 * 10u64.pow(11 - i as u32); + }); }); solution -} \ No newline at end of file +} +