This commit is contained in:
StNicolay 2024-12-07 10:47:17 +03:00
parent 68152e225f
commit df4a5a12d7
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 63 additions and 2 deletions

View File

@ -83,6 +83,7 @@ fn get_visited(board: &[Vec<Position>], mut pos: (usize, usize)) -> HashSet<(usi
visited visited
} }
#[allow(dead_code)]
pub fn task1() { pub fn task1() {
let (board, pos) = parse_board(&std::fs::read_to_string("input.txt").unwrap()); let (board, pos) = parse_board(&std::fs::read_to_string("input.txt").unwrap());
let mut visited = get_visited(&board, pos); let mut visited = get_visited(&board, pos);
@ -109,6 +110,7 @@ fn is_loop(board: &[Vec<Position>], mut pos: (usize, usize)) -> bool {
} }
} }
#[allow(dead_code)]
pub fn task2() { pub fn task2() {
let (mut board, pos) = parse_board(&std::fs::read_to_string("input.txt").unwrap()); let (mut board, pos) = parse_board(&std::fs::read_to_string("input.txt").unwrap());
let visited = get_visited(&board, pos); let visited = get_visited(&board, pos);

58
src/day7.rs Normal file
View File

@ -0,0 +1,58 @@
use crate::get_lines;
use itertools::Itertools;
#[derive(Clone, Copy)]
enum Operation {
Add,
Mul,
Concentration,
}
impl Operation {
fn apply(self, a: u64, b: u64) -> u64 {
match self {
Self::Add => a + b,
Self::Mul => a * b,
Self::Concentration => {
let val = format!("{a}{b}");
val.parse().unwrap()
}
}
}
}
fn inner(ops: &[Operation]) {
let lines = get_lines("input.txt");
let result: u64 = lines
.filter_map(|line| {
let (target, numbers) = line.split_once(": ").unwrap();
let target: u64 = target.parse().unwrap();
let numbers: Vec<u64> = numbers.split(' ').map(|num| num.parse().unwrap()).collect();
let ops_set = (0..numbers.len() - 1)
.map(|_| ops.iter().copied())
.multi_cartesian_product();
for ops in ops_set {
let mut result = numbers[0];
for (n, op) in numbers[1..].iter().zip(ops) {
result = op.apply(result, *n);
}
if result == target {
return Some(target);
}
}
None
})
.sum();
println!("{result}");
}
#[allow(dead_code)]
pub fn task1() {
inner(&[Operation::Add, Operation::Mul]);
}
#[allow(dead_code)]
pub fn task2() {
inner(&[Operation::Add, Operation::Mul, Operation::Concentration]);
}

View File

@ -4,6 +4,7 @@ mod day3;
mod day4; mod day4;
mod day5; mod day5;
mod day6; mod day6;
mod day7;
use std::{ use std::{
fs::File, fs::File,
@ -21,6 +22,6 @@ fn parse_line(line: &str, pattern: char) -> impl Iterator<Item = i32> + '_ {
} }
fn main() { fn main() {
day6::task1(); day7::task1();
day6::task2(); day7::task2();
} }