refactor: many things

This commit is contained in:
Lennard Brinkhaus 2023-12-04 14:26:41 +01:00
parent 10bc68a71b
commit 1107d64d6b
3 changed files with 32 additions and 88 deletions

View File

@ -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);

View File

@ -105,7 +105,6 @@ fn get_numbers_around(map: &Vec<Vec<char>>, row: usize, col: usize) -> Vec<Strin
if data.len() > 0 {
nums.push(data);
data = String::new()
}
}
@ -159,7 +158,6 @@ fn get_numbers_around(map: &Vec<Vec<char>>, row: usize, col: usize) -> Vec<Strin
if data.len() > 0 {
nums.push(data);
data = String::new()
}
if map.len() - 1 > row {
@ -218,46 +216,12 @@ fn get_numbers_around(map: &Vec<Vec<char>>, row: usize, col: usize) -> Vec<Strin
if data.len() > 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<char>>) -> (Vec<String>, Vec<String>) {
let mut machine_part = vec![];
let mut non_machine_part = vec![];

View File

@ -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<usize> {
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<usize> = winning
.trim()
.split(" ")
.filter(|value| value.len() > 0)
.map(|value| value.parse::<usize>().unwrap())
.collect();
let check_num: Vec<usize> = check
.trim()
.split(" ")
.filter(|value| value.len() > 0)
.map(|value| value.parse::<usize>().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<usize> {
fn solve_02(data: &str) -> anyhow::Result<usize> {
let list: Vec<usize> = data
.lines()
.map(|data| data.split_once(":").unwrap().1)
.map(|data| data.split_once("|").unwrap())
.map(|(winning, check)| {
let winning_num: Vec<usize> = winning
.trim()
.split(" ")
.filter(|value| value.len() > 0)
.map(|value| value.parse::<usize>().unwrap())
.map(|data| parse_card(data))
.map(|(winning, check)| winning.iter().filter(|number| check.contains(number)).count())
.collect();
let check_num: Vec<usize> = check
.trim()
.split(" ")
.filter(|value| value.len() > 0)
.map(|value| value.parse::<usize>().unwrap())
.collect();
winning_num.iter().filter(|number| check_num.contains(number)).count()
})
.collect();
let mut data: HashMap<usize, usize> = HashMap::new();
for (index, value) in list.iter().enumerate() {
@ -77,24 +46,35 @@ pub fn solve_02(data: &str) -> anyhow::Result<usize> {
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);
}
*data.entry(index + i as usize).or_insert(1) += 1 * data[&index].clone();
}
}
Ok(data.values().sum())
}
fn parse_card(line: &str) -> (Vec<usize>, Vec<usize>) {
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<usize> {
line
.trim()
.split(" ")
.filter(|value| value.len() > 0)
.map(|value| value.parse::<usize>().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