Day 17 parsing
This commit is contained in:
8
input/day17.txt
Normal file
8
input/day17.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
x=495, y=2..7
|
||||||
|
y=7, x=495..501
|
||||||
|
x=501, y=3..7
|
||||||
|
x=498, y=2..4
|
||||||
|
x=506, y=1..2
|
||||||
|
x=498, y=10..13
|
||||||
|
x=504, y=10..13
|
||||||
|
y=13, x=498..504
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
// aoc_2018::tasks::day24::task1();
|
aoc_2018::tasks::day17::task1();
|
||||||
aoc_2018::tasks::day23::task2();
|
|
||||||
}
|
}
|
||||||
|
|||||||
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 day13;
|
||||||
pub mod day14;
|
pub mod day14;
|
||||||
pub mod day15;
|
pub mod day15;
|
||||||
|
pub mod day17;
|
||||||
pub mod day20;
|
pub mod day20;
|
||||||
pub mod day22;
|
pub mod day22;
|
||||||
pub mod day23;
|
pub mod day23;
|
||||||
|
|||||||
Reference in New Issue
Block a user