Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/advent-of-code-2021
Path: blob/main/src/day02.rs
97 views
1
pub fn run() {
2
part1();
3
part2();
4
}
5
6
fn part1() {
7
let mut pos = 0;
8
let mut depth = 0;
9
for (command, value) in parse_commands() {
10
match command {
11
"forward" => pos += value,
12
"down" => depth += value,
13
"up" => depth -= value,
14
_ => unreachable!(),
15
}
16
}
17
println!("{}", pos * depth)
18
}
19
20
fn part2() {
21
let mut pos = 0;
22
let mut depth = 0;
23
let mut aim = 0;
24
for (command, value) in parse_commands() {
25
match command {
26
"forward" => {
27
pos += value;
28
depth += aim * value
29
}
30
"down" => aim += value,
31
"up" => aim -= value,
32
_ => unreachable!(),
33
}
34
}
35
println!("{}", pos * depth)
36
}
37
38
fn parse_commands() -> Vec<(&'static str, i32)> {
39
include_str!("inputs/day02")
40
.lines()
41
.map(|line: &str| {
42
let (command, value) = line.split_once(" ").unwrap();
43
(command, value.parse().unwrap())
44
})
45
.collect()
46
}
47
48