Skip to content

Commit

Permalink
Merge pull request #61 from ephemient/rs/day7
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 7, 2024
2 parents bd72807 + 8d42f5a commit b402d45
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Development occurs in language-specific directories:
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)|[day4.rs](rs/src/day4.rs)|
|[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt)|[day5.py](py/aoc2024/day5.py)|[day5.rs](rs/src/day5.rs)|
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day6.kt)|[day6.py](py/aoc2024/day6.py)|[day6.rs](rs/src/day6.rs)|
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|[day7.py](py/aoc2024/day7.py)||
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)|[day7.py](py/aoc2024/day7.py)|[day7.rs](rs/src/day7.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, day2, day3, day4, day5, day6};
use aoc2024::{day1, day2, day3, day4, day5, day6, day7};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand Down Expand Up @@ -50,6 +50,12 @@ fn aoc2024_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day6::part2(black_box(&data))));
g.finish();

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

Ok(())
}

Expand Down
95 changes: 95 additions & 0 deletions rs/src/day7.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
fn solve<F, I>(data: &str, op: F) -> usize
where
F: Fn(usize, usize) -> I,
I: IntoIterator<Item = usize>,
{
data.lines()
.filter_map(|line| {
let (lhs, rhs) = line.split_once(": ")?;
let lhs = lhs.parse::<usize>().ok()?;
let rhs = rhs
.split_ascii_whitespace()
.map(|value| value.parse())
.collect::<Result<Vec<_>, _>>()
.ok()?;
let mut stack = vec![(lhs, rhs)];
while let Some((x, mut rest)) = stack.pop() {
let y = rest.pop()?;
if rest.is_empty() {
if x == y {
return Some(lhs);
}
continue;
}
for z in op(x, y) {
stack.push((z, rest.clone()));
}
}
None
})
.sum()
}

pub fn part1(data: &str) -> usize {
solve(data, |x, y| {
let mut values = vec![];
if let Some(z) = x.checked_sub(y) {
values.push(z);
}
if x % y == 0 {
values.push(x / y);
}
values
})
}

pub fn part2(data: &str) -> usize {
solve(data, |x, y| {
let mut values = vec![];
if let Some(z) = x.checked_sub(y) {
values.push(z);
}
if x % y == 0 {
values.push(x / y);
}
if x > y {
let mut d = 10;
while d <= y {
d *= 10;
}
if x % d == y {
values.push(x / d);
}
}
values
})
}

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

static EXAMPLE: &str = indoc! {"
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
"};

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

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

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

Ok(())
}

0 comments on commit b402d45

Please sign in to comment.