formatted everything with rustfmt
This commit is contained in:
16
README.md
16
README.md
@@ -1,13 +1,13 @@
|
|||||||
# Advent of Code 2025
|
# Advent of Code 2025
|
||||||
```bash
|
```bash
|
||||||
day01:A => 1150 ( 130us)
|
day01:A => 1150 ( 137us)
|
||||||
day01:B => 6738 ( 161us)
|
day01:B => 6738 ( 170us)
|
||||||
day02:A => 31839939622 ( 14191us)
|
day02:A => 31839939622 ( 13640us)
|
||||||
day02:B => 41662374059 (135384us)
|
day02:B => 41662374059 (129163us)
|
||||||
day03:A => 16812 ( 154us)
|
day03:A => 16812 ( 169us)
|
||||||
day03:B => 166345822896410 ( 356us)
|
day03:B => 166345822896410 ( 412us)
|
||||||
day04:A => 1518 ( 304us)
|
day04:A => 1518 ( 274us)
|
||||||
day04:B => 0 ( 9us)
|
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: File = File::open("src/day01/input01.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
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;
|
let mut dial: i32 = 50;
|
||||||
file_content.split("\n").enumerate().for_each(|(_, line)| {
|
file_content.split("\n").enumerate().for_each(|(_, line)| {
|
||||||
@@ -24,3 +25,4 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
let mut file: File = File::open("src/day01/input01.txt").unwrap();
|
let mut file: File = File::open("src/day01/input01.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
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;
|
let mut dial: i32 = 50;
|
||||||
file_content.split("\n").enumerate().for_each(|(_, line)| {
|
file_content.split("\n").enumerate().for_each(|(_, line)| {
|
||||||
@@ -36,3 +37,4 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,19 +6,29 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
let mut file: File = File::open("src/day02/input02.txt").unwrap();
|
let mut file: File = File::open("src/day02/input02.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
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| {
|
file_content.split(",").for_each(|id_range| {
|
||||||
match id_range.split_once("-") {
|
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"),
|
None => panic!("Invalid id range"),
|
||||||
}.for_each(|id| {
|
}
|
||||||
|
.for_each(|id| {
|
||||||
let id_len = id.ilog10() as usize + 1;
|
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);
|
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
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,24 +6,31 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
let mut file: File = File::open("src/day02/input02.txt").unwrap();
|
let mut file: File = File::open("src/day02/input02.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
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| {
|
file_content.split(",").for_each(|id_range| {
|
||||||
match id_range.split_once("-") {
|
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"),
|
None => panic!("Invalid id range"),
|
||||||
}.for_each(|id| {
|
}
|
||||||
|
.for_each(|id| {
|
||||||
let id_len = id.ilog10() as usize + 1;
|
let id_len = id.ilog10() as usize + 1;
|
||||||
|
|
||||||
// try subdivide into 1..len/2 numbers
|
// 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
|
// no clean subdivision
|
||||||
if id_len % j != 0 {continue}
|
if id_len % j != 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// perform subdivision as map
|
// perform subdivision as map
|
||||||
let mut i = (j..id_len+1).step_by(j).map(|i|
|
let mut i = (j..id_len + 1)
|
||||||
(id / 10usize.pow((id_len - i) as u32)) % 10usize.pow(j as u32)
|
.step_by(j)
|
||||||
);
|
.map(|i| (id / 10usize.pow((id_len - i) as u32)) % 10usize.pow(j as u32));
|
||||||
|
|
||||||
// check or equal parts
|
// check or equal parts
|
||||||
let all_equal = if let Some(first) = i.next() {
|
let all_equal = if let Some(first) = i.next() {
|
||||||
@@ -43,3 +50,4 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
src/day03/Cargo.lock
generated
9
src/day03/Cargo.lock
generated
@@ -5,3 +5,12 @@ version = 4
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "day03"
|
name = "day03"
|
||||||
version = "0.1.0"
|
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::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use iter_first_max::IterFirstMaxExt;
|
|
||||||
|
|
||||||
pub fn solve() -> u64 {
|
pub fn solve() -> u64 {
|
||||||
let mut solution: u64 = 0;
|
let mut solution: u64 = 0;
|
||||||
|
|
||||||
let mut file: File = File::open("src/day03/input03.txt").unwrap();
|
let mut file: File = File::open("src/day03/input03.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
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| {
|
file_content.lines().for_each(|battery_bank| {
|
||||||
let batteries = battery_bank.chars().map(|c| c as u8 - 0x30).collect::<Vec<u8>>();
|
let batteries = battery_bank
|
||||||
let (i, biggest_battery) = batteries.iter().enumerate().first_max_by_key(|(_, v)| *v).unwrap();
|
.chars()
|
||||||
solution += if i < battery_bank.len()-1 {
|
.map(|c| c as u8 - 0x30)
|
||||||
10 * biggest_battery + batteries[i+1..].iter().max().unwrap()
|
.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 {
|
} else {
|
||||||
10 * batteries[..i].iter().max().unwrap() + biggest_battery
|
10 * batteries[..i].iter().max().unwrap() + biggest_battery
|
||||||
} as u64;
|
} as u64;
|
||||||
@@ -21,3 +29,4 @@ pub fn solve() -> u64 {
|
|||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
use iter_first_max::IterFirstMaxExt;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use iter_first_max::IterFirstMaxExt;
|
|
||||||
|
|
||||||
fn get_n_max(vec: &Vec<u8>, n: usize) -> Vec<u8> {
|
fn get_n_max(vec: &Vec<u8>, n: usize) -> Vec<u8> {
|
||||||
let mut n = n;
|
let mut n = n;
|
||||||
@@ -14,10 +14,9 @@ fn get_n_max(vec: &Vec<u8>, n: usize) -> Vec<u8> {
|
|||||||
max.push(*biggest);
|
max.push(*biggest);
|
||||||
if i > vec.len() - 1 - n {
|
if i > vec.len() - 1 - n {
|
||||||
n -= vec.len() - i - 1;
|
n -= vec.len() - i - 1;
|
||||||
max.append(&mut vec[i+1..].to_vec());
|
max.append(&mut vec[i + 1..].to_vec());
|
||||||
}
|
} else {
|
||||||
else {
|
let mut found = get_n_max(&vec[i + 1..].to_vec(), n);
|
||||||
let mut found = get_n_max(&vec[i+1..].to_vec(), n);
|
|
||||||
n -= found.len();
|
n -= found.len();
|
||||||
max.append(&mut found);
|
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: File = File::open("src/day03/input03.txt").unwrap();
|
||||||
let mut file_content: String = String::new();
|
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| {
|
file_content.lines().for_each(|battery_bank| {
|
||||||
let batteries = battery_bank.chars().map(|c| c as u8 - 0x30).collect::<Vec<u8>>();
|
let batteries = battery_bank
|
||||||
get_n_max(&batteries, 12).iter().enumerate().for_each(|(i, battery)| {
|
.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 += *battery as u64 * 10u64.pow(11 - i as u32);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user