-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd01.rs
More file actions
125 lines (101 loc) · 2.94 KB
/
d01.rs
File metadata and controls
125 lines (101 loc) · 2.94 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::collections::HashMap;
use crate::Day;
pub struct Day01 {}
impl Day for Day01 {
fn year(&self) -> u16 {
2016
}
fn day(&self) -> u8 {
1
}
fn part_one(&self) -> String {
let commands: Vec<Command> = parse_input(&self.read_default_input());
let mut facing: usize = 0;
let mut point: Point2D = Point2D::new(0, 0);
for command in commands {
update_facing(&mut facing, command.direction);
let direction = DIRECTIONS[facing];
point.x += command.amount * direction.0;
point.y += command.amount * direction.1;
}
point.taxicab_distance(Point2D::new(0, 0)).to_string()
}
fn part_two(&self) -> String {
let commands: Vec<Command> = parse_input(&self.read_default_input());
let mut facing: usize = 0;
let mut point: Point2D = Point2D::new(0, 0);
let mut pos_history: HashMap<String, bool> = HashMap::new();
'outer: for command in commands {
update_facing(&mut facing, command.direction);
let direction = DIRECTIONS[facing];
for _ in 0..command.amount {
point.x += direction.0;
point.y += direction.1;
match pos_history.get(point.hash().as_str()) {
Some(_) => {
break 'outer;
}
None => {
pos_history.insert(point.hash(), true);
continue;
}
}
}
}
point.taxicab_distance(Point2D::new(0, 0)).to_string()
}
}
const DIRECTIONS: [(i16, i16); 4] = [
// x, y
(0, 1), // N - 0
(1, 0), // E - 1
(0, -1), // S - 2
(-1, 0), // W - 3
];
fn wrap_index(desired: i16, size: i16) -> usize {
u16::try_from(((desired + size) % size) % size).unwrap() as usize
}
fn update_facing(was: &mut usize, turn: String) {
if turn == "R" {
*was = wrap_index(i16::try_from(*was).unwrap() + 1, 4);
} else if turn == "L" {
*was = wrap_index(i16::try_from(*was).unwrap() - 1, 4);
}
}
fn parse_input(input: &str) -> Vec<Command> {
input
.split(", ")
.map(Command::parse)
.collect::<Vec<Command>>()
}
#[derive(Debug)]
struct Command {
direction: String,
amount: i16,
}
impl Command {
pub fn parse(input: &str) -> Command {
let direction = &input[0..1];
let amount = input[1..].parse::<i16>().unwrap();
Command {
direction: String::from(direction),
amount,
}
}
}
#[derive(Debug)]
struct Point2D {
x: i16,
y: i16,
}
impl Point2D {
pub fn new(x: i16, y: i16) -> Point2D {
Point2D { x, y }
}
fn hash(&self) -> String {
format!("{},{}", self.x, self.y)
}
fn taxicab_distance(&self, other: Point2D) -> i16 {
(self.x - other.x).abs() + (self.y - other.y).abs()
}
}