Skip to content

Commit bad7f9f

Browse files
committed
formatting
1 parent 936dac9 commit bad7f9f

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

day02/src/main.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use std::fs;
22

33
enum Instr {
4-
Up(usize),
5-
Down(usize),
6-
Forward(usize)
4+
Up(usize),
5+
Down(usize),
6+
Forward(usize),
77
}
88

9-
fn to_instr(line:&str) -> Instr {
10-
let tokens:Vec<&str> = line.split_ascii_whitespace().collect();
9+
fn to_instr(line: &str) -> Instr {
10+
let tokens: Vec<&str> = line.split_ascii_whitespace().collect();
1111
match (tokens[0], tokens[1].parse::<usize>().unwrap()) {
1212
("down", num) => Instr::Down(num),
1313
("up", num) => Instr::Up(num),
1414
("forward", num) => Instr::Forward(num),
15-
_ => panic!()
15+
_ => panic!(),
1616
}
1717
}
1818

@@ -25,35 +25,37 @@ fn parse(fname: &str) -> Vec<Instr> {
2525
}
2626

2727
#[test]
28-
fn test_part1(){
28+
fn test_part1() {
2929
assert_eq!(part1(parse("test0")), 150)
3030
}
3131

3232
fn part1(instrs: Vec<Instr>) -> usize {
33-
let (h,d) = instrs
33+
let (h, d) = instrs
3434
.iter()
35-
.fold((0usize,0usize), |(horiz,depth), instr| match instr {
36-
Instr::Up(x) => (horiz,depth-x),
37-
Instr::Down(x) => (horiz, depth+x),
38-
Instr::Forward(x) => (horiz+x, depth),
35+
.fold((0usize, 0usize), |(horiz, depth), instr| match instr {
36+
Instr::Up(x) => (horiz, depth - x),
37+
Instr::Down(x) => (horiz, depth + x),
38+
Instr::Forward(x) => (horiz + x, depth),
3939
});
40-
h*d
40+
h * d
4141
}
4242

4343
#[test]
44-
fn test_part2(){
44+
fn test_part2() {
4545
assert_eq!(part2(parse("test0")), 900)
4646
}
4747

4848
fn part2(instrs: Vec<Instr>) -> usize {
49-
let (h,d, _) = instrs
50-
.iter()
51-
.fold((0usize,0usize,0usize), |(horiz,depth, aim), instr| match instr {
52-
Instr::Up(x) => (horiz, depth, aim-x),
53-
Instr::Down(x) => (horiz, depth, aim+x),
54-
Instr::Forward(x) => (horiz+x, depth+(aim*x), aim),
55-
});
56-
h*d
49+
let (h, d, _) =
50+
instrs.iter().fold(
51+
(0usize, 0usize, 0usize),
52+
|(horiz, depth, aim), instr| match instr {
53+
Instr::Up(x) => (horiz, depth, aim - x),
54+
Instr::Down(x) => (horiz, depth, aim + x),
55+
Instr::Forward(x) => (horiz + x, depth + (aim * x), aim),
56+
},
57+
);
58+
h * d
5759
}
5860

5961
fn main() {

0 commit comments

Comments
 (0)