-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rs
executable file
·84 lines (72 loc) · 2.04 KB
/
solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! nom = "7.1.3"
//! ```
use nom::{
character::complete::{alpha1, digit1},
combinator::map,
multi::{many0, separated_list1},
sequence::delimited,
IResult,
};
fn parse_digit_line(input: &str) -> IResult<&str, String> {
let core = separated_list1(alpha1, digit1);
let delimitor_pre = many0(alpha1);
let delimitor_post = many0(alpha1);
map(delimited(delimitor_pre, core, delimitor_post), |v| {
v.concat()
})(input)
}
fn part1(_input: &str) -> u32 {
1
}
fn part2(_input: &str) -> u32 {
2
}
type CustomizedResult<T> = Result<T, Box<dyn std::error::Error>>;
fn main() -> CustomizedResult<()> {
let args: Vec<String> = std::env::args().collect();
match &args[..] {
[_name, filename, part] => {
let input = std::fs::read_to_string(filename)?;
if part == "part1" {
println!("{}", part1(&input));
Ok(())
} else if part == "part2" {
println!("{}", part2(&input));
Ok(())
} else {
println!("Error: Either part1 or part2 are expected");
Ok(())
}
}
_ => {
println!("Error: both filename and part1/part2 are expected");
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use crate::{parse_digit_line, part1, part2};
type TestResult<T> = Result<T, Box<dyn std::error::Error>>;
#[test]
fn can_parse_line() {
assert_eq!(Ok(("", String::from("12"))), parse_digit_line("12a"));
}
#[test]
fn sample_test1() -> TestResult<()> {
let input =
std::fs::read_to_string("/home/pawel/Work/my-advent-of-code/2023/1/sample1.txt")?;
assert_eq!(1, part1(&input));
Ok(())
}
#[test]
fn sample_test2() -> TestResult<()> {
let input =
std::fs::read_to_string("/home/pawel/Work/my-advent-of-code/2023/1/sample1.txt")?;
assert_eq!(2, part2(&input));
Ok(())
}
}