Add src/06part1.rs
This commit is contained in:
		
							parent
							
								
									ef37c4c19f
								
							
						
					
					
						commit
						9292a54468
					
				
							
								
								
									
										29
									
								
								src/06part1.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/06part1.rs
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
use std::fs;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let file_path = "input/06input.txt";
 | 
			
		||||
    let contents = fs::read_to_string(file_path).expect("should read file");
 | 
			
		||||
    let mut lines = contents.lines();
 | 
			
		||||
    let time_str = lines.next().unwrap().strip_prefix("Time:      ").unwrap();
 | 
			
		||||
    let times: Vec<u128> = time_str.split_whitespace().map(|x| x.parse::<u128>().ok().unwrap()).collect();
 | 
			
		||||
    let distance_str = lines.next().unwrap().strip_prefix("Distance:  ").unwrap();
 | 
			
		||||
    let distances: Vec<u128> = distance_str.split_whitespace().map(|x| x.parse::<u128>().ok().unwrap()).collect();
 | 
			
		||||
    let mut total = 1;
 | 
			
		||||
    for i in 0..times.len() {
 | 
			
		||||
        let mut j = 0;
 | 
			
		||||
        let mut k = times[i]-1;
 | 
			
		||||
        loop {
 | 
			
		||||
            if j*(times[i]-j) <= distances[i] {
 | 
			
		||||
                j += 1;
 | 
			
		||||
            }
 | 
			
		||||
            if k*(times[i]-k) <= distances[i] {
 | 
			
		||||
                k -= 1;
 | 
			
		||||
            }
 | 
			
		||||
            if j*(times[i]-j) > distances[i] && k*(times[i]-k) > distances[i] {
 | 
			
		||||
                total *= k - j + 1;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    println!("{}", total);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user