Guest User

Untitled

a guest
Dec 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #[test]
  2. fn split_line() {
  3. let lines = vec![100, 200, 300];
  4. let input = String::from("100 200 300");
  5. assert_eq!(lines, split(input))
  6. }
  7.  
  8. #[test]
  9. fn test_min_max() {
  10. let input = String::from("100 200 300");
  11. let split = split(input);
  12. assert_eq!((100, 300), get_min_max(split))
  13. }
  14.  
  15. #[test]
  16. fn test_diff() {
  17. let input = String::from("100 200 300");
  18. let split = split(input);
  19. let (min, max) = get_min_max(split);
  20. assert_eq!(200, diff(min, max))
  21. }
  22.  
  23. #[test]
  24. fn test_main() {
  25. let input = "5 1 9 5
  26. 7 5 3
  27. 2 4 6 8";
  28. let result = checksum(input);
  29. assert_eq!(18, result);
  30. }
  31.  
  32. fn split(s : String) -> Vec<i32> {
  33. s.split_whitespace().map(|x| x.parse::<i32>().unwrap()).collect()
  34. }
  35.  
  36. fn get_min_max(nums : Vec<i32>) -> (i32, i32) {
  37. let min = *nums.iter().min().unwrap();
  38. let max = *nums.iter().max().unwrap();
  39. (min, max)
  40. }
  41.  
  42. fn diff(min : i32, max : i32) -> i32 {
  43. max - min
  44. }
  45.  
  46. fn checksum(input_str : &str) -> i32 {
  47. let mut out : i32 = 0;
  48. for line in input_str.lines() {
  49. let split = split(String::from(line));
  50. let (min, max) = get_min_max(split);
  51. let diff = diff(min, max);
  52. out += diff;
  53. }
  54. out
  55. }
Add Comment
Please, Sign In to add comment