Better version
This commit is contained in:
		
					parent
					
						
							
								edf113b698
							
						
					
				
			
			
				commit
				
					
						1e7a441ba8
					
				
			
		
					 3 changed files with 21 additions and 14 deletions
				
			
		
							
								
								
									
										1
									
								
								src/lib.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/lib.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
pub mod prob1;
 | 
			
		||||
							
								
								
									
										17
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,21 +1,10 @@
 | 
			
		|||
use _1_calorie_counting::prob1::solve;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let args: Vec<String> = std::env::args().collect();
 | 
			
		||||
    let file_path = args
 | 
			
		||||
        .get(1)
 | 
			
		||||
        .expect("Please provide an argument on command-line.");
 | 
			
		||||
    let contents =
 | 
			
		||||
        std::fs::read_to_string(file_path).expect("Please provide a text file as an argument.");
 | 
			
		||||
    let lines: Vec<&str> = contents.split("\n").collect();
 | 
			
		||||
 | 
			
		||||
    let mut max = 0;
 | 
			
		||||
    let mut accumulated = 0;
 | 
			
		||||
    lines.iter().for_each(|number| {
 | 
			
		||||
        if let Ok(yay) = number.parse::<i32>() {
 | 
			
		||||
            accumulated += yay;
 | 
			
		||||
        } else {
 | 
			
		||||
            max = max.max(accumulated);
 | 
			
		||||
            accumulated = 0;
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
    let max = solve(file_path);
 | 
			
		||||
    println!("{max}");
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										17
									
								
								src/prob1/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/prob1/mod.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
pub fn solve(file_path: &str) -> i32 {
 | 
			
		||||
    let contents =
 | 
			
		||||
        std::fs::read_to_string(file_path).expect("Please provide a text file as an argument.");
 | 
			
		||||
    let lines: Vec<&str> = contents.split("\n").collect();
 | 
			
		||||
 | 
			
		||||
    let mut max = 0;
 | 
			
		||||
    let mut accumulated = 0;
 | 
			
		||||
    lines.iter().for_each(|number| {
 | 
			
		||||
        if let Ok(yay) = number.parse::<i32>() {
 | 
			
		||||
            accumulated += yay;
 | 
			
		||||
        } else {
 | 
			
		||||
            max = max.max(accumulated);
 | 
			
		||||
            accumulated = 0;
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
    max
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue