performans 🤓 (absolutely crazy)

This commit is contained in:
2024-12-07 00:28:11 +01:00
parent 3c713f830b
commit 921e669155

View File

@@ -4,29 +4,23 @@ fn main() -> std::io::Result<()> {
let args: Vec<_> = std::env::args().collect();
let filename = &args[1];
let mut contents = String::new();
let mut contents = vec![];
let mut file = File::open(filename)?;
file.read_to_string(&mut contents)?;
file.read_to_end(&mut contents)?;
let a = Instant::now();
let lines: Vec<_> = contents.lines().collect();
let lines: Vec<_> = contents.split(|&b| b == b'\n').collect();
let mut t = 0;
for (i, ln) in lines.iter().enumerate() {
for (j, ch) in ln.chars().enumerate() {
if ch == 'X' {
for (j, &ch) in ln.iter().enumerate() {
if ch == b'X' {
let map = [-1, 0, 1].map(|i| [-1, 0, 1].map(|j| (i, j)));
for dir in map.as_flattened() {
if dir.0 == 0 && dir.1 == 0 { continue; }
let r = eq_in_dir(
&lines
.iter()
.map(|ln| ln.chars().collect::<Vec<_>>())
.collect::<Vec<_>>(),
(j, i),
*dir,
"MAS".chars(),
);
if dir.0 == 0 && dir.1 == 0 {
continue;
}
let r = eq_in_dir(&lines, (j, i), *dir, "MAS".bytes());
if r {
t += 1;
}
@@ -42,19 +36,19 @@ fn main() -> std::io::Result<()> {
}
fn eq_in_dir(
buf: &[Vec<char>],
buf: &[&[u8]],
pos: (usize, usize),
dir: (isize, isize),
matches: impl Iterator<Item = char>,
matches: impl Iterator<Item = u8>,
) -> bool {
opt_eq_in_dir(buf, pos, dir, matches).is_some()
}
fn opt_eq_in_dir(
buf: &[Vec<char>],
buf: &[&[u8]],
pos: (usize, usize),
dir: (isize, isize),
mut matches: impl Iterator<Item = char>,
mut matches: impl Iterator<Item = u8>,
) -> Option<()> {
let Some(must_match) = matches.next() else {
return Some(());