Skip to content

Commit 6bbc9dc

Browse files
committed
Year 2024 Day 7
1 parent 61c7596 commit 6bbc9dc

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7878
| 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [Source](src/year2024/day04.rs) | 77 |
7979
| 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [Source](src/year2024/day05.rs) | 18 |
8080
| 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [Source](src/year2024/day06.rs) | 386 |
81+
| 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [Source](src/year2024/day07.rs) | 220 |
8182

8283
## 2023
8384

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,5 @@ mod year2024 {
298298
benchmark!(year2024, day04);
299299
benchmark!(year2024, day05);
300300
benchmark!(year2024, day06);
301+
benchmark!(year2024, day07);
301302
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,5 @@ pub mod year2024 {
297297
pub mod day04;
298298
pub mod day05;
299299
pub mod day06;
300+
pub mod day07;
300301
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,6 @@ fn year2024() -> Vec<Solution> {
367367
solution!(year2024, day04),
368368
solution!(year2024, day05),
369369
solution!(year2024, day06),
370+
solution!(year2024, day07),
370371
]
371372
}

src/year2024/day07.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! # Bridge Repair
2+
//!
3+
//! Concenation in part two can be implemented without time consuming conversion to or from strings.
4+
//! Instead multiply the left term by the next power of ten greater than the right term, then add
5+
//! the right term. For example:
6+
//!
7+
//! * 15 || 6 = 15 * 10 + 6
8+
//! * 17 || 14 = 17 * 100 + 14
9+
//!
10+
//! Finding the next power of 10 is quick as the input only contains 1 to 3 digit numbers.
11+
use crate::util::parse::*;
12+
13+
type Input = (u64, u64);
14+
15+
pub fn parse(input: &str) -> Input {
16+
let mut equation = Vec::new();
17+
let mut part_one = 0;
18+
let mut part_two = 0;
19+
20+
for line in input.lines() {
21+
equation.extend(line.iter_unsigned::<u64>());
22+
23+
// If an equation is valid for part one then it's also valid for part two.
24+
if check(&equation, equation[0], equation.len() - 1, false) {
25+
part_one += equation[0];
26+
part_two += equation[0];
27+
} else if check(&equation, equation[0], equation.len() - 1, true) {
28+
part_two += equation[0];
29+
}
30+
31+
equation.clear();
32+
}
33+
34+
(part_one, part_two)
35+
}
36+
37+
pub fn part1(input: &Input) -> u64 {
38+
input.0
39+
}
40+
41+
pub fn part2(input: &Input) -> u64 {
42+
input.1
43+
}
44+
45+
fn check(terms: &[u64], test_value: u64, index: usize, concat: bool) -> bool {
46+
(test_value == 0)
47+
|| (concat
48+
&& test_value % next_power_of_ten(terms[index]) == terms[index]
49+
&& check(terms, test_value / next_power_of_ten(terms[index]), index - 1, concat))
50+
|| (test_value % terms[index] == 0
51+
&& check(terms, test_value / terms[index], index - 1, concat))
52+
|| (test_value >= terms[index]
53+
&& check(terms, test_value - terms[index], index - 1, concat))
54+
}
55+
56+
fn next_power_of_ten(n: u64) -> u64 {
57+
let mut power = 10;
58+
59+
while power <= n {
60+
power *= 10;
61+
}
62+
63+
power
64+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,5 @@ mod year2024 {
287287
mod day04_test;
288288
mod day05_test;
289289
mod day06_test;
290+
mod day07_test;
290291
}

tests/year2024/day07_test.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use aoc::year2024::day07::*;
2+
3+
const EXAMPLE: &str = "\
4+
190: 10 19
5+
3267: 81 40 27
6+
83: 17 5
7+
156: 15 6
8+
7290: 6 8 6 15
9+
161011: 16 10 13
10+
192: 17 8 14
11+
21037: 9 7 18 13
12+
292: 11 6 16 20";
13+
14+
#[test]
15+
fn part1_test() {
16+
let input = parse(EXAMPLE);
17+
assert_eq!(part1(&input), 3749);
18+
}
19+
20+
#[test]
21+
fn part2_test() {
22+
let input = parse(EXAMPLE);
23+
assert_eq!(part2(&input), 11387);
24+
}

0 commit comments

Comments
 (0)