-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf8ca73
commit aa03e4d
Showing
3 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use advent::prelude::*; | ||
|
||
fn parse_input(input: &str) -> Vec<(Vector2, Vector2)> { | ||
input | ||
.lines() | ||
.map(|line| { | ||
let re = regex!(r"(?P<dir>[URDL]) (?P<val>\d+) \(#(?P<color>[0-9a-f]{6})\)"); | ||
let caps = re.captures(line).unwrap(); | ||
|
||
let dp1 = { | ||
let d = match &caps["dir"] { | ||
"U" => UP, | ||
"R" => RIGHT, | ||
"D" => DOWN, | ||
"L" => LEFT, | ||
_ => unreachable!(), | ||
}; | ||
let v: i64 = caps["val"].parse().unwrap(); | ||
d * v | ||
}; | ||
|
||
let dp2 = { | ||
let color = i64::from_str_radix(&caps["color"], 16).unwrap(); | ||
let d = match color % 16 { | ||
0 => RIGHT, | ||
1 => DOWN, | ||
2 => LEFT, | ||
3 => UP, | ||
d => panic!("invalid direction `{d}`"), | ||
}; | ||
let v = color / 16; | ||
d * v | ||
}; | ||
|
||
(dp1, dp2) | ||
}) | ||
.collect() | ||
} | ||
|
||
fn default_input() -> Vec<(Vector2, Vector2)> { | ||
parse_input(include_input!(2023 / 18)) | ||
} | ||
|
||
const UP: Vector2 = vector![0, 1]; | ||
const RIGHT: Vector2 = vector![1, 0]; | ||
const DOWN: Vector2 = vector![0, -1]; | ||
const LEFT: Vector2 = vector![-1, 0]; | ||
|
||
fn solve<I>(plan: I) -> i64 | ||
where | ||
I: Iterator<Item = Vector2>, | ||
{ | ||
let vertices: Vec<_> = plan | ||
.into_iter() | ||
.scan(vector![0, 0], |p, dp| { | ||
*p += dp; | ||
Some(*p) | ||
}) | ||
.collect(); | ||
|
||
let mut area2 = 0; | ||
let mut perimeter = 0; | ||
for [a, b] in vertices.iter().circular_array_windows() { | ||
area2 += a.x * b.y - a.y * b.x; | ||
perimeter += (b - a).l1_norm(); | ||
} | ||
|
||
area2.abs() / 2 + (perimeter / 2) + 1 | ||
} | ||
|
||
fn part1(plan: Vec<(Vector2, Vector2)>) -> i64 { | ||
solve(plan.into_iter().map(|(dp, _)| dp)) | ||
} | ||
|
||
fn part2(plan: Vec<(Vector2, Vector2)>) -> i64 { | ||
solve(plan.into_iter().map(|(_, dp)| dp)) | ||
} | ||
|
||
fn main() { | ||
let solution = advent::new(default_input).part(part1).part(part2).build(); | ||
solution.cli() | ||
} | ||
|
||
#[test] | ||
fn example() { | ||
let input = parse_input( | ||
"\ | ||
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)", | ||
); | ||
assert_eq!(part1(input.clone()), 62); | ||
assert_eq!(part2(input), 952408144115); | ||
} | ||
|
||
#[test] | ||
fn default() { | ||
let input = default_input(); | ||
assert_eq!(part1(input.clone()), 48503); | ||
assert_eq!(part2(input), 148442153147147); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -610,3 +610,7 @@ path = "2023/16.rs" | |
[[bin]] | ||
name = "202317" | ||
path = "2023/17.rs" | ||
|
||
[[bin]] | ||
name = "202318" | ||
path = "2023/18.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters