Archivist Anon

These are my own solutions to the Advent of Code problems. So far, they've all been in C, but I want to write a few in Common Lisp in the next new days.

Day One

Part 1
Day One, part 1
#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 = input;
        array data = arr_init(0, sizeof(int));
    
        char * line = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char * val = strsep(&line, " ");
            int temp = atoi(val);
            arr_add(&data, &temp);
        };
        for (int j = 0;j<data.size;j++){
            printf("val: %d\n", arr_get(int, data, j));
        }
        
        for (size_t i = 0;i<data.size;i++){
            for (size_t j = 0;j<data.size;j++){
                if (arr_get(int,data,i) + arr_get(int,data,j)== 2020){
                    printf("Found!  The multiple is: %d\n", arr_get(int,data,i) * arr_get(int,data,j) );
                }
            }
        }
    
        return 0;
    }
    
Part 2
Day One, part 2
#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 = input;
        array data = arr_init(0, sizeof(int));
    
        char * line = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char * val = strsep(&line, " ");
            int temp = atoi(val);
            arr_add(&data, &temp);
        };
        for (int j = 0;j<data.size;j++){
            //printf("val: %d\n", arr_get(int, data, j));
        }
        
        for (size_t i = 0;i<data.size;i++){
            for (size_t j = 0;j<data.size;j++){
                for (size_t k = 0;k<data.size;k++){
                    if (arr_get(int,data,i) + arr_get(int,data,j) + arr_get(int,data,k)== 2020){
                        printf("Found!  The multiple is: %d\n",  arr_get(int,data,i) 
                                                                *arr_get(int,data,j)
                                                                *arr_get(int,data,k));
                    }
                }
            }
        }
    
        return 0;
    }
    
Part 3
Day One, part 3
#include "../array.h"
    #include "../fileload.h"
    #include <stdlib.h>
    
    //For /g/'s bigboy dataset.  Target sum is 99920044
    
    int MAX_SIZE = 100000;
    
    int compare(const void* a, const void* b){
        return (*(int *)a < *(int *)b) ? -1 : 1;
        return 0;
    }
    
    
    int search_up(int*vals, int left, int right, int max){
        int here;
        while (right-left > 1){
            here = (left+right)/2;
            if(vals[here] == max) return here;
            if(vals[here] > max) right = here;
            if(vals[here] < max) left = here;
        }
        if(vals[right] > max){
            return left;
        }
            return right;
    }
    
    int search_down(int*vals, int left, int right, int min){
        int here;
        while (right-left > 1){
            here = (left+right)/2;
            if(vals[here] == min) return here;
            if(vals[here] > min) right = here;
            if(vals[here] < min) left = here;
        }
        if(vals[left] < min){
            return right;
        }
            return left;
    }
    
    int main (int argc, char**argv){
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char * input = file_contents(argv[1]);
    
        int size = 0;
        const int target = 99920044;
        int*arr = malloc(sizeof(int)*MAX_SIZE);
        char * line = 0;
        while((line = strsep(&input, "\n")) != NULL){
            if(strlen(line) == 0) break;
            char * val = strsep(&line, " ");
            arr[size++] = atoi(val);
            if (size > MAX_SIZE) {
                printf("Too many inputs\n");
                exit(-1);}
        };
        //int (*func)(const void *, const void *, void *) = &compare;
        qsort(&arr[0], size, sizeof(int), compare);
        for(int i = 0; i < size; i++){
            //printf("%d\n", arr[i]);
        }
        //a<b<c
        int true_left = 0;
        int true_right= size-1;
        //first pass
        while(true_right>true_left+2){
            int any=1;
            while(any){
                any = 0;
                int new_true_right = search_up(arr,true_left+2,true_right,target-arr[true_left]-arr[true_left+1]);
                if(new_true_right<true_right){
                    true_right = new_true_right;
                    any++;
                }
                int new_true_left = search_down(arr,true_left,true_right-2,target-arr[true_right]-arr[true_right-1]);
                if(new_true_left>true_left){
                    true_left=new_true_left;
                    any++;
                }
                //if(any)printf("true left, true right %d, %d\n", true_left, true_right);
            }
    
    
            int left=true_left;
            int right = true_right-1;
            int starget = target - arr[true_right];
            any = 1;
            while(any){
                any = 0;
                int new_right = search_up(arr,left+1,right,starget-arr[left]);
                if(new_right<right){
                    right = new_right;
                    any++;
                }
                int new_left = search_down(arr,left,right-1,starget-arr[right]);
                if(new_left>left){
                    left=new_left;
                    any++;
                }
                //printf("%d, %d\n", left, right);
            }
            //printf("starting iteration\n");
            while(left != right ){
                 int tempright = search_up(arr, left+1, right,starget-arr[left]);
                if(arr[left]+arr[tempright] == starget){
                    printf("FOUND IT!  the nums are %d, %d, %d\n", arr[left], arr[right], arr[true_right]);
                    printf("Sum is: %d, compared with %d\n", arr[left]+ arr[right]+ arr[true_right], target);
                    return 0;
                }
                left++;
            }
            true_right--;    
        }
    
        
    
        return 0;
    }
    

