Skip to content

Commit

Permalink
Merge pull request #13 from ephemient/rs/day2
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 2, 2024
2 parents e6049a5 + 1a7fcc7 commit 84a58e2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Development occurs in language-specific directories:
|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg)|[Python](py) ![Python CI](https://github.com/ephemient/aoc2024/workflows/Python%20CI/badge.svg)|[Rust](rs) ![Rust CI](https://github.com/ephemient/aoc2024/workflows/Rust%20CI/badge.svg)|
|--:|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)||
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)|[day2.rs](rs/src/day2.rs)|
8 changes: 7 additions & 1 deletion rs/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2024::day1;
use aoc2024::{day1, day2};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand All @@ -20,6 +20,12 @@ fn aoc2024_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day1::part2(black_box(&data))));
g.finish();

let data = get_day_input(2)?;
let mut g = c.benchmark_group("day 2");
g.bench_function("part 1", |b| b.iter(|| day2::part1(black_box(&data))));
g.bench_function("part 2", |b| b.iter(|| day2::part2(black_box(&data))));
g.finish();

Ok(())
}

Expand Down
81 changes: 81 additions & 0 deletions rs/src/day2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
fn parse(data: &str) -> Vec<Vec<i32>> {
data.lines()
.flat_map(|line| {
line.split_ascii_whitespace()
.map(|level| level.parse::<i32>().ok())
.collect::<Option<Vec<_>>>()
})
.collect::<Vec<_>>()
}

fn is_safe_1(report: &[i32]) -> bool {
let mut decreasing = false;
let mut increasing = false;
report
.iter()
.zip(report.iter().skip(1))
.all(|(x, y)| match x - y {
-3..=-1 => {
decreasing = true;
!increasing
}
1..=3 => {
increasing = true;
!decreasing
}
_ => false,
})
}

fn is_safe_2(report: &[i32]) -> bool {
if report.is_empty() {
return true;
}
let mut report2 = vec![0; report.len() - 1];
report2.copy_from_slice(&report[1..]);
is_safe_1(&report2)
|| (0..report2.len()).any(|i| {
report2[i] = report[i];
is_safe_1(&report2)
})
}

pub fn part1(data: &str) -> usize {
parse(data)
.into_iter()
.filter(|report| is_safe_1(report))
.count()
}

pub fn part2(data: &str) -> usize {
parse(data)
.into_iter()
.filter(|report| is_safe_2(report))
.count()
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

static EXAMPLE: &str = indoc! {"
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
"};

#[test]
fn part1_examples() {
assert_eq!(2, part1(EXAMPLE));
}

#[test]
fn part2_examples() {
assert_eq!(4, part2(EXAMPLE));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod day1;
pub mod day2;
10 changes: 9 additions & 1 deletion rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2024::day1;
use aoc2024::{day1, day2};
use std::collections::HashSet;
use std::env;
use std::fs;
Expand All @@ -24,5 +24,13 @@ fn main() -> io::Result<()> {
println!();
}

if args.is_empty() || args.contains("2") {
println!("Day 2");
let data = get_day_input(2)?;
println!("{:?}", day2::part1(&data));
println!("{:?}", day2::part2(&data));
println!();
}

Ok(())
}

0 comments on commit 84a58e2

Please sign in to comment.