-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday02.rs
42 lines (41 loc) · 1.25 KB
/
day02.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
use std::fs::File;
use std::io::{BufRead, BufReader};
pub fn part1() -> i64 {
let filename = "../input/02.txt";
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let (mut h, mut d): (i64, i64) = (0, 0);
for line in reader.lines() {
let line = line.unwrap();
let parts: Vec<_> = line.split_whitespace().collect();
let distance = parts[1].parse::<i64>().unwrap();
match parts[0] {
"forward" => h += distance,
"up" => d -= distance,
"down" => d += distance,
_ => (),
}
}
h * d
}
pub fn part2() -> i64 {
let filename = "../input/02.txt";
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let (mut h, mut d, mut aim): (i64, i64, i64) = (0, 0, 0);
for line in reader.lines() {
let line = line.unwrap();
let parts: Vec<_> = line.split_whitespace().collect();
let distance = parts[1].parse::<i64>().unwrap();
match parts[0] {
"forward" => {
h += distance;
d += distance * aim;
}
"up" => aim -= distance,
"down" => aim += distance,
_ => (),
}
}
h * d
}