Skip to content

Commit

Permalink
speed improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
majcn committed Dec 18, 2023
1 parent a5b3ecf commit b40e6ae
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/bin/18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ where
D: Fn(&Dig) -> Direction,
L: Fn(&Dig) -> u32,
{
let mut trench = 0;
let mut polygon = vec![(0, 0)];
for dig in data {
let x = polygon[polygon.len() - 1].0;
let y = polygon[polygon.len() - 1].1;

for i in 1..=get_length(dig) {
let next_location = match get_direction(dig) {
Direction::Left => (x - i as i64, y),
Direction::Right => (x + i as i64, y),
Direction::Up => (x, y - i as i64),
Direction::Down => (x, y + i as i64),
};
let n = get_length(dig) as i64;
let next_location = match get_direction(dig) {
Direction::Left => (x - n, y),
Direction::Right => (x + n, y),
Direction::Up => (x, y - n),
Direction::Down => (x, y + n),
};

polygon.push(next_location);
}
trench += n as u64;
polygon.push(next_location);
}

// Shoelace formula
Expand All @@ -71,10 +72,10 @@ where
.unsigned_abs();

// Pick's theorem
let boundaries = polygon.len() as u64 - 1;
let boundaries = trench;
let interior_points = (area_twice / 2) - boundaries / 2 + 1;

interior_points + polygon.len() as u64 - 1
interior_points + boundaries
}

pub fn part_one(input: &str) -> Option<u64> {
Expand Down

0 comments on commit b40e6ae

Please sign in to comment.