1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
Florent DENEF
17740888b5 Adding prob7, unfinished. 2023-03-02 10:33:47 +01:00
Florent DENEF
ce2778a09f Simple reformat 2023-03-02 10:33:35 +01:00
4 changed files with 1052 additions and 15 deletions

1027
prob7.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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;

View File

@ -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
View 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)
}