Skip to content

Commit

Permalink
AoC 2024 Day 7 - rust
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 7, 2024
1 parent 36ed448 commit 6ec5119
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| python3 | [](src/main/python/AoC2024_01.py) | [](src/main/python/AoC2024_02.py) | [](src/main/python/AoC2024_03.py) | [](src/main/python/AoC2024_04.py) | [](src/main/python/AoC2024_05.py) | [](src/main/python/AoC2024_06.py) | [](src/main/python/AoC2024_07.py) | | | | | | | | | | | | | | | | | | |
| java | [](src/main/java/AoC2024_01.java) | [](src/main/java/AoC2024_02.java) | [](src/main/java/AoC2024_03.java) | [](src/main/java/AoC2024_04.java) | [](src/main/java/AoC2024_05.java) | [](src/main/java/AoC2024_06.java) | [](src/main/java/AoC2024_07.java) | | | | | | | | | | | | | | | | | | |
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | | | | | | | | | | | | | | | | | | | |
| rust | [](src/main/rust/AoC2024_01/src/main.rs) | [](src/main/rust/AoC2024_02/src/main.rs) | [](src/main/rust/AoC2024_03/src/main.rs) | [](src/main/rust/AoC2024_04/src/main.rs) | [](src/main/rust/AoC2024_05/src/main.rs) | [](src/main/rust/AoC2024_06/src/main.rs) | [](src/main/rust/AoC2024_07/src/main.rs) | | | | | | | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

## 2023
Expand Down
7 changes: 7 additions & 0 deletions src/main/rust/AoC2024_07/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "AoC2024_07"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../aoc" }
128 changes: 128 additions & 0 deletions src/main/rust/AoC2024_07/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#![allow(non_snake_case)]

use aoc::Puzzle;
use std::collections::HashSet;

#[derive(Eq, Hash, PartialEq)]
enum Op {
Add,
Multiply,
Concatenate,
}

struct AoC2024_07;

impl AoC2024_07 {
fn solve(
&self,
input: &<AoC2024_07 as aoc::Puzzle>::Input,
ops: &HashSet<Op>,
) -> u64 {
fn can_obtain(sol: u64, terms: &[u64], ops: &HashSet<Op>) -> bool {
if terms.len() == 1 {
return sol == terms[0];
}
let last = terms.last().unwrap();
let prev_terms = &terms[..terms.len() - 1];
if ops.contains(&Op::Multiply)
&& sol % last == 0
&& can_obtain(sol.div_euclid(*last), prev_terms, ops)
{
return true;
}
if ops.contains(&Op::Add)
&& sol > *last
&& can_obtain(sol - *last, prev_terms, ops)
{
return true;
}
if ops.contains(&Op::Concatenate) {
let (s_sol, s_last) = (sol.to_string(), last.to_string());
if s_sol.len() > s_last.len() && s_sol.ends_with(&s_last) {
let new_sol = &s_sol[..s_sol.len() - s_last.len()];
if can_obtain(
new_sol.parse::<u64>().unwrap(),
prev_terms,
ops,
) {
return true;
}
}
}
false
}

input
.iter()
.filter(|(sol, terms)| can_obtain(*sol, terms, ops))
.map(|(sol, _)| sol)
.sum()
}
}

impl aoc::Puzzle for AoC2024_07 {
type Input = Vec<(u64, Vec<u64>)>;
type Output1 = u64;
type Output2 = u64;

aoc::puzzle_year_day!(2024, 7);

fn parse_input(&self, lines: Vec<String>) -> Self::Input {
lines
.iter()
.map(|line| {
let (left, right) = line.split_once(": ").unwrap();
let sol = left.parse::<u64>().unwrap();
let terms = right
.split_whitespace()
.map(|s| s.parse::<u64>().unwrap())
.collect();
(sol, terms)
})
.collect()
}

fn part_1(&self, input: &Self::Input) -> Self::Output1 {
self.solve(input, &HashSet::from([Op::Add, Op::Multiply]))
}

fn part_2(&self, input: &Self::Input) -> Self::Output2 {
self.solve(
input,
&HashSet::from([Op::Add, Op::Multiply, Op::Concatenate]),
)
}

fn samples(&self) {
aoc::puzzle_samples! {
self, part_1, TEST, 3749,
self, part_2, TEST, 11387
};
}
}

fn main() {
AoC2024_07 {}.run(std::env::args());
}

const TEST: &str = "\
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
";

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

#[test]
pub fn samples() {
AoC2024_07 {}.samples();
}
}
7 changes: 7 additions & 0 deletions src/main/rust/Cargo.lock

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

0 comments on commit 6ec5119

Please sign in to comment.