aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

common.rs (1063B)


      1 const MAX_OVERFLOW: i64 = 10000000000;
      2 
      3 pub 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 
     13 fn get_newpos(pos: i64, k: i64, n: i64) -> i64 {
     14     (pos + k + MAX_OVERFLOW * (n-1)) % (n-1)
     15 }
     16 
     17 fn 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 
     25 pub 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 
     33 pub 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 }