Completed day one
This commit is contained in:
		
							
								
								
									
										50
									
								
								day_01/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								day_01/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| use std::iter::zip; | ||||
|  | ||||
| const WORD_LIST: [&str; 9] = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; | ||||
| const DIGIT_LIST: [&str; 9] = ["one1one", "two2two", "three3three", "four4four", "five5five", "six6six", "seven7seven", "eight8eight", "nine9nine"]; | ||||
|  | ||||
| fn find_first_digit(line: &str) -> u32 { | ||||
|     match line.chars().find(|character| { character.is_digit(10) }) { | ||||
|         Some(x) => x.to_digit(10).unwrap(), | ||||
|         None => 0, | ||||
|     } | ||||
| } | ||||
|  | ||||
| fn find_last_digit(line: &str) -> u32 { | ||||
|     match line.chars().rev().find(|character| { character.is_digit(10) }) { | ||||
|         Some(x) => x.to_digit(10).unwrap(), | ||||
|         None => 0, | ||||
|     } | ||||
| } | ||||
|  | ||||
| fn replace_with_digits(input: &str) -> String { | ||||
|     let mut output = input.to_string(); | ||||
|     for (word, digit) in zip(WORD_LIST, DIGIT_LIST) { | ||||
|         output = output.replace(word, digit).to_string(); | ||||
|     } | ||||
|     output | ||||
| } | ||||
|  | ||||
| pub fn part1(input: &str) -> String { | ||||
|     let calibration_lines = input | ||||
|         .split("\n") | ||||
|         .map(|line| { | ||||
|             let first = find_first_digit(line); | ||||
|             let last = find_last_digit(line); | ||||
|             format!("{}{}", first, last).parse::<u32>().unwrap() | ||||
|         }) | ||||
|         .collect::<Vec<_>>(); | ||||
|     format!("{}", calibration_lines.into_iter().sum::<u32>()) | ||||
| } | ||||
|  | ||||
| pub fn part2(input: &str) -> String { | ||||
|     let parsed_inputs = &replace_with_digits(input); | ||||
|     dbg!(parsed_inputs); | ||||
|     part1(parsed_inputs) | ||||
| } | ||||
|  | ||||
| pub mod prelude { | ||||
|     pub use super::part1; | ||||
|     pub use super::part2; | ||||
| } | ||||
|  | ||||
							
								
								
									
										34
									
								
								day_01/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								day_01/src/main.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| use day_01::prelude::*; | ||||
|  | ||||
| fn main() { | ||||
|     let input = include_str!("../input.txt"); | ||||
|     println!("{}", part1(input)); | ||||
|     println!("{}", part2(input)); | ||||
| } | ||||
|  | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|  | ||||
|     #[test] | ||||
|     fn test_part1() { | ||||
|         let test_input = "1abc2 | ||||
| pqr3stu8vwx | ||||
| a1b2c3d4e5f | ||||
| treb7uchet"; | ||||
|         assert_eq!(part1(test_input), "142".to_string()); | ||||
|     } | ||||
|  | ||||
|     #[test] | ||||
|     fn test_part2() { | ||||
|         let test_input = "two1nine | ||||
| eightwothree | ||||
| abcone2threexyz | ||||
| xtwone3four | ||||
| 4nineeightseven2 | ||||
| zoneight234 | ||||
| 7pqrstsixteen"; | ||||
|         assert_eq!(part2(test_input), "281".to_string()); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Connor Johnstone
					Connor Johnstone