diff --git a/.aoc_tiles/tiles/2022/01.png b/.aoc_tiles/tiles/2022/01.png new file mode 100644 index 0000000..7266f86 Binary files /dev/null and b/.aoc_tiles/tiles/2022/01.png differ diff --git a/.aoc_tiles/tiles/2022/02.png b/.aoc_tiles/tiles/2022/02.png new file mode 100644 index 0000000..2dd639b Binary files /dev/null and b/.aoc_tiles/tiles/2022/02.png differ diff --git a/.aoc_tiles/tiles/2022/03.png b/.aoc_tiles/tiles/2022/03.png new file mode 100644 index 0000000..83eecee Binary files /dev/null and b/.aoc_tiles/tiles/2022/03.png differ diff --git a/.aoc_tiles/tiles/2022/04.png b/.aoc_tiles/tiles/2022/04.png new file mode 100644 index 0000000..af65e1c Binary files /dev/null and b/.aoc_tiles/tiles/2022/04.png differ diff --git a/.aoc_tiles/tiles/2022/05.png b/.aoc_tiles/tiles/2022/05.png new file mode 100644 index 0000000..33f19f3 Binary files /dev/null and b/.aoc_tiles/tiles/2022/05.png differ diff --git a/.aoc_tiles/tiles/2022/06.png b/.aoc_tiles/tiles/2022/06.png new file mode 100644 index 0000000..2613b93 Binary files /dev/null and b/.aoc_tiles/tiles/2022/06.png differ diff --git a/.aoc_tiles/tiles/2022/07.png b/.aoc_tiles/tiles/2022/07.png new file mode 100644 index 0000000..747f727 Binary files /dev/null and b/.aoc_tiles/tiles/2022/07.png differ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..677bd11 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,32 @@ +repos: + - repo: local + hooks: + - id: rust-linting + name: Rust linting + entry: cargo fmt --all -- + pass_filenames: true + types: [file, rust] + language: system + - id: rust-clippy + name: Rust clippy + entry: cargo clippy --lib --all-features --tests -- + pass_filenames: false + types: [file, rust] + language: system + - repo: https://github.com/LiquidFun/aoc_tiles + rev: 0.7.2 + hooks: + - id: aoc-tiles + language: system + # Optionally use these arguments. Auto add tiles to git adds the tiles to git, + # possibly amends your commit by creating the tile images and updating the README. + # Language sorting shows the preference of the order of the languages to use. + # Exclude paterns are globs which can be used to exclude files when creating + # the tiles. See the customization section in the README for more flags. + # Simply remove the comments (#) below for args and the flags you want. + args: + - --auto-add-tiles-to-git=amend + - --overwrite-year=2022 + - --tile-width-px=200 + # - --language-sorting=jl,kt,py,rs + # - --exclude-patterns=2021/*/*.apl,2021/*/*.py,2021/*/*.cpp diff --git a/README.md b/README.md new file mode 100644 index 0000000..648b411 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ + +

+ 2022 - 12 ⭐ - Rust +

+ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/day7.rs b/src/day7.rs new file mode 100644 index 0000000..9784d36 --- /dev/null +++ b/src/day7.rs @@ -0,0 +1,142 @@ +use std::{ + collections::{HashMap, HashSet}, + fs::File, +}; + +use aoc_runner_derive::{aoc, aoc_generator}; +use itertools::Itertools; + +#[derive(Debug)] +enum DirEntry { + Directory, + File(u64), +} + +type Filesystem = HashMap>; + +fn new_cwd(old_cwd: &str, target: &str) -> String { + match target { + "/" => "/".to_owned(), + ".." => { + let mut tokens = old_cwd.split('/').collect_vec(); + tokens.pop(); + tokens.pop(); + tokens.join("/") + "/" + } + s => old_cwd.to_owned() + s + "/", + } +} + +fn dir_usage(fs: &Filesystem, path: &str) -> u64 { + fs.iter() + .filter(|(k, _v)| k.starts_with(path)) + .map(|(_k, v)| { + v.values() + .map(|v| match v { + DirEntry::File(n) => *n, + _ => 0, + }) + .sum::() + }) + .sum() +} + +#[aoc_generator(day7)] +fn parse(input: &str) -> Filesystem { + let mut cwd = "/".to_owned(); + let mut fs = Filesystem::new(); + let mut lines = input.lines(); + + while let Some(line) = lines.next() { + let tokens = line.split_ascii_whitespace().collect_vec(); + if tokens[0] == "$" { + // command + match tokens[1] { + "cd" => { + cwd = new_cwd(&cwd, tokens[2]); + fs.entry(cwd.to_owned()).or_insert(HashMap::new()); + } + "ls" => {} + s => { + panic!("invalid command `{s}`") + } + } + } else { + // file entry + if let Ok(size) = tokens[0].parse() { + fs.entry(cwd.to_owned()).and_modify(|e| { + e.insert(tokens[1].into(), DirEntry::File(size)); + }); + } + // dir entry + else { + fs.entry(cwd.to_owned() + tokens[1] + "/").or_insert(HashMap::new()); + fs.entry(cwd.to_owned()).and_modify(|e| { + e.insert(tokens[1].into(), DirEntry::Directory); + }); + } + } + } + + fs +} + +const AVAIL_DISK: u64 = 70000000; +const MIN_FREE: u64 = 30000000; + +#[aoc(day7, part1)] +fn part1(fs: &Filesystem) -> u64 { + fs.keys() + .map(|path| dir_usage(&fs, path)) + .filter(|v| *v <= 100000) + .sum() +} + +#[aoc(day7, part2)] +fn part2(fs: &Filesystem) -> u64 { + let mut usages = fs.keys().map(|path| dir_usage(&fs, path)).sorted().collect_vec(); + + let total_usage = usages.pop().unwrap(); // sorted list, largest dir will always be the root which can't be deleted + let need_to_free_at_least = MIN_FREE - (AVAIL_DISK - total_usage); + + *usages.iter().find(|s| **s >= need_to_free_at_least).unwrap() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE: &str = "$ 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"; + + #[test] + fn part1_example() { + assert_eq!(part1(&parse(EXAMPLE)), 95437); + } + + #[test] + fn part2_example() { + assert_eq!(part2(&parse(EXAMPLE)), 24933642); + } +} diff --git a/src/lib.rs b/src/lib.rs index 53776f7..3a6a465 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod day7; mod day4; mod day1; mod day2;