Adds normalize_line_endings function for input processing

master
Joey Pollack 2 years ago
parent 433bbdf0f5
commit 7c3e9def00

@ -140,6 +140,12 @@ fn find_closest_from_range(seed_ranges: &Vec<SeedRange>, maps: &Vec<Map>) -> 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<SeedRange>, maps: &Vec<Map>) -> 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<SeedRange>, maps: &Vec<Map>) -> i64
closest_location
}
fn is_valid_seed(value: i64, seed_ranges: &Vec<SeedRange>) -> bool
fn is_seed_valid(value: i64, seed_ranges: &Vec<SeedRange>) -> bool
{
for range in seed_ranges
{

@ -0,0 +1,2 @@
Time: 47 84 74 67
Distance: 207 1394 1209 1014

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

@ -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<Race>
{
let mut times: Vec<i32> = Vec::new();
let mut record_dists: Vec<i32> = 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<u8> = 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")
}
Loading…
Cancel
Save