Skip to content

Commit

Permalink
Day 14b
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 14, 2023
1 parent c531b90 commit 8ce281a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion day14/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"
aoc = { path = "../aoc" }
color-eyre = "0.6.2"
nom = "7.1.3"
sqrid = "0.0.19"
sqrid = "0.0.21"
51 changes: 4 additions & 47 deletions day14/src/bin/day14a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,11 @@ use std::io::{stdin, BufRead};

use day14::*;

pub use sqrid::Qr;
pub type Sqrid = sqrid::sqrid_create!(100, 100, false);
pub type Qa = sqrid::qa_create!(Sqrid);
pub type Grid = sqrid::grid_create!(Sqrid, Cell);

pub type SqridDebug = sqrid::sqrid_create!(10, 10, false);
pub type QaDebug = sqrid::qa_create!(SqridDebug);
pub type GridDebug = sqrid::grid_create!(SqridDebug, Cell);

fn process(rows: usize, bufin: impl BufRead) -> Result<usize> {
fn process(size: usize, bufin: impl BufRead) -> Result<usize> {
let input = parser::parse(bufin)?;
let mut grid = Grid::default();
for (y, line) in input.into_iter().enumerate() {
for (x, cell) in line.into_iter().enumerate() {
let qa = Qa::try_from((x as u16, y as u16))?;
grid[qa] = cell;
}
}
for qa_rock in Qa::iter() {
if grid[qa_rock] != Cell::Rock {
continue;
}
let mut qa = qa_rock;
while let Ok(qa_new) = qa + Qr::N {
if grid[qa_new] != Cell::Empty {
break;
}
qa = qa_new;
}
if qa != qa_rock {
grid[qa_rock] = Cell::Empty;
grid[qa] = Cell::Rock;
}
}
let g = QaDebug::iter()
.map(|qa| grid[Qa::try_from(qa.tuple()).unwrap()])
.collect::<GridDebug>();
eprintln!("{}", g);
Ok(Qa::iter()
.map(|qa| {
if grid[qa] == Cell::Rock {
let t = qa.tuple();
rows - t.1 as usize
} else {
0
}
})
.sum())
let mut grid = Grid::try_from(input)?;
grid = tilt(size, grid, Qr::N);
Ok(grid_load(size, &grid))
}

#[test]
Expand Down
60 changes: 60 additions & 0 deletions day14/src/bin/day14b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use std::collections::HashMap;
use std::io::{stdin, BufRead};

use day14::*;

pub use sqrid::Qr;
pub type Sqrid = sqrid::sqrid_create!(100, 100, false);
pub type Qa = sqrid::qa_create!(Sqrid);
pub type Grid = sqrid::grid_create!(Sqrid, Cell);

pub type SqridD = sqrid::sqrid_create!(10, 10, false);
pub type QaD = sqrid::qa_create!(SqridD);
pub type GridD = sqrid::grid_create!(SqridD, Cell);

const CYCLES: u64 = 1000000000;

fn _grid_display(grid: &Grid) {
let grid_d = QaD::iter()
.map(|qa| grid[Qa::try_from(qa.tuple()).unwrap()])
.collect::<GridD>();
eprintln!("{}", grid_d);
}

fn process(size: usize, bufin: impl BufRead) -> Result<usize> {
let input = parser::parse(bufin)?;
let mut grid = Grid::try_from(input)?;
let mut cache = HashMap::<Grid, u64>::default();
let mut icycle = 0;
while icycle < CYCLES {
eprintln!("cycle {}", icycle);
for qr in [Qr::N, Qr::W, Qr::S, Qr::E] {
grid = tilt(size, grid, qr);
}
if icycle < CYCLES / 2 {
if let Some(first) = cache.get(&grid) {
let diff = icycle - first;
icycle += diff * ((CYCLES - first) / diff - 1);
}
cache.insert(grid, icycle);
}
icycle += 1;
}
Ok(grid_load(size, &grid))
}

#[test]
fn test() -> Result<()> {
assert_eq!(process(10, EXAMPLE.as_bytes())?, 64);
Ok(())
}

fn main() -> Result<()> {
color_eyre::install()?;
println!("{}", process(100, stdin().lock())?);
Ok(())
}
47 changes: 46 additions & 1 deletion day14/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ O.#..O.#.#
#OO..#....
";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
pub enum Cell {
#[default]
Empty,
Expand All @@ -37,6 +37,11 @@ impl TryFrom<char> for Cell {
}
}

pub use sqrid::Qr;
pub type Sqrid = sqrid::sqrid_create!(100, 100, false);
pub type Qa = sqrid::qa_create!(Sqrid);
pub type Grid = sqrid::grid_create!(Sqrid, Cell);

impl fmt::Display for Cell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -75,3 +80,43 @@ fn test() -> Result<()> {
assert_eq!(input[0].len(), 10);
Ok(())
}

pub fn tilt(size: usize, mut grid: Grid, qr: Qr) -> Grid {
let qas = if qr == Qr::N || qr == Qr::W {
Qa::iter().collect::<Vec<_>>()
} else {
Qa::iter().rev().collect::<Vec<_>>()
};
for qa_rock in qas {
if grid[qa_rock] != Cell::Rock {
continue;
}
let mut qa = qa_rock;
while let Ok(qa_new) = qa + qr {
let t = qa_new.tuple();
let t = (t.0 as usize, t.1 as usize);
if t.0 >= size || t.1 >= size || grid[qa_new] != Cell::Empty {
break;
}
qa = qa_new;
}
if qa != qa_rock {
grid[qa_rock] = Cell::Empty;
grid[qa] = Cell::Rock;
}
}
grid
}

pub fn grid_load(size: usize, grid: &Grid) -> usize {
Qa::iter()
.map(|qa| {
if grid[qa] == Cell::Rock {
let t = qa.tuple();
size - t.1 as usize
} else {
0
}
})
.sum()
}

0 comments on commit 8ce281a

Please sign in to comment.