Skip to content

Commit 2b30e63

Browse files
committed
Add day 7 part 1 and 2
1 parent 158fb3b commit 2b30e63

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

input_examples/day07.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

src/days.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod day03;
55
mod day04;
66
mod day05;
77
mod day06;
8+
mod day07;
89

910
use common::generic_day::GenericDay;
1011

@@ -21,6 +22,7 @@ pub fn run_day(day: u8, input_folder: String) {
2122
4 => run_my_day(day04::Day04::new(input_folder)),
2223
5 => run_my_day(day05::Day05::new(input_folder)),
2324
6 => run_my_day(day06::Day06::new(input_folder)),
25+
7 => run_my_day(day07::Day07::new(input_folder)),
2426
_ => panic!("Day not found!"),
2527
}
2628
}

src/days/day07.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use std::fs::File;
2+
use std::io::BufRead;
3+
use std::io::BufReader;
4+
5+
use super::common::generic_day;
6+
7+
struct EquationInput {
8+
result: i64,
9+
inputs: Vec<i64>,
10+
}
11+
12+
pub struct Day07 {
13+
input_file: String,
14+
equation_inputs: Vec<EquationInput>,
15+
}
16+
17+
impl Day07 {
18+
pub fn new(input_folder: String) -> Day07 {
19+
let mut day07: Day07 = Day07 {
20+
input_file: format!("{}/day07.txt", input_folder),
21+
equation_inputs: Vec::new(),
22+
};
23+
day07.parse_input();
24+
day07
25+
}
26+
27+
fn parse_line(&mut self, line: &str) {
28+
let values = line.split(":").collect::<Vec<_>>();
29+
self.equation_inputs.push(EquationInput {
30+
result: values[0].parse().unwrap(),
31+
inputs: values[1]
32+
.split_whitespace()
33+
.map(|x| x.parse().unwrap())
34+
.collect::<Vec<_>>(),
35+
});
36+
}
37+
38+
fn parse_input(&mut self) {
39+
let _ = BufReader::new(File::open(&self.input_file).unwrap())
40+
.lines()
41+
.map(|x| x.unwrap())
42+
.map(|line| self.parse_line(&line))
43+
.collect::<Vec<_>>();
44+
}
45+
46+
fn evaluate_which_equations_could_be_true(
47+
&self,
48+
operations: Vec<&dyn Fn(i64, i64) -> i64>,
49+
) -> i64 {
50+
let mut sum_of_potential_true_equations = 0;
51+
for equation_input in &self.equation_inputs {
52+
let mut results = vec![equation_input.inputs[0]];
53+
for input in equation_input.inputs[1..].iter() {
54+
let mut tmp_results: Vec<i64> = Vec::new();
55+
for result in results {
56+
for operation in &operations {
57+
tmp_results.push(operation(result, *input));
58+
}
59+
}
60+
results = tmp_results.clone();
61+
}
62+
63+
for result in results {
64+
if equation_input.result == result {
65+
sum_of_potential_true_equations += equation_input.result;
66+
break;
67+
}
68+
}
69+
}
70+
sum_of_potential_true_equations
71+
}
72+
}
73+
74+
fn add(a: i64, b: i64) -> i64 {
75+
a + b
76+
}
77+
78+
fn multiply(a: i64, b: i64) -> i64 {
79+
a * b
80+
}
81+
82+
fn concat(a: i64, b: i64) -> i64 {
83+
(a.to_string() + &b.to_string()).parse().unwrap()
84+
}
85+
86+
impl generic_day::GenericDay for Day07 {
87+
fn part1(&self) -> i64 {
88+
self.evaluate_which_equations_could_be_true(vec![&add, &multiply])
89+
}
90+
91+
fn part2(&self) -> i64 {
92+
self.evaluate_which_equations_could_be_true(vec![&add, &multiply, &concat])
93+
}
94+
}
95+
96+
#[cfg(test)]
97+
mod tests {
98+
use super::*;
99+
use crate::days::GenericDay;
100+
101+
#[test]
102+
fn result_part1() {
103+
let day07: Day07 = Day07::new(String::from("input_examples"));
104+
assert_eq!(day07.part1(), 3749);
105+
}
106+
107+
#[test]
108+
fn result_part2() {
109+
let day07: Day07 = Day07::new(String::from("input_examples"));
110+
assert_eq!(day07.part2(), 11387);
111+
}
112+
}

0 commit comments

Comments
 (0)