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.

archivist/day01-1.c
archivist/day01-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 = 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;
    }
    
archivist/day01-2.c
archivist/day01-2.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 = 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;
    }
    
archivist/day01-3.c
archivist/day01-3.c
#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;
    }
    
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;
    }
    
archivist/day03-1.c
archivist/day03-1.c
#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;
    }
    
archivist/day03-2.c
archivist/day03-2.c
#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++;;}
archivist/day03-3.c
archivist/day03-3.c
#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;
    }
    
archivist/day04-1.c
archivist/day04-1.c
#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;
    }
    
archivist/day04-2.c
archivist/day04-2.c
#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;
    }
    
archivist/day05-1.c
archivist/day05-1.c
#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define PLANE_LENGTH 128
    #define PLANE_WIDTH 8
    
    int blookup(int min,int max, char * code, int n_splits){
        for(int i = 0; i < n_splits; i++){
            if(code[i]=='B' || code[i]=='R') 
                min = (min+max)/2+1;
            if(code[i]=='F' || code[i]=='L') 
                max = (min+max)/2;
        }
        //printf("min %d, max %d\n", min, max);
        return min;
    };
    
    int main (int argc, char**argv){
        int  length = 0;
        FILE *f = fopen(argv[1], "r");
        char *input;
        if (!f) return -1; 
        fseek(f, 0, SEEK_END);
        length = ftell(f);
        rewind(f);
        input = (char*)malloc(sizeof(char) * (length+1));
        fread(input, sizeof(char), length, f);
        //printf("length is %d\n", length);
        fclose(f);
        ((char*)input)[length] = '\0';
    
        int biggest_possible_id = (0 + 8*127+7);
        printf("Biggest possible ID: %d\n", biggest_possible_id);
        int biggest_id = 0;
        char * line = 0;
        while((line = strsep(&input, "\n"))!= NULL){
            if(strlen(line) == 0) break;
            int row = 0;
            int column = 0;
            int id = 0;
            //printf("%s\n", line);
            //boarding seat code is 10 chars long
            row = blookup(0, PLANE_LENGTH-1, line,7);
            column = blookup(0, PLANE_WIDTH-1, line+7,3);
            id=row*8+column;
            //printf("Row %d, Column %d, ID %d\n", row, column, id);
            if(id>biggest_id) biggest_id=id;
        }
            printf("Biggest ID so far: %d\n", biggest_id);
        
        return 0;
    }
    
archivist/day05-2.c
archivist/day05-2.c
#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define PLANE_LENGTH 128
    #define PLANE_WIDTH 8
    
    int compare(const void* a, const void* b){
        return (*(int *)a < *(int *)b) ? -1 : 1;
        return 0;
    }
    
    int blookup(int min,int max, char * code, int n_splits){
        for(int i = 0; i < n_splits; i++){
            if(code[i]=='B' || code[i]=='R') 
                min = (min+max)/2+1;
            if(code[i]=='F' || code[i]=='L') 
                max = (min+max)/2;
        }
        //printf("min %d, max %d\n", min, max);
        return min;
    };
    
    int main (int argc, char**argv){
        int  length = 0;
        FILE *f = fopen(argv[1], "r");
        char *input;
        if (!f) return -1; 
        fseek(f, 0, SEEK_END);
        length = ftell(f); rewind(f);
        input = (char*)malloc(sizeof(char) * (length+1));
        fread(input, sizeof(char), length, f);
        fclose(f);
        ((char*)input)[length] = '\0';
        int n_lines = 0;
        for(int i = 0; i < length; i++){
            if(input[i] == '\n')
                n_lines++;
        }
    
        printf("no. of lines: %d\n", n_lines);
        int * idlist = malloc(sizeof(int)*n_lines);
    
        int list_index = 0;
        char * line = 0;
        while((line = strsep(&input, "\n"))!= NULL){
            if(strlen(line) == 0) break;
            int row = 0;
            int column = 0;
            int id = 0;
            row = blookup(0, PLANE_LENGTH-1, line,7);
            column = blookup(0, PLANE_WIDTH-1, line+7,3);
            id=row*8+column;
            idlist[list_index++] = id;
        }
        qsort(idlist, n_lines, sizeof(int), &compare);
        for(int i = 0; i < n_lines-1; i++){
            if ((idlist[i+1]-idlist[i]) > 1){
                printf("ID between %d and %d\n", idlist[i], idlist[i+1]);
                printf("My ID is %d\n", idlist[i]+1);
            }
        }
        return 0;
    }
    
archivist/day06.c
archivist/day06.c
#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    const int coffset = 97;
    
    int countbits(int N){
        int count = 0;
        while(N > 0){
            count += N & 1;
            N >>= 1;
        }
        return count;
    }
    
    int part1(char* in);
    int part2(char* in);
    
    int main (int argc, char**argv){
        int  length = 0;
        FILE *f = fopen(argv[1], "r");
        char *input;
        if (!f) return -1; 
        fseek(f, 0, SEEK_END);
        length = ftell(f);
        rewind(f);
        input = (char*)malloc(sizeof(char) * (length+1));
        fread(input, sizeof(char), length, f);
        fclose(f);
        ((char*)input)[length] = '\0';
    
        //part1(input);    
        part2(input);
        return 0;
    }
    
    int part1(char* in){
        char * line = 0;
        int sum = 0;
        int groupbits = 0;
        while((line = strsep(&in, "\n"))!= NULL){
            if(strlen(line) == 0){
                sum += countbits(groupbits);
                groupbits = 0;
                continue;
            }
            for(int i=0;i<strlen(line);i++){
                groupbits |= (1 << ((int)line[i] - coffset));
            }
        }
        printf("Sum of sums: %d\n", sum);
    }
    
    int part2(char* in){
        char * line = 0;
        int sum = 0;
        int groupbits = ~0;
        int passengerbits = 0;
        while((line = strsep(&in, "\n"))!= NULL){
            if(strlen(line) == 0){
                sum += countbits(groupbits);
                groupbits = ~0;
                continue;
            }
            for(int i=0;i<strlen(line);i++){
                passengerbits |= (1 << ((int)line[i] - coffset));
            }
            groupbits &= passengerbits;
            passengerbits = 0;
        }
        printf("Sum of sums: %d\n", sum);
    }
    
