day 7 part 1 in rust!
This commit is contained in:
parent
82a1ecab7d
commit
7aa2025dff
22
07/main.go
22
07/main.go
@ -36,7 +36,6 @@ func makeScanner(test bool) *bufio.Scanner {
|
|||||||
|
|
||||||
type Folder struct {
|
type Folder struct {
|
||||||
name string
|
name string
|
||||||
parent *Folder
|
|
||||||
files map[string]int
|
files map[string]int
|
||||||
folders map[string]*Folder
|
folders map[string]*Folder
|
||||||
size int
|
size int
|
||||||
@ -79,7 +78,6 @@ func partOne() {
|
|||||||
name := strings.Split(line, " ")[1]
|
name := strings.Split(line, " ")[1]
|
||||||
stack[len(stack)-1].folders[name] = &Folder{
|
stack[len(stack)-1].folders[name] = &Folder{
|
||||||
name: name,
|
name: name,
|
||||||
parent: stack[len(stack)-1],
|
|
||||||
files: map[string]int{},
|
files: map[string]int{},
|
||||||
folders: map[string]*Folder{},
|
folders: map[string]*Folder{},
|
||||||
}
|
}
|
||||||
@ -87,12 +85,16 @@ func partOne() {
|
|||||||
stack[len(stack)-1].files[strings.Split(line, " ")[1]] = mustAtoi(strings.Split(line, " ")[0])
|
stack[len(stack)-1].files[strings.Split(line, " ")[1]] = mustAtoi(strings.Split(line, " ")[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// compute root size
|
// finish computing directory sizes
|
||||||
for _, size := range root.files {
|
for len(stack) > 0 {
|
||||||
root.size += size
|
curr := stack[len(stack)-1]
|
||||||
|
for _, size := range curr.files {
|
||||||
|
curr.size += size
|
||||||
}
|
}
|
||||||
for _, f := range root.folders {
|
for _, f := range curr.folders {
|
||||||
root.size += f.size
|
curr.size += f.size
|
||||||
|
}
|
||||||
|
stack = stack[0 : len(stack)-1]
|
||||||
}
|
}
|
||||||
fmt.Println(sum)
|
fmt.Println(sum)
|
||||||
}
|
}
|
||||||
@ -105,7 +107,7 @@ func partTwo() {
|
|||||||
files: map[string]int{},
|
files: map[string]int{},
|
||||||
folders: map[string]*Folder{},
|
folders: map[string]*Folder{},
|
||||||
}
|
}
|
||||||
spaceToFree := 30000000 - (70000000 - 49192532)
|
spaceToFree := 30000000 - (70000000 - 49192532) // size of all files, precomputed
|
||||||
toDelete := 70000000
|
toDelete := 70000000
|
||||||
stack := []*Folder{&root}
|
stack := []*Folder{&root}
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@ -135,7 +137,6 @@ func partTwo() {
|
|||||||
name := strings.Split(line, " ")[1]
|
name := strings.Split(line, " ")[1]
|
||||||
stack[len(stack)-1].folders[name] = &Folder{
|
stack[len(stack)-1].folders[name] = &Folder{
|
||||||
name: name,
|
name: name,
|
||||||
parent: stack[len(stack)-1],
|
|
||||||
files: map[string]int{},
|
files: map[string]int{},
|
||||||
folders: map[string]*Folder{},
|
folders: map[string]*Folder{},
|
||||||
}
|
}
|
||||||
@ -152,6 +153,9 @@ func partTwo() {
|
|||||||
for _, f := range curr.folders {
|
for _, f := range curr.folders {
|
||||||
curr.size += f.size
|
curr.size += f.size
|
||||||
}
|
}
|
||||||
|
if curr.size > spaceToFree && curr.size < toDelete {
|
||||||
|
toDelete = curr.size
|
||||||
|
}
|
||||||
stack = stack[0 : len(stack)-1]
|
stack = stack[0 : len(stack)-1]
|
||||||
}
|
}
|
||||||
fmt.Println(toDelete)
|
fmt.Println(toDelete)
|
||||||
|
61
07/main.rs
61
07/main.rs
@ -7,18 +7,11 @@ use std::path::Path;
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct ElfFile {
|
struct Folder {
|
||||||
name: String,
|
folders: HashMap<String, u32>,
|
||||||
size: u32,
|
size: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct Folder<'a> {
|
|
||||||
name: String,
|
|
||||||
files: HashMap<String, ElfFile>,
|
|
||||||
folders: HashMap<String, &'a Folder<'a>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
part_one();
|
part_one();
|
||||||
@ -41,58 +34,40 @@ where
|
|||||||
Ok(io::BufReader::new(file).lines())
|
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() {
|
fn part_one() {
|
||||||
if let Ok(lines) = read_lines("./inputs/input") {
|
if let Ok(lines) = read_lines("./inputs/input") {
|
||||||
let mut tree = Folder {
|
let mut sum = 0;
|
||||||
name: "/".to_string(),
|
let mut stack: Vec<u32> = vec![0];
|
||||||
files: HashMap::new(),
|
|
||||||
folders: HashMap::new(),
|
|
||||||
};
|
|
||||||
let mut stack: Vec<&mut Folder> = vec![&mut tree];
|
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if let Ok(command) = line {
|
if let Ok(command) = line {
|
||||||
match command.as_str() {
|
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
|
"$ 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 .." => {
|
"$ 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 ") => {
|
s if s.starts_with("$ cd ") => {
|
||||||
println!("push {}\n", s.split(" ").nth(2).unwrap())
|
stack.push(0);
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
s => {
|
s => {
|
||||||
// otherwise it must be a file
|
// otherwise it must be a file
|
||||||
let f = stack.pop().unwrap();
|
let mut f = stack.pop().unwrap();
|
||||||
let ef = ElfFile_FromStr(s);
|
let mut sp = s.split(" ");
|
||||||
f.files.insert(ef.name.clone(), ef);
|
let size = sp.next().unwrap().parse::<u32>().unwrap();
|
||||||
|
f += size;
|
||||||
stack.push(f);
|
stack.push(f);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("{:?}", tree);
|
println!("{:?}", sum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user