Skip to content

Commit

Permalink
add: day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Liu committed Dec 7, 2024
1 parent 25021eb commit 2de345b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ trim_trailing_whitespace = false

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
tab_width = 4
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
aoc_test:
cargo test --bin 07 -- --nocapture

solve:
cargo solve 6
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
105 changes: 105 additions & 0 deletions src/bin/07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
advent_of_code::solution!(7);

pub fn part_one(input: &str) -> Option<u64> {
let mut ans = 0;

let equations = parse_input(input);
for eq in equations {
if bt(eq.nums[0], &eq.answer, &eq.nums, 1) {
ans += eq.answer;
}
}

Some(ans)
}

pub fn part_two(input: &str) -> Option<u64> {
let mut ans = 0;

let equations = parse_input(input);
for eq in equations {
if bt2(eq.nums[0], &eq.answer, &eq.nums, 1) {
ans += eq.answer;
}
}

Some(ans)
}

#[derive(Debug)]
struct Equation {
answer: u64,
nums: Vec<u64>,
}

fn parse_input(input: &str) -> Vec<Equation> {
let mut equations = vec![];
for line in input.lines() {
let (answer, nums) = line.split_once(':').unwrap();
let eq = Equation {
answer: answer.parse::<u64>().unwrap(),
nums: nums
.split_whitespace()
.map(|num| num.parse::<u64>().unwrap())
.collect(),
};
equations.push(eq);
}
equations
}

fn bt(curr_val: u64, answer: &u64, nums: &Vec<u64>, i: usize) -> bool {
if curr_val > *answer {
return false;
}
if i == nums.len() {
return curr_val == *answer;
}

let next_val = nums[i];

bt(curr_val * next_val, answer, nums, i + 1) || bt(curr_val + next_val, answer, nums, i + 1)
}

fn bt2(curr_val: u64, answer: &u64, nums: &Vec<u64>, i: usize) -> bool {
if curr_val > *answer {
return false;
}
if i == nums.len() {
return curr_val == *answer;
}

let next_val = nums[i];

if let Some(concat_val) = concat_nums(curr_val, &next_val) {
if bt2(concat_val, answer, nums, i + 1) {
return true;
}
}

bt2(curr_val * next_val, answer, nums, i + 1) || bt2(curr_val + next_val, answer, nums, i + 1)
}

fn concat_nums(num1: u64, num2: &u64) -> Option<u64> {
let new_num = num1.to_string() + &num2.to_string();
let new_num = new_num.parse::<u64>().ok()?;

Some(new_num)
}

#[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 2de345b

Please sign in to comment.