day1 part1
This commit is contained in:
		
							
								
								
									
										15
									
								
								2023/day-01/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								2023/day-01/Cargo.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
[package]
 | 
			
		||||
name = "day-01"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
edition = "2021"
 | 
			
		||||
 | 
			
		||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
			
		||||
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "part1"
 | 
			
		||||
path = "src/part1.rs"
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "part2"
 | 
			
		||||
path = "src/part2.rs"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
							
								
								
									
										1000
									
								
								2023/day-01/src/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1000
									
								
								2023/day-01/src/input.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										35
									
								
								2023/day-01/src/part1.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								2023/day-01/src/part1.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
fn main() {
 | 
			
		||||
    let input = include_str!("./input.txt");
 | 
			
		||||
    let output = process(input);
 | 
			
		||||
    println!("{}", output);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn process(input: &str) -> i32 {
 | 
			
		||||
    let lines: i32 = input
 | 
			
		||||
        .lines()
 | 
			
		||||
        .into_iter()
 | 
			
		||||
        .map(|line| {
 | 
			
		||||
            let line_vec = line
 | 
			
		||||
                .chars()
 | 
			
		||||
                .filter(|c| c.is_digit(10))
 | 
			
		||||
                .collect::<Vec<char>>();
 | 
			
		||||
            let first = line_vec.clone().into_iter().nth(0);
 | 
			
		||||
            let last = line_vec.clone().into_iter().nth_back(0);
 | 
			
		||||
            [first.unwrap(), last.unwrap()].into_iter().collect()
 | 
			
		||||
        })
 | 
			
		||||
        .collect::<Vec<String>>()
 | 
			
		||||
        .iter()
 | 
			
		||||
        .map(|x| x.parse::<i32>().unwrap())
 | 
			
		||||
        .sum();
 | 
			
		||||
    lines
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_logic() {
 | 
			
		||||
        let input = include_str!("./testinput.txt");
 | 
			
		||||
        assert_eq!(process(input), 142);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										4
									
								
								2023/day-01/src/testinput.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								2023/day-01/src/testinput.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
1abc2
 | 
			
		||||
pqr3stu8vwx
 | 
			
		||||
a1b2c3d4e5f
 | 
			
		||||
treb7uchet
 | 
			
		||||
		Reference in New Issue
	
	Block a user