archivist/day07.lisp
archivist/day07.lisp
(asdf:load-system :split-sequence)
    (asdf:load-system :iterate)
    (defpackage #:aoc-7
      (:use #:cl #:split-sequence #:iterate))
    (in-package #:aoc-7)
    
    (defun cat (&rest strs)
        (concatenate 'string strs))
    
    (defun replace-all (string part replacement &key (test #'char=))
      "Returns a new string in which all the occurences of the part 
    is replaced with replacement."
      (with-output-to-string (out)
        (loop with part-length = (length part)
              for old-pos = 0 then (+ pos part-length)
              for pos = (search part string
                                :start2 old-pos
                                :test test)
              do (write-string string out
                               :start old-pos
                               :end (or pos (length string)))
              when pos do (write-string replacement out)
              while pos)))
    
    (defun getinput ()
        (let* ((fname "testinput")
               (lines (uiop:read-file-lines  fname)))
          lines
          ))
    
    (defun replac (lines)
        (mapcar 
            (lambda (x)
              (last (mapcar 
                (lambda (s r)
                  (setf x (replace-all x s r)))
                '("," "." " bags " " bag " "contain " "no other" " bags" " bag")
                </span><span style="color: #cccccc">(</span><span style="color: #cd0000">""</span>  <span style="color: #cd0000">""</span>  <span style="color: #3399cc">,</span><span style="color: #cccccc">(</span><span style="color: #cd00cd">string</span> <span style="color: #cd0000">#\newline</span><span style="color: #cccccc">)</span> <span style="color: #3399cc">,</span><span style="color: #cccccc">(</span><span style="color: #cd00cd">string</span> <span style="color: #cd0000">#\newline</span><span style="color: #cccccc">)</span> <span style="color: #cd0000">""</span> <span style="color: #cd0000">""</span> <span style="color: #cd0000">""</span> <span style="color: #cd0000">""</span><span style="color: #cccccc">)))</span>
              <span style="color: #cccccc">)</span>
            <span style="color: #00cdcd">lines</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">)</span>
    
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">split</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">lines</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">remove-if</span>
          <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
            <span style="color: #cccccc">(</span><span style="color: #cd00cd">eq</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span> <span style="color: #cd00cd">1</span><span style="color: #cccccc">))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> 
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                    <span style="color: #cccccc">(</span><span style="color: #00cdcd">split-sequence</span> <span style="color: #cd0000">#\newline</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                                    <span style="color: #cd0000">:remove-empty-subseqs</span> <span style="color: #cccccc">t))</span>
            <span style="color: #00cdcd">lines</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">split-num</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">str</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">parse-integer</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">split-sequence</span> <span style="color: #cd0000">#\space</span> <span style="color: #00cdcd">str</span> <span style="color: #cd0000">:count</span> <span style="color: #cd00cd">1</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">apply</span> 
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span> <span style="color: #00cdcd">y</span><span style="color: #cccccc">)</span> 
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">concatenate</span> <span style="color: #cd0000">'string</span> <span style="color: #00cdcd">x</span> <span style="color: #00cdcd">y</span><span style="color: #cccccc">))</span> 
            <span style="color: #cccccc">(</span><span style="color: #00cdcd">split-sequence</span> <span style="color: #cd0000">#\space</span> <span style="color: #00cdcd">str</span> <span style="color: #cd0000">:from-end</span> <span style="color: #cccccc">t</span> <span style="color: #cd0000">:count</span> <span style="color: #cd00cd">2</span><span style="color: #cccccc">))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">line-num</span>  <span style="color: #cccccc">(</span><span style="color: #00cdcd">line</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">cons</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">replace-all</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">line</span><span style="color: #cccccc">)</span> <span style="color: #cd0000">" "</span> <span style="color: #cd0000">""</span><span style="color: #cccccc">)</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">split-num</span>
                      <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">line</span><span style="color: #cccccc">))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">split-nums</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">lines</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span>
          <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                  <span style="color: #cccccc">(</span><span style="color: #00cdcd">line-num</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">))</span>
          <span style="color: #00cdcd">lines</span><span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">get-data</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">split-nums</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">split</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">replac</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getinput</span><span style="color: #cccccc">)))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">searchline</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">str</span> <span style="color: #00cdcd">line</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">reduce</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">&optional</span> <span style="color: #00cdcd">x</span> <span style="color: #00cdcd">y</span><span style="color: #cccccc">)</span>
                  <span style="color: #cccccc">(</span><span style="color: #cd00cd">or</span> <span style="color: #00cdcd">x</span> <span style="color: #00cdcd">y</span><span style="color: #cccccc">))</span>
                <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">sub</span><span style="color: #cccccc">)</span>
                          <span style="color: #cccccc">(</span><span style="color: #cd00cd">equal</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #00cdcd">sub</span><span style="color: #cccccc">)</span>
                                 <span style="color: #00cdcd">str</span><span style="color: #cccccc">))</span>
                        <span style="color: #00cdcd">line</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">getline</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">str</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> 
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">remove-if-not</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                           <span style="color: #cccccc">(</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">str</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">)))</span>
                         <span style="color: #00cdcd">data</span><span style="color: #cccccc">)))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">remline</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">str</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">remove-if</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                     <span style="color: #cccccc">(</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">str</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">)))</span>
                   <span style="color: #00cdcd">data</span><span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">rep-element</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">el</span> <span style="color: #00cdcd">data</span> <span style="color: #cdcd00">&optional</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">keepln</span> <span style="color: #cccccc">t))</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getline</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">second</span> <span style="color: #00cdcd">el</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">)</span>
            <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">first</span> <span style="color: #00cdcd">el</span><span style="color: #cccccc">)</span> 
                  <span style="color: #cccccc">(</span><span style="color: #00cdcd">rep-line</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getline</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">second</span> <span style="color: #00cdcd">el</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">))</span>
                            <span style="color: #00cdcd">data</span> <span style="color: #00cdcd">keepln</span><span style="color: #cccccc">))</span>
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #00cdcd">keepln</span> <span style="color: #00cdcd">el</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">first</span> <span style="color: #00cdcd">el</span><span style="color: #cccccc">))))</span>  <span style="color: #cccccc">)</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">rep-line</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">line</span> <span style="color: #00cdcd">data</span> <span style="color: #cdcd00">&optional</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">keepln</span> <span style="color: #cccccc">t))</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                  <span style="color: #cccccc">(</span><span style="color: #00cdcd">rep-element</span> <span style="color: #00cdcd">x</span> <span style="color: #00cdcd">data</span> <span style="color: #00cdcd">keepln</span><span style="color: #cccccc">))</span> 
                  <span style="color: #00cdcd">line</span><span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">rep</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">col1</span> <span style="color: #00cdcd">lines</span> <span style="color: #00cdcd">data</span> <span style="color: #cdcd00">&optional</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">keepln</span> <span style="color: #cccccc">t))</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
                  <span style="color: #cccccc">(</span><span style="color: #cd00cd">cons</span> <span style="color: #00cdcd">x</span>
                        <span style="color: #cccccc">(</span><span style="color: #00cdcd">rep-line</span> <span style="color: #00cdcd">l</span> <span style="color: #00cdcd">data</span> <span style="color: #00cdcd">keepln</span><span style="color: #cccccc">)))</span>
                <span style="color: #00cdcd">col1</span> <span style="color: #00cdcd">lines</span><span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">find-recur</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">el</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">cond</span> <span style="color: #cccccc">((</span><span style="color: #cd00cd">null</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">nil)</span>
              <span style="color: #cccccc">((</span><span style="color: #cd00cd">atom</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">el</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span>
              <span style="color: #cccccc">(t</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">el</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span>
                     <span style="color: #cccccc">t</span>
                     <span style="color: #cccccc">(</span><span style="color: #cd00cd">or</span>
                       <span style="color: #cccccc">(</span><span style="color: #00cdcd">find-recur</span> <span style="color: #00cdcd">el</span>
                                 <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span>
                       <span style="color: #cccccc">(</span><span style="color: #00cdcd">find-recur</span> <span style="color: #00cdcd">el</span>
                                 <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)))))))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">nostring</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">nl</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">or</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">atom</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">null</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)))</span> 
                      <span style="color: #cccccc">(</span><span style="color: #cd00cd">remove-if</span> 
                        <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                          <span style="color: #cccccc">(</span><span style="color: #cd00cd">typep</span> <span style="color: #00cdcd">x</span> <span style="color: #cd0000">'string</span> <span style="color: #cccccc">))</span>
                         <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
                      <span style="color: #00cdcd">l</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">cond</span> <span style="color: #cccccc">((</span><span style="color: #cd00cd">atom</span> <span style="color: #00cdcd">nl</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">nl</span><span style="color: #cccccc">)</span>
                <span style="color: #cccccc">((</span><span style="color: #cd00cd">null</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">nl</span><span style="color: #cccccc">))</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">nostring</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">nl</span><span style="color: #cccccc">))))</span>
                <span style="color: #cccccc">(t</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">nostring</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">nl</span><span style="color: #cccccc">))</span>
                         <span style="color: #cccccc">(</span><span style="color: #00cdcd">nostring</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">nl</span><span style="color: #cccccc">)))</span>
                         <span style="color: #cccccc">))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">count-recur</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">cond</span> <span style="color: #cccccc">((</span><span style="color: #cd00cd">null</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span> <span style="color: #cd00cd">0</span><span style="color: #cccccc">)</span>
             <span style="color: #cccccc">((</span><span style="color: #cd00cd">atom</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
             <span style="color: #cccccc">((</span><span style="color: #cd00cd">and</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">></span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span> <span style="color: #cd00cd">1</span><span style="color: #cccccc">)</span>
                   <span style="color: #cccccc">(</span><span style="color: #cd00cd">atom</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span>
                   <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">atom</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))))</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
                 <span style="color: #cccccc">(</span><span style="color: #cd00cd">*</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
                    <span style="color: #cccccc">(</span><span style="color: #00cdcd">count-recur</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)))))</span>
             <span style="color: #cccccc">(t</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">count-recur</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span>
                      <span style="color: #cccccc">(</span><span style="color: #00cdcd">count-recur</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part1</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">data</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">remline</span> <span style="color: #cd0000">"shinygold"</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">get-data</span><span style="color: #cccccc">)))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">fdata</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">bdata</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">count</span> <span style="color: #cd0000">'t</span> 
                 <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                    <span style="color: #cccccc">(</span><span style="color: #00cdcd">find-recur</span> <span style="color: #cd0000">"shinygold"</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">))</span>
                  <span style="color: #cccccc">(</span><span style="color: #00cdcd">rep</span> <span style="color: #00cdcd">fdata</span> <span style="color: #00cdcd">bdata</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part2</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">data</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">get-data</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">fdata</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">bdata</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">data</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">pdata</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">rep</span> <span style="color: #00cdcd">fdata</span> <span style="color: #00cdcd">bdata</span> <span style="color: #00cdcd">data</span> <span style="color: #cccccc">nil))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">gold</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getline</span> <span style="color: #cd0000">"shinygold"</span> <span style="color: #00cdcd">pdata</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">print</span> <span style="color: #00cdcd">pdata</span><span style="color: #cccccc">)</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">print</span> <span style="color: #00cdcd">gold</span><span style="color: #cccccc">)</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">print</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">nostring</span> <span style="color: #00cdcd">gold</span><span style="color: #cccccc">))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">gold</span><span style="color: #cccccc">)</span>
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">count-recur</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cdr</span> <span style="color: #00cdcd">gold</span><span style="color: #cccccc">))))</span>
    </pre>
    </blockquote>
    </details>
 </blockquote>
 <blockquote>
<a href="/resources/advent/2020/archivist/day08.lisp">archivist/day08.lisp</a>
    <details>
    <summary>archivist/day08.lisp</summary>
    <blockquote>
     <pre style="background: #000000;line-height: 125%; margin: 0;"><span></span><span style="color: #cccccc">(</span><span style="color: #00cdcd">asdf:load-system</span> <span style="color: #cd0000">:split-sequence</span><span style="color: #cccccc">)</span>
    <span style="color: #cccccc">(</span><span style="color: #00cdcd">asdf:load-system</span> <span style="color: #cd0000">:iterate</span><span style="color: #cccccc">)</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defpackage</span> <span style="color: #cd0000">#:aoc-8</span>
      <span style="color: #cccccc">(</span><span style="color: #cd0000">:use</span> <span style="color: #cd0000">#:cl</span> <span style="color: #cd0000">#:split-sequence</span> <span style="color: #cd0000">#:iterate</span><span style="color: #cccccc">))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">in-package</span> <span style="color: #cd0000">#:aoc-8</span><span style="color: #cccccc">)</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">cat</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">&rest</span> <span style="color: #00cdcd">strs</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">concatenate</span> <span style="color: #cd0000">'string</span> <span style="color: #00cdcd">strs</span><span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">replace-all</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">string</span> <span style="color: #00cdcd">part</span> <span style="color: #00cdcd">replacement</span> <span style="color: #cdcd00">&key</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">test</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">char=</span><span style="color: #cccccc">))</span>
      <span style="color: #cd0000">"Returns a new string in which all the occurences of the part </span>
    <span style="color: #cd0000">is replaced with replacement."</span>
      <span style="color: #cccccc">(</span><span style="color: #cd00cd">with-output-to-string</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">out</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">loop</span> <span style="color: #00cdcd">with</span> <span style="color: #00cdcd">part-length</span> <span style="color: #cd00cd">=</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">part</span><span style="color: #cccccc">)</span>
              <span style="color: #00cdcd">for</span> <span style="color: #00cdcd">old-pos</span> <span style="color: #cd00cd">=</span> <span style="color: #cd00cd">0</span> <span style="color: #00cdcd">then</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #00cdcd">pos</span> <span style="color: #00cdcd">part-length</span><span style="color: #cccccc">)</span>
              <span style="color: #00cdcd">for</span> <span style="color: #00cdcd">pos</span> <span style="color: #cd00cd">=</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">search</span> <span style="color: #00cdcd">part</span> <span style="color: #cd00cd">string</span>
                                <span style="color: #cd0000">:start2</span> <span style="color: #00cdcd">old-pos</span>
                                <span style="color: #cd0000">:test</span> <span style="color: #00cdcd">test</span><span style="color: #cccccc">)</span>
              <span style="color: #cd00cd">do</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">write-string</span> <span style="color: #cd00cd">string</span> <span style="color: #00cdcd">out</span>
                               <span style="color: #cd0000">:start</span> <span style="color: #00cdcd">old-pos</span>
                               <span style="color: #cd0000">:end</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">or</span> <span style="color: #00cdcd">pos</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #cd00cd">string</span><span style="color: #cccccc">)))</span>
              <span style="color: #cd00cd">when</span> <span style="color: #00cdcd">pos</span> <span style="color: #cd00cd">do</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">write-string</span> <span style="color: #00cdcd">replacement</span> <span style="color: #00cdcd">out</span><span style="color: #cccccc">)</span>
              <span style="color: #00cdcd">while</span> <span style="color: #00cdcd">pos</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defstruct</span> <span style="color: #00cdcd">machine</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">pointer</span> <span style="color: #cd00cd">0</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">accum</span> <span style="color: #cd00cd">0</span><span style="color: #cccccc">))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defparameter</span> <span style="color: #00cdcd">*vm*</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">make-machine</span><span style="color: #cccccc">))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">copy-array</span>  <span style="color: #cccccc">(</span><span style="color: #00cdcd">array</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">new</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">make-array</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">array-dimensions</span> <span style="color: #00cdcd">array</span><span style="color: #cccccc">)</span> <span style="color: #cd0000">:displaced-to</span> <span style="color: #00cdcd">array</span><span style="color: #cccccc">)))</span>
                   <span style="color: #cccccc">(</span><span style="color: #cd00cd">adjust-array</span> <span style="color: #00cdcd">new</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">array-dimensions</span> <span style="color: #00cdcd">array</span><span style="color: #cccccc">)</span> <span style="color: #cd0000">:displaced-to</span> <span style="color: #cccccc">nil)))</span> 
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">getinput</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">fname</span> <span style="color: #cd0000">"bigboylarge"</span><span style="color: #cccccc">)</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">lines</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">uiop:read-file-lines</span>  <span style="color: #00cdcd">fname</span><span style="color: #cccccc">)))</span>
          <span style="color: #00cdcd">lines</span>
          <span style="color: #cccccc">))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">process</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">input</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">make-array</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">input</span><span style="color: #cccccc">)</span> 
                    <span style="color: #cd0000">:initial-contents</span>
                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span> 
                              <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">str-to-op</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                                    <span style="color: #cccccc">nil))</span>
                            <span style="color: #00cdcd">input</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">str-to-op</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">str</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">substr</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">subseq</span> <span style="color: #00cdcd">str</span> <span style="color: #cd00cd">0</span> <span style="color: #cd00cd">3</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cond</span> <span style="color: #cccccc">((</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">substr</span> <span style="color: #cd0000">"nop"</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">nop</span><span style="color: #cccccc">)</span> 
                      <span style="color: #cccccc">((</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">substr</span> <span style="color: #cd0000">"acc"</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">acc</span><span style="color: #cccccc">)</span>
                      <span style="color: #cccccc">((</span><span style="color: #cd00cd">equal</span> <span style="color: #00cdcd">substr</span> <span style="color: #cd0000">"jmp"</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">jmp</span><span style="color: #cccccc">))</span>
                <span style="color: #cccccc">(</span><span style="color: #cd00cd">parse-integer</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">subseq</span> <span style="color: #00cdcd">str</span> <span style="color: #cd00cd">4</span><span style="color: #cccccc">)))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">nop</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">vm</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">incf</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">acc</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">vm</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-accum</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-accum</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span>
                 <span style="color: #00cdcd">val</span><span style="color: #cccccc">))</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">incf</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">jmp</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">vm</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span>
                 <span style="color: #00cdcd">val</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part1</span> <span style="color: #cccccc">()</span>
      <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">vm</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">make-machine</span><span style="color: #cccccc">))</span>
            <span style="color: #cccccc">(</span><span style="color: #00cdcd">code</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">process</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getinput</span><span style="color: #cccccc">))))</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> 
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">while</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">and</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">aref</span> <span style="color: #00cdcd">code</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">))))</span>
                      <span style="color: #cccccc">(</span><span style="color: #cd00cd"><=</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">))))</span>
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">op</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">aref</span> <span style="color: #00cdcd">code</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">))))</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">t)</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">funcall</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">vm</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">print</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-accum</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part2-run</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">code</span><span style="color: #cccccc">)</span>
      <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">vm</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">make-machine</span><span style="color: #cccccc">)))</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> 
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">while</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">and</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd"><</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">))</span>
                      <span style="color: #cccccc">(</span><span style="color: #cd00cd">>=</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span> <span style="color: #cd00cd">0</span><span style="color: #cccccc">)</span>
                   <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">aref</span> <span style="color: #00cdcd">code</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">))))</span>
                      <span style="color: #cccccc">))</span>
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">op</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">aref</span> <span style="color: #00cdcd">code</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">))))</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">t)</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">funcall</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">vm</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">))</span>
              <span style="color: #cccccc">)</span>
            <span style="color: #cccccc">(</span><span style="color: #00cdcd">finally</span>  <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">>=</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-pointer</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span> 
                                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">))</span>
                                  <span style="color: #cccccc">(</span><span style="color: #00cdcd">machine-accum</span> <span style="color: #00cdcd">vm</span><span style="color: #cccccc">)</span>
                                  <span style="color: #cccccc">nil))))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">swap</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">fun</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">cond</span> <span style="color: #cccccc">((</span><span style="color: #cd00cd">eq</span> <span style="color: #00cdcd">fun</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">jmp</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">nop</span><span style="color: #cccccc">)</span>
              <span style="color: #cccccc">((</span><span style="color: #cd00cd">eq</span> <span style="color: #00cdcd">fun</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">nop</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">jmp</span><span style="color: #cccccc">)</span>
              <span style="color: #cccccc">(t</span> <span style="color: #00cdcd">fun</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part2</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">code</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">process</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getinput</span><span style="color: #cccccc">))))</span>
          <span style="color: #6f6fff">;(part2-run vcode)</span>
          <span style="color: #6f6fff">;reset</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">print</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1-</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">for</span> <span style="color: #00cdcd">i</span> <span style="color: #00cdcd">from</span> <span style="color: #cd00cd">0</span> <span style="color: #00cdcd">to</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1-</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">)))</span> 
                   <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">eq</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">mod</span> <span style="color: #00cdcd">i</span> <span style="color: #cd00cd">10000</span><span style="color: #cccccc">)</span> <span style="color: #cd00cd">0</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">print</span> <span style="color: #00cdcd">i</span><span style="color: #cccccc">))</span>
                   <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">op</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">aref</span> <span style="color: #00cdcd">code</span> <span style="color: #00cdcd">i</span><span style="color: #cccccc">)))</span>
                     <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">eq</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #00cdcd">acc</span><span style="color: #cccccc">))</span>
                         <span style="color: #cccccc">(</span><span style="color: #cdcd00">progn</span> 
                           <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> 
                                 <span style="color: #cccccc">(</span><span style="color: #00cdcd">swap</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)))</span>
                           <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">res</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">part2-run</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">)))</span>
                             <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #00cdcd">res</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #00cdcd">res</span><span style="color: #cccccc">)))</span>
                           
                           <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">for</span> <span style="color: #00cdcd">i</span> <span style="color: #00cdcd">from</span> <span style="color: #cd00cd">0</span> <span style="color: #00cdcd">to</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1-</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">code</span><span style="color: #cccccc">)))</span>
                                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">aref</span> <span style="color: #00cdcd">code</span> <span style="color: #00cdcd">i</span><span style="color: #cccccc">))</span> <span style="color: #cccccc">nil))</span>
                           <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)</span> 
                                 <span style="color: #cccccc">(</span><span style="color: #00cdcd">swap</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">caar</span> <span style="color: #00cdcd">op</span><span style="color: #cccccc">)))))))))</span>
    </pre>
    </blockquote>
    </details>
 </blockquote>
 <blockquote>
<a href="/resources/advent/2020/archivist/day09.lisp">archivist/day09.lisp</a>
    <details>
    <summary>archivist/day09.lisp</summary>
    <blockquote>
     <pre style="background: #000000;line-height: 125%; margin: 0;"><span></span><span style="color: #cccccc">(</span><span style="color: #00cdcd">asdf:load-system</span> <span style="color: #cd0000">:split-sequence</span><span style="color: #cccccc">)</span>
    <span style="color: #cccccc">(</span><span style="color: #00cdcd">asdf:load-system</span> <span style="color: #cd0000">:iterate</span><span style="color: #cccccc">)</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defpackage</span> <span style="color: #cd0000">#:aoc-9</span>
      <span style="color: #cccccc">(</span><span style="color: #cd0000">:use</span> <span style="color: #cd0000">#:cl</span> <span style="color: #cd0000">#:split-sequence</span> <span style="color: #cd0000">#:iterate</span><span style="color: #cccccc">))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">in-package</span> <span style="color: #cd0000">#:aoc-9</span><span style="color: #cccccc">)</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">getinput</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">parse-integer</span> 
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">uiop:read-file-lines</span> <span style="color: #cd0000">"input"</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">split</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">vlist</span> <span style="color: #00cdcd">index</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">values</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">subseq</span> <span style="color: #00cdcd">vlist</span> <span style="color: #cd00cd">0</span> <span style="color: #00cdcd">index</span><span style="color: #cccccc">)</span> 
                <span style="color: #cccccc">(</span><span style="color: #cd00cd">nthcdr</span> <span style="color: #00cdcd">index</span> <span style="color: #00cdcd">vlist</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">seek</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">l</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">while</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
          <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">a</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">pop</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)))</span> 
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> 
              <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">for</span> <span style="color: #00cdcd">b</span> <span style="color: #00cdcd">in</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">)</span>
                <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">eq</span> <span style="color: #00cdcd">val</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #00cdcd">a</span> <span style="color: #00cdcd">b</span><span style="color: #cccccc">))</span>
                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #cccccc">t)))</span>
              <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #cccccc">t)))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part1</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">all</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getinput</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">multiple-value-bind</span> 
            <span style="color: #cccccc">(</span><span style="color: #00cdcd">preamble</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span> 
            <span style="color: #cccccc">(</span><span style="color: #00cdcd">split</span> <span style="color: #00cdcd">all</span> <span style="color: #cd00cd">25</span><span style="color: #cccccc">)</span>
            <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">while</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span>
              <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">val</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">pop</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">))</span>
                    <span style="color: #cccccc">(</span><span style="color: #00cdcd">s</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">remove-if</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">></span> <span style="color: #00cdcd">x</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">))</span> 
                                  <span style="color: #cccccc">(</span><span style="color: #cd00cd">sort</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">copy-seq</span> <span style="color: #00cdcd">preamble</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd"><</span><span style="color: #cccccc">))))</span>
                <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">seek</span> <span style="color: #00cdcd">s</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">))</span>
                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">))</span>
                <span style="color: #cccccc">(</span><span style="color: #cd00cd">pop</span> <span style="color: #00cdcd">preamble</span><span style="color: #cccccc">)</span>
                <span style="color: #cccccc">(</span><span style="color: #cd00cd">nconc</span> <span style="color: #00cdcd">preamble</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">list</span> <span style="color: #00cdcd">val</span><span style="color: #cccccc">)))))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part2</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">all-in</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getinput</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">all</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">make-array</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">all-in</span><span style="color: #cccccc">)</span>
                                      <span style="color: #cd0000">:initial-contents</span>
                                      <span style="color: #00cdcd">all-in</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">val</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">part1</span><span style="color: #cccccc">)))</span>
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">for</span> <span style="color: #00cdcd">i</span> <span style="color: #00cdcd">from</span> <span style="color: #cd00cd">0</span> <span style="color: #00cdcd">to</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1-</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">all</span><span style="color: #cccccc">)))</span>
            <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">ret</span> 
                <span style="color: #cccccc">(</span><span style="color: #00cdcd">iterate</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">for</span> <span style="color: #00cdcd">j</span> <span style="color: #00cdcd">from</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1+</span> <span style="color: #00cdcd">i</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">to</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1-</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">all</span><span style="color: #cccccc">)))</span>
                  <span style="color: #cccccc">(</span><span style="color: #cdcd00">let</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">s</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">subseq</span> <span style="color: #00cdcd">all</span> <span style="color: #00cdcd">i</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">1+</span> <span style="color: #00cdcd">j</span><span style="color: #cccccc">))))</span>
                     <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">eq</span> <span style="color: #00cdcd">val</span>
                            <span style="color: #cccccc">(</span><span style="color: #cd00cd">reduce</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">+</span>
                                    <span style="color: #00cdcd">s</span><span style="color: #cccccc">))</span>
                        <span style="color: #cccccc">(</span><span style="color: #cdcd00">progn</span> 
                          <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">reduce</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">min</span> <span style="color: #00cdcd">s</span><span style="color: #cccccc">)</span>
                                     <span style="color: #cccccc">(</span><span style="color: #cd00cd">reduce</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">max</span> <span style="color: #00cdcd">s</span><span style="color: #cccccc">)))))))))</span>
              <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #00cdcd">ret</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">return</span> <span style="color: #00cdcd">ret</span><span style="color: #cccccc">))))))</span>
    </pre>
    </blockquote>
    </details>
 </blockquote>
 <blockquote>
<a href="/resources/advent/2020/archivist/day10.lisp">archivist/day10.lisp</a>
    <details>
    <summary>archivist/day10.lisp</summary>
    <blockquote>
     <pre style="background: #000000;line-height: 125%; margin: 0;"><span></span><span style="color: #cccccc">(</span><span style="color: #00cdcd">asdf:load-system</span> <span style="color: #cd0000">:split-sequence</span><span style="color: #cccccc">)</span>
    <span style="color: #cccccc">(</span><span style="color: #00cdcd">asdf:load-system</span> <span style="color: #cd0000">:iterate</span><span style="color: #cccccc">)</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defpackage</span> <span style="color: #cd0000">#:aoc-10</span>
      <span style="color: #cccccc">(</span><span style="color: #cd0000">:use</span> <span style="color: #cd0000">#:cl</span> <span style="color: #cd0000">#:split-sequence</span> <span style="color: #cd0000">#:iterate</span><span style="color: #cccccc">))</span>
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">in-package</span> <span style="color: #cd0000">#:aoc-10</span><span style="color: #cccccc">)</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">getinput</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span><span style="color: #cccccc">)</span>
                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">parse-integer</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">))</span>
          <span style="color: #cccccc">(</span><span style="color: #00cdcd">uiop:read-file-lines</span> <span style="color: #cd0000">"bigboy.txt"</span><span style="color: #cccccc">)))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part1</span> <span style="color: #cccccc">()</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">let*</span> <span style="color: #cccccc">((</span><span style="color: #00cdcd">input</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">sort</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">getinput</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd"><</span><span style="color: #cccccc">))</span>
               <span style="color: #cccccc">(</span><span style="color: #00cdcd">proc</span> 
                 <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">x</span> <span style="color: #00cdcd">y</span><span style="color: #cccccc">)</span>
                           <span style="color: #cccccc">(</span><span style="color: #cd00cd">-</span> <span style="color: #00cdcd">y</span> <span style="color: #00cdcd">x</span><span style="color: #cccccc">))</span>
                   <span style="color: #cccccc">(</span><span style="color: #cd00cd">cons</span> <span style="color: #cd00cd">0</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">subseq</span> <span style="color: #00cdcd">input</span> <span style="color: #cd00cd">0</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">length</span> <span style="color: #00cdcd">input</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">))</span>
                   <span style="color: #cccccc">(</span><span style="color: #cd00cd">nreverse</span> 
                     <span style="color: #cccccc">(</span><span style="color: #cd00cd">cons</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">+</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">last</span> <span style="color: #00cdcd">input</span><span style="color: #cccccc">))</span> <span style="color: #cd00cd">3</span><span style="color: #cccccc">)</span> 
                           <span style="color: #cccccc">(</span><span style="color: #cd00cd">nreverse</span> <span style="color: #00cdcd">input</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">)))))</span>
          <span style="color: #cccccc">(</span><span style="color: #cd00cd">*</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">count</span> <span style="color: #cd00cd">1</span> <span style="color: #00cdcd">proc</span><span style="color: #cccccc">)</span>
             <span style="color: #cccccc">(</span><span style="color: #cd00cd">count</span> <span style="color: #cd00cd">3</span> <span style="color: #00cdcd">proc</span><span style="color: #cccccc">))))</span>
    
    <span style="color: #cccccc">(</span><span style="color: #cd00cd">defun</span> <span style="color: #00cdcd">part2-walk</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">l</span> <span style="color: #00cdcd">cache</span><span style="color: #cccccc">)</span>
        <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">not</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">cadr</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span> 
         <span style="color: #cd00cd">1</span>
         <span style="color: #cccccc">(</span><span style="color: #cd00cd">reduce</span> <span style="color: #cccccc">#'</span><span style="color: #cd00cd">+</span> 
           <span style="color: #cccccc">(</span><span style="color: #cd00cd">mapcar</span> 
             <span style="color: #cccccc">(</span><span style="color: #cdcd00">lambda</span> <span style="color: #cccccc">(</span><span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span> 
              <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">and</span> 
                    <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span> 
                    <span style="color: #cccccc">(</span><span style="color: #cd00cd"><=</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">-</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">l</span><span style="color: #cccccc">))</span> <span style="color: #cd00cd">3</span><span style="color: #cccccc">))</span>
                <span style="color: #cccccc">(</span><span style="color: #cd00cd">multiple-value-bind</span> 
                  <span style="color: #cccccc">(</span><span style="color: #00cdcd">result</span> <span style="color: #00cdcd">exists</span><span style="color: #cccccc">)</span>
                  <span style="color: #cccccc">(</span><span style="color: #cd00cd">gethash</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">cache</span><span style="color: #cccccc">)</span>
                  <span style="color: #cccccc">(</span><span style="color: #cdcd00">if</span> <span style="color: #00cdcd">exists</span> <span style="color: #00cdcd">result</span>
                      <span style="color: #cccccc">(</span><span style="color: #cd00cd">setf</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">gethash</span> <span style="color: #cccccc">(</span><span style="color: #cd00cd">car</span> <span style="color: #00cdcd">remain</span><span style="color: #cccccc">)</span> <span style="color: #00cdcd">cache</span><span style="color: #cccccc">)</span>
                          <span style="color: #cccccc">(</span><span style="color: #00cdcd">part2-walk</span> <span style="color: #00cdcd">remain</span> <span style="color: #00cdcd">cache</span><span style="color: #cccccc">))))</span>
                <span style="color: #cd00cd">0</span><span style="color: #cccccc">))</span>
             <span style="color: #3399cc">(,(cdr l) ,(cddr l) ,(cdddr l))))))
    
    (defun part2-slow ()
        (let* ((input (cons 0 
                        (append (sort (getinput) #'<)
                          (list 
                            (+ 3 
                               (apply #'max 
                                      (getinput)))))))
               (mycache (make-hash-table :test #'equal)))
          (part2-walk input mycache)))
    
    (defun validp (a b)
        (<= (- b a) 3))
    
    (defun getif (val later)
        (if (validp val (car later))
            (cadr later)
            0))
    
    (defun part2 ()
      (let* ((input (nreverse (cons 0 (sort (getinput) #'<))))
             (tri (list (pop input) 1))
             (two (list (pop input) 1))
             (one-f (pop input))
             (one (list one-f
                        (+ (getif one-f two)
                           (getif one-f tri)))))
        (iterate (while input)
          (let* ((newval (pop input))
                 (new (list newval
                            (reduce #'+
                              (mapcar 
                                (lambda (x)
                                  (getif newval x))
                                (list tri two one))))))
            (setf tri (copy-list two)) 
            (setf two (copy-list one)) 
            (setf one (copy-list new))))
        (log (cadr one) 10)))
    
archivist/day11.lisp
archivist/day11.lisp
(asdf:load-system :split-sequence)
    (asdf:load-system :iterate)
    (asdf:load-system :alexandria)
    (defpackage #:aoc-11
      (:use #:cl #:split-sequence #:iterate #:alexandria))
    (in-package #:aoc-11)
    
    (defun getinput ()
        (let ((in (uiop:read-file-lines "input")))
          (make-array (list (length in)
                            (length (car in)))
                      :initial-contents in)))
    
    (defun cval (c)
        (cond ((eq c #\#) 1)
              ((eq c #\L) 0)
              ((eq c #.) 0)))
    
    (defun getval (x y arr dims)
        (if (or (> x (1- (car dims)))
                (< x 0)
                (> y (1- (cadr dims)))
                (< y 0)
                )
            0
            (cval (aref arr x y))))
    
    (defun next (x y arr dims)
        (let ((sum 
                (+ 
                  (getval (1+ x) y arr dims)
                  (getval (1+ x) (1+ y) arr dims)
                  (getval x (1+ y) arr dims)
                  (getval (1- x) (1+ y) arr dims)
                  (getval (1- x) y arr dims)
                  (getval (1- x) (1- y) arr dims)
                  (getval x (1- y) arr dims)
                  (getval (1+ x) (1- y) arr dims)
                   
                   ))
              (state (aref arr x y)))
          (cond ((and (eq state #\L)
                      (eq sum 0))
                 #\#)
                ((and (eq state #\#)
                      (>= sum 4))
                 #\L) 
                (t state))))
    
    
    (defun part1 ()
        (let* ((grid (getinput))
               (grid-2 (copy-array grid))
               (dims (array-dimensions grid)))
        (do ((stable nil)
             (filled 0 ))
            ((eq stable t) filled)
            (setf stable t)
            (setf filled 0)
          (do ((i 0 (1+ i)))
              ((> i (1- (car dims))))
            (do ((j 0 (1+ j)))
                ((> j (1- (cadr dims))))
                (let ((new (next i j grid dims)))
                  (if (not (eq new (aref grid i j)))
                      (setf stable nil))
                  (setf (aref grid-2 i j) new)
                  (if (equal #\# new) (incf filled)))))
          (setf grid (copy-array grid-2)))))
    
    (defun look (x y dirx diry arr dims)
        (if (or (> x (1- (car dims)))
                (< x 0)
                (> y (1- (cadr dims)))
                (< y 0))
            0
            (if (or (eq (aref arr x y) #\#)
                    (eq (aref arr x y) #\L)) 
                (cval (aref arr x y))
                (look (+ x dirx) (+ y diry) dirx diry
                      arr dims))))
    
    (defun next-2 (x y arr dims)
        (let ((sum 
                (+ 
                  (look (1+ x) y 1 0 arr dims)
                  (look (1+ x) (1+ y) 1 1 arr dims)
                  (look x (1+ y) 0 1 arr dims)
                  (look (1- x) (1+ y) -1 1 arr dims)
                  (look (1- x) y -1 0 arr dims)
                  (look (1- x) (1- y) -1 -1 arr dims)
                  (look x (1- y) 0 -1 arr dims)
                  (look (1+ x) (1- y) 1 -1 arr dims)))
              (state (aref arr x y)))
          ;(format t "~a,~a: ~a~%" x y sum)
          (cond ((and (eq state #\L)
                      (eq sum 0))
                 #\#)
                ((and (eq state #\#)
                      (>= sum 5))
                 #\L) 
                (t state))))
    
    (defun part2 ()
        (let* ((grid (getinput))
               (grid-2 (copy-array grid))
               (dims (array-dimensions grid)))
        (do ((stable nil)
             (filled 0 ))
            ((eq stable t) filled)
            (setf stable t)
            (setf filled 0)
          (do ((i 0 (1+ i)))
              ((> i (1- (car dims))))
            (do ((j 0 (1+ j)))
                ((> j (1- (cadr dims))))
                (let ((new (next-2 i j grid dims)))
                  (if (not (eq new (aref grid i j)))
                      (setf stable nil))
                  (setf (aref grid-2 i j) new)
                  (if (equal #\# new) (incf filled)))))
          (setf grid (copy-array grid-2))
          )))
    
archivist/day12.lisp
archivist/day12.lisp
(asdf:load-system :split-sequence)
    (asdf:load-system :iterate)
    (asdf:load-system :alexandria)
    (defpackage #:aoc-12
      (:use #:cl #:split-sequence #:iterate #:alexandria))
    (in-package #:aoc-12)
    
    (defun getinput ()
        (let ((in (uiop:read-file-lines "input")))
          (mapcar (lambda (x)
                    (list (coerce (subseq x 0 1)
                                  'character)
                          (parse-integer (subseq x 1)))) 
                  in)))
    
    (defstruct ship
      (x 0)
      (y 0)
      (heading 0))
    
    (defun rot (ship d)
        (setf (ship-heading ship)
              (mod (+ d (ship-heading ship))
                   360)))
    
    (defun f (ship val)
        (let ((px (round (* val 
                            (cos (* pi (/ (ship-heading ship) 180))))))
              (py (round (* val 
                            (sin (* -1 pi (/ (ship-heading ship) 180)))))))
          (move ship px py)))
    
    (defun move (ship x y)
        (setf (ship-x ship)
              (+ (ship-x ship)
                 x))
        (setf (ship-y ship)
              (+ (ship-y ship)
                 y)))
    
    (defun intcmd (ship cmd)
        (cond 
          ((eq (car cmd) #\N) 
           (move ship 0 (cadr cmd)))
          ((eq (car cmd) #\S) 
           (move ship 0 (* -1 (cadr cmd))))
          ((eq (car cmd) #\E)
           (move ship (cadr cmd) 0))
          ((eq (car cmd) #\W) 
           (move ship (* -1 (cadr cmd)) 0))
          ((eq (car cmd) #\R) 
           (rot ship (cadr cmd)))
          ((eq (car cmd) #\L) 
           (rot ship (* -1 (cadr cmd))))
          ((eq (car cmd) #\F) 
           (f ship (cadr cmd)))
    
              ))
    
    (defun part1 ()
        (let* ((in (getinput))
               (s (make-ship)))
          (iter (while in)
            (let ((c (pop in)))
              (intcmd s c))
          (print s))
          (+ (abs (ship-x s))
             (abs (ship-y s)))))
    
    (defstruct wpt
        (x 10)
        (y 1))
    
    (defun wrot (pt val)
        (let* ((ang (* pi (/ val 180)))
               (oldx (wpt-x pt)))
          (setf (wpt-x pt)
                (+ (* (round (cos ang)) (wpt-x pt))
                   (* (round (sin ang)) (wpt-y pt))))
          (setf (wpt-y pt)
                (+ (* (round (sin (* -1 ang))) oldx)
                   (* (round (cos ang)) (wpt-y pt))))))
    
    (defun wmove (pt x y)
        (setf (wpt-x pt)
              (+ (wpt-x pt)
                 x))
        (setf (wpt-y pt)
              (+ (wpt-y pt)
                 y)))
    (defun goto (ship pt val)
        (iter (repeat val)
          (setf (ship-x ship)
                (+ (ship-x ship)
                   (wpt-x pt)))
          (setf (ship-y ship)
                (+ (ship-y ship)
                   (wpt-y pt)))))
    
    (defun wintcmd (ship pt cmd)
        (cond 
          ((eq (car cmd) #\N) 
           (wmove pt 0 (cadr cmd)))
          ((eq (car cmd) #\S) 
           (wmove pt 0 (* -1 (cadr cmd))))
          ((eq (car cmd) #\E)
           (wmove pt (cadr cmd) 0))
          ((eq (car cmd) #\W) 
           (wmove pt (* -1 (cadr cmd)) 0))
          ((eq (car cmd) #\R) 
           (wrot pt (cadr cmd)))
          ((eq (car cmd) #\L) 
           (wrot pt (* -1 (cadr cmd))))
          ((eq (car cmd) #\F) 
           (goto ship pt (cadr cmd)))))
    
    (defun part2 ()
        (let* ((in (getinput))
               (s (make-ship))
               (p (make-wpt)))
          (iter (while in)
            (let ((c (pop in)))
              (wintcmd s p c))
          (print s)
          (print p)
          )
          (+ (abs (ship-x s))
             (abs (ship-y s)))))
    

Tags: