Compare commits

...

2 commits

Author SHA1 Message Date
Rh4096
a3f3964e96 day 7 2024-12-07 13:09:48 +07:00
Rh4096
289ae15cc5 day 6.9 2024-12-07 11:03:53 +07:00
23 changed files with 635 additions and 25 deletions

View file

@ -28,6 +28,7 @@ fn main() -> Result<()> {
4 => AocDayFourSolution,
5 => AocDayFiveSolution,
6 => AocDaySixSolution,
7 => AocDaySevenSolution,
};
Ok(())

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTenSolution;
impl AocSolution for AocDayTenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_10.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayElevenSolution;
impl AocSolution for AocDayElevenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_11.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwelveSolution;
impl AocSolution for AocDayTwelveSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_12.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayThirteenSolution;
impl AocSolution for AocDayThirteenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_13.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayFourteenSolution;
impl AocSolution for AocDayFourteenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_14.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayFifteenSolution;
impl AocSolution for AocDayFifteenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_15.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDaySixteenSolution;
impl AocSolution for AocDaySixteenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_16.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDaySeventeenSolution;
impl AocSolution for AocDaySeventeenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_17.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayEighteenSolution;
impl AocSolution for AocDayEighteenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_18.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayNineteenSolution;
impl AocSolution for AocDayNineteenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_19.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwentySolution;
impl AocSolution for AocDayTwentySolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_20.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwentyOneSolution;
impl AocSolution for AocDayTwentyOneSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_21.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,26 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwentyTwoSolution;
impl AocSolution for AocDayTwentyTwoSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_22.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwentyThreeSolution;
impl AocSolution for AocDayTwentyThreeSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_23.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwentyFourSolution;
impl AocSolution for AocDayTwentyFourSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_24.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayTwentyFiveSolution;
impl AocSolution for AocDayTwentyFiveSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_25.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1,21 +1,21 @@
use std::{collections::HashMap, fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
use crate::utils::{coord::Coord, Result};
const DIRS: [(i16, i16); 8] = [
(1, 0),
(1, -1),
(0, -1),
(-1, -1),
(-1, 0),
(-1, 1),
(0, 1),
(1, 1),
const DIRS: [Coord; 8] = [
Coord::new(1, 0),
Coord::new(1, -1),
Coord::new(0, -1),
Coord::new(-1, -1),
Coord::new(-1, 0),
Coord::new(-1, 1),
Coord::new(0, 1),
Coord::new(1, 1),
];
struct XmasGrid {
pub grid: HashMap<(i16, i16), char>,
pub grid: HashMap<Coord, char>,
}
impl XmasGrid {
@ -24,22 +24,20 @@ impl XmasGrid {
grid: src
.lines()
.zip(0..)
.flat_map(|(l, y)| l.chars().zip(0..).map(move |(c, x)| ((x, y), c)))
.flat_map(|(l, y)| l.chars().zip(0..).map(move |(c, x)| (Coord::new(x, y), c)))
.collect(),
}
}
fn find_xmas(&self, pos: (i16, i16)) -> usize {
fn find_xmas(&self, pos: Coord) -> usize {
let mut tmp = 0;
for (dx, dy) in DIRS {
for dir in DIRS {
let mut found = true;
let (mut x, mut y) = pos;
for nxt in ['M', 'A', 'S'] {
x += dx;
y += dy;
let nxt_pos = pos + dir;
if self.grid.get(&(x, y)) != Some(&nxt) {
if self.grid.get(&nxt_pos) != Some(&nxt) {
found = false;
break;
}
@ -53,13 +51,13 @@ impl XmasGrid {
tmp
}
fn find_x_mas(&self, pos: (i16, i16)) -> usize {
let (x, y) = pos;
fn find_x_mas(&self, pos: Coord) -> usize {
let (x, y) = (pos.x, pos.y);
let tr = self.grid.get(&(x + 1, y + 1));
let br = self.grid.get(&(x + 1, y - 1));
let bl = self.grid.get(&(x - 1, y - 1));
let tl = self.grid.get(&(x - 1, y + 1));
let tr = self.grid.get(&Coord::new(x + 1, y + 1));
let br = self.grid.get(&Coord::new(x + 1, y - 1));
let bl = self.grid.get(&Coord::new(x - 1, y - 1));
let tl = self.grid.get(&Coord::new(x - 1, y + 1));
match (tr, bl, tl, br) {
(Some('S'), Some('M'), Some('M'), Some('S')) => 1,

View file

@ -1 +1,177 @@
use std::{fs::read_to_string, ops::Mul, path::Path};
use super::AocSolution;
use crate::utils::Result;
fn compute(nums: &[u64], total: u64) -> bool {
let mut equations = vec![false; nums.len() - 1];
fn _inc_xf(flags: &mut [bool]) {
let mut carry = true;
for flag in flags.iter_mut().rev() {
if !carry {
break;
}
let tmp = *flag;
*flag = !tmp;
carry = tmp;
}
}
loop {
if equations.iter().zip(1..).fold(
nums[0],
|n, (&x, idx)| {
if x {
n * nums[idx]
} else {
n + nums[idx]
}
},
) == total
{
return true;
} else {
if equations.iter().all(|b| *b) {
break;
}
_inc_xf(&mut equations);
}
}
false
}
fn concat(n1: u64, n2: u64) -> u64 {
format!("{n1}{n2}").parse().unwrap()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Operation {
Add,
Mul,
Concat,
}
impl Operation {
fn inc(self) -> Self {
match self {
Self::Add => Self::Mul,
Self::Mul => Self::Concat,
Self::Concat => Self::Add,
}
}
}
fn compute_ver_two(nums: &[u64], total: u64) -> bool {
let mut equations = vec![Operation::Add; nums.len() - 1];
fn _inc_xf(flags: &mut [Operation]) {
let mut carry = true;
for flag in flags.iter_mut().rev() {
if !carry {
break;
}
let tmp = *flag;
*flag = tmp.inc();
carry = tmp == Operation::Concat;
}
}
loop {
if equations
.iter()
.zip(1..)
.fold(nums[0], |n, (&x, idx)| match x {
Operation::Add => n + nums[idx],
Operation::Mul => n * nums[idx],
Operation::Concat => concat(n, nums[idx]),
})
== total
{
return true;
} else {
if equations.iter().all(|b| *b == Operation::Concat) {
break;
}
_inc_xf(&mut equations);
}
}
false
}
#[derive(Clone, Copy)]
pub struct AocDaySevenSolution;
impl AocSolution for AocDaySevenSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_7.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
let mut sum = 0;
for line in input.lines() {
if let Some((total, nums)) = line.split_once(':') {
let total = total.parse::<u64>()?;
let nums = nums
.split_whitespace()
.map(|n| n.parse::<u64>().map_err(|e| e.into()))
.collect::<Result<Vec<u64>>>()?;
if compute(&nums, total) {
sum += total;
}
}
}
Ok(sum)
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
let mut sum = 0;
for line in input.lines() {
if let Some((total, nums)) = line.split_once(':') {
let total = total.parse::<u64>()?;
let nums = nums
.split_whitespace()
.map(|n| n.parse::<u64>().map_err(|e| e.into()))
.collect::<Result<Vec<u64>>>()?;
if compute_ver_two(&nums, total) {
sum += total;
}
}
}
Ok(sum)
}
}
#[test]
fn test() {
let sol = AocDaySevenSolution;
assert_eq!(
sol.part_one(
r#"190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20"#
)
.unwrap(),
3749
);
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayEightSolution;
impl AocSolution for AocDayEightSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_8.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -1 +1,25 @@
use std::{fs::read_to_string, path::Path};
use super::AocSolution;
use crate::utils::Result;
pub struct AocDayNineSolution;
impl AocSolution for AocDayNineSolution {
type Output = u64;
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_day_9.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
todo!()
}
fn part_two(&self, input: &str) -> Result<Self::Output> {
todo!()
}
}

View file

@ -9,7 +9,7 @@ pub struct Coord {
}
impl Coord {
pub fn new(x: i16, y: i16) -> Self {
pub const fn new(x: i16, y: i16) -> Self {
Self { x, y }
}
}

View file

@ -8,6 +8,8 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Error parsing integer: {0}")]
ParseNumErr(#[from] std::num::ParseIntError),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid command usage: {0}")]