Skip to content

Commit cbc9dc6

Browse files
committed
flatten
1 parent 2f246c0 commit cbc9dc6

File tree

4 files changed

+35
-55
lines changed

4 files changed

+35
-55
lines changed

src/main.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use std::{env, time::SystemTime};
2-
31
mod helpers;
42
mod solutions;
53

64
fn main() {
7-
let time = SystemTime::now();
5+
let time = std::time::SystemTime::now();
86

97
// reading command line arguments
10-
let args: Vec<String> = env::args().collect();
8+
let args: Vec<String> = std::env::args().collect();
119

1210
if args.len() == 1 {
1311
// if no arguments given, run all available solutions
@@ -17,10 +15,9 @@ fn main() {
1715
} else {
1816
// if arguments are given, run the respective solutions
1917
args.into_iter().skip(1).for_each(|arg| {
20-
let day = arg
21-
.parse()
22-
.unwrap_or_else(|_| panic!("not able to parse command line argument {arg}"));
23-
helpers::solve_day(day);
18+
if let Ok(day) = arg.parse() {
19+
helpers::solve_day(day);
20+
}
2421
})
2522
}
2623

src/solutions/day01.rs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ pub fn pt1(filename: &str) -> (u32, u32) {
1212
let mut biggest = 0;
1313
let mut current = 0;
1414

15-
// iterating through each line of the file
16-
read_lines(filename).for_each(|line| {
17-
if let Ok(line) = line {
18-
match line.as_str() {
19-
// if empty line, compare current block with biggest found until then
20-
"" => {
21-
biggest = biggest.max(current);
22-
current = 0;
23-
}
24-
// if number, continue the sum of current block
25-
_ => current += line.parse::<u32>().expect("unable to parse line {line}"),
15+
read_lines(filename).flatten().for_each(|line| {
16+
match line.as_str() {
17+
// if empty line, compare current block with biggest found until then
18+
"" => {
19+
biggest = biggest.max(current);
20+
current = 0;
2621
}
22+
// if number, continue the sum of current block
23+
_ => current += line.parse::<u32>().expect("unable to parse line {line}"),
2724
}
2825
});
2926

@@ -43,26 +40,24 @@ pub fn pt2(filename: &str) -> (u32, u32) {
4340
let mut current = 0;
4441

4542
// iterating through each line of the file
46-
read_lines(filename).for_each(|line| {
47-
if let Ok(line) = line {
48-
match line.as_str() {
49-
// if empty line, compare current block with 3 biggest known
50-
"" => {
51-
if current > biggest[0] {
52-
biggest[2] = biggest[1];
53-
biggest[1] = biggest[0];
54-
biggest[0] = current;
55-
} else if current > biggest[1] {
56-
biggest[2] = biggest[1];
57-
biggest[1] = current;
58-
} else if current > biggest[2] {
59-
biggest[2] = current;
60-
}
61-
current = 0;
43+
read_lines(filename).flatten().for_each(|line| {
44+
match line.as_str() {
45+
// if empty line, compare current block with 3 biggest known
46+
"" => {
47+
if current > biggest[0] {
48+
biggest[2] = biggest[1];
49+
biggest[1] = biggest[0];
50+
biggest[0] = current;
51+
} else if current > biggest[1] {
52+
biggest[2] = biggest[1];
53+
biggest[1] = current;
54+
} else if current > biggest[2] {
55+
biggest[2] = current;
6256
}
63-
// if number, continue the sum of current block
64-
_ => current += line.parse::<u32>().expect("unable to parse line {line}"),
57+
current = 0;
6558
}
59+
// if number, continue the sum of current block
60+
_ => current += line.parse::<u32>().expect("unable to parse line {line}"),
6661
}
6762
});
6863

src/solutions/day02.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@ use {crate::helpers::read_lines, std::time::SystemTime};
99
pub fn pt1(filename: &str) -> (u32, u32) {
1010
let time = SystemTime::now();
1111

12-
// iterating through each line
1312
let answer = read_lines(filename)
14-
// getting a `RoundMoves` out of each line and calculating its `total_points`
15-
.map(|line| match line {
16-
Ok(line) => RoundMoves::from_line(line).total_points(),
17-
_ => panic!("unable to read line"),
18-
})
19-
// getting the sum of all `total_points`
13+
.flatten()
14+
.map(|line| RoundMoves::from_line(line).total_points())
2015
.sum();
2116

2217
let time = time.elapsed().unwrap().as_millis() as u32;
@@ -27,14 +22,9 @@ pub fn pt1(filename: &str) -> (u32, u32) {
2722
pub fn pt2(filename: &str) -> (u32, u32) {
2823
let time = SystemTime::now();
2924

30-
// iterating through each line
3125
let answer = read_lines(filename)
32-
// getting a `RoundMoves` out of each line and calculating its `total_points`
33-
.map(|line| match line {
34-
Ok(line) => RoundOutcome::from_line(line).total_points(),
35-
_ => panic!("unable to read line"),
36-
})
37-
// getting the sum of all `total_points`
26+
.flatten()
27+
.map(|line| RoundOutcome::from_line(line).total_points())
3828
.sum();
3929

4030
let time = time.elapsed().unwrap().as_millis() as u32;

src/solutions/day03.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ pub fn pt1(filename: &str) -> (u32, u32) {
1010
let time = SystemTime::now();
1111

1212
let answer = read_lines(filename)
13-
.map(|line| match line {
14-
Ok(line) => Rucksack::from_line(line).value_of_common(),
15-
_ => panic!("unable to read line"),
16-
})
13+
.flatten()
14+
.map(|line| Rucksack::from_line(line).value_of_common())
1715
.sum();
1816

1917
let time = time.elapsed().unwrap().as_millis() as u32;

0 commit comments

Comments
 (0)