Skip to content

Commit f0c007c

Browse files
committed
feat(day_07): somewhat sloppy solution
1 parent 7a022bb commit f0c007c

File tree

7 files changed

+1334
-1
lines changed

7 files changed

+1334
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- [Day 4](https://github.com/ankjevel/adventofcode/tree/2022/day_04) ⭐️ ⭐️
99
- [Day 5](https://github.com/ankjevel/adventofcode/tree/2022/day_05) ⭐️ ⭐️
1010
- [Day 6](https://github.com/ankjevel/adventofcode/tree/2022/day_06) ⭐️ ⭐️
11-
- [Day 7](#)
11+
- [Day 7](https://github.com/ankjevel/adventofcode/tree/2022/day_07) ⭐️ ⭐️
1212
- [Day 8](#)
1313
- [Day 9](#)
1414
- [Day 10](#)

day_07/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "day_07"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Dennis Pettersson <mail@dennispettersson.se>"]
6+
7+
[lib]
8+
doctest = false
9+
10+
[[bin]]
11+
name = "day_07"
12+
13+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14+
15+
[dependencies]

day_07/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub mod part_01;
2+
pub mod part_02;
3+
4+
pub type Input = Vec<String>;
5+
6+
pub fn parse_input(input: &str) -> Input {
7+
input
8+
.lines()
9+
.map(str::trim)
10+
.filter(|string| !string.is_empty())
11+
.map(str::to_owned)
12+
.collect()
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
const EXAMPLE_DATA: &'static str = "
20+
example
21+
";
22+
23+
#[test]
24+
fn it_parses_example() {
25+
assert_eq!(parse_input(&EXAMPLE_DATA), vec!["example"]);
26+
}
27+
}

day_07/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::io::Result;
2+
3+
use day_07::{parse_input, part_01::main as part_01, part_02::main as part_02};
4+
5+
fn main() -> Result<()> {
6+
let input = parse_input(include_str!("../../input/day_07"));
7+
8+
println!("part_01: {:?}", part_01(&input)?);
9+
println!("part_02: {:?}", part_02(&input)?);
10+
11+
Ok(())
12+
}

day_07/src/part_01.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use std::{
2+
collections::{HashMap, VecDeque},
3+
io::Result,
4+
};
5+
6+
use crate::Input;
7+
8+
pub fn main(input: &Input) -> Result<u32> {
9+
let mut path: VecDeque<String> = VecDeque::new();
10+
let mut dirs: HashMap<String, u32> = HashMap::new();
11+
12+
for row in input.into_iter() {
13+
if row.starts_with('$') {
14+
if row.contains("$ cd") {
15+
let next_dir = row.replace("$ cd ", "");
16+
if next_dir.contains("..") {
17+
path.pop_back();
18+
} else {
19+
path.push_back(if next_dir.contains('/') {
20+
next_dir
21+
} else {
22+
format!("{}/", next_dir)
23+
});
24+
}
25+
}
26+
} else {
27+
if !row.starts_with("dir ") {
28+
let n: Vec<&str> = row.split(' ').collect();
29+
let size = n[0].parse::<u32>().unwrap();
30+
let mut current: Vec<String> = vec![];
31+
for path in path.clone().into_iter() {
32+
current.push(path);
33+
let path = current.join("");
34+
*dirs.entry(path).or_insert(0) += size;
35+
}
36+
}
37+
}
38+
}
39+
40+
Ok(dirs
41+
.into_values()
42+
.filter(|val| val <= &100_000)
43+
.sum::<u32>())
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use crate::parse_input;
49+
50+
use super::*;
51+
52+
const EXAMPLE_DATA: &'static str = "
53+
$ cd /
54+
$ ls
55+
dir a
56+
14848514 b.txt
57+
8504156 c.dat
58+
dir d
59+
$ cd a
60+
$ ls
61+
dir e
62+
29116 f
63+
2557 g
64+
62596 h.lst
65+
$ cd e
66+
$ ls
67+
584 i
68+
$ cd ..
69+
$ cd ..
70+
$ cd d
71+
$ ls
72+
4060174 j
73+
8033020 d.log
74+
5626152 d.ext
75+
7214296 k
76+
";
77+
78+
#[test]
79+
fn it_gets_the_example_correct() -> Result<()> {
80+
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 95437);
81+
Ok(())
82+
}
83+
}

day_07/src/part_02.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::{
2+
collections::{HashMap, VecDeque},
3+
io::Result,
4+
};
5+
6+
use crate::Input;
7+
8+
pub fn main(input: &Input) -> Result<u32> {
9+
let mut path: VecDeque<String> = VecDeque::new();
10+
let mut dirs: HashMap<String, u32> = HashMap::new();
11+
12+
for row in input.into_iter() {
13+
if row.starts_with('$') {
14+
if row.contains("$ cd") {
15+
let next_dir = row.replace("$ cd ", "");
16+
if next_dir.contains("..") {
17+
path.pop_back();
18+
} else {
19+
path.push_back(if next_dir.contains('/') {
20+
next_dir
21+
} else {
22+
format!("{}/", next_dir)
23+
});
24+
}
25+
}
26+
} else {
27+
if !row.starts_with("dir ") {
28+
let n: Vec<&str> = row.split(' ').collect();
29+
let size = n[0].parse::<u32>().unwrap();
30+
let mut current: Vec<String> = vec![];
31+
for path in path.clone().into_iter() {
32+
current.push(path);
33+
let path = current.join("");
34+
*dirs.entry(path).or_insert(0) += size;
35+
}
36+
}
37+
}
38+
}
39+
40+
let to_remove = 30_000_000 - (70_000_000 - dirs.get("/").unwrap_or(&0));
41+
42+
Ok(dirs
43+
.clone()
44+
.into_values()
45+
.filter(|val| val >= &to_remove)
46+
.min()
47+
.unwrap())
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use crate::parse_input;
53+
54+
use super::*;
55+
56+
const EXAMPLE_DATA: &'static str = "
57+
$ cd /
58+
$ ls
59+
dir a
60+
14848514 b.txt
61+
8504156 c.dat
62+
dir d
63+
$ cd a
64+
$ ls
65+
dir e
66+
29116 f
67+
2557 g
68+
62596 h.lst
69+
$ cd e
70+
$ ls
71+
584 i
72+
$ cd ..
73+
$ cd ..
74+
$ cd d
75+
$ ls
76+
4060174 j
77+
8033020 d.log
78+
5626152 d.ext
79+
7214296 k
80+
";
81+
82+
#[test]
83+
fn it_gets_the_example_correct() -> Result<()> {
84+
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 24933642);
85+
Ok(())
86+
}
87+
}

0 commit comments

Comments
 (0)