Compare commits
2 Commits
ce63f9771b
...
bb9d2d5c6a
Author | SHA1 | Date |
---|---|---|
|
bb9d2d5c6a | |
|
3905fdb350 |
|
@ -5,5 +5,9 @@ edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
test_input = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.66"
|
anyhow = "1.0.66"
|
||||||
|
|
|
@ -2,7 +2,12 @@ use anyhow::Result;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day1.txt")?;
|
let file_contents = fs::read_to_string("day1.txt")?;
|
||||||
|
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day1.txt")?;
|
||||||
|
|
||||||
let lines = file_contents.lines();
|
let lines = file_contents.lines();
|
||||||
let mut calories = Vec::with_capacity(lines.clone().count());
|
let mut calories = Vec::with_capacity(lines.clone().count());
|
||||||
let mut count: u32 = 0;
|
let mut count: u32 = 0;
|
||||||
|
|
|
@ -14,7 +14,12 @@ enum EndStates {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day2.txt")?;
|
let file_contents = fs::read_to_string("day2.txt")?;
|
||||||
|
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day2.txt")?;
|
||||||
|
|
||||||
let lines = file_contents.lines();
|
let lines = file_contents.lines();
|
||||||
let mut part_one_scores: Vec<u32> = Vec::with_capacity(1000);
|
let mut part_one_scores: Vec<u32> = Vec::with_capacity(1000);
|
||||||
let mut part_two_scores: Vec<u32> = Vec::with_capacity(1000);
|
let mut part_two_scores: Vec<u32> = Vec::with_capacity(1000);
|
||||||
|
|
|
@ -13,7 +13,12 @@ fn get_priority(t: &char) -> u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day3.txt")?;
|
let file_contents = fs::read_to_string("day3.txt")?;
|
||||||
|
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day3.txt")?;
|
||||||
|
|
||||||
let lines = file_contents.lines();
|
let lines = file_contents.lines();
|
||||||
let mut rucksacks: Vec<u32> = Vec::with_capacity(1000);
|
let mut rucksacks: Vec<u32> = Vec::with_capacity(1000);
|
||||||
let mut rucksacks2: Vec<u32> = Vec::with_capacity(1000);
|
let mut rucksacks2: Vec<u32> = Vec::with_capacity(1000);
|
||||||
|
|
|
@ -2,7 +2,12 @@ use anyhow::Result;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day4.txt")?;
|
let file_contents = fs::read_to_string("day4.txt")?;
|
||||||
|
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day4.txt")?;
|
||||||
|
|
||||||
let lines = file_contents.lines();
|
let lines = file_contents.lines();
|
||||||
let mut part_one_pairs = 0;
|
let mut part_one_pairs = 0;
|
||||||
let mut part_two_pairs = 0;
|
let mut part_two_pairs = 0;
|
||||||
|
|
|
@ -2,7 +2,12 @@ use anyhow::Result;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day5.txt")?;
|
let file_contents = fs::read_to_string("day5.txt")?;
|
||||||
|
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day5.txt")?;
|
||||||
|
|
||||||
let mut stacks: Vec<Vec<&str>> = Vec::with_capacity(10);
|
let mut stacks: Vec<Vec<&str>> = Vec::with_capacity(10);
|
||||||
let mut moves: Vec<(usize, usize, usize)> = Vec::with_capacity(1000);
|
let mut moves: Vec<(usize, usize, usize)> = Vec::with_capacity(1000);
|
||||||
let mut numstacks: usize = 0;
|
let mut numstacks: usize = 0;
|
||||||
|
|
|
@ -24,7 +24,13 @@ fn unique_run_position<'a>(line: &'a str, count: usize) -> usize {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day6.txt")?;
|
let file_contents = fs::read_to_string("day6.txt")?;
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day6.txt")?;
|
||||||
|
|
||||||
let lines = file_contents.lines();
|
let lines = file_contents.lines();
|
||||||
let mut line_counter = 0;
|
let mut line_counter = 0;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use std::fs;
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Dir {
|
struct Dir {
|
||||||
|
@ -68,7 +68,7 @@ fn part_two_sizes<'a>(dir: &'a Dir, needed: u32) -> Option<(u32, &'a str)> {
|
||||||
|
|
||||||
for (key, val) in map.iter() {
|
for (key, val) in map.iter() {
|
||||||
if *key >= needed {
|
if *key >= needed {
|
||||||
return Some((*key, val))
|
return Some((*key, val));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,15 +76,21 @@ fn part_two_sizes<'a>(dir: &'a Dir, needed: u32) -> Option<(u32, &'a str)> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_two_recurse<'a>(map: &mut BTreeMap<u32, &'a str>, dir: &'a Dir) {
|
fn part_two_recurse<'a>(map: &mut BTreeMap<u32, &'a str>, dir: &'a Dir) {
|
||||||
map.insert((dir.total_size as u32).try_into().unwrap(), dir.name.as_str());
|
map.insert(
|
||||||
|
(dir.total_size as u32).try_into().unwrap(),
|
||||||
|
dir.name.as_str(),
|
||||||
|
);
|
||||||
for subdir in dir.directories.iter() {
|
for subdir in dir.directories.iter() {
|
||||||
part_two_recurse(map, subdir);
|
part_two_recurse(map, subdir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() -> Result<()> {
|
pub fn run() -> Result<()> {
|
||||||
|
#[cfg(feature = "test_input")]
|
||||||
|
let file_contents = fs::read_to_string("tests/day7.txt")?;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "test_input"))]
|
||||||
let file_contents = fs::read_to_string("day7.txt")?;
|
let file_contents = fs::read_to_string("day7.txt")?;
|
||||||
//let file_contents = fs::read_to_string("tests/day7.txt")?;
|
|
||||||
|
|
||||||
let mut root = Dir {
|
let mut root = Dir {
|
||||||
name: String::from("/"),
|
name: String::from("/"),
|
||||||
|
@ -165,10 +171,18 @@ pub fn run() -> Result<()> {
|
||||||
let min_free_space = 30000000;
|
let min_free_space = 30000000;
|
||||||
let free_space = total_disk - root.total_size as u32;
|
let free_space = total_disk - root.total_size as u32;
|
||||||
let needed_space = min_free_space - free_space;
|
let needed_space = min_free_space - free_space;
|
||||||
println!("total size used {} free {} needed {}", root.total_size, free_space, needed_space);
|
println!(
|
||||||
|
"total size used {} free {} needed {}",
|
||||||
|
root.total_size, free_space, needed_space
|
||||||
|
);
|
||||||
|
|
||||||
if let Some((rm_space, rm_name)) = part_two_sizes(&root, needed_space) {
|
if let Some((rm_space, rm_name)) = part_two_sizes(&root, needed_space) {
|
||||||
println!("part two: should delete {} to reclaim {} total free space {}", rm_name, rm_space, free_space + rm_space);
|
println!(
|
||||||
|
"part two: should delete {} to reclaim {} total free space {}",
|
||||||
|
rm_name,
|
||||||
|
rm_space,
|
||||||
|
free_space + rm_space
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
println!("could not find part 2");
|
println!("could not find part 2");
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000
|
Loading…
Reference in New Issue