From d4897b5034c6ea0d1c2caa929e8b21ae8008f142 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 10 Sep 2024 10:45:58 +0100 Subject: [PATCH] completed longest substring and palindromic number --- src/input/3_test1 | 1 + src/input/3_test2 | 1 + src/input/3_test3 | 1 + src/input/9_test1 | 1 + src/input/9_test2 | 1 + src/input/9_test3 | 1 + src/main.rs | 2 + src/solutions/longest_substring.rs | 91 ++++++++++++++++++++++++++++++ src/solutions/mod.rs | 2 + src/solutions/palidromic_number.rs | 80 ++++++++++++++++++++++++++ 10 files changed, 181 insertions(+) create mode 100644 src/input/3_test1 create mode 100644 src/input/3_test2 create mode 100644 src/input/3_test3 create mode 100644 src/input/9_test1 create mode 100644 src/input/9_test2 create mode 100644 src/input/9_test3 create mode 100644 src/solutions/longest_substring.rs create mode 100644 src/solutions/palidromic_number.rs diff --git a/src/input/3_test1 b/src/input/3_test1 new file mode 100644 index 0000000..fe65e2d --- /dev/null +++ b/src/input/3_test1 @@ -0,0 +1 @@ +abcabcbb \ No newline at end of file diff --git a/src/input/3_test2 b/src/input/3_test2 new file mode 100644 index 0000000..71165b0 --- /dev/null +++ b/src/input/3_test2 @@ -0,0 +1 @@ +bbbbb \ No newline at end of file diff --git a/src/input/3_test3 b/src/input/3_test3 new file mode 100644 index 0000000..107f1b6 --- /dev/null +++ b/src/input/3_test3 @@ -0,0 +1 @@ +dvdf \ No newline at end of file diff --git a/src/input/9_test1 b/src/input/9_test1 new file mode 100644 index 0000000..5a396e2 --- /dev/null +++ b/src/input/9_test1 @@ -0,0 +1 @@ +121 \ No newline at end of file diff --git a/src/input/9_test2 b/src/input/9_test2 new file mode 100644 index 0000000..0a77e7b --- /dev/null +++ b/src/input/9_test2 @@ -0,0 +1 @@ +-121 \ No newline at end of file diff --git a/src/input/9_test3 b/src/input/9_test3 new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/src/input/9_test3 @@ -0,0 +1 @@ +10 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f146ee4..1d2c627 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ async fn main() -> Result<(), Box> { let problems: Vec> = vec![ Box::new(valid_parentheses::ValidParentheses {}), Box::new(merge_sorted_lists::MergeSortedLists {}), + Box::new(longest_substring::LongestSubstring {}), + Box::new(palidromic_number::PalidromicNumber {}), Box::new(xx::XX {}), ]; diff --git a/src/solutions/longest_substring.rs b/src/solutions/longest_substring.rs new file mode 100644 index 0000000..0500435 --- /dev/null +++ b/src/solutions/longest_substring.rs @@ -0,0 +1,91 @@ +use super::Solution; + +use std::collections::HashMap; + +pub struct LongestSubstring {} + +impl Solution for LongestSubstring { + fn solution( + &self, + input: &mut Vec, + ) -> Result, Box> { + let mut hm = HashMap::new(); + let mut largest = 0; + let mut lo = -1; + + for s in input { + for (hi, ch) in s.chars().enumerate() { + if let Some(i) = hm.insert(ch, hi as i32) { + lo = lo.max(i); + } + largest = largest.max(hi as i32 - lo); + } + } + + Ok(Box::new(largest)) + } + + fn get_id(&self) -> u32 { + 3 + } +} + +impl LongestSubstring {} + +/// Test from puzzle input +#[cfg(test)] +mod test { + use super::*; + use crate::*; + + #[test] + fn test1() { + let solution = longest_substring::LongestSubstring {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test1) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "3"); + } + + #[test] + fn test2() { + let solution = longest_substring::LongestSubstring {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test2) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "1"); + } + + #[test] + fn test3() { + let solution = longest_substring::LongestSubstring {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test3) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "3"); + } +} diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 61f2c9f..76c51d0 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -3,6 +3,8 @@ use std::{error::Error, fmt::Display, time::SystemTime}; pub mod valid_parentheses; pub mod merge_sorted_lists; +pub mod longest_substring; +pub mod palidromic_number; pub mod xx; pub trait Solution { diff --git a/src/solutions/palidromic_number.rs b/src/solutions/palidromic_number.rs new file mode 100644 index 0000000..457ee31 --- /dev/null +++ b/src/solutions/palidromic_number.rs @@ -0,0 +1,80 @@ +use super::Solution; + +pub struct PalidromicNumber {} + +impl Solution for PalidromicNumber { + fn solution( + &self, + input: &mut Vec, + ) -> Result, Box> { + let num: i32 = input.get(0).unwrap().parse::()?; + + let num_str: String = format!("{}", num); + let rev: String = num_str.chars().rev().collect::(); + Ok(Box::new(num_str == rev)) + } + + fn get_id(&self) -> u32 { + 9 + } +} + +impl PalidromicNumber {} + +/// Test from puzzle input +#[cfg(test)] +mod test { + use super::*; + use crate::*; + + #[test] + fn test1() { + let solution = palidromic_number::PalidromicNumber {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test1) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "true"); + } + + #[test] + fn test2() { + let solution = palidromic_number::PalidromicNumber {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test2) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "false"); + } + + #[test] + fn test3() { + let solution = palidromic_number::PalidromicNumber {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test3) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "false"); + } +}