-
Notifications
You must be signed in to change notification settings - Fork 5
/
day01.rs
53 lines (44 loc) · 1.44 KB
/
day01.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! # No Time for a Taxicab
//!
//! The solution is short as it leverages three utility classes, [`hash`] for speedy sets,
//! [`parse`] for extracting integers from surrounding text and [`point`] for two dimensional
//! rotations and translations.
//!
//! [`hash`]: crate::util::hash
//! [`parse`]: crate::util::parse
//! [`point`]: crate::util::point
use crate::util::hash::*;
use crate::util::parse::*;
use crate::util::point::*;
type Pair = (u8, i32);
pub fn parse(input: &str) -> Vec<Pair> {
let first = input.bytes().filter(u8::is_ascii_uppercase);
let second = input.iter_signed();
first.zip(second).collect()
}
pub fn part1(input: &[Pair]) -> i32 {
let mut position = ORIGIN;
let mut direction = UP;
for &(turn, amount) in input {
direction =
if turn == b'L' { direction.counter_clockwise() } else { direction.clockwise() };
position += direction * amount;
}
position.manhattan(ORIGIN)
}
pub fn part2(input: &[Pair]) -> i32 {
let mut position = ORIGIN;
let mut direction = UP;
let mut visited = FastSet::with_capacity(1000);
for &(turn, amount) in input {
direction =
if turn == b'L' { direction.counter_clockwise() } else { direction.clockwise() };
for _ in 0..amount {
position += direction;
if !visited.insert(position) {
return position.manhattan(ORIGIN);
}
}
}
unreachable!()
}