Day 17 parsing

This commit is contained in:
2023-05-25 23:01:57 +02:00
parent b480601ee8
commit 5c4e9a9e9d
4 changed files with 84 additions and 2 deletions

View File

@@ -1,4 +1,3 @@
fn main() {
// aoc_2018::tasks::day24::task1();
aoc_2018::tasks::day23::task2();
aoc_2018::tasks::day17::task1();
}

74
src/tasks/day17.rs Normal file
View 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)));
}
}

View File

@@ -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;