From 1107d64d6ba8eeffbee34a8b19ac014cad9cab0d Mon Sep 17 00:00:00 2001 From: Lennard Brinkhaus Date: Mon, 4 Dec 2023 14:26:41 +0100 Subject: [PATCH] refactor: many things --- src/day02.rs | 2 +- src/day03.rs | 36 ----------------------- src/day04.rs | 82 ++++++++++++++++++++-------------------------------- 3 files changed, 32 insertions(+), 88 deletions(-) diff --git a/src/day02.rs b/src/day02.rs index 10e4e06..754b3c4 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -55,7 +55,7 @@ pub fn solve_01(content: &str) -> usize{ content .lines() .filter_map(|line| { - let mut data =line + let data =line .split_once(":").unwrap(); let game_id = extract_game_id(data.0); diff --git a/src/day03.rs b/src/day03.rs index 7b7290a..ea98c80 100644 --- a/src/day03.rs +++ b/src/day03.rs @@ -105,7 +105,6 @@ fn get_numbers_around(map: &Vec>, row: usize, col: usize) -> Vec 0 { nums.push(data); - data = String::new() } } @@ -159,7 +158,6 @@ fn get_numbers_around(map: &Vec>, row: usize, col: usize) -> Vec 0 { nums.push(data); - data = String::new() } if map.len() - 1 > row { @@ -218,46 +216,12 @@ fn get_numbers_around(map: &Vec>, row: usize, col: usize) -> Vec 0 { nums.push(data); - data = String::new() } } return nums; } -fn get_cords_around(row: usize, col: usize, max_row: usize, max_col: usize) -> Vec<(usize, usize)> { - let mut cords = vec![]; - if row > 0 { - if col > 0 { - cords.push((row - 1, col - 1)); - } - cords.push((row - 1, col)); - if max_col - 1 > col { - cords.push((row - 1, col + 1)); - } - } - - if col > 0 { - cords.push((row, col - 1)); - } - - if max_col - 1 > col { - cords.push((row, col + 1)); - } - - if max_row - 1 > row { - if col > 0 { - cords.push((row + 1, col - 1)); - } - cords.push((row + 1, col)); - if max_col - 1 > col { - cords.push((row + 1, col + 1)); - } - } - - return cords; -} - fn parse_to_parts(map: Vec>) -> (Vec, Vec) { let mut machine_part = vec![]; let mut non_machine_part = vec![]; diff --git a/src/day04.rs b/src/day04.rs index 2ea6881..5ef4e64 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -1,75 +1,44 @@ use std::collections::HashMap; -use std::hash::Hash; pub fn execute_task01(content: &str) { let sum_of_numbers = solve_01(content).unwrap(); + assert_eq!(25231, sum_of_numbers); println!("Day04 - Task01 - Sum of correct Numbers: {}", sum_of_numbers) } pub fn execute_task02(content: &str) { let sum_of_numbers = solve_02(content).unwrap(); + assert_eq!(9721255, sum_of_numbers); println!("Day04 - Task02 - Sum of correct Numbers: {}", sum_of_numbers) } pub fn solve_01(data: &str) -> anyhow::Result { Ok(data .lines() - .map(|data| data.split_once(":").unwrap().1) - .map(|data| data.split_once("|").unwrap()) + .map(|data| parse_card(data)) .filter_map(|(winning, check)| { - let winning_num: Vec = winning - .trim() - .split(" ") - .filter(|value| value.len() > 0) - .map(|value| value.parse::().unwrap()) - .collect(); - - let check_num: Vec = check - .trim() - .split(" ") - .filter(|value| value.len() > 0) - .map(|value| value.parse::().unwrap()) - .collect(); - - let correct = winning_num.iter().filter(|number| check_num.contains(number)).count(); + let correct = winning.iter().filter(|number| check.contains(number)).count(); if correct > 0 { return Some(correct); } None }) - .map(|num| 2_i32.pow((num - 1) as u32) as usize) + .map(|num| 2_usize.pow((num-1) as u32)) .sum()) } -pub fn solve_02(data: &str) -> anyhow::Result { + +fn solve_02(data: &str) -> anyhow::Result { let list: Vec = data .lines() - .map(|data| data.split_once(":").unwrap().1) - .map(|data| data.split_once("|").unwrap()) - .map(|(winning, check)| { - let winning_num: Vec = winning - .trim() - .split(" ") - .filter(|value| value.len() > 0) - .map(|value| value.parse::().unwrap()) - .collect(); - - let check_num: Vec = check - .trim() - .split(" ") - .filter(|value| value.len() > 0) - .map(|value| value.parse::().unwrap()) - .collect(); - - winning_num.iter().filter(|number| check_num.contains(number)).count() - }) + .map(|data| parse_card(data)) + .map(|(winning, check)| winning.iter().filter(|number| check.contains(number)).count()) .collect(); - let mut data: HashMap = HashMap::new(); for (index, value) in list.iter().enumerate() { @@ -77,24 +46,35 @@ pub fn solve_02(data: &str) -> anyhow::Result { data.insert(index, 1); } - for _ in 0..data.get(&index).unwrap().clone() as i32 { - for i in 1..=value.clone() as i32 { - if list.len() <= (i as usize + index) { - continue; - } - - if !data.contains_key(&(index + i as usize)) { - data.insert(index + i as usize, 1); - } - - data.insert(index + i as usize, data.get(&(index + i as usize)).unwrap() + 1); + for i in 1..=value.clone() as i32 { + if list.len() <= (i as usize + index) { + continue; } + + *data.entry(index + i as usize).or_insert(1) += 1 * data[&index].clone(); } } Ok(data.values().sum()) } +fn parse_card(line: &str) -> (Vec, Vec) { + line + .split_once(":").unwrap().1 + .split_once("|") + .map(|(winning, check)| (parse_number_line(winning), parse_number_line(check))) + .unwrap() +} + +fn parse_number_line(line: &str) -> Vec { + line + .trim() + .split(" ") + .filter(|value| value.len() > 0) + .map(|value| value.parse::().unwrap()) + .collect() +} + #[test] pub fn test_solve_01() { let example = r#"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53