Day 7
This commit is contained in:
parent
68152e225f
commit
df4a5a12d7
@ -83,6 +83,7 @@ fn get_visited(board: &[Vec<Position>], mut pos: (usize, usize)) -> HashSet<(usi
|
||||
visited
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn task1() {
|
||||
let (board, pos) = parse_board(&std::fs::read_to_string("input.txt").unwrap());
|
||||
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() {
|
||||
let (mut board, pos) = parse_board(&std::fs::read_to_string("input.txt").unwrap());
|
||||
let visited = get_visited(&board, pos);
|
||||
|
58
src/day7.rs
Normal file
58
src/day7.rs
Normal 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]);
|
||||
}
|
@ -4,6 +4,7 @@ mod day3;
|
||||
mod day4;
|
||||
mod day5;
|
||||
mod day6;
|
||||
mod day7;
|
||||
|
||||
use std::{
|
||||
fs::File,
|
||||
@ -21,6 +22,6 @@ fn parse_line(line: &str, pattern: char) -> impl Iterator<Item = i32> + '_ {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
day6::task1();
|
||||
day6::task2();
|
||||
day7::task1();
|
||||
day7::task2();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user