From 72c2bfd82402bfa1f0de0fc0180f248df0b79962 Mon Sep 17 00:00:00 2001 From: OMGeeky <39029799+OMGeeky@users.noreply.github.com> Date: Wed, 6 Dec 2023 19:05:15 +0000 Subject: [PATCH] change from Vec to just T as input type --- src/day01.rs | 40 +++++++++++++++++++--------------------- src/day02.rs | 27 +++++++++++++-------------- src/day03.rs | 28 ++++++++++++++-------------- src/day04.rs | 28 ++++++++++++++-------------- src/lib.rs | 12 ++++++------ 5 files changed, 66 insertions(+), 69 deletions(-) diff --git a/src/day01.rs b/src/day01.rs index 55c47a2..118a6ec 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -8,22 +8,21 @@ impl Day for Day01 { type Output = usize; - fn get_test_data() -> Vec { - vec![ - "1abc2".to_string(), - "pqr3stu8vwx".to_string(), - "a1b2c3d4e5f".to_string(), - "treb7uchet".to_string(), - ] + fn get_test_data() -> Self::Input { + "1abc2 + pqr3stu8vwx + a1b2c3d4e5f + treb7uchet" + .to_string() } fn get_test_result() -> Self::Output { 142 } - fn run(data: Vec) -> Self::Output { + fn run(data: Self::Input) -> Self::Output { let mut sum = 0; - for line in data { + for line in data.lines().map(|x| x.trim().to_string()) { let mut digits = line.chars().filter(|c| c.is_digit(10)); let first = digits.next().unwrap_or(' '); let last = digits.last().unwrap_or(first); @@ -45,24 +44,23 @@ impl Day for Day01 { } } impl DayPart2 for Day01 { - fn get_test_data_part2() -> Vec { - vec![ - "two1nine".to_string(), - "eightwothree".to_string(), - "abcone2threexyz".to_string(), - "xtwone3four".to_string(), - "4nineeightseven2".to_string(), - "zoneight234".to_string(), - "7pqrstsixteen".to_string(), - ] + fn get_test_data_part2() -> String { + "two1nine + eightwothree + abcone2threexyz + xtwone3four + 4nineeightseven2 + zoneight234 + 7pqrstsixteen" + .to_string() } fn get_test_result_part2() -> usize { 281 } - fn run_part2(data: Vec) -> usize { + fn run_part2(data: String) -> usize { let mut sum = 0; - for line in data { + for line in data.lines().map(|x| x.trim()) { let mut l = String::new(); for i in 0..line.len() { let part = &line[i..]; diff --git a/src/day02.rs b/src/day02.rs index 6d43aff..255923c 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -8,24 +8,23 @@ impl Day for Day02 { type Input = String; type Output = usize; - fn get_test_data() -> Vec { - vec![ - "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green".to_string(), - "Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue".to_string(), - "Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red".to_string(), - "Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red".to_string(), - "Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green".to_string(), - ] + fn get_test_data() -> Self::Input { + "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green" + .to_string() } fn get_test_result() -> Self::Output { 8 } - fn run(data: Vec) -> Self::Output { + fn run(data: Self::Input) -> Self::Output { let mut result = 0; - for line in data { - let game = Game::from(line.clone()); + for line in data.lines().map(|x| x.trim().to_string()) { + let game = Game::from(line); println!("{}", &game); if game.is_possible() { result += game.id; @@ -35,9 +34,9 @@ impl Day for Day02 { } } impl DayPart2 for Day02 { - fn run_part2(data: Vec) -> Self::Output { + fn run_part2(data: Self::Input) -> Self::Output { let mut result = 0; - for line in data { + for line in data.lines().map(|x| x.trim().to_string()) { let game = Game::from(line.clone()); println!("{}", &game); result += game.get_game_power(); @@ -49,7 +48,7 @@ impl DayPart2 for Day02 { 2286 } - fn get_test_data_part2() -> Vec { + fn get_test_data_part2() -> Self::Input { Self::get_test_data() } } diff --git a/src/day03.rs b/src/day03.rs index f65a107..9b620a5 100644 --- a/src/day03.rs +++ b/src/day03.rs @@ -6,26 +6,26 @@ impl Day for Day03 { type Input = String; type Output = i32; - fn get_test_data() -> Vec { - vec![ - "467..114..".to_string(), - "...*......".to_string(), - "..35..633.".to_string(), - "......#...".to_string(), - "617*......".to_string(), - ".....+.58.".to_string(), - "..592.....".to_string(), - "......755.".to_string(), - "...$.*....".to_string(), - ".664.598..".to_string(), - ] + fn get_test_data() -> Self::Input { + +"467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..".to_string() } fn get_test_result() -> Self::Output { 4361 } - fn run(data: Vec) -> Self::Output { + fn run(data: Self::Input) -> Self::Output { + let data = data.lines().map(|x|x.to_string()).collect::>(); let mut result = 0; dbg!(&data); for y in 0..data.len() { diff --git a/src/day04.rs b/src/day04.rs index e4f938e..de0037b 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -12,25 +12,22 @@ impl Day for Day04 { type Input = String; type Output = usize; - fn get_test_data() -> Vec { + fn get_test_data() -> Self::Input { "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 -Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11" - .lines() - .map(|x| x.trim()) - .map(ToString::to_string) - .collect() +Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11".to_string() } fn get_test_result() -> Self::Output { 13 } - fn run(data: Vec) -> Self::Output { - data.iter() + fn run(data: Self::Input) -> Self::Output { + data.lines() + .map(|x| x.trim()) .map(|line| { let card = Card::from(line); // dbg!(&card); @@ -92,7 +89,12 @@ where } impl DayPart2 for Day04 { - fn run_part2(data: Vec) -> Self::Output { + fn run_part2(data: Self::Input) -> Self::Output { + let data = data + .lines() + .map(|x| x.trim()) + .map(ToString::to_string) + .collect::>(); let len = data.len(); let mut dict = Vec::with_capacity(len + 1); dict.push(0); @@ -110,23 +112,21 @@ impl DayPart2 for Day04 { dict.push(numbers); }); println!("{:?}", &dict.iter().enumerate().collect::>()); - get_for_range(1, dict.len()-1, &dict, 0) + get_for_range(1, dict.len() - 1, &dict, 0) } fn get_test_result_part2() -> Self::Output { 30 } - fn get_test_data_part2() -> Vec { + fn get_test_data_part2() -> Self::Input { "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11" - .lines() - .map(|x| x.trim().to_string()) - .collect() + .to_string() } } diff --git a/src/lib.rs b/src/lib.rs index 3eda17c..481f3f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -pub fn read_input(day: u8) -> Vec +pub fn read_input(day: u8) -> T where T: From, { @@ -6,7 +6,7 @@ where println!("Reading input from {}", filename); let input = std::fs::read_to_string(filename).unwrap(); utils::day!(); - input.lines().map(|s| s.to_string().into()).collect() + input.into() } mod day01; pub use day01::*; @@ -82,14 +82,14 @@ where type Input; type Output; - fn get_test_data() -> Vec; + fn get_test_data() -> Self::Input; fn get_test_result() -> Self::Output; - fn run(data: Vec) -> Self::Output; + fn run(data: Self::Input) -> Self::Output; } pub trait DayPart2: Day { - fn run_part2(data: Vec) -> Self::Output; + fn run_part2(data: Self::Input) -> Self::Output; fn get_test_result_part2() -> Self::Output; - fn get_test_data_part2() -> Vec; + fn get_test_data_part2() -> Self::Input; } pub trait DayConvenience: Day { fn run_day_test() {