aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-07-07 09:51:31 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-07-07 09:51:31 +0200
commit2f0e522317972711b5e03ad25b38bf03af17e8d5 (patch)
tree98c0ace3df13030722edac45e99d86e0117d6d31
parent58800a74af2c9f3578d3e62b3226aaf7c73aef87 (diff)
downloadaoc-2f0e522317972711b5e03ad25b38bf03af17e8d5.tar.gz
aoc-2f0e522317972711b5e03ad25b38bf03af17e8d5.zip
Day 20 2022
-rw-r--r--2022/20/a.rs8
-rw-r--r--2022/20/b.rs8
-rw-r--r--2022/20/common.rs37
3 files changed, 53 insertions, 0 deletions
diff --git a/2022/20/a.rs b/2022/20/a.rs
new file mode 100644
index 0000000..0b80099
--- /dev/null
+++ b/2022/20/a.rs
@@ -0,0 +1,8 @@
1mod common;
2use common::*;
3
4fn main() {
5 let mut a = read_from_stdin(1);
6 mix(&mut a);
7 println!("{}", groove_sum(&a));
8}
diff --git a/2022/20/b.rs b/2022/20/b.rs
new file mode 100644
index 0000000..ec10cf0
--- /dev/null
+++ b/2022/20/b.rs
@@ -0,0 +1,8 @@
1mod common;
2use common::*;
3
4fn main() {
5 let mut a = read_from_stdin(811589153);
6 for _ in 0..10 { mix(&mut a); }
7 println!("{}", groove_sum(&a));
8}
diff --git a/2022/20/common.rs b/2022/20/common.rs
new file mode 100644
index 0000000..cecc316
--- /dev/null
+++ b/2022/20/common.rs
@@ -0,0 +1,37 @@
1const MAX_OVERFLOW: i64 = 10000000000;
2
3pub fn read_from_stdin(key: i64) -> Vec<(usize, i64)> {
4 let mut line = String::new();
5 let mut v = vec![];
6 while std::io::stdin().read_line(&mut line).unwrap() > 0 {
7 v.push((v.len(), line[..line.len()-1].parse::<i64>().unwrap() * key));
8 line.clear();
9 }
10 v
11}
12
13fn get_newpos(pos: i64, k: i64, n: i64) -> i64 {
14 (pos + k + MAX_OVERFLOW * (n-1)) % (n-1)
15}
16
17fn move_to(a: &mut [(usize, i64)], i: usize, j: usize) {
18 if i < j {
19 for k in i..j { a.swap(k, k+1); }
20 } else {
21 for k in (j+1..=i).rev() { a.swap(k, k-1); }
22 }
23}
24
25pub fn mix(a: &mut Vec<(usize, i64)>) {
26 for i in 0..a.len() {
27 let pos = a.iter().position(|x| x.0 == i).unwrap();
28 let newpos = get_newpos(pos as i64, a[pos].1, a.len() as i64) as usize;
29 move_to(a, pos, newpos);
30 }
31}
32
33pub fn groove_sum(a: &Vec<(usize, i64)>) -> i64 {
34 let n = a.len();
35 let z = a.iter().position(|x| x.1 == 0).unwrap();
36 a[(z + 1000) % n].1 + a[(z + 2000) % n].1 + a[(z + 3000) % n].1
37}

Generated with cgit - Back to sebastiano.tronto.net