Skip to content

Commit 920aa7e

Browse files
committed
Cleaning and
1 parent 18f87c4 commit 920aa7e

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/bin/02.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ pub fn part_one(input: &str) -> Option<u32> {
1212
// }
1313
// }).sum();
1414

15-
1615
for line in input.lines() {
1716
// println!("{}", line);
1817

19-
let numbers: Vec<i32> = line.split_whitespace().filter_map(|item| {
20-
item.parse::<i32>().ok()
21-
}).collect();
18+
let numbers: Vec<i32> = line
19+
.split_whitespace()
20+
.filter_map(|item| item.parse::<i32>().ok())
21+
.collect();
2222

2323
if checking_level(&numbers) {
2424
safe_count += 1;
@@ -29,7 +29,7 @@ pub fn part_one(input: &str) -> Option<u32> {
2929
Some(safe_count as u32)
3030
}
3131

32-
fn checking_level(numbers: &Vec<i32>) -> bool {
32+
fn checking_level(numbers: &[i32]) -> bool {
3333
let mut is_increasing = true;
3434
let mut is_decreasing = true;
3535

@@ -42,18 +42,12 @@ fn checking_level(numbers: &Vec<i32>) -> bool {
4242

4343
if !(1..=3).contains(&diff) {
4444
//unsafe
45-
return false
45+
return false;
4646
}
4747

48-
if curr < next {
49-
//decreasing
50-
is_decreasing = false;
51-
} else if curr > next {
52-
//increas
53-
is_increasing= false;
54-
} else {
55-
// nochagning
56-
return false;
48+
match curr < next {
49+
true => is_decreasing = false,
50+
false => is_increasing = false,
5751
}
5852
}
5953
is_increasing || is_decreasing
@@ -65,9 +59,10 @@ pub fn part_two(input: &str) -> Option<u32> {
6559
for line in input.lines() {
6660
// println!("{}", line);
6761

68-
let numbers: Vec<i32> = line.split_whitespace().map(|item| {
69-
item.parse::<i32>().unwrap()
70-
}).collect();
62+
let numbers: Vec<i32> = line
63+
.split_whitespace()
64+
.map(|item| item.parse::<i32>().unwrap())
65+
.collect();
7166

7267
if checking_level(&numbers) {
7368
safe_count += 1;
@@ -77,7 +72,6 @@ pub fn part_two(input: &str) -> Option<u32> {
7772
let mut cleaned_level = numbers.to_vec();
7873
cleaned_level.remove(i);
7974

80-
8175
if checking_level(&cleaned_level) {
8276
safe_count += 1;
8377
break;

src/bin/03.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub fn part_two(input: &str) -> Option<u32> {
3030
let pattern = Regex::new(r"mul\((\d+),(\d+)\)|don't\(\)|do\(\)").unwrap();
3131
let mut is_enabled = true;
3232

33-
let result = pattern.captures_iter(input)
33+
let result = pattern
34+
.captures_iter(input)
3435
.filter_map(|captures| {
3536
if let (Some(x), Some(y)) = (captures.get(1), captures.get(2)) {
3637
if is_enabled {
@@ -66,7 +67,9 @@ mod tests {
6667

6768
#[test]
6869
fn test_part_two() {
69-
let result = part_two(&advent_of_code::template::read_file_part("examples", DAY, 2));
70+
let result = part_two(&advent_of_code::template::read_file_part(
71+
"examples", DAY, 2,
72+
));
7073
assert_eq!(result, Some(48));
7174
}
7275
}

0 commit comments

Comments
 (0)