Compare commits
No commits in common. "17740888b585e7dd98c835500a46d394454886f5" and "58e7eb909e2373f229ab6f45b59cf10469bcd1b4" have entirely different histories.
17740888b5
...
58e7eb909e
4 changed files with 15 additions and 1052 deletions
|
@ -1,7 +1,8 @@
|
||||||
|
extern crate core;
|
||||||
|
|
||||||
pub mod prob1;
|
pub mod prob1;
|
||||||
pub mod prob2;
|
pub mod prob2;
|
||||||
pub mod prob3;
|
pub mod prob3;
|
||||||
pub mod prob4;
|
pub mod prob4;
|
||||||
pub mod prob5;
|
pub mod prob5;
|
||||||
pub mod prob6;
|
pub mod prob6;
|
||||||
pub mod prob7;
|
|
||||||
|
|
|
@ -11,31 +11,37 @@ 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 {
|
pub fn solve(file_path: &str, unary_operator: &dyn Fn(&[u32], &[u32]) -> bool) -> i32 {
|
||||||
std::fs::read_to_string(file_path)
|
std::fs::read_to_string(file_path)
|
||||||
.unwrap_or_else(|err| panic!("Please provide a text file as an argument: {err}"))
|
.expect("Please provide a text file as an argument.")
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let tmp = line.split(',').collect::<Vec<&str>>();
|
let tmp = line.split(',').collect::<Vec<&str>>();
|
||||||
let first_pair = tmp
|
let first_pair = tmp
|
||||||
.first()
|
.first()
|
||||||
.unwrap_or_else(|| panic!("First character error"))
|
.expect("First character error")
|
||||||
.split('-')
|
.split('-')
|
||||||
.map(|first_split| {
|
.map(|first_split| {
|
||||||
first_split
|
first_split
|
||||||
.parse::<u32>()
|
.parse::<u32>()
|
||||||
.unwrap_or_else(|err| panic!("Parsing number in first pair failed: {err}"))
|
.expect("Parsing number in first pair failed")
|
||||||
})
|
})
|
||||||
.collect::<Vec<u32>>();
|
.collect::<Vec<u32>>();
|
||||||
let second_pair = tmp
|
let second_pair = tmp
|
||||||
.last()
|
.last()
|
||||||
.unwrap_or_else(|| panic!("First character error"))
|
.expect("First character error")
|
||||||
.split('-')
|
.split('-')
|
||||||
.map(|first_split| {
|
.map(|first_split| {
|
||||||
first_split
|
first_split
|
||||||
.parse::<u32>()
|
.parse::<u32>()
|
||||||
.unwrap_or_else(|err| panic!("Parsing number in first pair failed: {err}"))
|
.expect("Parsing number in first pair failed")
|
||||||
})
|
})
|
||||||
.collect::<Vec<u32>>();
|
.collect::<Vec<u32>>();
|
||||||
i32::from(unary_operator(&first_pair, &second_pair))
|
if unary_operator(&first_pair, &second_pair) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
.collect::<Vec<i32>>()
|
||||||
|
.iter()
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
17
src/prob7.rs
17
src/prob7.rs
|
@ -1,17 +0,0 @@
|
||||||
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