Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ziegfried committed Dec 18, 2023
1 parent 25b9fc5 commit 6afa20a
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions 2023/day18/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Problem: https://adventofcode.com/2023/day/18

use std::collections::HashSet;

use itertools::Itertools;
use itertools::{Itertools, MinMaxResult};
use sscanf::sscanf;
use std::collections::HashSet;

type Result = usize;
type Result = i64;
type Point = (i64, i64);
type Instruction = (Dir, i64, String);
type Input = Vec<Instruction>;

#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Dir {
Expand All @@ -27,10 +28,6 @@ impl Dir {
}
use Dir::*;

type Instruction = (Dir, i64, String);

type Input = Vec<Instruction>;

fn parse_input(input: &str) -> Input {
input
.trim()
Expand All @@ -54,17 +51,17 @@ fn parse_input(input: &str) -> Input {

// ------------------------------------------

fn full_surface(map: &HashSet<Point>) -> usize {
let (min_r, max_r) = match map.iter().map(|(r, _)| r).minmax() {
itertools::MinMaxResult::NoElements => panic!(),
itertools::MinMaxResult::OneElement(v) => (*v, *v),
itertools::MinMaxResult::MinMax(min, max) => (*min, *max),
};
let (min_c, max_c) = match map.iter().map(|(_, c)| c).minmax() {
fn unwrap_minmax<T: Copy>(mmr: MinMaxResult<&T>) -> (T, T) {
match mmr {
itertools::MinMaxResult::NoElements => panic!(),
itertools::MinMaxResult::OneElement(v) => (*v, *v),
itertools::MinMaxResult::MinMax(min, max) => (*min, *max),
};
}
}

fn full_surface(map: &HashSet<Point>) -> usize {
let (min_r, max_r) = unwrap_minmax(map.iter().map(|(r, _)| r).minmax());
let (min_c, max_c) = unwrap_minmax(map.iter().map(|(_, c)| c).minmax());
let mut fill_map = HashSet::new();
let mut queue = vec![(min_r - 1, min_c - 1)];
let lines = &((min_r - 1)..=(max_r + 1));
Expand Down Expand Up @@ -104,13 +101,12 @@ fn part1(input: &Input) -> Result {
}
}

full_surface(&map)
full_surface(&map) as i64
}

#[test]
fn test_part1() {
let input = parse_input(include_str!("test.txt"));
dbg!(&input);
assert_eq!(part1(&input), 62);
}

Expand Down Expand Up @@ -148,7 +144,7 @@ fn part2(input: &Input) -> Result {
map.push(cur);
}
}
showlace(&map) as usize
showlace(&map)
}

#[test]
Expand Down

0 comments on commit 6afa20a

Please sign in to comment.