1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Coleman bb9d2d5c6a add cfg(test_input) 2022-12-07 14:16:10 -05:00
Andrew Coleman 3905fdb350 cargo fmt 2022-12-07 14:11:37 -05:00
9 changed files with 70 additions and 7 deletions

View File

@ -5,5 +5,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = []
test_input = []
[dependencies]
anyhow = "1.0.66"

View File

@ -2,7 +2,12 @@ use anyhow::Result;
use std::fs;
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
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 mut calories = Vec::with_capacity(lines.clone().count());
let mut count: u32 = 0;

View File

@ -14,7 +14,12 @@ enum EndStates {
}
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
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 mut part_one_scores: Vec<u32> = Vec::with_capacity(1000);
let mut part_two_scores: Vec<u32> = Vec::with_capacity(1000);

View File

@ -13,7 +13,12 @@ fn get_priority(t: &char) -> u32 {
}
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
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 mut rucksacks: Vec<u32> = Vec::with_capacity(1000);
let mut rucksacks2: Vec<u32> = Vec::with_capacity(1000);

View File

@ -2,7 +2,12 @@ use anyhow::Result;
use std::fs;
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
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 mut part_one_pairs = 0;
let mut part_two_pairs = 0;

View File

@ -2,7 +2,12 @@ use anyhow::Result;
use std::fs;
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
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 moves: Vec<(usize, usize, usize)> = Vec::with_capacity(1000);
let mut numstacks: usize = 0;

View File

@ -24,7 +24,13 @@ fn unique_run_position<'a>(line: &'a str, count: usize) -> usize {
}
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
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 mut line_counter = 0;

View File

@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use std::fs;
use std::collections::BTreeMap;
use std::fs;
#[derive(Debug,Default)]
#[derive(Debug, Default)]
struct Dir {
name: String,
files: Vec<File>,
@ -68,7 +68,7 @@ fn part_two_sizes<'a>(dir: &'a Dir, needed: u32) -> Option<(u32, &'a str)> {
for (key, val) in map.iter() {
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) {
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() {
part_two_recurse(map, subdir);
}
}
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("tests/day7.txt")?;
let mut root = Dir {
name: String::from("/"),
@ -165,10 +171,18 @@ pub fn run() -> Result<()> {
let min_free_space = 30000000;
let free_space = total_disk - root.total_size as u32;
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) {
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 {
println!("could not find part 2");
}

14
2022/tests/day1.txt Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000