Skip to content

Commit

Permalink
d18p1
Browse files Browse the repository at this point in the history
  • Loading branch information
jortrr committed Jul 27, 2024
1 parent 30a5931 commit a0bc943
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 1 deletion.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
aocf = "0.1.21"
colored = "2.1.0"
hex_color = "3.0.0"
mut-binary-heap = "0.1.0"
regex = "1.10.2"
serde_json = "1.0.120"
Expand Down
174 changes: 173 additions & 1 deletion src/day18.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,175 @@
use std::{
cmp::{max, min},
iter::once,
};

use colored::{Colorize, CustomColor};
use grid::*;
use hex_color::HexColor;

mod grid;
mod macros;

type Int = i32;

#[derive(Debug)]
struct Point {
point: grid::Point,
terrain: Terrain,
}

impl Point {
fn new(p: grid::Point, c: String) -> Point {
Point {
point: p,
terrain: Terrain::Trench(c),
}
}
}

#[derive(Debug, Clone)]
enum Terrain {
Ground,
Trench(String),
}

impl Terrain {
fn to_string(&self) -> String {
use Terrain::*;
match self {
Ground => ".".to_string(),
Trench(color) => {
let c = HexColor::parse(&color).unwrap();
"#".custom_color(CustomColor::new(c.r, c.g, c.b))
.to_string()
}
}
}
}

#[derive(Debug)]
struct Polygon {
points: Vec<Point>,
}

impl Polygon {
fn new() -> Polygon {
Polygon { points: Vec::new() }
}

fn translate(mut self, x: Int, y: Int) -> Polygon {
Polygon {
points: self
.points
.iter()
.map(|p| Point {
point: p.point.translate(x, y),
terrain: p.terrain.clone(),
})
.collect(),
}
}

fn translate_to_px_py(self) -> Polygon {
let mut min_x = Int::MAX;
let mut min_y = Int::MAX;
for p in &self.points {
min_x = min(min_x, p.point.x);
min_y = min(min_y, p.point.y);
}
self.translate(-min_x, -min_y)
}

fn print(&self) {
let mut max_x = 0;
let mut max_y = 0;
for p in &self.points {
max_x = max(max_x, p.point.x);
max_y = max(max_y, p.point.y);
}
for y in 0..=max_y {
for x in 0..=max_x {
if let Some(p) = self
.points
.iter()
.find(|p| p.point.x == x && p.point.y == y)
{
print!("{}", p.terrain.to_string());
} else {
print!("{}", Terrain::Ground.to_string())
}
}
println!();
}
}

fn calc_area(&self) -> Int {
//Shoelace formule to calculate area of 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;
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;
}
area = (area / 2).abs();
// A = i + b/2 - 1
// i = A-b/2+1
// Filled polygon = i + b
b + area - b / 2 + 1
}
}
}

fn main() {
println!("Hello, World! from src/day18.rs!");
println!("Hello, World! from src/day18.rs!");
// Part 1 - Example
let dig_plan = vec_of_strings![
"R 6 (#70c710)",
"D 5 (#0dc571)",
"L 2 (#5713f0)",
"D 2 (#d2c081)",
"R 2 (#59c680)",
"D 2 (#411b91)",
"L 5 (#8ceee2)",
"U 2 (#caa173)",
"L 1 (#1b58a2)",
"U 2 (#caa171)",
"R 2 (#7807d2)",
"U 3 (#a77fa3)",
"L 2 (#015232)",
"U 2 (#7a21e3)",
];
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 i = op.split_whitespace();
(
i.next().unwrap(),
i.next().unwrap().parse::<Int>().unwrap(),
i.next().unwrap(),
)
};
let c = &c[1..c.len() - 1].to_string();
let direction = match d {
"R" => East,
"D" => South,
"U" => North,
"L" => West,
_ => panic!("Invalid direction: '{}'", d),
};
for i in 0..l {
polygon.points.push(Point::new(p, c.clone()));
p = p.move_to(&direction);
}
}
dbg!(&polygon);
polygon = polygon.translate_to_px_py();
polygon.print();
let a = polygon.calc_area();
test!(62, a);
}
6 changes: 6 additions & 0 deletions src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,10 @@ impl Point {
};
self
}

pub fn translate(mut self, x: Int, y: Int) -> Point {
self.x += x;
self.y += y;
self
}
}

0 comments on commit a0bc943

Please sign in to comment.