day14: added

This commit is contained in:
Ishan Jain 2023-12-14 12:57:33 +05:30
parent 5707b7cacc
commit 7460612996
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 360 additions and 2 deletions

View File

@ -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<Vec<u8>> = 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);
});
}

View File

@ -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<Vec<u8>> = data.lines().map(|x| x.bytes().collect()).collect();
let m = grid.len();
let mut map: HashMap<Vec<Vec<u8>>, Vec<Vec<u8>>> = 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<Vec<u8>>, 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<Vec<u8>>, map: &mut HashMap<Vec<Vec<u8>>, Vec<Vec<u8>>>) {
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);
});
}

100
src/day14/input.txt Normal file
View File

@ -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

10
src/day14/sample.txt Normal file
View File

@ -0,0 +1,10 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....