2021 day 1 IN RUST
This commit is contained in:
		
							parent
							
								
									4572fb5517
								
							
						
					
					
						commit
						7123ea76f5
					
				
					 1 changed files with 25 additions and 0 deletions
				
			
		
							
								
								
									
										25
									
								
								2021/day01/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								2021/day01/main.rs
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
use std::fs::File;
 | 
			
		||||
use std::io::prelude::*;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let mut file = File::open("input.txt")
 | 
			
		||||
        .expect("cannot open file");
 | 
			
		||||
    let mut data = String::new();
 | 
			
		||||
    
 | 
			
		||||
    file.read_to_string(&mut data)
 | 
			
		||||
        .expect("Error while reading file");
 | 
			
		||||
 | 
			
		||||
    let depths: Vec<i32> = data.lines()
 | 
			
		||||
        .map(|s| s.parse::<i32>().unwrap())
 | 
			
		||||
        .collect();
 | 
			
		||||
 | 
			
		||||
    let mut j = 0;
 | 
			
		||||
    depths.windows(2)
 | 
			
		||||
        .for_each(|i| if i[1] > i[0] {j+=1});
 | 
			
		||||
 | 
			
		||||
    let mut k = 0;
 | 
			
		||||
    depths.windows(4)
 | 
			
		||||
        .for_each(|i| if i[1..4].iter().sum::<i32>() > i[0..3].iter().sum::<i32>() {k+=1});
 | 
			
		||||
 | 
			
		||||
    print!("Part 1: {}\nPart 2: {}", j, k);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in a new issue