-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters