Skip to content

Commit de4710a

Browse files
committed
day7
1 parent d739d69 commit de4710a

File tree

3 files changed

+163
-8
lines changed

3 files changed

+163
-8
lines changed

Diff for: 2024/src/day6.rs

-8
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,6 @@ impl<T: Copy> Grid<T> {
126126
fn get(&self, r: usize, c: usize) -> Option<T> {
127127
self.0.get(r).and_then(|row| row.get(c)).copied()
128128
}
129-
130-
fn get_offset(&self, r: usize, r_offset: isize, c: usize, c_offset: isize) -> Option<T> {
131-
let r: isize = r.try_into().ok()?;
132-
let r: usize = (r + r_offset).try_into().ok()?;
133-
let c: isize = c.try_into().ok()?;
134-
let c: usize = (c + c_offset).try_into().ok()?;
135-
self.get(r, c)
136-
}
137129
}
138130

139131
struct Input(Grid<Cell>);

Diff for: 2024/src/day7.rs

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
use aoc_runner_derive::{aoc, aoc_generator};
2+
3+
/*
4+
--- Day 7: Bridge Repair ---
5+
The Historians take you to a familiar rope bridge over a river in the middle of a jungle. The Chief isn't on this side of the bridge, though; maybe he's on the other side?
6+
7+
When you go to cross the bridge, you notice a group of engineers trying to repair it. (Apparently, it breaks pretty frequently.) You won't be able to cross until it's fixed.
8+
9+
You ask how long it'll take; the engineers tell you that it only needs final calibrations, but some young elephants were playing nearby and stole all the operators from their calibration equations! They could finish the calibrations if only someone could determine which test values could possibly be produced by placing any combination of operators into their calibration equations (your puzzle input).
10+
11+
For example:
12+
13+
190: 10 19
14+
3267: 81 40 27
15+
83: 17 5
16+
156: 15 6
17+
7290: 6 8 6 15
18+
161011: 16 10 13
19+
192: 17 8 14
20+
21037: 9 7 18 13
21+
292: 11 6 16 20
22+
Each line represents a single equation. The test value appears before the colon on each line; it is your job to determine whether the remaining numbers can be combined with operators to produce the test value.
23+
24+
Operators are always evaluated left-to-right, not according to precedence rules. Furthermore, numbers in the equations cannot be rearranged. Glancing into the jungle, you can see elephants holding two different types of operators: add (+) and multiply (*).
25+
26+
Only three of the above equations can be made true by inserting operators:
27+
28+
190: 10 19 has only one position that accepts an operator: between 10 and 19. Choosing + would give 29, but choosing * would give the test value (10 * 19 = 190).
29+
3267: 81 40 27 has two positions for operators. Of the four possible configurations of the operators, two cause the right side to match the test value: 81 + 40 * 27 and 81 * 40 + 27 both equal 3267 (when evaluated left-to-right)!
30+
292: 11 6 16 20 can be solved in exactly one way: 11 + 6 * 16 + 20.
31+
The engineers just need the total calibration result, which is the sum of the test values from just the equations that could possibly be true. In the above example, the sum of the test values for the three equations listed above is 3749.
32+
33+
Determine which equations could possibly be true. What is their total calibration result?
34+
*/
35+
36+
struct Equation {
37+
test_value: u64,
38+
values: Vec<u64>,
39+
}
40+
41+
fn try_reach_test_value(target_value: u64, current_value: u64, values: &[u64]) -> bool {
42+
if values.is_empty() {
43+
return current_value == target_value;
44+
}
45+
46+
let this_value = values[0];
47+
let remaining_values = &values[1..];
48+
49+
try_reach_test_value(target_value, current_value + this_value, remaining_values)
50+
|| try_reach_test_value(target_value, current_value * this_value, remaining_values)
51+
}
52+
53+
fn concat_digits(a: u64, b: u64) -> u64 {
54+
let mut result = a;
55+
56+
let mut tmp = b;
57+
while tmp > 0 {
58+
result *= 10;
59+
tmp /= 10;
60+
}
61+
62+
result + b
63+
}
64+
65+
fn try_reach_test_value2(target_value: u64, current_value: u64, values: &[u64]) -> bool {
66+
if values.is_empty() {
67+
return current_value == target_value;
68+
}
69+
70+
let this_value = values[0];
71+
let remaining_values = &values[1..];
72+
73+
try_reach_test_value2(target_value, current_value + this_value, remaining_values)
74+
|| try_reach_test_value2(target_value, current_value * this_value, remaining_values)
75+
|| try_reach_test_value2(target_value, concat_digits(current_value, this_value), remaining_values)
76+
}
77+
78+
#[aoc_generator(day7)]
79+
fn parse_input(input: &str) -> Vec<Equation> {
80+
input.lines().map(|line| {
81+
let mut parts = line.split(":");
82+
let test_value = parts.next().unwrap().trim().parse().unwrap();
83+
let values = parts.next().unwrap().trim().split(' ').map(|v| v.trim().parse().unwrap()).collect();
84+
Equation { test_value, values }
85+
}).collect()
86+
}
87+
88+
89+
#[aoc(day7, part1)]
90+
fn part1(input: &Vec<Equation>) -> u64 {
91+
input.iter().filter_map(|eq| {
92+
let initial = eq.values[0];
93+
if try_reach_test_value(eq.test_value, initial, &eq.values[1..]) {
94+
Some (eq.test_value)
95+
} else {
96+
None
97+
}
98+
}).sum()
99+
}
100+
101+
#[aoc(day7, part2)]
102+
fn part2(input: &Vec<Equation>) -> u64 {
103+
input.iter().filter_map(|eq| {
104+
let initial = eq.values[0];
105+
if try_reach_test_value2(eq.test_value, initial, &eq.values[1..]) {
106+
Some (eq.test_value)
107+
} else {
108+
None
109+
}
110+
}).sum()
111+
}
112+
113+
114+
#[cfg(test)]
115+
mod tests {
116+
use super::*;
117+
118+
#[test]
119+
fn concat() {
120+
assert_eq!(concat_digits(123, 456), 123456);
121+
assert_eq!(concat_digits(123, 0), 123);
122+
assert_eq!(concat_digits(0, 456), 456);
123+
}
124+
125+
#[test]
126+
fn part1_example() {
127+
let input = parse_input(
128+
r#"
129+
190: 10 19
130+
3267: 81 40 27
131+
83: 17 5
132+
156: 15 6
133+
7290: 6 8 6 15
134+
161011: 16 10 13
135+
192: 17 8 14
136+
21037: 9 7 18 13
137+
292: 11 6 16 20
138+
"#
139+
.trim(),
140+
);
141+
assert_eq!(part1(&input), 3749);
142+
}
143+
144+
#[test]
145+
fn part2_example() {
146+
let input = parse_input(
147+
r#"
148+
190: 10 19
149+
3267: 81 40 27
150+
83: 17 5
151+
156: 15 6
152+
7290: 6 8 6 15
153+
161011: 16 10 13
154+
192: 17 8 14
155+
21037: 9 7 18 13
156+
292: 11 6 16 20
157+
"#
158+
.trim(),
159+
);
160+
assert_eq!(part2(&input), 11387);
161+
}
162+
}

Diff for: 2024/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod day7;
12
use aoc_runner_derive::aoc_lib;
23

34
mod day1;

0 commit comments

Comments
 (0)