formatted everything with rustfmt
This commit is contained in:
16
README.md
16
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
|
||||
```
|
||||
@@ -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)| {
|
||||
@@ -24,3 +25,4 @@ pub fn solve() -> u64 {
|
||||
|
||||
solution
|
||||
}
|
||||
|
||||
|
||||
@@ -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,7 +29,7 @@ pub fn solve() -> u64 {
|
||||
solution += 1;
|
||||
dial -= 100;
|
||||
}
|
||||
dial = (dial + 100000) % 100;
|
||||
dial = (dial + 100000) % 100;
|
||||
if dial == 0 {
|
||||
solution += 1;
|
||||
}
|
||||
@@ -36,3 +37,4 @@ pub fn solve() -> u64 {
|
||||
|
||||
solution
|
||||
}
|
||||
|
||||
|
||||
@@ -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::<usize>().expect("invalid range start")..b.parse::<usize>().expect("invalid range end"),
|
||||
Some((a, b)) => {
|
||||
a.parse::<usize>().expect("invalid range start")
|
||||
..b.parse::<usize>().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
|
||||
}
|
||||
|
||||
|
||||
@@ -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::<usize>().expect("invalid range start")..b.parse::<usize>().expect("invalid range end"),
|
||||
Some((a, b)) => {
|
||||
a.parse::<usize>().expect("invalid range start")
|
||||
..b.parse::<usize>().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() {
|
||||
@@ -43,3 +50,4 @@ pub fn solve() -> u64 {
|
||||
|
||||
solution
|
||||
}
|
||||
|
||||
|
||||
9
src/day03/Cargo.lock
generated
9
src/day03/Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
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::<Vec<u8>>();
|
||||
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::<Vec<u8>>();
|
||||
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;
|
||||
@@ -21,3 +29,4 @@ pub fn solve() -> u64 {
|
||||
|
||||
solution
|
||||
}
|
||||
|
||||
|
||||
@@ -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<u8>, n: usize) -> Vec<u8> {
|
||||
let mut n = n;
|
||||
@@ -14,10 +14,9 @@ fn get_n_max(vec: &Vec<u8>, n: usize) -> Vec<u8> {
|
||||
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::<Vec<u8>>();
|
||||
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::<Vec<u8>>();
|
||||
get_n_max(&batteries, 12)
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(i, battery)| {
|
||||
solution += *battery as u64 * 10u64.pow(11 - i as u32);
|
||||
});
|
||||
});
|
||||
|
||||
solution
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user