feat: added day 11 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (4) (push) Successful in 4s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (7) (push) Successful in 4s
Build and run challenges / Challenge for day (8) (push) Successful in 3s
Build and run challenges / Challenge for day (9) (push) Successful in 3s

Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
Louis Vallat 2024-12-12 11:51:49 +01:00
parent 15de4852fa
commit a8d826b94e
Signed by: louis
SSH Key Fingerprint: SHA256:0iPwDU/PZtEy/K13Oar4TzmcunmI9H5U9IsOR3jyT/Q
3 changed files with 65 additions and 1 deletions

View File

@ -11,7 +11,7 @@ jobs:
name: Challenge for day
strategy:
matrix:
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
runs-on: rust-bookworm
steps:
- name: Check out repository code

6
day11/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day11"
version = "0.1.0"
edition = "2021"
[dependencies]

58
day11/src/main.rs Normal file
View File

@ -0,0 +1,58 @@
use std::{env, fs};
use std::collections::HashMap;
fn read_input(path: &str) -> String {
fs::read_to_string(path).expect("Cannot read file.")
}
fn parse_input(input: &str) -> Vec<usize> {
input.split_whitespace().map(|x| x.parse().unwrap()).collect()
}
fn compute_stone(value: usize, remaining_blinks: usize, cache: &mut HashMap<(usize, usize), usize>) -> usize {
if cache.contains_key(&(value, remaining_blinks)) {
return *cache.get(&(value, remaining_blinks)).unwrap();
}
if remaining_blinks == 0 {
return 1;
}
let result;
if value == 0 {
result = compute_stone(1, remaining_blinks - 1, cache);
} else {
let digit = value.to_string();
if digit.len() % 2 == 0 {
let split = digit.split_at(digit.len() / 2);
result = compute_stone(split.0.parse().unwrap(), remaining_blinks - 1, cache)
+ compute_stone(split.1.parse().unwrap(), remaining_blinks - 1, cache);
} else {
result = compute_stone(value * 2024, remaining_blinks - 1, cache);
}
};
cache.insert((value, remaining_blinks), result);
result
}
fn part_1(stones: &Vec<usize>) -> usize {
stones.iter().map(|stone| compute_stone(*stone, 25, &mut HashMap::new())).sum()
}
fn part_2(stones: &Vec<usize>) -> usize {
stones.iter().map(|stone| compute_stone(*stone, 75, &mut HashMap::new())).sum()
}
fn main() {
let args: Vec<String> = env::args().collect();
for arg in args.iter().skip(1) {
let input = read_input(&arg);
let stones = parse_input(&input);
println!("[{}]", &arg);
println!("\t[Part 1] => Answer is '{}'.", part_1(&stones));
println!("\t[Part 2] => Answer is '{}'.", part_2(&stones));
}
}