heh
| -rw-r--r-- | src/main.rs | 36 | ||||
| -rw-r--r-- | src/util.rs | 12 |
2 files changed, 41 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 9449a55..5a6bc8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![allow(confusable_idents, uncommon_codepoints, mixed_script_confusables)] #![feature( + maybe_uninit_uninit_array, inline_const, slice_flatten, iter_collect_into, @@ -17,22 +18,39 @@ extern crate test; pub mod util; +use std::mem::MaybeUninit; + pub use util::prelude::*; -pub fn search(i: &[&[u8]]) -> (u8, u8) { - for (row, y) in i.iter().ι::<u8>() { - if let Some((_, x)) = row.iter().ι::<u8>().find(|&(&x, _)| x == b'S') { - return (x, y); +pub fn p2(i: &str) -> usize { + let i = i.as_bytes(); + // RNG src + let mut map: HashSet<(i64, i64)> = HashSet::from_iter([(65, 65)]); + let mut p = MaybeUninit::uninit_array::<3>(); + let mut n = 0; + for s in 0..328u64 { + if s % 131 == 65 { + p[n].write(map.len()); + n += 1; } + map = [Dir::N, Dir::E, Dir::S, Dir::W] + .into_iter() + .flat_map(|x| map.iter().map(move |&y| x + y)) + .filter(|&(x, y)| { + i[y.rem_euclid(131) as usize * 132 + x.rem_euclid(131) as usize] != b'#' + }) + .collect(); } - dang!(); + let n = 26501365 / 131; + let [a, b, c]: [usize; 3] = unsafe { rint(p) }; + a + n * (b - a) + n * (n - 1) / 2 * ((c - b) - (b - a)) } -pub fn run(i: &str) -> impl Display { +pub fn p1(i: &str) -> usize { let i = i.行().collect_vec(); let i = i.as_slice(); let mut n = 0; - let (x, y) = search(i); + let (x, y) = (65u8, 65u8); util::countg( (x, y, 0), &mut |(x, y, n)| { @@ -56,6 +74,10 @@ pub fn run(i: &str) -> impl Display { n } +pub fn run(i: &str) -> impl Display { + p2(i) +} + fn main() { let i = include_str!("inp.txt").trim(); println!("{}", run(i)); diff --git a/src/util.rs b/src/util.rs index eb0e11c..3b489a5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -357,6 +357,18 @@ pub fn dijkstra<N: Debug + Eq + Hash + Copy + Ord, I: Iterator<Item = (N, u16)>> dang!() } +impl std::ops::Add<(i64, i64)> for Dir { + type Output = (i64, i64); + fn add(self, (x, y): (i64, i64)) -> Self::Output { + match self { + Dir::N => (x, y - 1), + Dir::E => (x + 1, y), + Dir::S => (x, y + 1), + Dir::W => (x - 1, y), + } + } +} + impl std::ops::Add<(i32, i32)> for Dir { type Output = (i32, i32); fn add(self, (x, y): (i32, i32)) -> Self::Output { |