Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- // a string of ascii characters
- const PUZZLE: &str = "AaBCDd";
- println!("Part 1: {}", part_1(PUZZLE));
- }
- fn part_1(input: &str) -> usize {
- // The idea is to drop characters from input into v one by one. If
- // the new character c and the last character d in v react then
- // they both vanish. If they don't react then v gets a bit bigger
- // and we go for the next drop.
- // For the example input "AaBCDd" I would expect a vector
- // v = 'B', 'D' (because A and a react and D and d react)
- // so the returned length would be 2.
- let mut v = Vec::new();
- for c in input.chars() {
- match v.last().cloned() {
- None => {
- v.push(c);
- }
- Some(d) => if react(c, d) {
- v.pop();
- } else {
- v.push(c)
- },
- }
- }
- return v.len();
- }
- /// characters react if they are the same ascii character of different
- /// capitalisation.
- /// A a -> True
- /// a A -> True
- /// A A -> False
- /// a a -> False
- /// A b -> False
- fn react(c: char, d: char) -> bool {
- return (c != d) && c.eq_ignore_ascii_case(&d);
- }
Add Comment
Please, Sign In to add comment