Skip to content

Commit

Permalink
feat: day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefKuchar committed Dec 7, 2024
1 parent 781f289 commit df5230e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ itertools = "0.13.0"
pico-args = "0.5.0"
rayon = "1.10.0"
regex = "1.11.1"
strum = { version = "0.26.3", features = ["derive"] }
tinyjson = "2.5.1"

# Solution dependencies
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `52.5µs` | `83.5µs` |
| [Day 2](./src/bin/02.rs) | `179.5µs` | `390.3µs` |
| [Day 3](./src/bin/03.rs) | `358.1µs` | `335.4µs` |
| [Day 4](./src/bin/04.rs) | `5.8ms` | `110.4µs` |
| [Day 5](./src/bin/05.rs) | `1.5ms` | `36.2ms` |
| [Day 6](./src/bin/06.rs) | `632.3µs` | `184.0ms` |

**Total: 229.64ms**
| [Day 7](./src/bin/07.rs) | `4.8ms` | `254.2ms` |

**Total: 259.00ms**
<!--- benchmarking table --->

---
Expand Down
9 changes: 9 additions & 0 deletions data/examples/07.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
79 changes: 79 additions & 0 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
advent_of_code::solution!(7);

use itertools::Itertools;
use rayon::prelude::*;
use strum::{EnumIter, IntoEnumIterator};

#[derive(Debug, EnumIter, Clone)]
enum Operation {
Concat,
Add,
Mul,
}

fn solve(input: &str, skip: usize) -> Option<u64> {
Some(
input
.par_lines()
.map(|line| {
let mut parts = line.split(": ");
let result = parts.next().unwrap().parse::<u64>().unwrap();
let numbers = parts
.next()
.unwrap()
.split_whitespace()
.map(|n| n.parse::<u64>().unwrap())
.collect::<Vec<u64>>();
(result, numbers)
})
.filter(|(result, numbers)| {
(0..numbers.len() - 1)
.map(|_| Operation::iter().skip(skip))
.multi_cartesian_product()
.any(|c| {
let mut c = c.into_iter();
numbers.iter().map(|x| *x).reduce(|acc, n| {
let op = c.next().unwrap();
match op {
Operation::Add => acc + n,
Operation::Mul => acc * n,
Operation::Concat => {
let mut spacer = 1;
while n / spacer > 0 {
spacer *= 10;
}
acc * spacer + n
}
}
}) == Some(*result)
})
})
.map(|(result, _)| result)
.sum::<u64>(),
)
}

pub fn part_one(input: &str) -> Option<u64> {
solve(input, 1)
}

pub fn part_two(input: &str) -> Option<u64> {
solve(input, 0)
}

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

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(3749));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(11387));
}
}

0 comments on commit df5230e

Please sign in to comment.