mirror of
https://github.com/OMGeeky/advent-of-code-2023.git
synced 2026-01-22 03:04:12 +01:00
change from Vec<T> to just T as input type
This commit is contained in:
40
src/day01.rs
40
src/day01.rs
@@ -8,22 +8,21 @@ impl Day for Day01 {
|
||||
|
||||
type Output = usize;
|
||||
|
||||
fn get_test_data() -> Vec<Self::Input> {
|
||||
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::Input>) -> 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<String> {
|
||||
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<String>) -> 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..];
|
||||
|
||||
27
src/day02.rs
27
src/day02.rs
@@ -8,24 +8,23 @@ impl Day for Day02 {
|
||||
type Input = String;
|
||||
type Output = usize;
|
||||
|
||||
fn get_test_data() -> Vec<Self::Input> {
|
||||
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::Input>) -> 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::Input>) -> 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<Self::Input> {
|
||||
fn get_test_data_part2() -> Self::Input {
|
||||
Self::get_test_data()
|
||||
}
|
||||
}
|
||||
|
||||
28
src/day03.rs
28
src/day03.rs
@@ -6,26 +6,26 @@ impl Day for Day03 {
|
||||
type Input = String;
|
||||
type Output = i32;
|
||||
|
||||
fn get_test_data() -> Vec<Self::Input> {
|
||||
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::Input>) -> Self::Output {
|
||||
fn run(data: Self::Input) -> Self::Output {
|
||||
let data = data.lines().map(|x|x.to_string()).collect::<Vec<_>>();
|
||||
let mut result = 0;
|
||||
dbg!(&data);
|
||||
for y in 0..data.len() {
|
||||
|
||||
28
src/day04.rs
28
src/day04.rs
@@ -12,25 +12,22 @@ impl Day for Day04 {
|
||||
type Input = String;
|
||||
type Output = usize;
|
||||
|
||||
fn get_test_data() -> Vec<Self::Input> {
|
||||
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::Input>) -> 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::Input>) -> Self::Output {
|
||||
fn run_part2(data: Self::Input) -> Self::Output {
|
||||
let data = data
|
||||
.lines()
|
||||
.map(|x| x.trim())
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<_>>();
|
||||
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::<Vec<(_, _)>>());
|
||||
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<Self::Input> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
src/lib.rs
12
src/lib.rs
@@ -1,4 +1,4 @@
|
||||
pub fn read_input<T>(day: u8) -> Vec<T>
|
||||
pub fn read_input<T>(day: u8) -> T
|
||||
where
|
||||
T: From<String>,
|
||||
{
|
||||
@@ -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<Self::Input>;
|
||||
fn get_test_data() -> Self::Input;
|
||||
fn get_test_result() -> Self::Output;
|
||||
fn run(data: Vec<Self::Input>) -> Self::Output;
|
||||
fn run(data: Self::Input) -> Self::Output;
|
||||
}
|
||||
pub trait DayPart2: Day {
|
||||
fn run_part2(data: Vec<Self::Input>) -> Self::Output;
|
||||
fn run_part2(data: Self::Input) -> Self::Output;
|
||||
fn get_test_result_part2() -> Self::Output;
|
||||
fn get_test_data_part2() -> Vec<Self::Input>;
|
||||
fn get_test_data_part2() -> Self::Input;
|
||||
}
|
||||
pub trait DayConvenience: Day {
|
||||
fn run_day_test() {
|
||||
|
||||
Reference in New Issue
Block a user