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] != '\0'){
                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, [' ', '-', ':'], 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: