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 (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
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
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 (4) (push) Successful in 5s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
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:
parent
15de4852fa
commit
c6718f86e5
@ -11,7 +11,7 @@ jobs:
|
|||||||
name: Challenge for day
|
name: Challenge for day
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
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
|
runs-on: rust-bookworm
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
|
6
day11/Cargo.toml
Normal file
6
day11/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day11"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
70
day11/src/main.rs
Normal file
70
day11/src/main.rs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::{env, fs};
|
||||||
|
|
||||||
|
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>, cache: &mut HashMap<(usize, usize), usize>) -> usize {
|
||||||
|
stones
|
||||||
|
.iter()
|
||||||
|
.map(|stone| compute_stone(*stone, 25, cache))
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(stones: &Vec<usize>, cache: &mut HashMap<(usize, usize), usize>) -> usize {
|
||||||
|
stones
|
||||||
|
.iter()
|
||||||
|
.map(|stone| compute_stone(*stone, 75, cache))
|
||||||
|
.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);
|
||||||
|
let mut cache = HashMap::new();
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", part_1(&stones, &mut cache));
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", part_2(&stones, &mut cache));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user