Guest User

Untitled

a guest
Dec 9th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. fn main() {
  2. // a string of ascii characters
  3. const PUZZLE: &str = "AaBCDd";
  4. println!("Part 1: {}", part_1(PUZZLE));
  5. }
  6.  
  7. fn part_1(input: &str) -> usize {
  8. // The idea is to drop characters from input into v one by one. If
  9. // the new character c and the last character d in v react then
  10. // they both vanish. If they don't react then v gets a bit bigger
  11. // and we go for the next drop.
  12.  
  13. // For the example input "AaBCDd" I would expect a vector
  14. // v = 'B', 'D' (because A and a react and D and d react)
  15. // so the returned length would be 2.
  16. let mut v = Vec::new();
  17. for c in input.chars() {
  18. match v.last().cloned() {
  19. None => {
  20. v.push(c);
  21. }
  22. Some(d) => if react(&c, &d) {
  23. v.pop();
  24. } else {
  25. v.push(c)
  26. },
  27. }
  28. }
  29. return v.len();
  30. }
  31.  
  32. /// characters react if they are the same ascii character of different
  33. /// capitalisation.
  34. /// A a -> True
  35. /// a A -> True
  36. /// A A -> False
  37. /// a a -> False
  38. /// A b -> False
  39. fn react(c: &char, d: &char) -> bool {
  40. return (c != d) && c.eq_ignore_ascii_case(d);
  41. }
Add Comment
Please, Sign In to add comment