Skip to content

Commit

Permalink
Try to modularize input
Browse files Browse the repository at this point in the history
  • Loading branch information
MLNW committed Dec 3, 2023
1 parent 339685d commit cf2e481
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 61 deletions.
61 changes: 0 additions & 61 deletions 2023-rust/src/bin/03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,6 @@ use regex::Regex;

advent_of_code::solution!(3);

#[derive(PartialEq, Eq, Hash, Clone, Copy)]
struct Coordinate {
x: usize,
y: usize,
}

#[derive(Clone, Eq, PartialEq, Hash)]
struct Symbol {
character: char,
position: Coordinate,
}

struct EnginePart {
number: u32,
start: Coordinate,
end: Coordinate,
}

impl EnginePart {
fn neighbors(&self) -> Vec<Coordinate> {
let mut neighbors: Vec<Coordinate> = Vec::new();

let y = self.start.y;
for x in self.start.x..self.end.x + 1 {
// left
if x > 0 && x == self.start.x {
neighbors.push(Coordinate { x: x - 1, y });
neighbors.push(Coordinate { x: x - 1, y: y + 1 });
if y > 0 {
neighbors.push(Coordinate { x: x - 1, y: y - 1 });
}
}
// right
if x == self.end.x {
neighbors.push(Coordinate { x: x + 1, y });
neighbors.push(Coordinate { x: x + 1, y: y + 1 });
if y > 0 {
neighbors.push(Coordinate { x: x + 1, y: y - 1 });
}
}
//top
if y > 0 {
neighbors.push(Coordinate { x, y: y - 1 });
}
//bottom
neighbors.push(Coordinate { x, y: y + 1 });
}

neighbors
}

pub fn neighboring_symbol(&self, symbols: &HashMap<Coordinate, Symbol>) -> Option<Symbol> {
for neighbor in self.neighbors() {
if let Some(symbol) = symbols.get(&neighbor) {
return Some(symbol.clone());
}
}
None
}
}

pub fn part_one(input: &str) -> Option<u32> {
let parts = parse_engine_parts(input);
let symbols: HashMap<Coordinate, Symbol> = parse_symbols(input, r"(\+|-|\*|/|=|\$|@|%|#|&)");
Expand Down
63 changes: 63 additions & 0 deletions 2023-rust/src/input/input_03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::collections::HashMap;

#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub(crate) struct Coordinate {
x: usize,
y: usize,
}

#[derive(Clone, Eq, PartialEq, Hash)]
struct Symbol {
character: char,
position: Coordinate,
}

struct EnginePart {
number: u32,
start: Coordinate,
end: Coordinate,
}

impl EnginePart {
fn neighbors(&self) -> Vec<Coordinate> {
let mut neighbors: Vec<Coordinate> = Vec::new();

let y = self.start.y;
for x in self.start.x..self.end.x + 1 {
// left
if x > 0 && x == self.start.x {
neighbors.push(Coordinate { x: x - 1, y });
neighbors.push(Coordinate { x: x - 1, y: y + 1 });
if y > 0 {
neighbors.push(Coordinate { x: x - 1, y: y - 1 });
}
}
// right
if x == self.end.x {
neighbors.push(Coordinate { x: x + 1, y });
neighbors.push(Coordinate { x: x + 1, y: y + 1 });
if y > 0 {
neighbors.push(Coordinate { x: x + 1, y: y - 1 });
}
}
//top
if y > 0 {
neighbors.push(Coordinate { x, y: y - 1 });
}
//bottom
neighbors.push(Coordinate { x, y: y + 1 });
}

neighbors
}

pub fn neighboring_symbol(&self, symbols: &HashMap<Coordinate, Symbol>) -> Option<Symbol> {
for neighbor in self.neighbors() {
if let Some(symbol) = symbols.get(&neighbor) {
return Some(symbol.clone());
}
}
None
}
}

1 change: 1 addition & 0 deletions 2023-rust/src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod input_03;
1 change: 1 addition & 0 deletions 2023-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod day;
pub mod input;
pub mod template;

pub use day::*;

0 comments on commit cf2e481

Please sign in to comment.