Day Two

Part 1
Day Two, part 1
#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;
    }
    
Part 2
Day Two, part 2
#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;
    }
    

Day Three

Part 1
Day 3, part 1
#include "../array.h"
    #include "../fileload.h"
    #include <stdlib.h>
    
    int main (int argc, char**argv){
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
    
        int width = 0;
        int height = 0;
    
        char * input = file_contents(argv[1]);
        char*dispos = malloc(strlen(input)*sizeof(char)); 
    
        strcpy(dispos, input);
        array data = arr_init(0, sizeof(int));
    
        char * line = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) break;
            width=strlen(line);
            height++;
        };
    
    
        int xpos = 0;
        int ypos = 0;
    
        int n_trees = 0;
    
        printf("W: %d, H: %d\n", width,height);
    
        while(ypos < height){
            xpos = (xpos+3)%width;
            ypos = ypos+1;
            printf("%d, %d\n", xpos,ypos);
            //We have to use width+1 to account for newline characters
            if(input[xpos+ypos*(width+1)] == '#') {
                n_trees++;
                printf("you hit a tree\n");
            }
        }
    
        printf("You hit %d trees\n", n_trees);
    
    
        
        return 0;
    }
    
Part 2
Day 3, part 2
#include <stdio.h>
    /*TO THE*/ #include <stdlib.h>
    /*OCEAN!*/ #include <string.h>
    #define R 5
    //
    //
    int main(
    int argc,
    char **argv){
    int w=0;int h=0;
    int l=0;FILE*f=fopen(
    argv[1],"r");char*I;if                 (!f)
    return-1;fseek(f,0,SEEK_END);            l=ftell
    (f);printf("l is %d\n",l);                rewind(f)
    ;I=(char*)malloc(sizeof(char)*              (l+1))
    ;fread(I,sizeof(char),l,f);                 fclose
    (f);((char*)I)[l]='\0';for(int             i=0;
    i<l;i++){if(I[i]=='\n'){w=i;break         ;}}h=l
    /(w+1);printf("W: %d,H: %d\n"           ,w,h) ;int 
    s[R] [2]={{1,1},{3,1},{5,1},    {7,1},  {1,2}  };long 
    int tot=1;for(int run=0;run<R;   run++){int      x=0;
    int y=0;int n=0;while(y<h){x=(    x+s [run]        [0]
    )%w;y=y+srun;if(I[x+y*(w+1)   ]=='#')n++        ;
    }printf("You hit %d trees on run    %d\n",n,run)     ;
    tot *=n;}printf("Total multiple:     %ld\n",tot);    ;
    int i=0;i++;i++;i++;i++;i++;i++;i++;  i++;i++;i++;   i++;
    i++;i++;i++;i++;i++;i++;i++ ;i++;i++;  i++;i++;i++  ;i++;
    i++;i++;i++;i++;i++;i++;i++; i++;i++;   i++;i++;   i++;
    i++; i++;i++; i++; i++;i++;i++;i++;i++;  i++;     i++;
    i++;i++;i++;i++;i++;i++;i++;  i++;i++;    i++;   i++;
     i++;i++;i++;i++;i++;i++;i++;i++;i++;i++;  i++; i++;
i++;i++;i++; i++;i++;i++; i++;i++;i++;i++; i++;;}
Part 3
Day 3, part 3
#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "../bn.h"
    
    
    int main (int argc, char**argv){
        int width = 0;
        int height = 0;
    
        long int  length = 0;
        FILE *f = fopen(argv[1], "r");
        char  output[8192];
        output[0] = 'a';
        output[1024] = '\0';
        char *input;
        if (!f) return -1; 
        fseek(f, 0, SEEK_END);
        length = ftell(f);
        printf("length is %ld\n", length);
        rewind(f);
        input = (char*)malloc(sizeof(char) * (length+1));
        fread(input, sizeof(char), length, f);
        printf("length is %ld\n", length);
        fclose(f);
        ((char*)input)[length] = '\0';
        for(int i = 0; i < length; i++){
            if(input[i] == '\n'){
                width = i;
                break;
            }
        }
        height = length/(width+1);
    
    
        printf("W: %d, H: %d\n", width,height);
    
        int run_list_size = 15;
        int slopevals2 = {{ 2, 3, 4,  6,  8,  9, 12, 16, 18, 24, 32, 36, 48, 54, 64 },
                                { 1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 47 }};
        unsigned long long int running_mult_total = 1;
        struct bn total;
        struct bn tmp;
        struct bn tmp2;
        bignum_from_int(&total, 1);
        for(int i = 0;i<run_list_size;i++){
            for(int j = 0;j<run_list_size;j++){
                //printf("Slope %d, %d\n", slopevals0, slopevals1);
                int xpos = 0;
                int ypos = 0;
                int n_trees = 0;
                while(ypos < height){
                    xpos = (xpos+slopevals0)%width;
                    ypos = ypos+slopevals1;
                    //We have to use width+1 to account for newline characters
                    if(input[xpos+ypos*(width+1)] == '#') n_trees++;
                }
                printf("You hit %d trees on run %d\n", n_trees, i*15+j);
                if(n_trees == 0) n_trees++;
                //bignum_from_int(&total, 111231);
                bignum_assign(&tmp2, &total);
                bignum_from_int(&tmp,n_trees);
                bignum_mul(&tmp,&tmp2,&total);
                bignum_to_string(&total, output, sizeof(output));
                printf("running multiple: %s\n", output);
            }
        }
        bignum_to_string(&total, output, 1024);
        printf("Total multiple: %s\n", output);
        
        return 0;
    }
    

