fn cycle(input: &mut Vec>) { let mut sl: usize; // north for j in 0..input[0].len() { sl = 0; for i in 0..input.len() { if input[i][j] == 'O' { if sl != i { input[sl][j] = 'O'; input[i][j] = '.'; } sl += 1; } else if input[i][j] == '#' { sl = i + 1; } } } // west for i in 0..input.len() { sl = 0; for j in 0..input[0].len() { if input[i][j] == 'O' { if sl != j { input[i][sl] = 'O'; input[i][j] = '.'; } sl += 1; } else if input[i][j] == '#' { sl = j + 1; } } } // south for j in 0..input[0].len() { sl = input.len() - 1; for i in (0..input.len()).rev() { if input[i][j] == 'O' { if sl != i { input[sl][j] = 'O'; input[i][j] = '.'; } if i != 0 { sl -= 1; } } else if input[i][j] == '#' { if i != 0 { sl = i - 1; } } } } // east for i in 0..input.len() { sl = input[0].len() - 1; for j in (0..input[0].len()).rev() { if input[i][j] == 'O' { if sl != j { input[i][sl] = 'O'; input[i][j] = '.'; } if j != 0 { sl -= 1; } } else if input[i][j] == '#' { if j != 0 { sl = j - 1; } } } } } fn sum(input: &Vec>) -> u64 { let mut sum: u64 = 0; for i in 0..input.len() { for j in 0..input[0].len() { if input[i][j] == 'O' { sum += (input.len() - i) as u64; } } } return sum; } fn cycle_len(a: &Vec) -> u64 { for cl in 2..(a.len() / 3) { let mut ind = true; for i in 0..cl { if a[i] != a[cl + i] || a[cl + i] != a[cl * 2 + i] { ind = false; break; } } if ind { return cl as u64; } } return 0; } pub fn main() { let txt = std::fs::read_to_string("./input/day14.txt").unwrap(); let mut input = txt .lines() .map(|s| s.chars().collect::>()) .collect::>>(); let mut a = vec![]; for i in 0..1000 { cycle(&mut input); if i > 500 { a.push(sum(&input)); } } let cl = cycle_len(&a); assert_ne!(cl, 0); let c = 1000000000 - 501 - 1; println!("{}", a[(c % cl) as usize]); }