mirror of
https://github.com/javalsai/aoc.git
synced 2026-01-13 01:19:59 +01:00
day 4 smol profiling (next commit is crazy)
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::{fs::File, io::Read};
|
use std::{fs::File, io::Read, time::Instant};
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let args: Vec<_> = std::env::args().collect();
|
let args: Vec<_> = std::env::args().collect();
|
||||||
@@ -8,40 +8,16 @@ fn main() -> std::io::Result<()> {
|
|||||||
let mut file = File::open(filename)?;
|
let mut file = File::open(filename)?;
|
||||||
file.read_to_string(&mut contents)?;
|
file.read_to_string(&mut contents)?;
|
||||||
|
|
||||||
|
let a = Instant::now();
|
||||||
|
|
||||||
let lines: Vec<_> = contents.lines().collect();
|
let lines: Vec<_> = contents.lines().collect();
|
||||||
// let height = lines.len();
|
|
||||||
// let width = lines[0].len();
|
|
||||||
// let res_buf: Vec<Vec<_>> = (0..height)
|
|
||||||
// .map(|_| (0..width).map(|_| '.').collect())
|
|
||||||
// .collect();
|
|
||||||
// _ = &res_buf;
|
|
||||||
|
|
||||||
print_res(
|
|
||||||
&lines
|
|
||||||
.iter()
|
|
||||||
.map(|ln| ln.chars().collect::<Vec<_>>())
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// let r = eq_in_dir(
|
|
||||||
// &lines
|
|
||||||
// .iter()
|
|
||||||
// .map(|ln| ln.chars().collect::<Vec<_>>())
|
|
||||||
// .collect::<Vec<_>>(),
|
|
||||||
// (4, 0),
|
|
||||||
// (1, 1),
|
|
||||||
// "MAS".chars(),
|
|
||||||
// );
|
|
||||||
// println!("{r:?}");
|
|
||||||
// return Ok(());
|
|
||||||
|
|
||||||
let mut t = 0;
|
let mut t = 0;
|
||||||
for (i, ln) in lines.iter().enumerate() {
|
for (i, ln) in lines.iter().enumerate() {
|
||||||
for (j, ch) in ln.chars().enumerate() {
|
for (j, ch) in ln.chars().enumerate() {
|
||||||
if ch == 'X' {
|
if ch == 'X' {
|
||||||
// println!("X found at ({j}, {i})");
|
|
||||||
let map = [-1, 0, 1].map(|i| [-1, 0, 1].map(|j| (i, j)));
|
let map = [-1, 0, 1].map(|i| [-1, 0, 1].map(|j| (i, j)));
|
||||||
for dir in map.as_flattened() {
|
for dir in map.as_flattened() {
|
||||||
|
if dir.0 == 0 && dir.1 == 0 { continue; }
|
||||||
let r = eq_in_dir(
|
let r = eq_in_dir(
|
||||||
&lines
|
&lines
|
||||||
.iter()
|
.iter()
|
||||||
@@ -51,14 +27,17 @@ fn main() -> std::io::Result<()> {
|
|||||||
*dir,
|
*dir,
|
||||||
"MAS".chars(),
|
"MAS".chars(),
|
||||||
);
|
);
|
||||||
if r { t += 1; }
|
if r {
|
||||||
// println!(" {dir:?}: {r:?}");
|
t += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("total {t}");
|
let b = Instant::now();
|
||||||
|
|
||||||
|
println!("total {t} in {:?}", b - a);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,18 +56,14 @@ fn opt_eq_in_dir(
|
|||||||
dir: (isize, isize),
|
dir: (isize, isize),
|
||||||
mut matches: impl Iterator<Item = char>,
|
mut matches: impl Iterator<Item = char>,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
|
|
||||||
// println!(" {pos:?} {dir:?}");
|
|
||||||
let Some(must_match) = matches.next() else {
|
let Some(must_match) = matches.next() else {
|
||||||
return Some(());
|
return Some(());
|
||||||
};
|
};
|
||||||
// println!(" {must_match:?}");
|
|
||||||
|
|
||||||
let new_1 = do_the_thing(dir.1, pos.1)?;
|
let new_1 = do_the_thing(dir.1, pos.1)?;
|
||||||
let new_0 = do_the_thing(dir.0, pos.0)?;
|
let new_0 = do_the_thing(dir.0, pos.0)?;
|
||||||
let ln = buf.get(new_1)?;
|
let ln = buf.get(new_1)?;
|
||||||
let ch = ln.get(new_0)?;
|
let ch = ln.get(new_0)?;
|
||||||
// println!(" {ch:?}");
|
|
||||||
|
|
||||||
if must_match == *ch {
|
if must_match == *ch {
|
||||||
opt_eq_in_dir(buf, (new_0, new_1), dir, matches)
|
opt_eq_in_dir(buf, (new_0, new_1), dir, matches)
|
||||||
@@ -97,6 +72,7 @@ fn opt_eq_in_dir(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn do_the_thing(a: isize, b: usize) -> Option<usize> {
|
fn do_the_thing(a: isize, b: usize) -> Option<usize> {
|
||||||
let r = b as isize + a;
|
let r = b as isize + a;
|
||||||
if r < 0 {
|
if r < 0 {
|
||||||
@@ -104,9 +80,3 @@ fn do_the_thing(a: isize, b: usize) -> Option<usize> {
|
|||||||
}
|
}
|
||||||
Some(r as usize)
|
Some(r as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_res(buf: &[Vec<char>]) {
|
|
||||||
for ln in buf.iter() {
|
|
||||||
println!("{}", ln.iter().collect::<String>())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user