diff --git a/src/day14/1.rs b/src/day14/1.rs index b51073e..9d496d9 100644 --- a/src/day14/1.rs +++ b/src/day14/1.rs @@ -1 +1,57 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] + +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> usize { + let mut answer = 0; + + let mut grid: Vec> = data.lines().map(|x| x.bytes().collect()).collect(); + + let m = grid.len(); + let n = grid[0].len(); + + for i in 0..m { + for j in 0..n { + let c = grid[i][j]; + + if c != b'O' { + continue; + } + + // move the rock north + let mut start = i; + while start > 0 && grid[start - 1][j] == b'.' { + start -= 1; + } + + grid[i][j] = b'.'; + grid[start][j] = b'O'; + } + } + + for (i, line) in grid.iter().enumerate() { + for &c in line { + if c == b'O' { + answer += m - i; + } + } + } + + answer +} + +fn main() { + for input in INPUTS.iter() { + println!("total = {}", process(input)); + } +} + +#[bench] +fn part1(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[INPUTS.len() - 1]); + test::black_box(v); + }); +} diff --git a/src/day14/2.rs b/src/day14/2.rs index b51073e..e4d1a18 100644 --- a/src/day14/2.rs +++ b/src/day14/2.rs @@ -1 +1,193 @@ -const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; +#![feature(test)] + +use std::collections::{hash_map::Entry, HashMap}; + +extern crate test; + +const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; + +fn process(data: &str) -> usize { + let mut answer = 0; + + let mut grid: Vec> = data.lines().map(|x| x.bytes().collect()).collect(); + + let m = grid.len(); + + let mut map: HashMap>, Vec>> = HashMap::new(); + + let mut steps_left = 1_000_000_000; + + while steps_left > 0 { + let l = map.len(); + spin(&mut grid, &mut map); + steps_left -= 1; + if l == map.len() { + break; + } + } + + let mut index_map: HashMap<&Vec>, usize> = HashMap::new(); + let mut path = vec![0; map.len()]; + + let mut i = 0; + for (k, v) in map.iter() { + let k_idx: usize = match index_map.entry(k) { + Entry::Occupied(v) => *v.get(), + Entry::Vacant(e) => { + e.insert(i); + i += 1; + i - 1 + } + }; + + let v_idx: usize = match index_map.entry(v) { + Entry::Occupied(v) => *v.get(), + Entry::Vacant(e) => { + e.insert(i); + i += 1; + i - 1 + } + }; + + path[k_idx] = v_idx; + } + + let mut current = *index_map.get(&grid).unwrap(); + + let cycle_length = { + let mut c = current; + let mut start = true; + let mut length = 0; + + while start || c != current { + start = false; + c = path[c]; + length += 1; + } + length + }; + + steps_left %= cycle_length; + while steps_left > 0 { + current = path[current]; + steps_left -= 1; + } + + for (k, v) in index_map.iter() { + if *v == current { + for (i, line) in k.iter().enumerate() { + for &c in line { + if c == b'O' { + answer += m - i; + } + } + } + + break; + } + } + + answer +} + +fn spin(grid: &mut Vec>, map: &mut HashMap>, Vec>>) { + let start = grid.clone(); + + if let Some(v) = map.get(&start) { + *grid = v.clone(); + return; + } + let m = grid.len(); + let n = grid[0].len(); + + for i in 0..m { + for j in 0..n { + let c = grid[i][j]; + + if c != b'O' { + continue; + } + + // Roll north + let mut start = i; + while start > 0 && grid[start - 1][j] == b'.' { + start -= 1; + } + + grid[i][j] = b'.'; + grid[start][j] = b'O'; + } + } + + for row in grid.iter_mut() { + for j in 0..n { + let c = row[j]; + + if c != b'O' { + continue; + } + + // Roll West + let mut start = j; + while start > 0 && row[start - 1] == b'.' { + start -= 1; + } + + row[j] = b'.'; + row[start] = b'O'; + } + } + + for i in (0..m).rev() { + for j in 0..n { + let c = grid[i][j]; + + if c != b'O' { + continue; + } + + // Roll South + let mut start = i; + while start < m - 1 && grid[start + 1][j] == b'.' { + start += 1; + } + + grid[i][j] = b'.'; + grid[start][j] = b'O'; + } + } + + for row in grid.iter_mut() { + for j in (0..n).rev() { + let c = row[j]; + + if c != b'O' { + continue; + } + + // Roll East + let mut start = j; + while start < n - 1 && row[start + 1] == b'.' { + start += 1; + } + + row[j] = b'.'; + row[start] = b'O'; + } + } + map.insert(start, grid.clone()); +} + +fn main() { + for input in INPUTS.iter() { + println!("total = {}", process(input)); + } +} + +#[bench] +fn part2(b: &mut test::Bencher) { + b.iter(|| { + let v = process(INPUTS[INPUTS.len() - 1]); + test::black_box(v); + }); +} diff --git a/src/day14/input.txt b/src/day14/input.txt new file mode 100644 index 0000000..191973c --- /dev/null +++ b/src/day14/input.txt @@ -0,0 +1,100 @@ +O....##.##.....OO...O.O......#.OO.#.#....#.#..#..##....###..O.....O#..#.#.......O.#.##...O#OO.O....O +.#..OOO..#.#..O..O...O.#.......O..O....##..O##O..#..#.......O.O.#....O...O#.O#OOO...O...#O.#......O. +..#O...#O.......##.O.O..O#..OO..O...#.O...O.#...#.OOO.OO.O......#.....#.O..##.O.#.#...O.O#..#..#O.#. +O####.O.#.O...#..#...O.......#....#..O....OO#...O..##...#..##..O..####..OO.O..O.#OO..O.O..O##O.....# +OO..O.##..#.O##O.....#..#O#.O...#.......#O#...#.O....#..O.O.....O#.OO....O......O#O.#..O..OO.O.O.... +..#O.O...O...O..O.OO.O#OO...#......#.....O.#.....#.....O##.#......#O..OO.......#..#..O...#.......O.O +.O...#.......OO..O.....#....#.#.O.........#....#......#.OO...O.........O.O.O....O..#..#O..##...#.O.. +O#......#.O....#.O.#....#....O.......#O.O#O..O..#...#.O.#..O.OO...O..O....#O#OOOO................#.. +O..O.#..#........O......O..O..#..O.#.O...#O...O##..O.....#OO.#..#...........##.#...O##.O...#..#..O.. +..O.....OO.O#..#O#.....#......OOOO#..#.O.#.#..O..O.....O.O.#....#O...O...O.OO......#O#.#.##......O.O +.#.#............OO...O.OOO#O..#.#O#.O.#O#.O.O.....#.O..........#.....O#..O...O#.....O......##O..#OO. +.O#O....O#..#....OO.....#...O...O..O.O..O..##....#.#.....O....O..#O..........#O...#O##.#....O..##O.. +....OOO.O..O#..#.OO.....O#..O.O#...#.....##OO.....OO...##O.....O......O#..O##...#...##.O..O....#.#.. +.#......O.#..O...........O...#..O#.O.#O#..OO.........O.O...O##.........##.O#.........#.O......#...#. +O.#...##..#..........#OO........O#..##..##.#O#O..O..#O..O....O.OO....O...OOO...O.....#.OO#..#.#..... +.##.#O.##..#....O.##O..O.#..O...#...O....O#....O...O.#O#...........##O..O..#.O....O........O.#...OOO +.O#......OOOO...#..O....#..#.O#.#.##.##O#.#.#..O....O#..........O..#..O...#.....O........#....O.#..# +.....O......#..#...O....OO...O#OO...OO.O....#..O.O.....O#OO.#....#.#.#......O......OO........#.#.##. +......O......O...O##.#O........O.......#.##.O......O.#.#.#.....#.O....#..#O...O##..#..#.##...O#O.O.O +..OO.O...#..OO#.OO..O......##......O.#...#....O.OO..O...#.......#O##..OO....OO.O..#.O...#O..#.#O.... +O...O.#.O..#.OOO...........O..#.#....OO..OO.O....O#O#O#..OOOO....OOO.OOO#....#....O#..O#.O.#....O... +.......O.....O..O.#...O.#...#O.O..#......#...O#OOOO#..OOO..#..O....#...O..O..O.#....O.O.O.O...#O.... +#...O.#.........OO...#......OO....O.##OOO#O.......#.O..#..O.#O#.O...O.OOO.O##OO.#.O.O..#.....O...O#. +O.OOOOO##O#.......O#.....O...O.#...O..OOO.....##...O...O....O.#....##O....#.#OO..#..#O..OO#OO#...... +...........O...#.O....O##O..O.O....#.#O...O..OO.......#..#.#...#.........##..#.O...#.O...OOO..O.#OO. +..O##....#....O.#.O#O##.OOOOO..O.#..#...O..#...#.##.#O.....#..O..O.O#...#.O..#....#O.#........O#...# +#...O.......#.O....O......OO#.##..##O..O..O....O.O##.OOO......#......#...OO..O.O..........OO.O.O..O. +........OO.O.#.#.O...............O......O...O....#........O..O....O..O.....O.O..O.O.O#O#.O..##...... +O..O.......#O..O..O.O....O......O##.#...O##..#...O.O#.#.O..O.O#.#.....O...O#.#.#.....O.....#.#...... +.#......O..O..#O....O...O...O.......#..#..#.O...##..#......O.O.OOOO...##..#.OO......#O.O.O#...O.##O# +..OOO...#.O.O..#.#..O....##.O....#.O..OO#.O.......O..##O##.#OO.....#.....#.....O.#O.OO#....#O..OO.O. +..O#OO#.#....O.#..O.#OO#.#...O.#.#.OO....O..O.#...#..O..OO.#.O#..##.#......#O.#.#..O..O...OO#..OOO.. +#.#OO.....O...O......O............O...###..O.#.OO...O...#...O.....##OO#.......#...#.OO.#...#....#... +....O....#.#OO##OO...OO...#..#.#.....O.....O..O....#......O....O#....OO.#....O....O...#.O..#OOO.OOOO +.#..O.O.O.O.#.O##O..#..O...........O..........OO#.#..O...#O....#OO...O.#.....O.OOOO....O.......#O.#. +...O##....O.....#O..#.OO........#...O..#O#..OO#........###.O..O......O..#...##.#..#..#O.....O.#..#.# +.#..O.....O...OO#OO..#......O.#....#..#.#.....O.....OO.O...#.....###...O....O.......OO#..#....O.#..O +O...#.O..O....#.O....#...#.O....#.O..##.O.O#.O#O.O.....O.#OO#.#O......#.O..#.#..#........O.O...O..O# +......O.OOOO.O....O.#.##.O...#O....OOO...O..O###..OOO.........OO.O..O...OO.##..#.#..##.O.#...#....#. +.#O...#.##.O....#.#...##O....O#O.O.#.O.#.O...#...O.#.O#O....O.#..###.O.#OOOO.O#OO.....OOO#.....##..O +..O##O.#.#..O...#O..O.##.#O..O.#..OO.O..........O.....O#.#.OOO........#O.O#.#..O..OOO....#..O.O.#..O +OO.#O....OO#........O.O.#..#..O#.O.OO#O...O.#.O........O##.OO..O..#..O........O#.#O.............O... +................O#...........O........O#.O..O#..#O.O.O..#.....#.#O..O.O.#.....#.O.....#...#.#O..#..# +#....#...#....#.#O......##O.....#.#........#.O....OO#..OO.#....OO..#....O....OO..#..##......#O.O.#.. +O####O.##....####O..#.#.OO.O..#O#O...O.OOO...#O##..##......O...O..OO..#.O#O...O.#O........O.....#..# +...OOO.O.....O..#.O.....O.OO...OOO#O.........O...O..#...#OO.....#..O....#....#.......##.....#...#.O# +#.....O#..O#.#.....#..O......#..O.......O##.....##.##O..OO##.OO#..##......O......O....O....O#....O.. +O....OO......O.....O...O#.O..#.#...#....#..#....OO.O##O..##O..O#.O.#.OO#.OOO....#.OO..#O..O..#..##.. +O.O#.#OO..O..O...OO......OO.......#.O#O.O#....#.O#O..O#.#...O#..O...O....O.#OO#.#..O.##..O.....#..OO +.##.O...O.O..O....OO...O#....OO..#.....#.....OO##.#.O.#......#.O...O..O...##..#O.....#.O....#O...... +.OO#..O.O.....#.O.OO.##..#.....O......O..OO...O.O.O##OO.#O...O..O..O#...OO..O.....#.............O.OO +..O....#O..##.O..#...O#..#.OO....##.O.....#..##.O...#.O...O.#O.OO..#O...O##.O.......#.O.#.O..#O..... +.##..........O..#.#.........O....O..O#..##.#..#..O#OO..#..#O...O#O.#..#.#.O.OO......O###.....O.O.... +...#O..O..O#..O#..O#...OO.O.O.#.OO..#.#..#.#.......O.O..#OO#..O....O...#O#..#O....O.#O.#..#.#..O...# +OO###....OO..#.##OO......#..O###..O.....O.O.#...OO..#O....OO..#O..#.#..OO.#.....O......OOO........OO +.O....#..#.O.O........O..#....O...O...O#.....OOOO.####.O..#.##.##O##..O.OO...O.OO..#O...O..O.O.#.O.. +##O#...#...O.O#..O..O.#.OOO..O..##....#....##.O.#.O....O.#....O##...OO..O..#..O.O.#.....#OOO.O..O... +.O.....#....O#.O......#O...#.#.#...O.#..O.O..#...O.#O..O..O......#...........O##..O#O.#.#.#......... +OO.....O..#.O#.....##O..##....OO....O..O.O..#..O.O........O...#...O....O#O.O..#.O..##.#...#.....###. +.#..........O.....#O#O....O.OO.#..#...#..O.#...O.#.#.O#O....#O.O..#.....##O#.#.O..#O....O.##...#.O.O +.O....O....O##.O...O....O....O....O..O............#O...#.#...OOO....O#.....O#...##.......O.#........ +.#.......OOO#............OO..........O.#...O..OO....#..O.#...##.O#....O..O.#.O#O##.O.O...#.#O#.O.O.. +.#..#.#...........O.#..##..OO#.O..O..O.#OO##O.O..OOO....OO..#OO..#..###..O....##...........#O.###.O. +O....O..O...#........O.#OOOOO#.O.....O.#...O.OOO#.#......#.OO#.#.O..#..#O....##....#.###...#.###..#. +..O...O#.O.....#.OOO.O....O.......#O...O..#.#......#.#..O......OOO.#..O.#O..O..O#..#..#...#....O.#.O +O....O...OO.....#.....O#..#..##...O..O.....##....#O.....##.#..O#.O...#O.....O##.##O.....#O..O..O...# +O.##.OO.O.O.O...O..O..O...O.O...O.#..O...###....O..O##O#....OO.......#....#OO....#.#.......O....OO#O +.O......OO.....##...##.#..#..........##O..#.O#.#....#...#.OO..O.....#...O.......##.OO##....O..OO.... +O....O#.....#.OO..O#..OO.OO#..O...O.O.OO.O.O.#...O.#OO..O#.....#.#.....#..O....O...........O#.#.#... +#..#.#..#.....#O.O.#####........O..#......#O......##.........#....#.#O.#.O...O.#..O.O..#O.#O..OO..#O +...O..O...O.OOOO.O#O###..O...#..#.......O#......O..O#O.OO.#..#.O.O...O....O.....###...#......##....O +..#.#O........O#O..##OOO....O##.#..O...#.O.O##..#OO.....#OO.#......O...#...#.O.#.O#.....O...O...##.# +O.....OO....OO#..O.....O....O..#.#.......OO#O........##.O..O#O.#.##.O..O.#.#.#......#O.##....O.#OO.O +..O..OO..O..O#....O...#....OO#O.......O....O..##O.O.O#..#O#....#..O.....#O.#.O#....##....O......#... +......#O#..O#..OO#...#......#.#.......O#..#.O#.O.O.OO...#..#O..##..#O..O...#..#.O....##O.O##O..O.#.O +..#O...#...O.........OO........O.O.O#......OO.#.....O.O..O#.#O#.....####.OO....#O#OO..#O.O..O##O...# +.#.O..#...O.#.O.OO###.#.OO#..O.#OO.#.#.#....O...#O#..OOOO#.......#.O...OO.....O#.#...##............. +.#.#.OO....O.........O..##O.#.O........O.#O.O##...O....O.O.#OOO....OO.O..O.OO...O..#OO...#......#..# +......O.....O..O...#...#......O..#O#...#...#...O...OO.........O..O.O.OOO.O.#.O.O....OO.O.#O..O...O.. +........#....OO......#.....#O#...#..O.O..#..O..O.##O.O..........#...#.O...O#.O..........OOOO.O....O. +O..#.O.O....OO...#.O.......O....#.O#...O#.......OO..#O###O#O.O.O.#...#...#O#.O..O...OOO.OOO.#.#..... +.#.OO......O#.#...OO.#..O....O#O.O#..........O......#.#.O....O...O.#O.#OO........#..#...##..#....OOO +O.....#....O......O......OO#O#.##.##OO..O............O.OO...O#.....#...##...#...OO.OO.......O.....O. +.....OO.O......O.........O.#.O.....#.#O......O..O..#.#.##.........O##OO....O..OO...#........#...##.. +#..###......O.....O#OO...#..#O....O.OO..O...........#..#.#...O##O...O.O....#.OO..#.#.OO....#..#O.O.# +.........O#.O#...##.#.#...O#.O.........O.#..OO........O.O.....#O....O#.#..O.O..#....#.....#.......O. +...#O...O.#...OOOO.#O..O.O#.....#.#.....O..#.....O.#.O......O.##O.O..O........#.........#O..O#..#... +#.#.#O#.O..#O.#O..#.##..O#.O..#.O.OOO...........O..O.O.#O..#....O....#O.##O.##O......O..OOO...#O...O +O.#O##.....#.O.O.OO.O..O....O...#O...........O.##O##...OO...##.#.#.#.#.O..O...#......O...O.#..#.#.O. +.OO....O.....#O.O........O#.......O...O.O##O..#....O.#O.#.O.....##..#...#.OO.O.O..##...#O.OOO#O..#.# +##O#OOO.##...#.O...#....#...O#.....O....#.OOO.O.##..OO.OO...O.O.O..O.O..#.#.#.##...O...#..#.#.O..... +..O#..#OOO#....O...#.O.#...#....#...#....O..........O.O#.....O.....O.OO...O.#.......O.#...O......... +#..#.#.O#.OO.O...O..#....#.O..OO.....#..O...O.#.#...OO....O#..O....###...###..O.O.O.O.O..OOO.....OO. +#..#..#.O..#..O.O..O#....##O.OO.O#OO.......O........#..O..#.O..#.#OOOO...O..#OOO....#..#..OOO......O +.#O.#.#.#...#OOO.OO..#O.##.....O..#O.##.O.O.O..OOO#....O.##......OOOOOO...O..O......#...#.O...O.#... +#O..O.O###...##O...O........##.O#....#O..O.O.O#.#OO##O.O.O#.#.##O.OO........O.........###O.O.#O#OO#O +O..#O......#.#O...O#...#####..#O#...O.##OO......O.....O.......O.O.#..O.O...........#OO..#..OOO....#. +....O.O#..#O#.#....#O.O##..#...O...#O.#.#.##O...OO....O..#..OO#........#O.........#O..O.#..OO....... +#..##.#.O#O..#O..OO...#.....#..O#..O....O#.....OO.O......O..#....O.##O#O...O....#..O.......O....O.## +#....#O..#O.O..#....OO#.#OO...O..O.......O..#......#.......#.O.#..OO..O........OO.......#O.O.#O....O diff --git a/src/day14/sample.txt b/src/day14/sample.txt new file mode 100644 index 0000000..b92d1a3 --- /dev/null +++ b/src/day14/sample.txt @@ -0,0 +1,10 @@ +O....#.... +O.OO#....# +.....##... +OO.#O....O +.O.....O#. +O.#..O.#.# +..O..#O..O +.......O.. +#....###.. +#OO..#.... \ No newline at end of file