Skip to content

Commit

Permalink
2023-03: Solve first puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
MLNW committed Dec 3, 2023
1 parent 910d4cf commit b9ad953
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 3 deletions.
7 changes: 4 additions & 3 deletions 2023-rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `66.9µs` | `657.5µs` |
| [Day 2](./src/bin/02.rs) | `42.2µs` | `48.9µs` |
| [Day 1](./src/bin/01.rs) | `63.8µs` | `626.0µs` |
| [Day 2](./src/bin/02.rs) | `40.1µs` | `46.9µs` |
| [Day 3](./src/bin/03.rs) | `424.5µs` | `-` |

**Total: 0.82ms**
**Total: 1.20ms**
<!--- benchmarking table --->

## Usage
Expand Down
10 changes: 10 additions & 0 deletions 2023-rust/data/examples/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
138 changes: 138 additions & 0 deletions 2023-rust/src/bin/03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use std::collections::HashMap;

use regex::Regex;

advent_of_code::solution!(3);

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

#[derive(Debug)]
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 is_valid(&self, symbols: &HashMap<Coordinate, Symbol>) -> bool {
for neighbor in self.neighbors() {
if symbols.contains_key(&neighbor) {
return true;
}
}
false
}
}

#[derive(Debug)]
struct Symbol {
character: char,
}

pub fn part_one(input: &str) -> Option<u32> {
let parts = parse_engine_parts(input);
let symbols: HashMap<Coordinate, Symbol> = parse_symbols(input);

parts
.iter()
.filter(|part| part.is_valid(&symbols))
.map(|part| part.number)
.reduce(|acc, number| acc + number)
}

pub fn part_two(_input: &str) -> Option<u32> {
None
}

fn parse_engine_parts(input: &str) -> Vec<EnginePart> {
let mut parts: Vec<EnginePart> = Vec::new();
let regex = Regex::new(r"\d+").unwrap();
for (y, line) in input.lines().enumerate() {
for number_match in regex.find_iter(line) {
parts.push(EnginePart {
number: number_match.as_str().parse().unwrap(),
start: Coordinate {
x: number_match.start(),
y,
},
end: Coordinate {
x: number_match.end() - 1,
y,
},
})
}
}
parts
}

fn parse_symbols(input: &str) -> HashMap<Coordinate, Symbol> {
let mut symbols: HashMap<Coordinate, Symbol> = HashMap::new();
let regex = Regex::new(r"(\+|-|\*|/|=|\$|@|%|#|&)").unwrap();
for (y, line) in input.lines().enumerate() {
for symbol_match in regex.find_iter(line) {
symbols.insert(
Coordinate {
x: symbol_match.start(),
y,
},
Symbol {
character: symbol_match.as_str().chars().next().unwrap(),
},
);
}
}
symbols
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(4361));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}

0 comments on commit b9ad953

Please sign in to comment.