day7: both parts
No files matched your search
|
After Width: | Height: | Size: 4.8 KiB |
|
After Width: | Height: | Size: 5.3 KiB |
|
After Width: | Height: | Size: 5.7 KiB |
|
After Width: | Height: | Size: 5.0 KiB |
|
After Width: | Height: | Size: 4.4 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
After Width: | Height: | Size: 5.2 KiB |
@@ -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
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
<!-- AOC TILES BEGIN -->
|
||||||
|
<h1 align="center">
|
||||||
|
2022 - 12 ⭐ - Rust
|
||||||
|
</h1>
|
||||||
|
<a href="src/day1.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/01.png" width="200">
|
||||||
|
</a>
|
||||||
|
<a href="src/day2.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/02.png" width="200">
|
||||||
|
</a>
|
||||||
|
<a href="src/day3.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/03.png" width="200">
|
||||||
|
</a>
|
||||||
|
<a href="src/day4.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/04.png" width="200">
|
||||||
|
</a>
|
||||||
|
<a href="None">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/05.png" width="200">
|
||||||
|
</a>
|
||||||
|
<a href="src/day6.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/06.png" width="200">
|
||||||
|
</a>
|
||||||
|
<a href="src/day7.rs">
|
||||||
|
<img src=".aoc_tiles/tiles/2022/07.png" width="200">
|
||||||
|
</a>
|
||||||
|
<!-- AOC TILES END -->
|
||||||
@@ -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<String, HashMap<String, DirEntry>>;
|
||||||
|
|
||||||
|
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::<u64>()
|
||||||
|
})
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
mod day7;
|
||||||
mod day4;
|
mod day4;
|
||||||
mod day1;
|
mod day1;
|
||||||
mod day2;
|
mod day2;
|
||||||
|
|||||||