Day 17 stuff

This commit is contained in:
2023-05-26 10:02:17 +02:00
parent 5c4e9a9e9d
commit b922b808fc

View File

@@ -2,8 +2,9 @@ use itertools::Itertools;
use regex::Regex; use regex::Regex;
use crate::utils; use crate::utils;
use std::collections::{HashMap, HashSet}; use std::collections::HashMap;
use Tile::*;
fn horizontal(line: &str) -> Option<(i32, i32, i32)> { 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(); let regex = Regex::new(r"y=(?P<y>\d+), x=(?P<x1>\d+)..(?P<x2>\d+)").unwrap();
regex.captures(line).map(|m| { regex.captures(line).map(|m| {
@@ -27,36 +28,36 @@ fn vertical(line: &str) -> Option<(i32, i32, i32)> {
} }
pub fn task1() { pub fn task1() {
let input = utils::read_file("input/day17.txt"); let input = utils::read_file("input/day17.txt");
let clay_tiles: HashSet<Pos> = input.lines().fold(HashSet::new(), |mut tiles, line| { let clay_tiles: HashMap<Pos, Tile> = input.lines().fold(HashMap::new(), |mut tiles, line| {
if let Some((x, y1, y2)) = vertical(line) { if let Some((x, y1, y2)) = vertical(line) {
for y in y1..=y2 { for y in y1..=y2 {
tiles.insert(Pos(x, y)); tiles.insert(Pos(x, y), Clay);
} }
} }
if let Some((y, x1, x2)) = horizontal(line) { if let Some((y, x1, x2)) = horizontal(line) {
for x in x1..=x2 { for x in x1..=x2 {
tiles.insert(Pos(x, y)); tiles.insert(Pos(x, y), Clay);
} }
} }
tiles tiles
}); });
let (x_min, x_max) = clay_tiles let (x_min, x_max) = clay_tiles
.iter() .keys()
.map(|p| p.0) .map(|p| p.0)
.minmax() .minmax()
.into_option() .into_option()
.unwrap(); .unwrap();
let (y_min, y_max) = clay_tiles let (y_min, y_max) = clay_tiles
.iter() .keys()
.map(|p| p.1) .map(|p| p.1)
.minmax() .minmax()
.into_option() .into_option()
.unwrap(); .unwrap();
}
dbg!(x_min); enum Tile {
dbg!(x_max); Clay,
dbg!(y_min); Water,
dbg!(y_max);
} }
#[derive(PartialEq, Eq, Hash)] #[derive(PartialEq, Eq, Hash)]