refactor: many things
This commit is contained in:
parent
10bc68a71b
commit
1107d64d6b
@ -55,7 +55,7 @@ pub fn solve_01(content: &str) -> usize{
|
|||||||
content
|
content
|
||||||
.lines()
|
.lines()
|
||||||
.filter_map(|line| {
|
.filter_map(|line| {
|
||||||
let mut data =line
|
let data =line
|
||||||
.split_once(":").unwrap();
|
.split_once(":").unwrap();
|
||||||
|
|
||||||
let game_id = extract_game_id(data.0);
|
let game_id = extract_game_id(data.0);
|
||||||
|
36
src/day03.rs
36
src/day03.rs
@ -105,7 +105,6 @@ fn get_numbers_around(map: &Vec<Vec<char>>, row: usize, col: usize) -> Vec<Strin
|
|||||||
|
|
||||||
if data.len() > 0 {
|
if data.len() > 0 {
|
||||||
nums.push(data);
|
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 {
|
if data.len() > 0 {
|
||||||
nums.push(data);
|
nums.push(data);
|
||||||
data = String::new()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if map.len() - 1 > row {
|
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 {
|
if data.len() > 0 {
|
||||||
nums.push(data);
|
nums.push(data);
|
||||||
data = String::new()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nums;
|
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>) {
|
fn parse_to_parts(map: Vec<Vec<char>>) -> (Vec<String>, Vec<String>) {
|
||||||
let mut machine_part = vec![];
|
let mut machine_part = vec![];
|
||||||
let mut non_machine_part = vec![];
|
let mut non_machine_part = vec![];
|
||||||
|
82
src/day04.rs
82
src/day04.rs
@ -1,75 +1,44 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::hash::Hash;
|
|
||||||
|
|
||||||
pub fn execute_task01(content: &str) {
|
pub fn execute_task01(content: &str) {
|
||||||
let sum_of_numbers = solve_01(content).unwrap();
|
let sum_of_numbers = solve_01(content).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(25231, sum_of_numbers);
|
||||||
println!("Day04 - Task01 - Sum of correct Numbers: {}", sum_of_numbers)
|
println!("Day04 - Task01 - Sum of correct Numbers: {}", sum_of_numbers)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute_task02(content: &str) {
|
pub fn execute_task02(content: &str) {
|
||||||
let sum_of_numbers = solve_02(content).unwrap();
|
let sum_of_numbers = solve_02(content).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(9721255, sum_of_numbers);
|
||||||
println!("Day04 - Task02 - Sum of correct Numbers: {}", sum_of_numbers)
|
println!("Day04 - Task02 - Sum of correct Numbers: {}", sum_of_numbers)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_01(data: &str) -> anyhow::Result<usize> {
|
pub fn solve_01(data: &str) -> anyhow::Result<usize> {
|
||||||
Ok(data
|
Ok(data
|
||||||
.lines()
|
.lines()
|
||||||
.map(|data| data.split_once(":").unwrap().1)
|
.map(|data| parse_card(data))
|
||||||
.map(|data| data.split_once("|").unwrap())
|
|
||||||
.filter_map(|(winning, check)| {
|
.filter_map(|(winning, check)| {
|
||||||
let winning_num: Vec<usize> = winning
|
let correct = winning.iter().filter(|number| check.contains(number)).count();
|
||||||
.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();
|
|
||||||
|
|
||||||
if correct > 0 {
|
if correct > 0 {
|
||||||
return Some(correct);
|
return Some(correct);
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
.map(|num| 2_i32.pow((num - 1) as u32) as usize)
|
.map(|num| 2_usize.pow((num-1) as u32))
|
||||||
.sum())
|
.sum())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn solve_02(data: &str) -> anyhow::Result<usize> {
|
|
||||||
|
fn solve_02(data: &str) -> anyhow::Result<usize> {
|
||||||
let list: Vec<usize> = data
|
let list: Vec<usize> = data
|
||||||
.lines()
|
.lines()
|
||||||
.map(|data| data.split_once(":").unwrap().1)
|
.map(|data| parse_card(data))
|
||||||
.map(|data| data.split_once("|").unwrap())
|
.map(|(winning, check)| winning.iter().filter(|number| check.contains(number)).count())
|
||||||
.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();
|
|
||||||
|
|
||||||
winning_num.iter().filter(|number| check_num.contains(number)).count()
|
|
||||||
})
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
||||||
let mut data: HashMap<usize, usize> = HashMap::new();
|
let mut data: HashMap<usize, usize> = HashMap::new();
|
||||||
|
|
||||||
for (index, value) in list.iter().enumerate() {
|
for (index, value) in list.iter().enumerate() {
|
||||||
@ -77,24 +46,35 @@ pub fn solve_02(data: &str) -> anyhow::Result<usize> {
|
|||||||
data.insert(index, 1);
|
data.insert(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for _ in 0..data.get(&index).unwrap().clone() as i32 {
|
for i in 1..=value.clone() as i32 {
|
||||||
for i in 1..=value.clone() as i32 {
|
if list.len() <= (i as usize + index) {
|
||||||
if list.len() <= (i as usize + index) {
|
continue;
|
||||||
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())
|
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]
|
#[test]
|
||||||
pub fn test_solve_01() {
|
pub fn test_solve_01() {
|
||||||
let example = r#"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
let example = r#"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||||
|
Loading…
Reference in New Issue
Block a user