mirror of
https://github.com/Rhelvetican/aoc2024.git
synced 2025-01-11 04:31:42 +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() {}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
println!("{}", part_two());
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../../input/day1.txt");
|
use super::AocSolution;
|
||||||
|
|
||||||
pub fn part_one() -> u64 {
|
#[derive(Clone, Copy)]
|
||||||
let (mut left, mut right) = INPUT
|
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()
|
.lines()
|
||||||
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
||||||
.filter_map(
|
.filter_map(
|
||||||
|
@ -25,13 +32,12 @@ pub fn part_one() -> u64 {
|
||||||
.zip(right)
|
.zip(right)
|
||||||
.map(|(l, r)| if l > r { l - r } else { r - l })
|
.map(|(l, r)| if l > r { l - r } else { r - l })
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
fn part_two(&self) -> Self::Output {
|
||||||
pub fn part_two() -> u64 {
|
|
||||||
let mut cache = HashMap::<u64, u64>::new();
|
let mut cache = HashMap::<u64, u64>::new();
|
||||||
let mut similarity_score = 0;
|
let mut similarity_score = 0;
|
||||||
|
|
||||||
let (left, right) = INPUT
|
let (left, right) = Self::INPUT
|
||||||
.lines()
|
.lines()
|
||||||
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
.filter_map(|s| s.split_once(' ').map(|(s, x)| (s.trim(), x.trim())))
|
||||||
.filter_map(
|
.filter_map(
|
||||||
|
@ -57,4 +63,5 @@ pub fn part_two() -> u64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
similarity_score
|
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_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