|
@@ -1,3 +1,92 @@
|
|
|
|
|
+use std::fs::read_to_string;
|
|
|
|
|
+
|
|
|
|
|
+fn read_lines(filename: &str) -> Vec<String> {
|
|
|
|
|
+ let mut result: Vec<String> = Vec::new();
|
|
|
|
|
+
|
|
|
|
|
+ for line in read_to_string(filename).unwrap().lines() {
|
|
|
|
|
+ result.push(line.to_string())
|
|
|
|
|
+ // result.push(line);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ result
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn parse_line(line: &str) -> i32 {
|
|
|
|
|
+ // Vec<&str>
|
|
|
|
|
+ let digits: [char; 10] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
|
|
|
|
|
+ // let s: String = String::from(line);
|
|
|
|
|
+ let mut f: String = String::from(line)
|
|
|
|
|
+ .chars()
|
|
|
|
|
+ .fold(String::new(), |mut acc, c| {
|
|
|
|
|
+ if digits.contains(&c) {
|
|
|
|
|
+ if acc.len() == 2 {
|
|
|
|
|
+ acc.pop();
|
|
|
|
|
+ }
|
|
|
|
|
+ acc.push(c);
|
|
|
|
|
+ }
|
|
|
|
|
+ acc
|
|
|
|
|
+ });
|
|
|
|
|
+ if f.len() == 1 {
|
|
|
|
|
+ f.push_str(&(f.clone()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let j: i32 = f.parse::<i32>().unwrap();
|
|
|
|
|
+ j
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
fn main() {
|
|
fn main() {
|
|
|
println!("Hello, world!");
|
|
println!("Hello, world!");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+#[cfg(test)]
|
|
|
|
|
+mod tests {
|
|
|
|
|
+ use crate::parse_line;
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn test_parse_line_1() {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ let l: &str = "1abc2";
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ let calib_val: i32 = parse_line(l);
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ assert_eq!(calib_val, 12);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn test_parse_line_2() {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ let l: &str = "pqr3stu8vwx";
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ let calib_val: i32 = parse_line(l);
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ assert_eq!(calib_val, 38);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn test_parse_line_3() {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ let l: &str = "a1b2c3d4e5f";
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ let calib_val: i32 = parse_line(l);
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ assert_eq!(calib_val, 15);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn test_parse_line_4() {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ let l: &str = "treb7uchet";
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ let calib_val: i32 = parse_line(l);
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ assert_eq!(calib_val, 77);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|