This commit is contained in:
Spectre 2024-12-03 00:56:35 +01:00
parent b2fc33499f
commit 6b868037d1
3 changed files with 91 additions and 12 deletions

View file

@ -4,10 +4,41 @@ pub struct Day01;
impl Solution for Day01 {
fn part_1(&self, input: &str) -> Answer {
Answer::Unimplemented
let (mut first, mut second) = parse(input);
first.sort();
second.sort();
let res: u32 = first.iter().zip(second)
.map(|(a, b)| a.abs_diff(b))
.sum();
Answer::Number(res as u64)
}
fn part_2(&self, input: &str) -> Answer {
Answer::Unimplemented
let (mut first, mut second) = parse(input);
first.sort();
second.sort();
let res: u32 = first.iter()
.map(|a|
a * second.iter()
.filter(|&b| b==a)
.count() as u32
)
.sum();
Answer::Number(res as u64)
}
}
fn parse(input: &str) -> (Vec<u32>, Vec<u32>) {
input.lines()
.map(|line| line.split_once(" ")
.unwrap()
)
.map(|(x, y)| (x.parse::<u32>().unwrap(), y.parse::<u32>().unwrap()))
.unzip()
}

View file

@ -4,10 +4,46 @@ pub struct Day02;
impl Solution for Day02 {
fn part_1(&self, input: &str) -> Answer {
Answer::Unimplemented
let data = parse(input);
let res = data.iter()
.filter(line_is_safe)
.count();
Answer::Number(res as u64)
}
fn part_2(&self, input: &str) -> Answer {
Answer::Unimplemented
let data = parse(input);
let res = data.iter()
.filter(|line|
(0..line.len()).any(|i| {
let mut new_vec = vec![];
for j in 0..line.len() {
if i != j {
new_vec.push(line[j])
}
}
line_is_safe(&&new_vec)
}
)
)
.count();
Answer::Number(res as u64)
}
}
fn parse(input: &str) -> Vec<Vec<u32>> {
input.lines().map(|l|
l.split_whitespace().map(|x| x.parse().unwrap()).collect()
).collect()
}
fn line_is_safe(line: &&Vec<u32>) -> bool {
(line.is_sorted() || line.iter().rev().is_sorted())
&& line.windows(2)
.all(|w| (1..=3).contains(&w[0].abs_diff(w[1]))
)
}