Day 2

Previous: Day 01 | Next: Day 03

Remember: you’re here forever

###AUTO-ACQUIRED DATA FOLLOWS…
archivist/day02-1.c
archivist/day02-1.c
\#include "../array.h"
    \#include "../fileload.h"
    
    int main (int argc, char\*\*argv){
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char \* input = file\_contents(argv[1]);
        char\*dispos = strdup(input);
        array data = arr\_init(0, sizeof(int));
    
        int n\_valid\_passwords = 0;
    
        char \* line = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) break;
            int polchar\_occurences = 0;
            char \* val = line;
            printf("string: %s\n", val);
            int min = atoi(val);
            strsep(&val, "-");
            int max = atoi(val);
            strsep(&val, " ");
            char polchar = val[0];
            strsep(&val, " ");
            printf("%d, %d, %c\n", min, max, polchar);
            while(val[0] != &\#39;\0&\#39;){
                printf("%c", val[0]);
                if (val[0] == polchar) polchar\_occurences++;
                val++;
            }
            printf("\nnumber of times %c occured: %d\n", polchar, polchar\_occurences);
            if(min <= polchar\_occurences && polchar\_occurences <= max) n\_valid\_passwords++;
        };
    
        printf("Number of valid passwords:%d\n", n\_valid\_passwords);
    
        for (size\_t j = 0;j<data.size;j++){
            printf("val: %d\n", arr\_get(int, data, j));
        }
        
        return 0;
    }
    
archivist/day02-2.c
archivist/day02-2.c
\#include "../array.h"
    \#include "../fileload.h"
    
    int main (int argc, char\*\*argv){
        char\* input = file\_contents(argv[1]);
        int n\_valid\_passwords = 0;
        char\* line = NULL;
        int ind[2];
    
        while((line = strsep(&input, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char\* val = line;
            ind[0] = atoi(val)-1;
            strsep(&val, "-");
            ind[1] = atoi(val)-1;
            strsep(&val, " ");
            char polchar = val[0];
            strsep(&val, " ");
            n\_valid\_passwords += ((val[ind[0]]==polchar) != (val[ind[1]]==polchar));
        };
    
        printf("Number of valid passwords:%d\n", n\_valid\_passwords);
        return 0;
    }
    
steveklabnik/steveklabnik-day02.rs
steveklabnik/steveklabnik-day02.rs
macro\_rules! parse {
      ($input:ident, [$($split:literal),+], $($name:ident$(: $ty:ty)?),+) => {
          let mut iter = $input
              .split(|c| matches!(c, $($split)|+))
              .filter(|s| !s.is\_empty());
    
          $( let $name = iter.next().unwrap()$(.parse::<$ty>().unwrap())?; )+
      };
    }
    
    fn solve(input: &str) -> (u32, u32) {
      input.lines().fold((0, 0), |(a, b), s| {
          parse!(s, [&\#39; &\#39;, &\#39;-&\#39;, &\#39;:&\#39;], min: usize, max: usize, c: char, pass);
          let c = c as u8;
    
          (
              a + u32::from((min..=max).contains(&pass.bytes().filter(|&b| b == c).count())),
              b + u32::from((pass.as\_bytes()[min - 1] == c) ^ (pass.as\_bytes()[max - 1] == c))
          )
      })
    }
    

Tags: