Skip to content

Commit 8b9e7fb

Browse files
author
mflinn-broad
committed
run formatter
1 parent e74c959 commit 8b9e7fb

File tree

5 files changed

+97
-134
lines changed

5 files changed

+97
-134
lines changed

src/days/day1.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use crate::util;
22

33
fn process_input(input: String) -> Vec<i64> {
4-
input.split_whitespace()
4+
input
5+
.split_whitespace()
56
.map(|num| num.parse().unwrap())
67
.collect()
78
}
89

9-
fn part_1 (data: &Vec<i64>) -> usize {
10+
fn part_1(data: &Vec<i64>) -> usize {
1011
get_num_increasing_windows(2, data)
1112
}
1213

13-
fn part_2 (data: &Vec<i64>) -> usize {
14+
fn part_2(data: &Vec<i64>) -> usize {
1415
get_num_increasing_windows(4, data)
1516
}
1617

@@ -29,25 +30,21 @@ pub fn run() {
2930
}
3031

3132
#[cfg(test)]
32-
mod tests{
33+
mod tests {
3334
use super::*;
34-
35+
3536
extern crate test;
3637
use test::Bencher;
3738

3839
#[bench]
3940
fn bench_part_1(b: &mut Bencher) {
4041
let input = process_input(util::read_input("inputs/day1.txt").unwrap());
41-
b.iter(|| {
42-
part_1(&input)
43-
});
42+
b.iter(|| part_1(&input));
4443
}
4544

4645
#[bench]
4746
fn bench_part_2(b: &mut Bencher) {
4847
let input = process_input(util::read_input("inputs/day1.txt").unwrap());
49-
b.iter(||{
50-
part_2(&input)
51-
})
48+
b.iter(|| part_2(&input))
5249
}
5350
}

src/days/day2.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,39 @@ impl From<&str> for Direction {
1313
"forward" => Direction::Forward,
1414
"down" => Direction::Down,
1515
"up" => Direction::Up,
16-
_ => panic!()
16+
_ => panic!(),
1717
}
1818
}
1919
}
2020

2121
fn process_input(input: String) -> Vec<(Direction, i64)> {
22-
input.lines()
22+
input
23+
.lines()
2324
.map(|line| {
2425
let cell: Vec<_> = line.trim().split_whitespace().collect();
2526
(Direction::from(cell[0]), cell[1].parse().unwrap())
26-
})
27+
})
2728
.collect()
2829
}
2930

30-
fn part_1(input: &Vec<(Direction, i64)>) -> i64 {
31-
let position = input.iter()
32-
.fold((0, 0), |acc, (dir, amt) | {
33-
match dir {
34-
Direction::Forward => (acc.0 + amt, acc.1),
35-
Direction::Down => (acc.0, acc.1 + amt),
36-
Direction::Up => (acc.0, acc.1 - amt),
37-
}
38-
});
31+
fn part_1(input: &Vec<(Direction, i64)>) -> i64 {
32+
let position = input.iter().fold((0, 0), |acc, (dir, amt)| match dir {
33+
Direction::Forward => (acc.0 + amt, acc.1),
34+
Direction::Down => (acc.0, acc.1 + amt),
35+
Direction::Up => (acc.0, acc.1 - amt),
36+
});
3937
position.0 * position.1
4038
}
4139

4240
fn part_2(input: &Vec<(Direction, i64)>) -> i64 {
43-
let coordinates = input.iter()
44-
.fold((0, 0, 0), |acc, (dir, amt)| {
45-
match dir {
46-
Direction::Forward => (acc.0 + amt, acc.1 + amt * acc.2, acc.2),
47-
Direction::Down => (acc.0, acc.1, acc.2 + amt),
48-
Direction::Up => (acc.0, acc.1, acc.2 - amt),
49-
}
50-
});
41+
let coordinates = input.iter().fold((0, 0, 0), |acc, (dir, amt)| match dir {
42+
Direction::Forward => (acc.0 + amt, acc.1 + amt * acc.2, acc.2),
43+
Direction::Down => (acc.0, acc.1, acc.2 + amt),
44+
Direction::Up => (acc.0, acc.1, acc.2 - amt),
45+
});
5146
coordinates.0 * coordinates.1
5247
}
5348

54-
5549
pub fn run() {
5650
let input = util::read_input("inputs/day2.txt");
5751
let input = process_input(input.unwrap());
@@ -60,25 +54,21 @@ pub fn run() {
6054
}
6155

6256
#[cfg(test)]
63-
mod tests{
57+
mod tests {
6458
use super::*;
65-
59+
6660
extern crate test;
6761
use test::Bencher;
6862

6963
#[bench]
7064
fn bench_part_1(b: &mut Bencher) {
7165
let input = process_input(util::read_input("inputs/day2.txt").unwrap());
72-
b.iter(|| {
73-
part_1(&input)
74-
});
66+
b.iter(|| part_1(&input));
7567
}
7668

7769
#[bench]
7870
fn bench_part_2(b: &mut Bencher) {
7971
let input = process_input(util::read_input("inputs/day2.txt").unwrap());
80-
b.iter(||{
81-
part_2(&input)
82-
})
72+
b.iter(|| part_2(&input))
8373
}
8474
}

src/days/day3.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ const WIDTH: usize = 12;
44
const LENGTH: usize = 1000;
55

66
fn get_most_common_bits(nums: Vec<usize>) -> Vec<usize> {
7-
nums
8-
.iter()
9-
.fold(vec![0; WIDTH], |counts, bits| {
10-
counts
11-
.into_iter()
12-
.enumerate()
13-
.map(|(i, curr)| curr + ((bits & 1 << i) >> i))
14-
.collect()
15-
})
7+
nums.iter().fold(vec![0; WIDTH], |counts, bits| {
8+
counts
9+
.into_iter()
10+
.enumerate()
11+
.map(|(i, curr)| curr + ((bits & 1 << i) >> i))
12+
.collect()
13+
})
1614
}
1715

1816
fn part_1(input: &str) -> usize {
19-
let nums:Vec<usize> = input.lines()
17+
let nums: Vec<usize> = input
18+
.lines()
2019
.map(|line| usize::from_str_radix(line.trim(), 2).unwrap())
2120
.collect();
2221
let counts = get_most_common_bits(nums);
@@ -33,20 +32,20 @@ fn part_1(input: &str) -> usize {
3332
gamma * epsilon
3433
}
3534

36-
3735
fn part_2(input: &str) -> usize {
38-
let nums: Vec<usize> = input.lines()
36+
let nums: Vec<usize> = input
37+
.lines()
3938
.map(|line| usize::from_str_radix(line.trim(), 2).unwrap())
4039
.collect();
4140
let oxy = (0..WIDTH)
42-
.rev()
43-
.scan(nums.clone(), |oxy, i| {
44-
let one = oxy.iter().filter(|n| *n & 1 << i > 0).count() >= (oxy.len() + 1) / 2;
45-
oxy.drain_filter(|n| (*n & 1 << i > 0) != one);
46-
oxy.first().copied()
47-
})
48-
.last()
49-
.unwrap();
41+
.rev()
42+
.scan(nums.clone(), |oxy, i| {
43+
let one = oxy.iter().filter(|n| *n & 1 << i > 0).count() >= (oxy.len() + 1) / 2;
44+
oxy.drain_filter(|n| (*n & 1 << i > 0) != one);
45+
oxy.first().copied()
46+
})
47+
.last()
48+
.unwrap();
5049

5150
let co2 = (0..WIDTH)
5251
.rev()
@@ -61,33 +60,28 @@ fn part_2(input: &str) -> usize {
6160
oxy * co2
6261
}
6362

64-
6563
pub fn run() {
6664
let input = util::read_input("inputs/day3.txt").unwrap();
6765
println!("part 1: {}", part_1(&input));
6866
println!("part 2: {}", part_2(&input));
6967
}
7068

7169
#[cfg(test)]
72-
mod tests{
70+
mod tests {
7371
use super::*;
74-
72+
7573
extern crate test;
7674
use test::Bencher;
7775

7876
#[bench]
7977
fn bench_part_1(b: &mut Bencher) {
8078
let input = util::read_input("inputs/day3.txt").unwrap();
81-
b.iter(|| {
82-
part_1(&input)
83-
});
79+
b.iter(|| part_1(&input));
8480
}
8581

8682
#[bench]
8783
fn bench_part_2(b: &mut Bencher) {
8884
let input = util::read_input("inputs/day3.txt").unwrap();
89-
b.iter(||{
90-
part_2(&input)
91-
})
85+
b.iter(|| part_2(&input))
9286
}
9387
}

0 commit comments

Comments
 (0)