Compare commits
	
		
			2 commits
		
	
	
		
			
				58e7eb909e
			
			...
			
				17740888b5
			
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
							 | 
						17740888b5 | ||
| 
							 | 
						ce2778a09f | 
					 4 changed files with 1052 additions and 15 deletions
				
			
		| 
						 | 
				
			
			@ -1,8 +1,7 @@
 | 
			
		|||
extern crate core;
 | 
			
		||||
 | 
			
		||||
pub mod prob1;
 | 
			
		||||
pub mod prob2;
 | 
			
		||||
pub mod prob3;
 | 
			
		||||
pub mod prob4;
 | 
			
		||||
pub mod prob5;
 | 
			
		||||
pub mod prob6;
 | 
			
		||||
pub mod prob6;
 | 
			
		||||
pub mod prob7;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,37 +11,31 @@ pub fn complete_overlap(first_pair: &[u32], second_pair: &[u32]) -> bool {
 | 
			
		|||
 | 
			
		||||
pub fn solve(file_path: &str, unary_operator: &dyn Fn(&[u32], &[u32]) -> bool) -> i32 {
 | 
			
		||||
    std::fs::read_to_string(file_path)
 | 
			
		||||
        .expect("Please provide a text file as an argument.")
 | 
			
		||||
        .unwrap_or_else(|err| panic!("Please provide a text file as an argument: {err}"))
 | 
			
		||||
        .lines()
 | 
			
		||||
        .map(|line| {
 | 
			
		||||
            let tmp = line.split(',').collect::<Vec<&str>>();
 | 
			
		||||
            let first_pair = tmp
 | 
			
		||||
                .first()
 | 
			
		||||
                .expect("First character error")
 | 
			
		||||
                .unwrap_or_else(|| panic!("First character error"))
 | 
			
		||||
                .split('-')
 | 
			
		||||
                .map(|first_split| {
 | 
			
		||||
                    first_split
 | 
			
		||||
                        .parse::<u32>()
 | 
			
		||||
                        .expect("Parsing number in first pair failed")
 | 
			
		||||
                        .unwrap_or_else(|err| panic!("Parsing number in first pair failed: {err}"))
 | 
			
		||||
                })
 | 
			
		||||
                .collect::<Vec<u32>>();
 | 
			
		||||
            let second_pair = tmp
 | 
			
		||||
                .last()
 | 
			
		||||
                .expect("First character error")
 | 
			
		||||
                .unwrap_or_else(|| panic!("First character error"))
 | 
			
		||||
                .split('-')
 | 
			
		||||
                .map(|first_split| {
 | 
			
		||||
                    first_split
 | 
			
		||||
                        .parse::<u32>()
 | 
			
		||||
                        .expect("Parsing number in first pair failed")
 | 
			
		||||
                        .unwrap_or_else(|err| panic!("Parsing number in first pair failed: {err}"))
 | 
			
		||||
                })
 | 
			
		||||
                .collect::<Vec<u32>>();
 | 
			
		||||
            if unary_operator(&first_pair, &second_pair) {
 | 
			
		||||
                1
 | 
			
		||||
            } else {
 | 
			
		||||
                0
 | 
			
		||||
            }
 | 
			
		||||
            i32::from(unary_operator(&first_pair, &second_pair))
 | 
			
		||||
        })
 | 
			
		||||
        .collect::<Vec<i32>>()
 | 
			
		||||
        .iter()
 | 
			
		||||
        .sum()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								src/prob7.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/prob7.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
use nom::{branch::alt, bytes::complete::tag, combinator::map, IResult};
 | 
			
		||||
 | 
			
		||||
use crate::prob7::Instructions::{Cd, Dir, Ls};
 | 
			
		||||
 | 
			
		||||
enum Instructions {
 | 
			
		||||
    Ls,
 | 
			
		||||
    Cd,
 | 
			
		||||
    Dir,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn parse_instruction(i: &str) -> IResult<&str, Instructions> {
 | 
			
		||||
    alt((
 | 
			
		||||
        map(tag("ls"), |_| Ls),
 | 
			
		||||
        map(tag("cd"), |_| Cd),
 | 
			
		||||
        map(tag("dir"), |_| Dir),
 | 
			
		||||
    ))(i)
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue