Skip to content

Commit ae7a2f1

Browse files
committed
day7
1 parent 1e18ad2 commit ae7a2f1

File tree

5 files changed

+190
-9
lines changed

5 files changed

+190
-9
lines changed

2024/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Advent of Code in Rust 🦀
22

33
![AoC2024](https://img.shields.io/badge/Advent_of_Code-2024-8A2BE2)
4-
![Stars: 12](https://img.shields.io/badge/Stars-12⭐-blue)
5-
![Rust: 6](https://img.shields.io/badge/Rust-6-cyan?logo=Rust)
6-
![Python: 4](https://img.shields.io/badge/Python-4-cyan?logo=Python)
4+
![Stars: 14](https://img.shields.io/badge/Stars-14⭐-blue)
5+
![Rust: 7](https://img.shields.io/badge/Rust-7-cyan?logo=Rust)
6+
![Python: 5](https://img.shields.io/badge/Python-5-cyan?logo=Python)
77

8-
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 12
8+
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 14
99

1010
Puzzle | Stars | Languages
1111
---------------------------------------------------------------- | ----- | -----------
@@ -15,3 +15,4 @@ Puzzle | Stars | Langu
1515
[Day 4: Ceres Search](https://adventofcode.com/2024/day/4) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day4/day4.rs) [![Python](../scripts/assets/python.png)](../2024/day4/day4.py)
1616
[Day 5: Print Queue](https://adventofcode.com/2024/day/5) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day5/day5.rs)
1717
[Day 6: Guard Gallivant](https://adventofcode.com/2024/day/6) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day6/day6.rs)
18+
[Day 7: Bridge Repair](https://adventofcode.com/2024/day/7) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day7/day7.rs) [![Python](../scripts/assets/python.png)](../2024/day7/day7.py)

2024/day7/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day7"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }
8+
9+
[[bin]]
10+
name = "day7"
11+
path = "day7.rs"

2024/day7/day7.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//! [Day 7: Bridge Repair](https://adventofcode.com/2024/day/7)
2+
3+
// use std::collections::{HashMap,HashSet};
4+
5+
struct Equation {
6+
test_value: u64,
7+
values: Vec<u64>,
8+
}
9+
10+
struct Puzzle {
11+
equations: Vec<Equation>,
12+
}
13+
14+
impl Puzzle {
15+
fn new() -> Puzzle {
16+
Puzzle {
17+
equations: Vec::new(),
18+
}
19+
}
20+
21+
/// Get the puzzle input.
22+
fn configure(&mut self, path: &str) {
23+
let data = std::fs::read_to_string(path).unwrap();
24+
25+
for line in data.lines() {
26+
let (test_value, values) = line.split_once(':').unwrap();
27+
28+
let equation = Equation {
29+
test_value: test_value.parse().unwrap(),
30+
values: values
31+
.split_ascii_whitespace()
32+
.map(|x| x.parse().unwrap())
33+
.collect(),
34+
};
35+
36+
self.equations.push(equation);
37+
}
38+
}
39+
40+
/// Check if there is a combination of operators + and * that solves the equation
41+
fn check_two_operators(equation: &Equation) -> bool {
42+
// the number of operations
43+
let n = (equation.values.len() - 1).try_into().unwrap();
44+
45+
// iterate over all combinations
46+
for mut i in 0..2_u32.pow(n) {
47+
let mut result = equation.values[0];
48+
49+
for value in &equation.values[1..] {
50+
if i % 2 == 0 {
51+
result += value;
52+
} else {
53+
result *= value;
54+
}
55+
i /= 2;
56+
57+
if result > equation.test_value {
58+
// unnecessary to continue: result will never equal test value
59+
break;
60+
}
61+
}
62+
63+
if result == equation.test_value {
64+
return true;
65+
}
66+
}
67+
68+
false
69+
}
70+
71+
/// Check if there is a combination of operators + * || that solves the equation
72+
fn check_three_operators(equation: &Equation) -> bool {
73+
// pre-compute the power of 10 for operator ||
74+
let mut pow10 = Vec::new();
75+
76+
for value in &equation.values[1..] {
77+
let mut p = 1;
78+
let mut value = *value;
79+
while value != 0 {
80+
p *= 10;
81+
value /= 10;
82+
}
83+
pow10.push(p);
84+
}
85+
86+
// the number of operations
87+
let n = (equation.values.len() - 1).try_into().unwrap();
88+
89+
// iterate over all combinations
90+
for mut i in 0..3_u32.pow(n) {
91+
let mut result = equation.values[0];
92+
93+
for (k, value) in equation.values[1..].iter().enumerate() {
94+
result = match i % 3 {
95+
0 => result + value,
96+
1 => result * value,
97+
_ => result * pow10[k] + value,
98+
};
99+
i /= 3;
100+
101+
if result > equation.test_value {
102+
break;
103+
}
104+
}
105+
106+
if result == equation.test_value {
107+
return true;
108+
}
109+
}
110+
111+
false
112+
}
113+
114+
/// Solve part one.
115+
fn part1(&self) -> u64 {
116+
self.equations
117+
.iter()
118+
.filter(|e| Self::check_two_operators(e))
119+
.map(|x| x.test_value)
120+
.sum()
121+
}
122+
123+
/// Solve part two.
124+
fn part2(&self) -> u64 {
125+
self.equations
126+
.iter()
127+
.filter(|e| Self::check_three_operators(e))
128+
.map(|x| x.test_value)
129+
.sum()
130+
}
131+
}
132+
133+
fn main() {
134+
let args = aoc::parse_args();
135+
let mut puzzle = Puzzle::new();
136+
puzzle.configure(args.path.as_str());
137+
println!("{}", puzzle.part1());
138+
println!("{}", puzzle.part2());
139+
}
140+
141+
/// Test from puzzle input
142+
#[cfg(test)]
143+
mod test {
144+
use super::*;
145+
146+
#[test]
147+
fn test01() {
148+
let mut puzzle = Puzzle::new();
149+
puzzle.configure("test.txt");
150+
assert_eq!(puzzle.part1(), 3749);
151+
}
152+
153+
#[test]
154+
fn test02() {
155+
let mut puzzle = Puzzle::new();
156+
puzzle.configure("test.txt");
157+
assert_eq!(puzzle.part2(), 11387);
158+
}
159+
}

2024/day7/test.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
190: 10 19
2+
3267: 81 40 27
3+
83: 17 5
4+
156: 15 6
5+
7290: 6 8 6 15
6+
161011: 16 10 13
7+
192: 17 8 14
8+
21037: 9 7 18 13
9+
292: 11 6 16 20

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Advent of Code in Rust 🦀
22

3-
![Stars: 462](https://img.shields.io/badge/Stars-462⭐-blue)
4-
![Rust: 177](https://img.shields.io/badge/Rust-177-cyan?logo=Rust)
5-
![Python: 117](https://img.shields.io/badge/Python-117-cyan?logo=Python)
3+
![Stars: 464](https://img.shields.io/badge/Stars-464⭐-blue)
4+
![Rust: 178](https://img.shields.io/badge/Rust-178-cyan?logo=Rust)
5+
![Python: 118](https://img.shields.io/badge/Python-118-cyan?logo=Python)
66

77
Solutions of [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org), and sometimes in [Python](https://www.python.org/).
88

99
Made for fun 😎 and to practice Rust. Many thanks to [Eric Wastl](https://twitter.com/ericwastl).
1010

11-
## 2024 (current event) ([Calendar](https://adventofcode.com/2024)) ([Solutions](2024/)) : 12
11+
## 2024 (current event) ([Calendar](https://adventofcode.com/2024)) ([Solutions](2024/)) : 14
1212

1313
Puzzle | Stars | Languages
1414
---------------------------------------------------------------- | ----- | -----------
@@ -18,12 +18,13 @@ Puzzle | Stars | Langu
1818
[Day 4: Ceres Search](https://adventofcode.com/2024/day/4) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day4/day4.rs) [![Python](./scripts/assets/python.png)](./2024/day4/day4.py)
1919
[Day 5: Print Queue](https://adventofcode.com/2024/day/5) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day5/day5.rs)
2020
[Day 6: Guard Gallivant](https://adventofcode.com/2024/day/6) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day6/day6.rs)
21+
[Day 7: Bridge Repair](https://adventofcode.com/2024/day/7) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day7/day7.rs) [![Python](./scripts/assets/python.png)](./2024/day7/day7.py)
2122

2223
## Paste years
2324

2425
Calendar | Solutions | Stars | Rust | Python
2526
-------- | --------- | ----- | ---- | ------
26-
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 12⭐ | 6 | 4
27+
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 14⭐ | 7 | 5
2728
[Advent of Code 2023](https://adventofcode.com/2023) | [Solutions](2023/README.md) | 50⭐ | 24 | 11
2829
[Advent of Code 2022](https://adventofcode.com/2022) | [Solutions](2022/README.md) | 50⭐ | 24 | 18
2930
[Advent of Code 2021](https://adventofcode.com/2021) | [Solutions](2021/README.md) | 50⭐ | 23 | 12

0 commit comments

Comments
 (0)