day 7 part 1 in rust!

This commit is contained in:
David 2022-12-07 22:07:18 -05:00
parent 82a1ecab7d
commit 7aa2025dff
2 changed files with 33 additions and 54 deletions

View File

@ -36,7 +36,6 @@ func makeScanner(test bool) *bufio.Scanner {
type Folder struct {
name string
parent *Folder
files map[string]int
folders map[string]*Folder
size int
@ -79,7 +78,6 @@ func partOne() {
name := strings.Split(line, " ")[1]
stack[len(stack)-1].folders[name] = &Folder{
name: name,
parent: stack[len(stack)-1],
files: map[string]int{},
folders: map[string]*Folder{},
}
@ -87,12 +85,16 @@ func partOne() {
stack[len(stack)-1].files[strings.Split(line, " ")[1]] = mustAtoi(strings.Split(line, " ")[0])
}
}
// compute root size
for _, size := range root.files {
root.size += size
}
for _, f := range root.folders {
root.size += f.size
// finish computing directory sizes
for len(stack) > 0 {
curr := stack[len(stack)-1]
for _, size := range curr.files {
curr.size += size
}
for _, f := range curr.folders {
curr.size += f.size
}
stack = stack[0 : len(stack)-1]
}
fmt.Println(sum)
}
@ -105,7 +107,7 @@ func partTwo() {
files: map[string]int{},
folders: map[string]*Folder{},
}
spaceToFree := 30000000 - (70000000 - 49192532)
spaceToFree := 30000000 - (70000000 - 49192532) // size of all files, precomputed
toDelete := 70000000
stack := []*Folder{&root}
for scanner.Scan() {
@ -135,7 +137,6 @@ func partTwo() {
name := strings.Split(line, " ")[1]
stack[len(stack)-1].folders[name] = &Folder{
name: name,
parent: stack[len(stack)-1],
files: map[string]int{},
folders: map[string]*Folder{},
}
@ -152,6 +153,9 @@ func partTwo() {
for _, f := range curr.folders {
curr.size += f.size
}
if curr.size > spaceToFree && curr.size < toDelete {
toDelete = curr.size
}
stack = stack[0 : len(stack)-1]
}
fmt.Println(toDelete)

View File

@ -7,18 +7,11 @@ use std::path::Path;
use std::time::Instant;
#[derive(Debug, Clone)]
struct ElfFile {
name: String,
struct Folder {
folders: HashMap<String, u32>,
size: u32,
}
#[derive(Debug, Clone)]
struct Folder<'a> {
name: String,
files: HashMap<String, ElfFile>,
folders: HashMap<String, &'a Folder<'a>>,
}
fn main() {
let now = Instant::now();
part_one();
@ -41,58 +34,40 @@ where
Ok(io::BufReader::new(file).lines())
}
fn ElfFile_FromStr(s: &str) -> ElfFile {
let mut f = s.split(" ");
return ElfFile {
size: f.next().unwrap().parse::<u32>().unwrap(),
name: f.next().unwrap().to_string(),
};
}
fn newFolder(s: &str) -> Folder {
return Folder {
name: s.to_string(),
files: HashMap::new(),
folders: HashMap::new(),
};
}
fn part_one() {
if let Ok(lines) = read_lines("./inputs/input") {
let mut tree = Folder {
name: "/".to_string(),
files: HashMap::new(),
folders: HashMap::new(),
};
let mut stack: Vec<&mut Folder> = vec![&mut tree];
let mut sum = 0;
let mut stack: Vec<u32> = vec![0];
for line in lines {
if let Ok(command) = line {
match command.as_str() {
"$ cd /" => continue, // skip start
"$ cd /" => continue, // skip start
"$ ls" => continue, // we can assume this if we're not moving between directories
s if s.starts_with("dir ") => continue, // we'll get there when we `cd`
"$ cd .." => {
// stack.pop();
let f = stack.pop().unwrap();
if f < 100000 {
sum += f
}
let mut p = stack.pop().unwrap();
p += f;
stack.push(p);
}
s if s.starts_with("$ cd ") => {
println!("push {}\n", s.split(" ").nth(2).unwrap())
}
s if s.starts_with("dir ") => {
let f = stack.pop().unwrap();
let ef = newFolder(s.split(" ").nth(1).unwrap());
f.folders.insert(ef.name.clone(), &ef);
stack.push(f);
stack.push(0);
}
s => {
// otherwise it must be a file
let f = stack.pop().unwrap();
let ef = ElfFile_FromStr(s);
f.files.insert(ef.name.clone(), ef);
let mut f = stack.pop().unwrap();
let mut sp = s.split(" ");
let size = sp.next().unwrap().parse::<u32>().unwrap();
f += size;
stack.push(f);
}
};
}
}
println!("{:?}", tree);
println!("{:?}", sum);
}
}