|
@@ -5,7 +5,6 @@ fn read_lines(filename: &str) -> Vec<String> {
|
|
|
|
|
|
|
|
for line in read_to_string(filename).unwrap().lines() {
|
|
for line in read_to_string(filename).unwrap().lines() {
|
|
|
result.push(line.to_string())
|
|
result.push(line.to_string())
|
|
|
- // result.push(line);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
result
|
|
result
|
|
@@ -34,13 +33,24 @@ fn parse_line(line: &str) -> i32 {
|
|
|
j
|
|
j
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+fn process(lines: Vec<String>) -> i32 {
|
|
|
|
|
+ let sum: i32 = lines.iter().fold(0, |acc, line| {
|
|
|
|
|
+ let line_str: &str = line.as_str();
|
|
|
|
|
+ let val = parse_line(line_str);
|
|
|
|
|
+ acc + val
|
|
|
|
|
+ });
|
|
|
|
|
+ sum
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
fn main() {
|
|
fn main() {
|
|
|
- println!("Hello, world!");
|
|
|
|
|
|
|
+ let lines: Vec<String> = read_lines("input.txt");
|
|
|
|
|
+ let sum = process(lines);
|
|
|
|
|
+ println!("{}", sum);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
mod tests {
|
|
|
- use crate::parse_line;
|
|
|
|
|
|
|
+ use crate::{parse_line, process};
|
|
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
|
fn test_parse_line_1() {
|
|
fn test_parse_line_1() {
|
|
@@ -89,4 +99,20 @@ mod tests {
|
|
|
// assert
|
|
// assert
|
|
|
assert_eq!(calib_val, 77);
|
|
assert_eq!(calib_val, 77);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn test_process() {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ let mut v: Vec<String> = Vec::new();
|
|
|
|
|
+ v.push(String::from("1abc2"));
|
|
|
|
|
+ v.push(String::from("pqr3stu8vwx"));
|
|
|
|
|
+ v.push(String::from("a1b2c3d4e5f"));
|
|
|
|
|
+ v.push(String::from("treb7uchet"));
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ let sum: i32 = process(v);
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ assert_eq!(sum, 142);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|