Add day 20

This commit is contained in:
Sebastian Knackstedt 2022-12-20 18:07:58 +01:00
parent 47a7381575
commit a264e1e6d3
Signed by: sebastian
GPG Key ID: CDCD9AF904D93EF7
3 changed files with 5057 additions and 1 deletions

5000
day20.txt Normal file

File diff suppressed because it is too large Load Diff

7
day20_test.txt Normal file
View File

@ -0,0 +1,7 @@
1
2
-3
3
-2
0
4

View File

@ -1,3 +1,52 @@
fn main() { use base::read_file;
println!("Not ready");
fn do_encryption(key: &Vec<(usize, i64)>, data: &mut Vec<(usize, i64)>) {
let wrap_size = data.len() - 1;
for (index_to_find, enc_datum) in key {
let index = data.iter().position(|(org_index, _)| org_index == index_to_find).unwrap();
if *enc_datum == 0 {
continue;
}
let element = data.remove(index);
if *enc_datum > 0 {
let enc_datum = *enc_datum as usize;
data.rotate_left((enc_datum % wrap_size) as usize);
} else {
let enc_datum = enc_datum.abs() as usize;
data.rotate_right((enc_datum % wrap_size) as usize);
}
data.insert(index, element);
}
}
fn calc_result(data: Vec<(usize, i64)>) -> i64 {
let zero_index = data.iter().position(|(_, value)| *value == 0).unwrap();
println!("Found zero at {}", zero_index);
let wrap_size = data.len();
let number_1 = data[(zero_index + 1000) % wrap_size].1;
let number_2 = data[(zero_index + 2000) % wrap_size].1;
let number_3 = data[(zero_index + 3000) % wrap_size].1;
number_1 + number_2 + number_3
}
fn main() {
let enc_data = read_file("day20.txt").iter().filter_map(|line| line.parse().ok()).enumerate().collect::<Vec<(usize, i64)>>();
let mut result = enc_data.clone();
do_encryption(&enc_data, &mut result);
println!("Task 1: {}", calc_result(result));
let enc_data = enc_data.iter().map(|(idx, value)| (*idx, value * 811589153)).collect::<Vec<_>>();
let mut result = enc_data.clone();
for _ in 0..10 {
do_encryption(&enc_data, &mut result);
}
println!("Task 2: {}", calc_result(result));
} }