day22 (spaghetti)
This commit is contained in:
parent
d87aad0252
commit
37a4505f45
1 changed files with 60 additions and 2 deletions
|
|
@ -1,13 +1,71 @@
|
|||
use std::{collections::HashMap, iter::zip};
|
||||
|
||||
use itertools::Itertools;
|
||||
use shared::{Solution, Answer};
|
||||
|
||||
pub struct Day22;
|
||||
|
||||
impl Solution for Day22 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
let data = parse(input);
|
||||
|
||||
let out = data.iter().map(|x| {
|
||||
let mut y = *x;
|
||||
for _ in 0..2000 {
|
||||
y = secret(y);
|
||||
}
|
||||
y
|
||||
}).sum();
|
||||
|
||||
Answer::Number(out)
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
let data = parse(input);
|
||||
|
||||
let prices = data.iter().map(|x| {
|
||||
let mut p: [i64; 2001] = [0; 2001];
|
||||
p[0] = *x as i64;
|
||||
for i in 1..=2000 {
|
||||
p[i] = secret(p[i-1] as u64) as i64;
|
||||
}
|
||||
p.map(|z| z%10)
|
||||
}).collect_vec();
|
||||
|
||||
let deltas = prices.iter().map(|p| p.windows(2).map(|x| x[1] - x[0]).collect_vec()).collect_vec();
|
||||
let ones = prices.iter().map(|p| p.iter().skip(4).collect_vec()).collect_vec();
|
||||
|
||||
let mut finals: HashMap<Vec<i64>, i64> = HashMap::new();
|
||||
|
||||
for (d, p) in zip(deltas, ones) {
|
||||
let mut prices_per_code: HashMap<Vec<i64>, i64> = HashMap::new();
|
||||
for (i, window) in d.windows(4).enumerate() {
|
||||
if !prices_per_code.contains_key(window) { prices_per_code.insert(window.to_owned(), *p[i]); };
|
||||
}
|
||||
|
||||
prices_per_code.iter()
|
||||
.for_each(|(key, value)| { match finals.get(key) {
|
||||
None => finals.insert(key.to_owned(), *value),
|
||||
Some(x) => finals.insert(key.to_owned(), value + x),
|
||||
}; }
|
||||
);
|
||||
}
|
||||
|
||||
let out: i64 = finals.into_values().max().unwrap();
|
||||
|
||||
Answer::Number(out as u64)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Vec<u64> {
|
||||
input.trim()
|
||||
.lines()
|
||||
.map(|x| x.parse::<u64>().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn secret(x: u64) -> u64 {
|
||||
let x = ((x * 64) ^ x) % 16777216;
|
||||
let x = ((x / 32) ^ x) % 16777216;
|
||||
((x * 2048) ^ x) % 16777216
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue