-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.rs
82 lines (74 loc) · 2.11 KB
/
12.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
extern crate num_complex;
use num_complex::Complex;
use std::fs;
use std::str::FromStr;
#[derive(Debug)]
enum Command {
N(i32),
W(i32),
S(i32),
E(i32),
L(i32),
R(i32),
F(i32),
}
impl FromStr for Command {
type Err = ();
fn from_str(line: &str) -> Result<Self, Self::Err> {
// Line is of the form `<c><n>`, e.g. "N15" or "R90"
let mut line = line.to_string();
let c = line.remove(0);
let n: i32 = line.parse().unwrap();
match c {
'N' => Ok(Self::N(n)),
'W' => Ok(Self::W(n)),
'S' => Ok(Self::S(n)),
'E' => Ok(Self::E(n)),
'L' => Ok(Self::L(n)),
'R' => Ok(Self::R(n)),
'F' => Ok(Self::F(n)),
_ => panic!("Unknown command: {}", c),
}
}
}
#[doc(hidden)]
fn main() {
// Read input
let data: Vec<Command> = fs::read_to_string("input/12.txt")
.expect("Can't read input data")
.lines()
.map(|line| line.trim().parse().unwrap())
.collect();
// Solutions
let i = Complex::i();
// Part 1
let mut pos = Complex::new(0, 0);
let mut dir = Complex::new(1, 0);
for cmd in &data {
match cmd {
Command::N(n) => pos += n * i,
Command::W(n) => pos -= n,
Command::S(n) => pos -= n * i,
Command::E(n) => pos += n,
Command::L(n) => dir *= i.powi(n / 90),
Command::R(n) => dir *= i.powi(-n / 90),
Command::F(n) => pos += n * dir,
}
}
println!("Part 1: {}", pos.l1_norm());
// Part 2
let mut pos = Complex::new(0, 0);
let mut waypoint = Complex::new(10, 1);
for cmd in &data {
match cmd {
Command::N(n) => waypoint += n * i,
Command::W(n) => waypoint -= n,
Command::S(n) => waypoint -= n * i,
Command::E(n) => waypoint += n,
Command::L(n) => waypoint *= i.powi(n / 90),
Command::R(n) => waypoint *= i.powi(-n / 90),
Command::F(n) => pos += n * waypoint,
}
}
println!("Part 2: {}", pos.l1_norm());
}