Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[test]
- fn split_line() {
- let lines = vec![100, 200, 300];
- let input = String::from("100 200 300");
- assert_eq!(lines, split(input))
- }
- #[test]
- fn test_min_max() {
- let input = String::from("100 200 300");
- let split = split(input);
- assert_eq!((100, 300), get_min_max(split))
- }
- #[test]
- fn test_diff() {
- let input = String::from("100 200 300");
- let split = split(input);
- let (min, max) = get_min_max(split);
- assert_eq!(200, diff(min, max))
- }
- #[test]
- fn test_main() {
- let input = "5 1 9 5
- 7 5 3
- 2 4 6 8";
- let result = checksum(input);
- assert_eq!(18, result);
- }
- fn split(s : String) -> Vec<i32> {
- s.split_whitespace().map(|x| x.parse::<i32>().unwrap()).collect()
- }
- fn get_min_max(nums : Vec<i32>) -> (i32, i32) {
- let min = *nums.iter().min().unwrap();
- let max = *nums.iter().max().unwrap();
- (min, max)
- }
- fn diff(min : i32, max : i32) -> i32 {
- max - min
- }
- fn checksum(input_str : &str) -> i32 {
- let mut out : i32 = 0;
- for line in input_str.lines() {
- let split = split(String::from(line));
- let (min, max) = get_min_max(split);
- let diff = diff(min, max);
- out += diff;
- }
- out
- }
Add Comment
Please, Sign In to add comment