mirror of
https://github.com/Rhelvetican/aoc2024.git
synced 2024-12-23 06:51:40 +00:00
day 2
This commit is contained in:
parent
f20f8c9018
commit
be9ade3ca0
4 changed files with 135 additions and 55 deletions
|
@ -1,5 +1 @@
|
|||
use aoc2024::solutions::day_1::part_two;
|
||||
|
||||
fn main() {
|
||||
println!("{}", part_two());
|
||||
}
|
||||
fn main() {}
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
const INPUT: &str = include_str!("../../input/day1.txt");
|
||||
use super::AocSolution;
|
||||
|
||||
pub fn part_one() -> u64 {
|
||||
let (mut left, mut right) = INPUT
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct AocDayOneSolution;
|
||||
|
||||
impl AocSolution for AocDayOneSolution {
|
||||
type Output = u64;
|
||||
const INPUT: &str = include_str!("../../input/day1.txt");
|
||||
|
||||
fn part_one(&self) -> Self::Output {
|
||||
let (mut left, mut right) = Self::INPUT
|
||||
.lines()
|
||||
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
||||
.filter_map(
|
||||
|
@ -25,13 +32,12 @@ pub fn part_one() -> u64 {
|
|||
.zip(right)
|
||||
.map(|(l, r)| if l > r { l - r } else { r - l })
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn part_two() -> u64 {
|
||||
}
|
||||
fn part_two(&self) -> Self::Output {
|
||||
let mut cache = HashMap::<u64, u64>::new();
|
||||
let mut similarity_score = 0;
|
||||
|
||||
let (left, right) = INPUT
|
||||
let (left, right) = Self::INPUT
|
||||
.lines()
|
||||
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
||||
.filter_map(
|
||||
|
@ -57,4 +63,5 @@ pub fn part_two() -> u64 {
|
|||
}
|
||||
|
||||
similarity_score
|
||||
}
|
||||
}
|
||||
|
|
68
src/solutions/day_2.rs
Normal file
68
src/solutions/day_2.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use super::AocSolution;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct AocDayTwoSolution;
|
||||
|
||||
struct LevelValidator;
|
||||
|
||||
impl LevelValidator {
|
||||
fn is_safe(&self, levels: &[i32]) -> bool {
|
||||
let dec = levels[0] > levels[1];
|
||||
|
||||
for (cur, nxt) in levels.windows(2).map(|w| (w[0], w[1])) {
|
||||
let diff = cur - nxt;
|
||||
|
||||
if diff == 0 || diff.abs() > 3 || (diff > 0) != dec {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn is_safe_with_rem(&self, levels: &[i32]) -> bool {
|
||||
for i in 0..levels.len() {
|
||||
let mut lvl = levels[0..i].to_vec();
|
||||
lvl.extend_from_slice(&levels[(i + 1)..]);
|
||||
|
||||
if self.is_safe(&lvl) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl AocSolution for AocDayTwoSolution {
|
||||
type Output = usize;
|
||||
const INPUT: &str = include_str!("../../input/day2.txt");
|
||||
|
||||
fn part_one(&self) -> Self::Output {
|
||||
let validator = LevelValidator;
|
||||
|
||||
Self::INPUT
|
||||
.lines()
|
||||
.map(|n| {
|
||||
n.split_whitespace()
|
||||
.filter_map(|x| x.parse::<i32>().ok())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.filter(|s| validator.is_safe(s))
|
||||
.count()
|
||||
}
|
||||
|
||||
fn part_two(&self) -> Self::Output {
|
||||
let validator = LevelValidator;
|
||||
|
||||
Self::INPUT
|
||||
.lines()
|
||||
.map(|n| {
|
||||
n.split_whitespace()
|
||||
.filter_map(|x| x.parse::<i32>().ok())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.filter(|s| validator.is_safe(s) || validator.is_safe_with_rem(s))
|
||||
.count()
|
||||
}
|
||||
}
|
|
@ -1 +1,10 @@
|
|||
pub mod day_1;
|
||||
pub mod day_2;
|
||||
|
||||
pub trait AocSolution {
|
||||
type Output;
|
||||
const INPUT: &str;
|
||||
|
||||
fn part_one(&self) -> Self::Output;
|
||||
fn part_two(&self) -> Self::Output;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue