From 433bbdf0f56a06629ae4352f8d2137cc66e5afba Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Fri, 15 Dec 2023 13:31:21 -0500 Subject: [PATCH] Skip day 5 part 2, starting on day 6 --- day_5/data/scratch | 25 +++++++++++++++++ day_5/src/main.rs | 57 ++++++++++++++++++++++++++++++--------- day_6/.gitignore | 1 + day_6/.vscode/launch.json | 45 +++++++++++++++++++++++++++++++ day_6/Cargo.lock | 7 +++++ day_6/Cargo.toml | 8 ++++++ day_6/build.rs | 31 +++++++++++++++++++++ day_6/src/main.rs | 26 ++++++++++++++++++ 8 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 day_5/data/scratch create mode 100644 day_6/.gitignore create mode 100644 day_6/.vscode/launch.json create mode 100644 day_6/Cargo.lock create mode 100644 day_6/Cargo.toml create mode 100644 day_6/build.rs create mode 100644 day_6/src/main.rs diff --git a/day_5/data/scratch b/day_5/data/scratch new file mode 100644 index 0000000..4602609 --- /dev/null +++ b/day_5/data/scratch @@ -0,0 +1,25 @@ +1972667147 405592018 +1450194064 27782252 +348350443 61862174 +3911195009 181169206 +626861593 138786487 +2886966111 275299008 +825403564 478003391 +514585599 6102091 +2526020300 15491453 +3211013652 546191739 + + + +TO LOW: +6335758 + + +New lowest location: 852082781 - from seed: 348350443 (VALID) +New lowest location: 624704155 - from seed: 368099087 (VALID) +New lowest location: 462405710 - from seed: 421798005 (INVALID!) +New lowest location: 123535056 - from seed: 1211303880 (VALID) -- INCORRECT +New lowest location: 122300259 - from seed: 1537339145 (INVALID!) +New lowest location: 102613526 - from seed: 2301593560 (VALID) -- ???? +New lowest location: 44948412 - from seed: 2488220507 (INVALID!) +New lowest location: 6335758 - from seed: 2837209310 (INVALID!) -- INCORRECT \ No newline at end of file diff --git a/day_5/src/main.rs b/day_5/src/main.rs index 6197cd9..39d2a2f 100644 --- a/day_5/src/main.rs +++ b/day_5/src/main.rs @@ -121,28 +121,61 @@ fn find_closest_from_range(seed_ranges: &Vec, maps: &Vec) -> i64 { let mut closest_location = i64::MAX; - for seed_range in seed_ranges + // for seed_range in seed_ranges + // { + // println!("Checking seed range: start: {}, length: {}", seed_range.start, seed_range.length); + + // let start = seed_range.start; + // let end = seed_range.start + seed_range.length + 1; + // for seed in start..end + // { + // let location = find_seed_location(seed, maps); + + // if location < closest_location + // { + // closest_location = location; + // println!("New lowest location: {}", closest_location); + // // best_seed = *seed; + // } + // } + // } + + // SUPER BRUTE FORCE ALL SEED VALUES BETWEEN SMALLEST SEED AND LARGEST SEED (INCLUDES INVALID SEEDS!) + let smallest_seed: i64 = 348350443; + let largest_seed: i64 = 4092364215 + 1; + + for seed in smallest_seed..largest_seed { - println!("Checking seed range: start: {}, length: {}", seed_range.start, seed_range.length); + let location = find_seed_location(seed, maps); - let start = seed_range.start; - let end = seed_range.start + seed_range.length + 1; - for seed in start..end + if location < closest_location { - let location = find_seed_location(seed, maps); - - if location < closest_location + closest_location = location; + let valid = match is_valid_seed(seed, seed_ranges) { - closest_location = location; - println!("New lowest location: {}", closest_location); - // best_seed = *seed; - } + true => "VALID", + false => "INVALID!" + }; + println!("New lowest location: {} - from seed: {} ({})", closest_location, seed, valid); } } closest_location } +fn is_valid_seed(value: i64, seed_ranges: &Vec) -> bool +{ + for range in seed_ranges + { + if value >= range.start && value <= range.start + range.length + { + return true; + } + } + + false +} + fn find_closest_location(seeds: &Vec, maps: &Vec) -> i64 { // let mut best_seed = 0; diff --git a/day_6/.gitignore b/day_6/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/day_6/.gitignore @@ -0,0 +1 @@ +/target diff --git a/day_6/.vscode/launch.json b/day_6/.vscode/launch.json new file mode 100644 index 0000000..e781b1e --- /dev/null +++ b/day_6/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'day_1'", + "cargo": { + "args": [ + "build", + "--bin=day_1", + "--package=day_1" + ], + "filter": { + "name": "day_1", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'day_1'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=day_1", + "--package=day_1" + ], + "filter": { + "name": "day_1", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/day_6/Cargo.lock b/day_6/Cargo.lock new file mode 100644 index 0000000..e370a42 --- /dev/null +++ b/day_6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day_6" +version = "0.1.0" diff --git a/day_6/Cargo.toml b/day_6/Cargo.toml new file mode 100644 index 0000000..b2a2c35 --- /dev/null +++ b/day_6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day_6" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_6/build.rs b/day_6/build.rs new file mode 100644 index 0000000..88d5909 --- /dev/null +++ b/day_6/build.rs @@ -0,0 +1,31 @@ + +use std::{env, fs, path::Path}; + +fn main() +{ + // let out_dir = env::var("OUT_DIR").unwrap(); + // let cwd = env::var("CARGO_MANIFEST_DIR").unwrap(); + // println!("CWD: {}\n", cwd); + // let data_dir = cwd + "\\data"; + // let data_path = Path::new(&data_dir); + // println!("Data path: {}", data_path.to_string_lossy()); + + // let data_path = Path::new("data/test_input"); + + let out_path = format!("target/{}", &env::var("PROFILE").unwrap()); + let out_path = Path::new(&out_path); + //let out_path = Path::new(&out_dir).join(&env::var("PROFILE").unwrap()); + let out_path_data = out_path.join(Path::new("data")); + + if !out_path_data.exists() + { + fs::create_dir(&out_path_data).expect(&format!("Could not create data directory at: {}", out_path_data.to_string_lossy())); + } + + for file in fs::read_dir("data").unwrap() + { + let file = file.unwrap(); + let dest = out_path.join(file.path()); + fs::copy(file.path(), &dest).expect(&format!("Could not copy file {} to {}", file.path().to_string_lossy(), dest.to_string_lossy())); + } +} \ No newline at end of file diff --git a/day_6/src/main.rs b/day_6/src/main.rs new file mode 100644 index 0000000..8c8e203 --- /dev/null +++ b/day_6/src/main.rs @@ -0,0 +1,26 @@ + +use std::{io::prelude::*, fs::File, path::Path, io }; + +fn main() +{ + println!("Hello test!"); +} + + +fn load_data(file_name: &str) -> String +{ + let mut file = match File::open(Path::new(file_name)) + { + Ok(file) => file, + Err(why) => panic!("Could not open file {}: {}", Path::new(file_name).display(), why), + }; + + let mut s = String::new(); + let file_contents = match file.read_to_string(&mut s) + { + Err(why) => panic!("couldn't read {}: {}", Path::new(file_name).display(), why), + Ok(_) => s, + }; + + return file_contents; +} \ No newline at end of file