feat: added day 22 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (19) (push) Successful in 2s
Build and run challenges / Challenge for day (2) (push) Successful in 4s
Build and run challenges / Challenge for day (20) (push) Successful in 3s
Build and run challenges / Challenge for day (22) (push) Successful in 3s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (1) (push) Successful in 4s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 3s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 3s
Build and run challenges / Challenge for day (17) (push) Successful in 4s
Build and run challenges / Challenge for day (21) (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 6s
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 (11) (push) Successful in 3s
Build and run challenges / Challenge for day (12) (push) Successful in 3s
Build and run challenges / Challenge for day (18) (push) Successful in 3s
Build and run challenges / Challenge for day (19) (push) Successful in 2s
Build and run challenges / Challenge for day (2) (push) Successful in 4s
Build and run challenges / Challenge for day (20) (push) Successful in 3s
Build and run challenges / Challenge for day (22) (push) Successful in 3s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (1) (push) Successful in 4s
Build and run challenges / Challenge for day (10) (push) Successful in 3s
Build and run challenges / Challenge for day (13) (push) Successful in 3s
Build and run challenges / Challenge for day (14) (push) Successful in 3s
Build and run challenges / Challenge for day (15) (push) Successful in 3s
Build and run challenges / Challenge for day (16) (push) Successful in 3s
Build and run challenges / Challenge for day (17) (push) Successful in 4s
Build and run challenges / Challenge for day (21) (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 6s
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
27060735f8
commit
aa65136b92
@ -11,7 +11,7 @@ jobs:
|
||||
name: Challenge for day
|
||||
strategy:
|
||||
matrix:
|
||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
|
||||
day_number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
|
||||
runs-on: rust-bookworm
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
|
6
day22/Cargo.toml
Normal file
6
day22/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day22"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
66
day22/src/main.rs
Normal file
66
day22/src/main.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use std::{env, fs};
|
||||
|
||||
const ITERATIONS: usize = 2000;
|
||||
|
||||
fn xor_shift(mut secret: i64) -> i64 {
|
||||
secret ^= secret << 6;
|
||||
secret &= (1 << 24) - 1;
|
||||
secret ^= secret >> 5;
|
||||
secret ^= secret << 11;
|
||||
secret &= (1 << 24) - 1;
|
||||
secret
|
||||
}
|
||||
|
||||
fn read_input(path: &str) -> String {
|
||||
fs::read_to_string(path).expect("Cannot read file.")
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> Vec<i64> {
|
||||
input
|
||||
.lines()
|
||||
.filter(|line| !line.is_empty())
|
||||
.map(|line| line.parse().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn compute_market(secrets: &Vec<i64>) -> (i64, i64) {
|
||||
let mut secrets_sum = 0;
|
||||
let (mut last_monkey, mut cache) = (vec![None; 160000], vec![0; 160000]);
|
||||
|
||||
for (monkey, &secret) in secrets.iter().enumerate() {
|
||||
let mut secret = secret;
|
||||
let mut previous_price = 0;
|
||||
let mut a;
|
||||
let (mut b, mut c, mut d) = (0, 0, 0);
|
||||
|
||||
for i in 0..ITERATIONS {
|
||||
secret = xor_shift(secret);
|
||||
let price = secret % 10;
|
||||
(a, b, c, d) = (b, c, d, 9 + price - previous_price);
|
||||
|
||||
if i > 2 {
|
||||
let key = (a * 6859 + b * 361 + c * 19 + d) as usize;
|
||||
if last_monkey[key].is_none() || last_monkey[key].unwrap() != monkey {
|
||||
last_monkey[key] = Some(monkey);
|
||||
cache[key] += price;
|
||||
}
|
||||
}
|
||||
previous_price = price;
|
||||
}
|
||||
secrets_sum += secret;
|
||||
}
|
||||
|
||||
(secrets_sum, *cache.iter().max().unwrap())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
for arg in args.iter().skip(1) {
|
||||
let input = read_input(&arg);
|
||||
let secrets = parse_input(&input);
|
||||
let (secrets_sum, best_price) = compute_market(&secrets);
|
||||
println!("[{}]", &arg);
|
||||
println!("\t[Part 1] => Answer is '{}'.", secrets_sum);
|
||||
println!("\t[Part 2] => Answer is '{}'.", best_price);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user