Skip to content

Commit

Permalink
d18p2 - Example
Browse files Browse the repository at this point in the history
  • Loading branch information
jortrr committed Jul 27, 2024
1 parent 11572b4 commit 5fa7fe6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
48 changes: 39 additions & 9 deletions src/day18.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hex_color::HexColor;
mod grid;
mod macros;

type Int = i32;
type Int = i64;

#[derive(Debug)]
struct Point {
Expand Down Expand Up @@ -108,9 +108,16 @@ impl Polygon {
if self.points.is_empty() {
0
} else {
let b = self.points.len() as Int;
let points: Vec<_> = self.points.iter().chain(once(&self.points[0])).collect();
let mut area = 0;
let b = {
let mut sum = 0;
for i in 0..points.len() - 1 {
let (p_0, p_1) = (points[i].point, points[i + 1].point);
sum += p_0.distance_to(&p_1) as Int;
}
sum
};
let mut area: Int = 0;
for i in 0..self.points.len() {
let (p_0, p_1) = (points[i].point, points[i + 1].point);
area += p_0.x * p_1.y - p_1.x * p_0.y;
Expand All @@ -123,11 +130,11 @@ impl Polygon {
}
}

fn from_dig_plan(dig_plan: Vec<String>) -> Polygon {
fn from_dig_plan(dig_plan: &Vec<String>, swapped: bool) -> Polygon {
let mut polygon: Polygon = Polygon::new();
let mut p = grid::Point::new(0, 0);
for op in dig_plan {
let (d, l, c) = {
let (mut d, mut l, c) = {
let mut i = op.split_whitespace();
(
i.next().unwrap(),
Expand All @@ -136,16 +143,33 @@ impl Polygon {
)
};
let c = &c[1..c.len() - 1].to_string();
if swapped {
let hex_l = &c[1..6];
let hex_d = &c[6..7];
l = Int::from_str_radix(hex_l, 16).unwrap();
d = match hex_d {
"0" => "R",
"1" => "D",
"2" => "L",
"3" => "U",
_ => panic!("Invalid hex direction: '{}'", hex_d),
};
}
let direction = match d {
"R" => East,
"D" => South,
"U" => North,
"L" => West,
_ => panic!("Invalid direction: '{}'", d),
};
for _ in 0..l {
if swapped {
polygon.points.push(Point::new(p, c.clone()));
p = p.move_to(&direction);
p = p.move_distance(&direction, l);
} else {
for _ in 0..l {
polygon.points.push(Point::new(p, c.clone()));
p = p.move_to(&direction);
}
}
}
debug!(false, "{:?}", &polygon);
Expand Down Expand Up @@ -173,12 +197,18 @@ fn main() {
"L 2 (#015232)",
"U 2 (#7a21e3)",
];
let polygon = Polygon::from_dig_plan(dig_plan);
let polygon = Polygon::from_dig_plan(&dig_plan, false);
dbg!(&polygon);
polygon.print();
let a = polygon.calc_area();
test!(62, a);
// Part 1
let polygon = Polygon::from_dig_plan(aoc::get(2023, 18));
let polygon = Polygon::from_dig_plan(&aoc::get(2023, 18), false);
let a = polygon.calc_area();
test!(48652, a);
// Part 2 - Example
let polygon = Polygon::from_dig_plan(&dig_plan, true);
//dbg!(&polygon);
let a = polygon.calc_area();
test!(952408144115 as i64, a);
}
18 changes: 13 additions & 5 deletions src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::fmt::Display;

pub type Int = i32;
pub type Int = i64;

#[derive(Debug)]
pub struct Grid<T> {
Expand Down Expand Up @@ -162,21 +162,25 @@ impl Display for Point {

impl Point {
pub fn move_to(&self, direction: &Direction) -> Point {
self.move_distance(direction, 1)
}

pub fn move_distance(&self, direction: &Direction, distance: Int) -> Point {
match direction {
North => Point {
x: self.x,
y: self.y - 1,
y: self.y - distance,
},
East => Point {
x: self.x + 1,
x: self.x + distance,
y: self.y,
},
South => Point {
x: self.x,
y: self.y + 1,
y: self.y + distance,
},
West => Point {
x: self.x - 1,
x: self.x - distance,
y: self.y,
},
}
Expand Down Expand Up @@ -228,4 +232,8 @@ impl Point {
self.y += y;
self
}

pub fn distance_to(&self, other: &Point) -> f64 {
(((self.x - other.x).pow(2) + (self.y - other.y).pow(2)) as f64).sqrt()
}
}

0 comments on commit 5fa7fe6

Please sign in to comment.