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+s[run][1];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 slopevals[2][15] = {{ 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", slopevals[0][i], slopevals[1][j]);
                int xpos = 0;
                int ypos = 0;
                int n\_trees = 0;
                while(ypos < height){
                    xpos = (xpos+slopevals[0][i])%width;
                    ypos = ypos+slopevals[1][j];
                    //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: