From dec9308b2fe3b811ee4131feb87bb507f7a2e855 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sat, 7 Dec 2024 12:47:53 +0100 Subject: [PATCH] updated the convert_to array function --- src/bin/2024/01.rs | 4 ++-- src/bin/2024/02.rs | 2 +- src/strings.rs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bin/2024/01.rs b/src/bin/2024/01.rs index 4b28e82..23b1fde 100644 --- a/src/bin/2024/01.rs +++ b/src/bin/2024/01.rs @@ -1,4 +1,4 @@ -use advent_of_code::strings::{parsenumber, splitspace}; +use advent_of_code::strings::{convert_to_array, parsenumber, splitspace}; #[allow(unused_imports)] use advent_of_code::{include_data, include_example}; @@ -37,7 +37,7 @@ fn similarity(leftlist: &Vec, rightlist: &Vec) -> u32 { fn main() { let mut leftlist = Vec::::with_capacity(1000); let mut rightlist = Vec::::with_capacity(1000); - for (left, right) in DATA.split("\n").map(splitspace) { + for (left, right) in convert_to_array::<_, _, '\n'>(DATA, splitspace) { leftlist.push(parsenumber(left)); rightlist.push(parsenumber(right)); } diff --git a/src/bin/2024/02.rs b/src/bin/2024/02.rs index a52da09..4eb9faa 100644 --- a/src/bin/2024/02.rs +++ b/src/bin/2024/02.rs @@ -101,7 +101,7 @@ fn safe(record: Vec) -> bool { } fn main() { - let numbers = convert_to_array(DATA, get_numbers); + let numbers = convert_to_array::<_,_,'\n'>(DATA, get_numbers); let mut safe_reports = 0; let mut safe_with_dampener = 0; for report in numbers { diff --git a/src/strings.rs b/src/strings.rs index 05f4a5a..976ffa9 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -1,7 +1,7 @@ const MAXMUL: u32 = 100000; pub fn splitspace(input: &str) -> (&str, &str) { - let mut output = input.split_ascii_whitespace(); + let mut output = input.clone().split_ascii_whitespace(); (output.next().unwrap(), output.next().unwrap()) } @@ -162,6 +162,6 @@ pub fn line_to_char(line: &str) -> Vec { Vec::from_iter(line.chars()) } -pub fn convert_to_array T>(input: &str, func: F) -> Vec { - input.split('\n').map(func).collect() +pub fn convert_to_array T, const S: char>(input: &str, func: F) -> Vec { + input.split(S).map(func).collect() }