feat: added day 7 part 1 and 2
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 4s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (7) (push) Successful in 3s
All checks were successful
Build and run challenges / Challenge for day (1) (push) Successful in 3s
Build and run challenges / Challenge for day (2) (push) Successful in 3s
Build and run challenges / Challenge for day (4) (push) Successful in 4s
Build and run challenges / Challenge for day (5) (push) Successful in 5s
Build and run challenges / Challenge for day (6) (push) Successful in 5s
Build and run challenges / Challenge for day (3) (push) Successful in 11s
Build and run challenges / Challenge for day (7) (push) Successful in 3s
Signed-off-by: Louis Vallat <contact@louis-vallat.dev>
This commit is contained in:
parent
57e93cffdc
commit
8bb3a8cda6
@ -11,7 +11,7 @@ jobs:
|
||||
name: Challenge for day
|
||||
strategy:
|
||||
matrix:
|
||||
day_number: [1, 2, 3, 4, 5, 6]
|
||||
day_number: [1, 2, 3, 4, 5, 6, 7]
|
||||
runs-on: rust-bookworm
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
|
6
day7/Cargo.toml
Normal file
6
day7/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day7"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
100
day7/src/main.rs
Normal file
100
day7/src/main.rs
Normal file
@ -0,0 +1,100 @@
|
||||
use std::{env, fs};
|
||||
|
||||
fn read_input(path: &str) -> String {
|
||||
fs::read_to_string(path).expect("Cannot read file.")
|
||||
}
|
||||
|
||||
fn add(a: u64, b: u64) -> u64 {
|
||||
a + b
|
||||
}
|
||||
|
||||
fn mul(a: u64, b: u64) -> u64 {
|
||||
a * b
|
||||
}
|
||||
|
||||
fn concat(a: u64, b: u64) -> u64 {
|
||||
format!("{a}{b}").parse().unwrap()
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> Vec<(u64, Vec<u64>)> {
|
||||
input
|
||||
.lines()
|
||||
.filter(|line| !line.is_empty())
|
||||
.map(|line| {
|
||||
let split_line = line.split_once(": ").unwrap();
|
||||
(
|
||||
split_line.0.parse().unwrap(),
|
||||
split_line
|
||||
.1
|
||||
.split_whitespace()
|
||||
.map(|s| s.parse().unwrap())
|
||||
.rev()
|
||||
.collect(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn is_valid(data: &mut Vec<u64>, acc: u64, target: u64, operators: &Vec<&fn(u64, u64) -> u64>) -> bool {
|
||||
if data.is_empty() {
|
||||
return target == acc;
|
||||
}
|
||||
|
||||
if acc > target {
|
||||
return false;
|
||||
}
|
||||
|
||||
let old = data.pop().unwrap();
|
||||
for operator in operators {
|
||||
if is_valid(data, operator(acc, old), target, operators) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
data.push(old);
|
||||
false
|
||||
}
|
||||
|
||||
fn sum_valid(calibration_data: &Vec<(u64, Vec<u64>)>, operators: &Vec<&fn(u64, u64) -> u64>) -> u64 {
|
||||
let mut sum_valid = 0;
|
||||
let calibration_data = calibration_data.clone();
|
||||
|
||||
for calibration in calibration_data {
|
||||
let mut x = calibration.1;
|
||||
let acc = x.pop().unwrap();
|
||||
if is_valid(&mut x, acc, calibration.0, operators) {
|
||||
sum_valid += calibration.0;
|
||||
}
|
||||
}
|
||||
|
||||
sum_valid
|
||||
}
|
||||
|
||||
fn part_1(input: &Vec<(u64, Vec<u64>)>) -> u64 {
|
||||
let valid_operators = vec![
|
||||
&(add as fn(u64, u64) -> u64),
|
||||
&(mul as fn(u64, u64) -> u64)
|
||||
];
|
||||
|
||||
sum_valid(input, &valid_operators)
|
||||
}
|
||||
|
||||
fn part_2(input: &Vec<(u64, Vec<u64>)>) -> u64 {
|
||||
let valid_operators = vec![
|
||||
&(add as fn(u64, u64) -> u64),
|
||||
&(mul as fn(u64, u64) -> u64),
|
||||
&(concat as fn(u64, u64) -> u64)
|
||||
];
|
||||
|
||||
sum_valid(input, &valid_operators)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
for arg in args.iter().skip(1) {
|
||||
let input = read_input(&arg);
|
||||
let calibration_data = parse_input(&input);
|
||||
println!("[{}]", &arg);
|
||||
println!("\t[Part 1] => Answer is '{}'.", part_1(&calibration_data));
|
||||
println!("\t[Part 2] => Answer is '{}'.", part_2(&calibration_data));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user