Day Four

Part 1
Day 4, part 1
#include "../array.h"
    #include "../fileload.h"
    #include <stdlib.h>
    
    /*
     *  byr (Birth Year)
     *  iyr (Issue Year)
     *  eyr (Expiration Year)
     *  hgt (Height)
     *  hcl (Hair Color)
     *  ecl (Eye Color)
     *  pid (Passport ID)
     *  cid (Country ID)
     */
    
    #define BYR 1<<0
    #define IYR 1<<1
    #define EYR 1<<2
    #define HGT 1<<3
    #define HCL 1<<4
    #define ECL 1<<5
    #define PID 1<<6
    #define CID 1<<7
    
    typedef struct field{
        int bitcode;
        char label[4];
    }field;
    
    int main (int argc, char**argv){
        field fields[8];
        strcpy(fields[0].label, "BYR"); 
        strcpy(fields[1].label, "IYR"); 
        strcpy(fields[2].label, "EYR"); 
        strcpy(fields[3].label, "HGT"); 
        strcpy(fields[4].label, "HCL"); 
        strcpy(fields[5].label, "ECL"); 
        strcpy(fields[6].label, "PID"); 
        strcpy(fields[7].label, "CID"); 
    
        int all_on = 0;
        for(int i = 0; i<8; i++){
            fields[i].bitcode = 1<<i;
            all_on = all_on | fields[i].bitcode;
            fields[i].label[3] = '\0';
        }
    
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char * input = file_contents(argv[1]);
        char*dispos = input;
    
        int n_valids = 0;
        int bitmap_default = 0 | CID;
        int bitmap = 0 | CID;
    
        char * line= 0;
        char * val = 0;
        char * fieldname = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) { 
                //printf("Passport done\n");
                if (bitmap == all_on) {
                    //printf("Valid passport\n");
                    n_valids++;
                }else{
                    //printf("Invalid passport\n");
                }
                bitmap = bitmap_default | CID;
                continue;
            }
            while(val =strsep(&line, " ")){
                fieldname = strsep(&val, ":");
                for(int i = 0; i<8; i++){
                    if(strcasecmp(fieldname, fields[i].label) == 0){
                        bitmap = bitmap | fields[i].bitcode;
                        break;
                    }
                }
            }
        };
        printf("There are %d valid passports among the bunch.\n", n_valids);
        
        return 0;
    }
    
