-
-
Save samueltardieu/45487d8f36184401a4faaf85de18145e to your computer and use it in GitHub Desktop.
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
fn parse(input: &str) -> impl Iterator<Item = (u8, i64, u32)> + '_ { | |
input.lines().map(|s| { | |
let mut s = s.split(&[' ', '(', '#', ')']).filter(|s| s.len() > 0); | |
( | |
s.next().unwrap().as_bytes()[0], | |
s.next().unwrap().parse().unwrap(), | |
u32::from_str_radix(s.next().unwrap(), 16).unwrap(), | |
) | |
}) | |
} | |
#[aoc(day18, part1)] | |
fn part1(input: &str) -> i64 { | |
part(parse(input)) | |
} | |
#[aoc(day18, part2)] | |
fn part2(input: &str) -> i64 { | |
part(parse(input).map(|(_, _, color)| (b"RDLU"[color as usize % 16], color as i64 / 16, 0))) | |
} | |
fn part(commands: impl Iterator<Item = (u8, i64, u32)>) -> i64 { | |
let (mut x, mut y, mut t) = (0, 0, 2); | |
let mut edges = commands | |
.map(|(dir, len, ..)| { | |
match dir { | |
b'U' => y -= len, | |
b'D' => y += len, | |
b'L' => x -= len, | |
_ => x += len, | |
}; | |
t += len; | |
(x, y) | |
}) | |
.collect::<Vec<_>>(); | |
edges.push(edges[0]); | |
(edges | |
.windows(2) | |
.map(|i| i[0].0 * i[1].1 - i[0].1 * i[1].0) | |
.sum::<i64>() | |
+ t) | |
/ 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment