Advertisement
Guest User

Untitled

a guest
May 25th, 2019
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.49 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::{fs, io::{self, Write}};
  3.  
  4. fn main() -> io::Result<()> {
  5.     let input = fs::read_to_string("sygnaly.txt")?;
  6.     let input = input.lines().collect::<Vec<_>>();
  7.  
  8.     let mut output = fs::File::create("wyniki4.txt")?;
  9.     output.write_all(excersise_1(&input).as_bytes())?;
  10.     output.write_all(b"\n")?;
  11.     output.write_all(excersise_2(&input).as_bytes())?;
  12.     output.write_all(b"\n")?;
  13.  
  14.     for line in excersise_3(&input) {
  15.         output.write_all(line.as_bytes())?;
  16.         output.write_all(b"\n")?;
  17.     }
  18.  
  19.     Ok(())
  20. }
  21.  
  22. fn excersise_1(input: &Vec<&str>) -> String {
  23.     input
  24.         .iter()
  25.         .skip(39)
  26.         .step_by(40)
  27.         .map(|line| line.chars().nth(9).unwrap())
  28.         .collect::<String>()
  29. }
  30.  
  31. fn excersise_2(input: &Vec<&str>) -> String {
  32.     let mut letters = HashMap::with_capacity(26);
  33.     let mut max = (0, "");
  34.     for line in input {
  35.         letters.clear();
  36.         for c in line.chars() {
  37.             letters.entry(c).or_insert(());
  38.         }
  39.         if letters.len() > max.0 {
  40.             max = (letters.len(), line);
  41.         }
  42.     }
  43.     format!("{} {}", max.1, max.0)
  44. }
  45.  
  46. fn excersise_3<'a>(input: &'a Vec<&str>) -> Vec<&'a str> {
  47.    input.iter().filter(|line| {
  48.        let bytes = line.as_bytes();
  49.        for i in 0..(bytes.len() - 1) {
  50.            for j in 1..bytes.len() {
  51.                if (bytes[i] as i16 - bytes[j] as i16).abs() > 10 {
  52.                    return false;
  53.                }
  54.            }
  55.        }
  56.        true
  57.    })
  58.    .map(|line| *line)
  59.    .collect::<Vec<_>>()
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement