1
0

Added day 7 part 1

This commit is contained in:
Ishan Jain 2022-12-07 17:07:43 +05:30
parent 94289d0c7a
commit bd982a21c5
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 1162 additions and 39 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
/target /target
go.mod
go.sum
test.go
test

File diff suppressed because it is too large Load Diff

View File

@ -1 +1,23 @@
mjqjpqmgbljsphdztnvjfqwrcgsmlb $ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

View File

@ -1,66 +1,78 @@
#![feature(byte_slice_trim_ascii)]
#![feature(test)] #![feature(test)]
extern crate test; extern crate test;
const INPUTS: [&[u8]; 2] = [ const INPUTS: [&str; 2] = [
include_bytes!("../inputs/sample.txt"), include_str!("../inputs/sample.txt"),
include_bytes!("../inputs/input.txt"), include_str!("../inputs/input.txt"),
]; ];
fn parse(input: &[u8]) -> &[u8] { fn parse(input: &'static str) -> impl Iterator<Item = &'static str> {
input.trim_ascii() let input = input.trim().lines();
input
} }
fn main() { fn main() {
for input in INPUTS.iter() { for input in INPUTS.iter() {
let output = parse(input); let output = parse(input);
let score = solution::<14>(output); let score = solution(output);
println!("{}", score); println!("{}", score);
} }
} }
fn solution<const N: usize>(input: &[u8]) -> usize { fn solution(input: impl Iterator<Item = &'static str>) -> u32 {
for idx in 0..input.len() - N { let mut out = vec![];
let mut buffer = 0u32; let mut stack = vec![];
for &c in &input[idx..(idx + N)] { let mut current_folder_size = 0;
buffer |= 1 << (c - b'a') as usize;
for line in input {
if line.starts_with("$ ls") || line.starts_with("dir") {
continue;
} }
if buffer.count_ones() as usize == N { if line.starts_with("$ cd") {
return idx + N; let dir = line.trim_start_matches("$ cd ");
match dir {
".." => {
let v = stack.pop().unwrap();
out.push(current_folder_size);
current_folder_size += v;
}
"/" => continue,
_ => {
stack.push(current_folder_size);
current_folder_size = 0;
}
}
} else {
let (size, _) = line.split_once(' ').unwrap();
let size = size.parse::<u32>().unwrap();
current_folder_size += size;
} }
} }
0 while let Some(v) = stack.pop() {
out.push(current_folder_size);
current_folder_size += v;
}
out.push(current_folder_size);
out.into_iter().filter(|&c| c <= 100000).sum::<u32>()
} }
#[bench] #[bench]
fn solution_bench(b: &mut test::Bencher) { fn solution_bench(b: &mut test::Bencher) {
b.iter(|| { b.iter(|| {
let input = parse(INPUTS[1]); let input = parse(INPUTS[1]);
let result = solution::<14>(input); let result = solution(input);
test::black_box(result); test::black_box(result);
}) })
} }
#[test]
fn tests() {
assert_eq!(
solution::<14>("bvwbjplbgvbhsrlpgdmjqwftvncz".as_bytes()),
23
);
assert_eq!(
solution::<14>("nppdvjthqldpwncqszvftbrmjlhg".as_bytes()),
23
);
assert_eq!(
solution::<14>("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg".as_bytes()),
29
);
assert_eq!(
solution::<14>("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw".as_bytes()),
26
);
}