Part 2
Day 4, part 2
#include "../fileload.h"
    #include <stdlib.h>
    /*
     *  byr (Birth Year)
     *  iyr (Issue Year)
     *  eyr (Expiration Year)
     *  hgt (Height)
     *  hcl (Hair Color)
     *  ecl (Eye Color)
     *  pid (Passport ID)
     *  cid (Country ID)
     */
    typedef struct field{
        int bitcode;
        char label[4];
        int (*validate)(const void *);
    }field;
    
    int valBYR(const void*value){
        char * str = (char*)value;
        int yr = atoi(str);
        if(strlen(str) == 4 && 1920<=yr && yr <= 2002) return 1;
        return 0;
    }
    int valIYR(const void*value){
        char * str = (char*)value;
        int yr = atoi(str);
        if(        strlen(str) == 4 
                && 2010<=yr 
                && yr<=2020) 
            return 1;
        return 0;
    }         
    int valEYR(const void*value){
        char * str = (char*)value;
        int yr = atoi(str);
        if(        strlen(str) == 4 
                && 2020<=yr 
                && yr<=2030) 
            return 1;
        return 0;
    }         
    int valHGT(const void*value){
        char * str = (char*)value;
        int len = strlen(str);
        if(strcmp(str+len-2, "cm") == 0){
            int h = atoi(str);
            if(    150 <= h && h <= 193) return 1;
        }else
        if(strcmp(str+len-2, "in") == 0){
            int h = atoi(str);
            if(    59 <= h && h <= 76) return 1;
        }
        
        return 0;
    }         
    int valHCL(const void*value){
        char * str = (char*)value;
        if(strlen(str) != 7) return 0;
        if(str[0] != '#') return 0;
        for(int i = 1; i < 7; i++){
            if (     !(str[i]>=48&&str[i]<=57) 
                  &&!(str[i]>=97&&str[i]<=102)) return 0;
        }
        return 1;
    }         
    int valECL(const void*value){
        char * str = (char*)value;
        if(strcmp(str,"amb")==0)return 1;
        if(strcmp(str,"blu")==0)return 1;
        if(strcmp(str,"brn")==0)return 1;
        if(strcmp(str,"gry")==0)return 1;
        if(strcmp(str,"grn")==0)return 1;
        if(strcmp(str,"hzl")==0)return 1;
        if(strcmp(str,"oth")==0)return 1;
        return 0;
    }         
    int valPID(const void*value){
        char * str = (char*)value;
        if(strlen(str) != 9) return 0;
        for(int i = 0; i < 9; i++){
            if (str[i]<48||str[i]>57) return 0;
        }
        return 1;
    }         
    int valCID(const void*value){
        //lol no
        return 1;
    }
    
    int main (int argc, char**argv){
        field fields[8];
        strcpy(fields[0].label, "BYR"); fields[0].validate = valBYR;
        strcpy(fields[1].label, "IYR"); fields[1].validate = valIYR;
        strcpy(fields[2].label, "EYR"); fields[2].validate = valEYR;
        strcpy(fields[3].label, "HGT"); fields[3].validate = valHGT;
        strcpy(fields[4].label, "HCL"); fields[4].validate = valHCL;
        strcpy(fields[5].label, "ECL"); fields[5].validate = valECL;
        strcpy(fields[6].label, "PID"); fields[6].validate = valPID;
        strcpy(fields[7].label, "CID"); fields[7].validate = valCID;
    
        int all_on = 0;
        for(int i = 0; i<8; i++){
            fields[i].bitcode = 1<<i;
            all_on = all_on | fields[i].bitcode;
            fields[i].label[3] = '\0';
        }
    
    
        if(argc < 2){
            printf("not enough inputs\n");
            return -1;
        }
        char * input = file_contents(argv[1]);
        char*dispos = input;
    
        int n_valids = 0;
        int bitmap_default = 0 | CID;
        int bitmap = 0 | CID;
    
        char * line= 0;
        char * val = 0;
        char * fieldname = 0;
        while((line = strsep(&dispos, "\n")) != NULL){
            if(strlen(line) == 0) { 
                //printf("Passport done\n");
                if (bitmap == all_on) {
                    //printf("Valid passport\n");
                    n_valids++;
                }else{
                    //printf("Invalid passport\n");
                }
                bitmap = bitmap_default | CID;
                continue;
            }
            while(val =strsep(&line, " ")){
                fieldname = strsep(&val, ":");
                for(int i = 0; i<8; i++){
                    if(strcasecmp(fieldname, fields[i].label) == 0){
                        bitmap = bitmap | (fields[i].bitcode
                                          *fields[i].validate(val));
                        break;
                    }
                }
            }
        };
        printf("There are %d valid passports among the bunch.\n", n_valids);
        
        return 0;
    }
    

End.


Tags: