Day 17 parsing
This commit is contained in:
74
src/tasks/day17.rs
Normal file
74
src/tasks/day17.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
|
||||
use crate::utils;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
fn horizontal(line: &str) -> Option<(i32, i32, i32)> {
|
||||
let regex = Regex::new(r"y=(?P<y>\d+), x=(?P<x1>\d+)..(?P<x2>\d+)").unwrap();
|
||||
regex.captures(line).map(|m| {
|
||||
(
|
||||
m.name("y").unwrap().as_str().parse::<i32>().unwrap(),
|
||||
m.name("x1").unwrap().as_str().parse::<i32>().unwrap(),
|
||||
m.name("x2").unwrap().as_str().parse::<i32>().unwrap(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn vertical(line: &str) -> Option<(i32, i32, i32)> {
|
||||
let regex = Regex::new(r"x=(?P<y>\d+), y=(?P<x1>\d+)..(?P<x2>\d+)").unwrap();
|
||||
regex.captures(line).map(|m| {
|
||||
(
|
||||
m.name("y").unwrap().as_str().parse::<i32>().unwrap(),
|
||||
m.name("x1").unwrap().as_str().parse::<i32>().unwrap(),
|
||||
m.name("x2").unwrap().as_str().parse::<i32>().unwrap(),
|
||||
)
|
||||
})
|
||||
}
|
||||
pub fn task1() {
|
||||
let input = utils::read_file("input/day17.txt");
|
||||
let clay_tiles: HashSet<Pos> = input.lines().fold(HashSet::new(), |mut tiles, line| {
|
||||
if let Some((x, y1, y2)) = vertical(line) {
|
||||
for y in y1..=y2 {
|
||||
tiles.insert(Pos(x, y));
|
||||
}
|
||||
}
|
||||
if let Some((y, x1, x2)) = horizontal(line) {
|
||||
for x in x1..=x2 {
|
||||
tiles.insert(Pos(x, y));
|
||||
}
|
||||
}
|
||||
tiles
|
||||
});
|
||||
let (x_min, x_max) = clay_tiles
|
||||
.iter()
|
||||
.map(|p| p.0)
|
||||
.minmax()
|
||||
.into_option()
|
||||
.unwrap();
|
||||
let (y_min, y_max) = clay_tiles
|
||||
.iter()
|
||||
.map(|p| p.1)
|
||||
.minmax()
|
||||
.into_option()
|
||||
.unwrap();
|
||||
|
||||
dbg!(x_min);
|
||||
dbg!(x_max);
|
||||
dbg!(y_min);
|
||||
dbg!(y_max);
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
struct Pos(i32, i32);
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
assert_eq!(horizontal("y=10, x=5..20"), Some((10, 5, 20)));
|
||||
assert_eq!(vertical("x=10, y=5..20"), Some((10, 5, 20)));
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ pub mod day12;
|
||||
pub mod day13;
|
||||
pub mod day14;
|
||||
pub mod day15;
|
||||
pub mod day17;
|
||||
pub mod day20;
|
||||
pub mod day22;
|
||||
pub mod day23;
|
||||
|
||||
Reference in New Issue
Block a user