Skip to content

Commit

Permalink
Solved 07!
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekuskus committed Dec 7, 2024
1 parent 6e7d9a7 commit c25716a
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 2 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
debug_print = "1.0.0"
itertools = "0.13.0"
regex = "1.11.1"

[lib]
Expand Down Expand Up @@ -36,3 +37,7 @@ path = "src/05.rs"
[[bin]]
name = "06"
path = "src/06.rs"

[[bin]]
name = "07"
path = "src/07.rs"
2 changes: 1 addition & 1 deletion inputs
153 changes: 153 additions & 0 deletions src/07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use itertools::Itertools;
use setup_utils::*;
use std::path::Path;

// Symbols to replace: 07 3749 11387 1399219271639 275791737999003

#[cfg(test)]
mod tests {
use setup_utils::read_lines;
use std::path::Path;

#[test]
fn part1() -> Result<(), String> {
let lines = read_lines(Path::new("./inputs/07-example.txt"));
let result = crate::part1(&lines);
if result == 3749 {
Ok(())
} else {
Err(format!(
"07: Bad result for Part 1 example, expected 3749 got {}",
result
))
}
}

#[test]
fn part2() -> Result<(), String> {
let lines = read_lines(Path::new("./inputs/07-example.txt"));
let result = crate::part2(&lines);
if result == 11387 {
Ok(())
} else {
Err(format!(
"07: Bad result for Part 2 example, expected 11387 got {}",
result
))
}
}

#[test]
fn full() -> Result<(), String> {
let lines = read_lines(Path::new("./inputs/07-full.txt"));
let result1 = crate::part1(&lines);
let result2 = crate::part2(&lines);

match (result1, result2) {
(1399219271639, 275791737999003) => Ok(()),
(_, 275791737999003) => Err(format!(
"07: Bad result for Part 1, expected 1399219271639 got {}",
result1
)),
(1399219271639, _) => Err(format!(
"07: Bad result for Part 2, expected 275791737999003 got {}",
result2
)),
(_, _) => Err(format!(
"07: Bad result for Part 1 & 2, expected (1399219271639, 275791737999003) got ({}, {})",
result1, result2
)),
}
}
}

fn main() {
let linesfull = read_lines(Path::new("./inputs/07-full.txt"));
let lines1 = read_lines(Path::new("./inputs/07-example.txt"));

println!("07-full.txt");
println!("{}", part1(&linesfull));
println!("{}\n", part2(&linesfull));

println!("07-1-example.txt");
println!("{}", part1(&lines1));
println!("{}", part2(&lines1));
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Ops {
Add,
Multiply,
Concat,
}

fn process_ops(ops: Vec<&Ops>, operands: &Vec<i64>) -> i64 {
operands[1..]
.iter()
.enumerate()
.fold(operands[0], |acc, (idx, &num)| match ops[idx] {
Ops::Add => acc + num,
Ops::Multiply => acc * num,
Ops::Concat => {
let mut lhs = acc.to_string();
lhs.push_str(num.to_string().as_str());
lhs.parse::<i64>().unwrap()
}
})
}

fn part1(lines: &Vec<String>) -> i64 {
let operations = [Ops::Add, Ops::Multiply];

lines
.iter()
.filter_map(|l| {
let mut spl = l.split(":");
let lhs = spl.next().unwrap().parse::<i64>().unwrap();
let operands = spl
.next()
.unwrap()
.trim()
.split_ascii_whitespace()
.map(|s| s.parse::<i64>().unwrap())
.collect_vec();

if itertools::repeat_n(operations.iter(), operands.len() - 1) // permutation with replacements
.multi_cartesian_product()
.any(|ops| process_ops(ops, &operands) == lhs)
{
Some(lhs)
} else {
None
}
})
.sum()
}

fn part2(lines: &Vec<String>) -> i64 {
let operations = [Ops::Add, Ops::Multiply, Ops::Concat];

lines
.iter()
.filter_map(|l| {
let mut spl = l.split(":");
let lhs = spl.next().unwrap().parse::<i64>().unwrap();
let operands = spl
.next()
.unwrap()
.trim()
.split_ascii_whitespace()
.map(|s| s.parse::<i64>().unwrap())
.collect_vec();

if itertools::repeat_n(operations.iter(), operands.len() - 1) // permutation with replacements
.multi_cartesian_product()
.any(|ops| process_ops(ops, &operands) == lhs)
{
Some(lhs)
} else {
None
}
})
.sum()
}

0 comments on commit c25716a

Please sign in to comment.