Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix early exit from validation before all terms are used #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
maneatingape merged 1 commit into maneatingape:main from markjfisher:main
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix early exit from validation before all terms are used
  • Loading branch information
markjfisher committed Dec 7, 2024
commit 25ebadb2ed09fc2a44031439281a7594a7efb25c
15 changes: 11 additions & 4 deletions src/year2024/day07.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,17 @@ pub fn part2(input: &Input) -> u64 {
}

fn valid(terms: &[u64], test_value: u64, index: usize, concat: bool) -> bool {
(test_value == 0)
|| (concat
&& test_value % next_power_of_ten(terms[index]) == terms[index]
&& valid(terms, test_value / next_power_of_ten(terms[index]), index - 1, concat))
if test_value == 0 {
return index == 0;
}

if index == 0 {
return false;
}

(concat
&& test_value % next_power_of_ten(terms[index]) == terms[index]
&& valid(terms, test_value / next_power_of_ten(terms[index]), index - 1, concat))
|| (test_value % terms[index] == 0
&& valid(terms, test_value / terms[index], index - 1, concat))
|| (test_value >= terms[index]
Expand Down
9 changes: 9 additions & 0 deletions tests/year2024/day07_test.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ const EXAMPLE: &str = "\
21037: 9 7 18 13
292: 11 6 16 20";

const EXAMPLE2: &str = "\
190: 10 19
11174: 15 8 9 79 74
729: 6 6 7 37 650";

#[test]
fn part1_test() {
let input = parse(EXAMPLE);
assert_eq!(part1(&input), 3749);
let input2 = parse(EXAMPLE2);
assert_eq!(part1(&input2), 190);
}

#[test]
fn part2_test() {
let input = parse(EXAMPLE);
assert_eq!(part2(&input), 11387);
let input2 = parse(EXAMPLE2);
assert_eq!(part2(&input2), 11364);
}

AltStyle によって変換されたページ (->オリジナル) /