From 7c3e9def0070ed1b30b296301d9eb64018a83194 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Fri, 15 Dec 2023 15:00:34 -0500 Subject: [PATCH] Adds normalize_line_endings function for input processing --- day_5/src/main.rs | 10 ++++++++-- day_6/data/input | 2 ++ day_6/data/test_input | 2 ++ day_6/src/main.rs | 46 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 day_6/data/input create mode 100644 day_6/data/test_input diff --git a/day_5/src/main.rs b/day_5/src/main.rs index 39d2a2f..cbf73e7 100644 --- a/day_5/src/main.rs +++ b/day_5/src/main.rs @@ -140,6 +140,12 @@ fn find_closest_from_range(seed_ranges: &Vec, maps: &Vec) -> i64 // } // } + // OTHER THINGS TO TRY: + // Super brute force but reject any seeds that are not valid (using the is_seed_valid function). + // Maybe this will include some seeds that were left out before by mistake? + // + // + // 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; @@ -151,7 +157,7 @@ fn find_closest_from_range(seed_ranges: &Vec, maps: &Vec) -> i64 if location < closest_location { closest_location = location; - let valid = match is_valid_seed(seed, seed_ranges) + let valid = match is_seed_valid(seed, seed_ranges) { true => "VALID", false => "INVALID!" @@ -163,7 +169,7 @@ fn find_closest_from_range(seed_ranges: &Vec, maps: &Vec) -> i64 closest_location } -fn is_valid_seed(value: i64, seed_ranges: &Vec) -> bool +fn is_seed_valid(value: i64, seed_ranges: &Vec) -> bool { for range in seed_ranges { diff --git a/day_6/data/input b/day_6/data/input new file mode 100644 index 0000000..33f68ee --- /dev/null +++ b/day_6/data/input @@ -0,0 +1,2 @@ +Time: 47 84 74 67 +Distance: 207 1394 1209 1014 \ No newline at end of file diff --git a/day_6/data/test_input b/day_6/data/test_input new file mode 100644 index 0000000..b39f49d --- /dev/null +++ b/day_6/data/test_input @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file diff --git a/day_6/src/main.rs b/day_6/src/main.rs index 8c8e203..05f3d78 100644 --- a/day_6/src/main.rs +++ b/day_6/src/main.rs @@ -1,11 +1,34 @@ use std::{io::prelude::*, fs::File, path::Path, io }; +#[derive(Clone, Debug)] +struct Race +{ + time: i32, + record_dist: i32, +} + fn main() { - println!("Hello test!"); + let input = load_data("data/test_input"); + + let races = parse_input(&input); } +///////////////////////////////////////////////////////////////////// +// LOAD AND PARSE INPUT +///////////////////////////////////////////////////////////////////// + +fn parse_input(input: &str) -> Vec +{ + + let mut times: Vec = Vec::new(); + let mut record_dists: Vec = Vec::new(); + + + + Vec::new() +} fn load_data(file_name: &str) -> String { @@ -22,5 +45,24 @@ fn load_data(file_name: &str) -> String Ok(_) => s, }; - return file_contents; + return normalize_line_endings(&file_contents); +} + +fn normalize_line_endings(data: &str) -> String +{ + let mut data: Vec = data.as_bytes().into(); + + let mut idx = 0; + while idx < data.len() + { + if data[idx as usize] == b'\r' + { + data.remove(idx as usize); + idx -= 1; + } + + idx += 1; + } + + String::from_utf8(data).expect("normalize_line_endings - FAILED: string was not valid utf_8") } \ No newline at end of file