Day 1
This commit is contained in:
		
						commit
						8350d5d70b
					
				
					 5 changed files with 2051 additions and 0 deletions
				
			
		
							
								
								
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
/target
 | 
			
		||||
Cargo.lock
 | 
			
		||||
							
								
								
									
										8
									
								
								Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Cargo.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
[package]
 | 
			
		||||
name = "aoc2021"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2021"
 | 
			
		||||
 | 
			
		||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
							
								
								
									
										2000
									
								
								input/01a.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2000
									
								
								input/01a.txt
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										33
									
								
								src/bin/01.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/bin/01.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
const INPUT: &str = include_str!("../../input/01a.txt");
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let nums = input();
 | 
			
		||||
    let count = count_increases(nums);
 | 
			
		||||
    println!("First solution: {}", count);
 | 
			
		||||
 | 
			
		||||
    let input: Vec<i32> = input().collect();
 | 
			
		||||
    let nums = input.windows(3).map(|w| w.iter().sum());
 | 
			
		||||
    let count = count_increases(nums);
 | 
			
		||||
    println!("Second solution: {}", count);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn count_increases(it: impl Iterator<Item = i32>) -> usize {
 | 
			
		||||
    let mut count = 0;
 | 
			
		||||
    let mut prev = None;
 | 
			
		||||
 | 
			
		||||
    for n in it {
 | 
			
		||||
        if let Some(prev) = prev {
 | 
			
		||||
            if n > prev {
 | 
			
		||||
                count += 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        prev = Some(n);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn input() -> impl Iterator<Item = i32> {
 | 
			
		||||
    INPUT.lines().map(|l| l.parse::<i32>().unwrap())
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								src/lib.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/lib.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn it_works() {
 | 
			
		||||
        let result = 2 + 2;
 | 
			
		||||
        assert_eq!(result, 4);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue