From 289ae15cc5aaa6c35df0a9f74f8caa74d036e803 Mon Sep 17 00:00:00 2001 From: Rh4096 Date: Sat, 7 Dec 2024 11:03:53 +0700 Subject: [PATCH] day 6.9 --- src/solutions/day_10.rs | 24 +++++++++++++++++++++ src/solutions/day_11.rs | 24 +++++++++++++++++++++ src/solutions/day_12.rs | 24 +++++++++++++++++++++ src/solutions/day_13.rs | 24 +++++++++++++++++++++ src/solutions/day_14.rs | 24 +++++++++++++++++++++ src/solutions/day_15.rs | 24 +++++++++++++++++++++ src/solutions/day_16.rs | 24 +++++++++++++++++++++ src/solutions/day_17.rs | 24 +++++++++++++++++++++ src/solutions/day_18.rs | 24 +++++++++++++++++++++ src/solutions/day_19.rs | 24 +++++++++++++++++++++ src/solutions/day_20.rs | 24 +++++++++++++++++++++ src/solutions/day_21.rs | 24 +++++++++++++++++++++ src/solutions/day_22.rs | 25 ++++++++++++++++++++++ src/solutions/day_23.rs | 24 +++++++++++++++++++++ src/solutions/day_24.rs | 24 +++++++++++++++++++++ src/solutions/day_25.rs | 24 +++++++++++++++++++++ src/solutions/day_4.rs | 46 ++++++++++++++++++++--------------------- src/solutions/day_7.rs | 24 +++++++++++++++++++++ src/solutions/day_8.rs | 24 +++++++++++++++++++++ src/solutions/day_9.rs | 24 +++++++++++++++++++++ src/utils/coord.rs | 2 +- 21 files changed, 480 insertions(+), 25 deletions(-) diff --git a/src/solutions/day_10.rs b/src/solutions/day_10.rs index d1acc1a..a78b198 100644 --- a/src/solutions/day_10.rs +++ b/src/solutions/day_10.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_11.rs b/src/solutions/day_11.rs index 8b44a6c..3279298 100644 --- a/src/solutions/day_11.rs +++ b/src/solutions/day_11.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_12.rs b/src/solutions/day_12.rs index 872e4dd..f3dfebe 100644 --- a/src/solutions/day_12.rs +++ b/src/solutions/day_12.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_13.rs b/src/solutions/day_13.rs index 9f350d3..6bd954d 100644 --- a/src/solutions/day_13.rs +++ b/src/solutions/day_13.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_14.rs b/src/solutions/day_14.rs index 36c7134..0ed8566 100644 --- a/src/solutions/day_14.rs +++ b/src/solutions/day_14.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_15.rs b/src/solutions/day_15.rs index 487f2f6..bdfd526 100644 --- a/src/solutions/day_15.rs +++ b/src/solutions/day_15.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_16.rs b/src/solutions/day_16.rs index d3b8926..357e533 100644 --- a/src/solutions/day_16.rs +++ b/src/solutions/day_16.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_17.rs b/src/solutions/day_17.rs index ec16b6e..38ccb36 100644 --- a/src/solutions/day_17.rs +++ b/src/solutions/day_17.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_18.rs b/src/solutions/day_18.rs index a2a725f..b431c82 100644 --- a/src/solutions/day_18.rs +++ b/src/solutions/day_18.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_19.rs b/src/solutions/day_19.rs index 63edac4..33b9189 100644 --- a/src/solutions/day_19.rs +++ b/src/solutions/day_19.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_20.rs b/src/solutions/day_20.rs index f97104c..fec4471 100644 --- a/src/solutions/day_20.rs +++ b/src/solutions/day_20.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_21.rs b/src/solutions/day_21.rs index ac8b628..87c1b40 100644 --- a/src/solutions/day_21.rs +++ b/src/solutions/day_21.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_22.rs b/src/solutions/day_22.rs index c60c8ca..0bce5a1 100644 --- a/src/solutions/day_22.rs +++ b/src/solutions/day_22.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_23.rs b/src/solutions/day_23.rs index 168aebf..18fabfe 100644 --- a/src/solutions/day_23.rs +++ b/src/solutions/day_23.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_24.rs b/src/solutions/day_24.rs index ec40882..ecbeb0f 100644 --- a/src/solutions/day_24.rs +++ b/src/solutions/day_24.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_25.rs b/src/solutions/day_25.rs index 435ec37..22f2225 100644 --- a/src/solutions/day_25.rs +++ b/src/solutions/day_25.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_4.rs b/src/solutions/day_4.rs index 4aef4f2..141f205 100644 --- a/src/solutions/day_4.rs +++ b/src/solutions/day_4.rs @@ -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, } 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, diff --git a/src/solutions/day_7.rs b/src/solutions/day_7.rs index 0d46306..dc523c1 100644 --- a/src/solutions/day_7.rs +++ b/src/solutions/day_7.rs @@ -1 +1,25 @@ +use std::{fs::read_to_string, path::Path}; + +use super::AocSolution; +use crate::utils::Result; + pub struct AocDaySevenSolution; + +impl AocSolution for AocDaySevenSolution { + type Output = u64; + + fn get_input(&self, path: Option<&Path>) -> Result { + Ok(match path { + Some(p) => read_to_string(p)?, + None => read_to_string("./input/day_day_7.txt")?, + }) + } + + fn part_one(&self, input: &str) -> Result { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_8.rs b/src/solutions/day_8.rs index a6a1c83..1ea9f0a 100644 --- a/src/solutions/day_8.rs +++ b/src/solutions/day_8.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/solutions/day_9.rs b/src/solutions/day_9.rs index 526ebc6..7980846 100644 --- a/src/solutions/day_9.rs +++ b/src/solutions/day_9.rs @@ -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 { + 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 { + todo!() + } + + fn part_two(&self, input: &str) -> Result { + todo!() + } +} diff --git a/src/utils/coord.rs b/src/utils/coord.rs index 06cf0ec..fe2f850 100644 --- a/src/utils/coord.rs +++ b/src/utils/coord.rs @@ -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 } } }