-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.rs
140 lines (125 loc) · 3.38 KB
/
day1.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// vi: set shiftwidth=4 tabstop=4 expandtab:
use common::input::check_answer;
use common::input::get_answers;
use common::input::get_first_line_from_file;
use core::str::FromStr;
use std::collections::HashSet;
use std::time::Instant;
const INPUT_FILEPATH: &str = "../resources/year2016_day1_input.txt";
const ANSWERS_FILEPATH: &str = "../resources/year2016_day1_answer.txt";
type Int = i32;
#[derive(Debug, PartialEq)]
enum Turn {
Left,
Right,
}
impl FromStr for Turn {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"L" => Ok(Self::Left),
"R" => Ok(Self::Right),
_ => Err(()),
}
}
}
#[derive(Debug, PartialEq)]
struct Action {
turn: Turn,
length: Int,
}
impl FromStr for Action {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut char_it = s.chars();
let first_char = char_it.next();
match first_char {
Some(c) => Ok(Self {
turn: c.to_string().parse().map_err(|()| {})?,
length: char_it.collect::<String>().parse().map_err(|_| {})?,
}),
None => Err(()),
}
}
}
type InputContent = Vec<Action>;
fn get_input_from_str(string: &str) -> InputContent {
string
.split(", ")
.map(|s| s.parse::<Action>().unwrap())
.collect()
}
// TODO: Define logic in https://doc.rust-lang.org/std/iter/fn.from_fn.html ?
fn part1(arg: &InputContent) -> Int {
let mut x: Int = 0;
let mut y: Int = 0;
let mut dx: Int = 0;
let mut dy: Int = 1;
for Action { turn, length } in arg {
match turn {
Turn::Left => (dx, dy) = (-dy, dx),
Turn::Right => (dx, dy) = (dy, -dx),
};
x += dx * length;
y += dy * length;
}
x.abs() + y.abs()
}
fn part2(arg: &InputContent) -> Int {
let mut x: Int = 0;
let mut y: Int = 0;
let mut dx: Int = 0;
let mut dy: Int = 1;
let mut positions = HashSet::new();
positions.insert((x, y));
for Action { turn, length } in arg {
match turn {
Turn::Left => (dx, dy) = (-dy, dx),
Turn::Right => (dx, dy) = (dy, -dx),
};
for _i in 0..*length {
x += dx;
y += dy;
if positions.contains(&(x, y)) {
return x.abs() + y.abs();
}
positions.insert((x, y));
}
}
0
}
fn main() {
let before = Instant::now();
let data = get_input_from_str(&get_first_line_from_file(INPUT_FILEPATH));
let (ans, ans2) = get_answers(ANSWERS_FILEPATH);
let solved = true;
let res = part1(&data);
check_answer(&res.to_string(), ans, solved);
let res2 = part2(&data);
check_answer(&res2.to_string(), ans2, solved);
println!("Elapsed time: {:.2?}", before.elapsed());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_action_from_str() {
assert_eq!(
Action::from_str("R5"),
Ok(Action {
turn: Turn::Right,
length: 5
})
);
}
#[test]
fn test_part1() {
const EXAMPLE: &str = "R5, L5, R5, R3";
assert_eq!(part1(&get_input_from_str(EXAMPLE)), 12);
}
#[test]
fn test_part2() {
const EXAMPLE: &str = "R8, R4, R4, R8";
assert_eq!(part2(&get_input_from_str(EXAMPLE)), 4);
}
}