heh
hmmmm
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 43 | ||||
| -rw-r--r-- | src/util.rs | 18 |
3 files changed, 47 insertions, 16 deletions
@@ -6,10 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -arrayvec = "0.7.4" itertools = "0.12.0" memchr = "2.6.4" -rayon = "1.8.0" [profile.release] lto = true codegen-units = 1 diff --git a/src/main.rs b/src/main.rs index 741f805..8b75538 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,13 +21,26 @@ pub use util::prelude::*; pub fn p1(i: &str) -> u16 { let mut x = 0i16; let mut y = 0i16; - // boundary points, shoelace - let (b, a) = i.行().fold((0, 0), |(b, a), i| { - let d = unsafe { rint::<_, Dir>(C! { i[0] }) }; - let c = if C! { i[3] } == b' ' { - (C! { i[2] } - b'0') - } else { - (C! { i[2] } - b'0') * 10 + C! { i[3] } - b'0' + let mut a = 0i32; + let mut b = 0u16; + let mut i = i.as_bytes(); + loop { + let d = unsafe { + rint::<_, Dir>(match i.by() { + Ok(x) => x, + Err(_) => break, + }) + }; + let c = match unsafe { i.rd::<3>().unwrap_unchecked() } { + [_, b, b' '] => { + i = C! { &i[10..] }; + b - b'0' + } + [_, a, b] => { + _ = i.read(&mut [0; 11]); + // i = C! { &i[11..] }; + (a - b'0') * 10 + (b - b'0') + } }; let (ox, oy) = (x, y); (x, y) = match d { @@ -37,11 +50,15 @@ pub fn p1(i: &str) -> u16 { Dir::S => (x, y - c as i16), Dir::W => (x - c as i16, y), }; - ( - b + c as u16, - a + ((x as i32 + ox as i32) * (y as i32 - oy as i32)), - ) - }); + + unsafe { b = b.unchecked_add(c as u16) }; + unsafe { + a = a.unchecked_add( + ((x as i32).unchecked_add(ox as i32)).unchecked_mul(y as i32 - oy as i32), + ) + }; + } + // use shoelace formula to get the area, then use picks formula to count the number of inner points ((a.abs() / 2) as u16) + (1 + b / 2) } @@ -79,7 +96,7 @@ pub fn p2(i: &str) -> u64 { } pub fn run(i: &str) -> impl Display { - p2(i) + p1(i) } fn main() { diff --git a/src/util.rs b/src/util.rs index d7b44a0..b5e0bbc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -14,7 +14,7 @@ pub mod prelude { pub use super::{ even, gcd, lcm, pa, Dir, GreekTools, IntoCombinations, IntoLines, IterͶ, NumTupleIterTools, ParseIter, Printable, Skip, TakeLine, TupleIterTools2, TupleIterTools3, TupleUtils, - UnifiedTupleUtils, Widen, 読む, Ͷ, Α, Κ, Λ, Μ, + UnifiedTupleUtils, Widen, 読む, 読む::Ext, Ͷ, Α, Κ, Λ, Μ, }; pub use itertools::izip; pub use itertools::Itertools; @@ -24,6 +24,7 @@ pub mod prelude { collections::{hash_map::Entry, HashMap, HashSet, VecDeque}, fmt::{Debug, Display}, hint::black_box as boxd, + io::{self, Read, Write}, iter, mem::{replace as rplc, swap, transmute as rint}, ops::Range, @@ -664,6 +665,21 @@ macro_rules! ι { ι!(usize); pub mod 読む { + use std::io::{self, Read}; + pub trait Ext { + fn rd<const N: usize>(&mut self) -> io::Result<[u8; N]>; + fn by(&mut self) -> io::Result<u8> { + Ok(self.rd::<1>()?[0]) + } + } + + impl<T: Read> Ext for T { + fn rd<const N: usize>(&mut self) -> io::Result<[u8; N]> { + let mut buf = [0; N]; + self.read_exact(&mut buf)?; + Ok(buf) + } + } use crate::util::prelude::*; pub fn κ(x: &[u8], v: &mut Vec<u64>) { let mut s = 0; |