Compare commits
2 Commits
3b9ca8cf2e
...
0b64a90112
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b64a90112 | |||
| ba824eadbe |
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)| {
|
||||
@@ -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 {
|
||||
// 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,17 +1,25 @@
|
||||
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();
|
||||
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 {
|
||||
@@ -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;
|
||||
@@ -15,8 +15,7 @@ fn get_n_max(vec: &Vec<u8>, n: usize) -> Vec<u8> {
|
||||
if i > vec.len() - 1 - n {
|
||||
n -= vec.len() - i - 1;
|
||||
max.append(&mut vec[i + 1..].to_vec());
|
||||
}
|
||||
else {
|
||||
} 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)| {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +1,41 @@
|
||||
--- Day 3: Lobby ---
|
||||
--- Day 4: Printing Department ---
|
||||
|
||||
You descend a short staircase, enter the surprisingly vast lobby, and are quickly cleared by the security checkpoint. When you get to the main elevators, however, you discover that each one has a red light above it: they're all offline.
|
||||
You ride the escalator down to the printing department. They're clearly getting ready for Christmas; they have lots of large rolls of paper everywhere, and there's even a massive printer in the corner (to handle the really big print jobs).
|
||||
|
||||
"Sorry about that," an Elf apologizes as she tinkers with a nearby control panel. "Some kind of electrical surge seems to have fried them. I'll try to get them online soon."
|
||||
Decorating here will be easy: they can make their own decorations. What you really need is a way to get further into the North Pole base while the elevators are offline.
|
||||
|
||||
You explain your need to get further underground. "Well, you could at least take the escalator down to the printing department, not that you'd get much further than that without the elevators working. That is, you could if the escalator weren't also offline."
|
||||
"Actually, maybe we can help with that," one of the Elves replies when you ask for help. "We're pretty sure there's a cafeteria on the other side of the back wall. If we could break through the wall, you'd be able to keep moving. It's too bad all of our forklifts are so busy moving those big rolls of paper around."
|
||||
|
||||
"But, don't worry! It's not fried; it just needs power. Maybe you can get it running while I keep working on the elevators."
|
||||
If you can optimize the work the forklifts are doing, maybe they would have time to spare to break through the wall.
|
||||
|
||||
There are batteries nearby that can supply emergency power to the escalator for just such an occasion. The batteries are each labeled with their joltage rating, a value from 1 to 9. You make a note of their joltage ratings (your puzzle input). For example:
|
||||
The rolls of paper (@) are arranged on a large grid; the Elves even have a helpful diagram (your puzzle input) indicating where everything is located.
|
||||
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
For example:
|
||||
|
||||
The batteries are arranged into banks; each line of digits in your input corresponds to a single bank of batteries. Within each bank, you need to turn on exactly two batteries; the joltage that the bank produces is equal to the number formed by the digits on the batteries you've turned on. For example, if you have a bank like 12345 and you turn on batteries 2 and 4, the bank would produce 24 jolts. (You cannot rearrange batteries.)
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
|
||||
You'll need to find the largest possible joltage each bank can produce. In the above example:
|
||||
The forklifts can only access a roll of paper if there are fewer than four rolls of paper in the eight adjacent positions. If you can figure out which rolls of paper the forklifts can access, they'll spend less time looking and more time breaking down the wall to the cafeteria.
|
||||
|
||||
In 987654321111111, you can make the largest joltage possible, 98, by turning on the first two batteries.
|
||||
In 811111111111119, you can make the largest joltage possible by turning on the batteries labeled 8 and 9, producing 89 jolts.
|
||||
In 234234234234278, you can make 78 by turning on the last two batteries (marked 7 and 8).
|
||||
In 818181911112111, the largest joltage you can produce is 92.
|
||||
In this example, there are 13 rolls of paper that can be accessed by a forklift (marked with x):
|
||||
|
||||
The total output joltage is the sum of the maximum joltage from each bank, so in this example, the total output joltage is 98 + 89 + 78 + 92 = 357.
|
||||
..xx.xx@x.
|
||||
x@@.@.@.@@
|
||||
@@@@@.x.@@
|
||||
@.@@@@..@.
|
||||
x@.@@@@.@x
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
x.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
x.x.@@@.x.
|
||||
|
||||
There are many batteries in front of you. Find the maximum joltage possible from each bank; what is the total output joltage?
|
||||
Consider your complete diagram of the paper roll locations. How many rolls of paper can be accessed by a forklift?
|
||||
@@ -1,25 +1,131 @@
|
||||
--- Part Two ---
|
||||
|
||||
The escalator doesn't move. The Elf explains that it probably needs more joltage to overcome the static friction of the system and hits the big red "joltage limit safety override" button. You lose count of the number of times she needs to confirm "yes, I'm sure" and decorate the lobby a bit while you wait.
|
||||
Now, the Elves just need help accessing as much of the paper as they can.
|
||||
|
||||
Now, you need to make the largest joltage by turning on exactly twelve batteries within each bank.
|
||||
Once a roll of paper can be accessed by a forklift, it can be removed. Once a roll of paper is removed, the forklifts might be able to access more rolls of paper, which they might also be able to remove. How many total rolls of paper could the Elves remove if they keep repeating this process?
|
||||
|
||||
The joltage output for the bank is still the number formed by the digits of the batteries you've turned on; the only difference is that now there will be 12 digits in each bank's joltage output instead of two.
|
||||
Starting with the same example as above, here is one way you could remove as many rolls of paper as possible, using highlighted @ to indicate that a roll of paper is about to be removed, and using x to indicate that a roll of paper was just removed:
|
||||
|
||||
Consider again the example from before:
|
||||
Initial state:
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
Remove 13 rolls of paper:
|
||||
..xx.xx@x.
|
||||
x@@.@.@.@@
|
||||
@@@@@.x.@@
|
||||
@.@@@@..@.
|
||||
x@.@@@@.@x
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
x.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
x.x.@@@.x.
|
||||
|
||||
Now, the joltages are much larger:
|
||||
Remove 12 rolls of paper:
|
||||
.......x..
|
||||
.@@.x.x.@x
|
||||
x@@@@...@@
|
||||
x.@@@@..x.
|
||||
.@.@@@@.x.
|
||||
.x@@@@@@.x
|
||||
.x.@.@.@@@
|
||||
..@@@.@@@@
|
||||
.x@@@@@@@.
|
||||
....@@@...
|
||||
|
||||
In 987654321111111, the largest joltage can be found by turning on everything except some 1s at the end to produce 987654321111.
|
||||
In the digit sequence 811111111111119, the largest joltage can be found by turning on everything except some 1s, producing 811111111119.
|
||||
In 234234234234278, the largest joltage can be found by turning on everything except a 2 battery, a 3 battery, and another 2 battery near the start to produce 434234234278.
|
||||
In 818181911112111, the joltage 888911112111 is produced by turning on everything except some 1s near the front.
|
||||
Remove 7 rolls of paper:
|
||||
..........
|
||||
.x@.....x.
|
||||
.@@@@...xx
|
||||
..@@@@....
|
||||
.x.@@@@...
|
||||
..@@@@@@..
|
||||
...@.@.@@x
|
||||
..@@@.@@@@
|
||||
..x@@@@@@.
|
||||
....@@@...
|
||||
|
||||
The total output joltage is now much larger: 987654321111 + 811111111119 + 434234234278 + 888911112111 = 3121910778619.
|
||||
Remove 5 rolls of paper:
|
||||
..........
|
||||
..x.......
|
||||
.x@@@.....
|
||||
..@@@@....
|
||||
...@@@@...
|
||||
..x@@@@@..
|
||||
...@.@.@@.
|
||||
..x@@.@@@x
|
||||
...@@@@@@.
|
||||
....@@@...
|
||||
|
||||
What is the new total output joltage?
|
||||
Remove 2 rolls of paper:
|
||||
..........
|
||||
..........
|
||||
..x@@.....
|
||||
..@@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@x.
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
...@@.....
|
||||
..x@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
...x@.....
|
||||
...@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
....x.....
|
||||
...@@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Remove 1 roll of paper:
|
||||
..........
|
||||
..........
|
||||
..........
|
||||
...x@@....
|
||||
...@@@@...
|
||||
...@@@@@..
|
||||
...@.@.@@.
|
||||
...@@.@@@.
|
||||
...@@@@@..
|
||||
....@@@...
|
||||
|
||||
Stop once no more rolls of paper are accessible by a forklift. In this example, a total of 43 rolls of paper can be removed.
|
||||
|
||||
Start with your original diagram. How many rolls of paper in total can be removed by the Elves and their forklifts?
|
||||
@@ -2,11 +2,9 @@ use day01::{part01a, part01b};
|
||||
use day02::{part02a, part02b};
|
||||
use day03::{part03a, part03b};
|
||||
use day04::{part04a, part04b};
|
||||
use std::fmt::format;
|
||||
use std::time::Instant;
|
||||
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Result;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user