Day 4

Previous: Day 03 | Next: Day 05

Manlets BTFO

###AUTO-ACQUIRED DATA FOLLOWS…
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] = &\#39;\0&\#39;;
        }
    
        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] != &\#39;\#&\#39;) 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] = &\#39;\0&\#39;;
        }
    
    
        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;
    }
    
day-4/day04-main.py
day-4/day04-main.py
import re
    FIELDS = [&\#39;byr&\#39;, &\#39;iyr&\#39;, &\#39;eyr&\#39;, &\#39;hgt&\#39;, &\#39;hcl&\#39;, &\#39;ecl&\#39;, &\#39;pid&\#39;]
    validPassports = 0
    with open("input", "r") as file:
        passports = file.read().split(&\#39;\n\n&\#39;)
        for passport in passports:
    
            \# part a
            \# presentFields = all(passport.count(field) == 1 for field in FIELDS)
            \# if presentFields:
            \#     validPassports += 1
    
            \# part b
            regex = (
                r"(?sm)"  \# multiline search
                r"(?=.\*byr:(19[2-9]\d|200[0-2]))?"  \# 1920 <= byr <= 2002
                r"(?=.\*iyr:(201\d|2020))?"  \# 2010 <= iyr <= 2020
                r"(?=.\*eyr:(202\d|2030))?"  \# 2020 <= eyr <= 2030
                r"(?=.\*hgt:(1[5-8]\dcm|19[0-3]cm|6\din|59in|7[0-6]in))?"  \# 150cm <= hgt <= 193cm or 59in <= hgt <= 76in
                r"(?=.\*hcl:\#([0-9a-f]\*))?"  \# hcl = \#(six digits or a-f)
                r"(?=.\*ecl:(amb|blu|brn|gry|grn|hzl|oth))?"  \# ecl only one of determined values
                r"(?=.\*pid:(\d{9})\b)?")  \# pid of 9 digits
            if all(field != None for field in re.match(regex, passport).groups()):
                validPassports += 1
    
    
    print(validPassports)
    
deutschanon/deutschanon-day04.c
deutschanon/deutschanon-day04.c
\#include <stdio.h>
    \#include <stdlib.h>
    \#include <string.h>
    
    int main(int argc,char\*\* argv){
        char suche[8][4] = { "byr","iyr","eyr","hgt","hcl","ecl","pid","cid"};
        char werte[7][20];
        char eingabewerte[20];
        char puffer[500];
        char pos[8] = { 0,0,0,0,0,0,0,0};
        char hex[16] = { &\#39;0&\#39;,&\#39;1&\#39;,&\#39;2&\#39;,&\#39;3&\#39;,&\#39;4&\#39;,&\#39;5&\#39;,&\#39;6&\#39;,&\#39;7&\#39;,&\#39;8&\#39;,&\#39;9&\#39;,&\#39;a&\#39;,&\#39;b&\#39;,&\#39;c&\#39;,&\#39;d&\#39;,&\#39;e&\#39;,&\#39;f&\#39;};
        char ecl[7][4] = { "amb","blu","brn","gry","grn","hzl","oth"};
        FILE \*input;
        if(argc < 2){
            input = fopen("out.txt","r");
        }else{
            input = fopen(argv[1],"r");
        }
    
        int i = 0;
        int valide = 0;
        int korrekt = 0;
        int byr = 0;
        int iyr = 0;
        int eyr = 0;
        int hgt = 0;
        char hgt\_unit = 0;
    
        while((puffer[i] = getc(input)) != EOF){
            if(puffer[i] == &\#39;\n&\#39;){
                for(int j = 0; j < 7; j++){
                    pos[j] = strstr(puffer, suche[j]) - puffer;
                    //printf("%d;",pos[j]);
                }
                //printf("\n");
                for(int j = 0; j < 7; j++){
                    int z = 0;
                    while(puffer[z+pos[j]+4] != &\#39; &\#39; && puffer[z+pos[j]+4] != &\#39;\n&\#39; && puffer[z+pos[j]+4] != 0){
                        werte[j][z] = puffer[z+pos[j]+4];
                        z++;
                    }
                    werte[j][z] = &\#39;\0&\#39;;
                    if(j == 0){
                        byr = atoi(werte[0]);
                    }
                    if(j == 1){
                        iyr = atoi(werte[1]);
                    }
                    if(j == 2){
                        eyr = atoi(werte[2]);
                    }
                    if(j == 3){
                        //printf("%s\n",werte[3]);
                        hgt = atoi(werte[3]);
                        hgt\_unit = werte[3][3];
                    }
                }
                /\*
              for(int j = 0; j <  7; j++){
                  printf("%s;",werte[j]);
              }
              printf("\n");\*/
                //printf("byr: %d,iyr: %d,eyr: %d,hgt: %d,hgt\_unit: %c\n",byr,iyr,eyr,hgt,hgt\_unit);
                
                korrekt = 1;
                while(korrekt){
                    if((byr < 1920) || (byr > 2002)){
                        korrekt = 0;
                        continue;
                    }
                    if((iyr < 2010) || (iyr > 2020)){
                        korrekt = 0;
                        continue;
                    }
                    if((eyr < 2020) || (eyr > 2030)){
                        korrekt = 0;
                        continue;
                    }
                    if((hgt\_unit == &\#39;c&\#39;) || (hgt\_unit == &\#39;m&\#39;)){
                        if(hgt < 150 || hgt > 193){
                            korrekt = 0;
                            continue;
                        }
                    }else{
                        if(hgt < 59 || hgt > 76){
                            korrekt = 0;
                            continue;
                        }
                    }
                    if(werte[4][0] == &\#39;\#&\#39;){
                        int test = 0;
                        for(int j = 1; j < 8; j++){
                            for(int k = 0; k < 16;k++){
                                if(werte[4][j] == hex[k]){
                                    test++;
                                }
                            }
                        }
                        if(test != 6){
                            korrekt = 0;
                            continue;
                        }
                    }else{
                        korrekt = 0;
                        continue;
                    }
                    korrekt = 0;
                    for(int j = 0; j < 7; j++){
                        if(strstr(werte[5],ecl[j]) != NULL){
                            korrekt = 1;
                        }
                    }
                    //printf("%s\t%d",werte[6],strlen(werte[6]));
                    if(strlen(werte[6])!=9){
                        korrekt = 0;
                        continue;
                    }/\*else{
                      for(int j = 0; j < 9;j++){
                          if(werte[6][j] < 30 || werte[6][j] > 39){
                              korrekt = 0;
                              continue;
                          }
                      }
                  }\*/
    
                    break;
                }
                if(korrekt == 1){
                    valide++;
                    //printf("%s",puffer);
                    printf("byr:%d iyr:%d eyr:%d hgt: %d%c hcl: %s ecl: %s pid: %s\n",byr,iyr,eyr,hgt,hgt\_unit,werte[4],werte[5],werte[6]);
                }
                
                for(int j = 0; j < 120;j++){
                    puffer[j] = 0;
                }
                for(int j = 0; j < 8; j++){
                    pos[j] = 0;
                }
                i = -1;
            }
            i++;
        }
        printf("%d gültig\n",valide);
        return 0;
    }
    
pythonpassion/coding-is-my-passion-day04.py
pythonpassion/coding-is-my-passion-day04.py
def partOne():
        file = open("input.txt", "r")
        validCount = 0
        currPassport = ""
        for line in file:
            if (len(line) == 1):
                if("byr:" in currPassport and "iyr:" in currPassport and 
                    "eyr:" in currPassport and "hgt:" in currPassport and "hcl:" in currPassport
                    and "ecl:" in currPassport and "pid:" in currPassport):
                    validCount += 1
                currPassport = ""
    
            else:
                currPassport += line
    
        if("byr:" in currPassport and "iyr:" in currPassport and 
            "eyr:" in currPassport and "hgt:" in currPassport and "hcl:" in currPassport
            and "ecl:" in currPassport and "pid:" in currPassport):
            validCount += 1
    
        print(str(validCount) + " valid passports detected.")
    
    
    \# ABANDON ALL HOPE, YE WHO ENTER HERE
    def partTwo():
        file = open("input.txt", "r")
        validCount = 0
        validFields = [False, False, False, False, False, False, False]
        currPassport = ""
    
        for line in file:
    
            if (len(line) == 1):
                index = currPassport.find("byr:")
                if(index != -1):
                    byr = int(currPassport[(index+4):(index+8)])
                    validFields[0] = (byr >= 1920 and byr <= 2002)
                
                index = currPassport.find("iyr:")
                if(index != -1):
                    iyr = int(currPassport[(index+4):(index+8)])
                    validFields[1] = (iyr >= 2010 and iyr <= 2020)
    
                index = currPassport.find("eyr:")
                if(index != -1):
                    eyr = int(currPassport[(index+4):(index+8)])
                    validFields[2] = (eyr >= 2020 and eyr <= 2030)
    
                index = currPassport.find("hgt:")
                if(index != -1):
                    endIndex = currPassport.find("c", index)
                    if(endIndex != -1 and (endIndex - index) < 8):
                        hgt = currPassport[(index+4):endIndex]
                        hgt = int(hgt)
                        validFields[3] = (hgt >= 150 and hgt <= 193)
                    else:
                        endIndex = currPassport.find("i", index)
                        if(endIndex != -1 and (endIndex - index) < 8):
                            hgt = currPassport[(index+4):endIndex]
                            hgt = int(hgt)
                            validFields[3] = (hgt >= 59 and hgt <= 76)
    
                index = currPassport.find("hcl:")
                if(index != -1):
                    if(currPassport[(index+4)] == &\#39;\#&\#39;):
                        lookinGood = True
                        i = (index + 5)
                        while (i < (index+11) and lookinGood):
                            if (currPassport[i] != &\#39;0&\#39; and
                                currPassport[i] != &\#39;1&\#39; and
                                currPassport[i] != &\#39;2&\#39; and
                                currPassport[i] != &\#39;3&\#39; and
                                currPassport[i] != &\#39;4&\#39; and
                                currPassport[i] != &\#39;5&\#39; and
                                currPassport[i] != &\#39;6&\#39; and
                                currPassport[i] != &\#39;7&\#39; and
                                currPassport[i] != &\#39;8&\#39; and
                                currPassport[i] != &\#39;9&\#39; and
                                currPassport[i] != &\#39;a&\#39; and
                                currPassport[i] != &\#39;b&\#39; and
                                currPassport[i] != &\#39;c&\#39; and
                                currPassport[i] != &\#39;d&\#39; and
                                currPassport[i] != &\#39;e&\#39; and
                                currPassport[i] != &\#39;f&\#39;):
                                lookinGood = False
                            i += 1
                        validFields[4] = ((currPassport[(index+11)] == " " or currPassport[(index+11)] == "\n") and lookinGood)
    
                index = currPassport.find("ecl:")
                if(index != -1):
                    ecl = currPassport[(index+4):(index+7)]
                    validFields[5] = (ecl == "amb" or ecl == "blu" or ecl == "brn" or ecl == "gry" or ecl == "grn" or ecl == "hzl" or ecl == "oth")
                
                index = currPassport.find("pid:")
                if(index != -1):
                    endIndex = currPassport.find(" ", index)
                    if ((endIndex-index) > 16):
                        endIndex = currPassport.find("\n", index)
                    pid = currPassport[(index+4):endIndex]
                    try:
                        pid = int(pid)
                        pid = str(pid)
                        validFields[6] = (len(pid) == 9)
                    except:
                        pass
                
                if(validFields[0] and validFields[1] and validFields[2] and validFields[3]
                    and validFields[4] and validFields[5] and validFields[6]):
                    validCount += 1
                currPassport = ""
                validFields = [False, False, False, False, False, False, False]
    
            else:
                currPassport += line
    
        print(str(validCount) + " valid passports detected.")
    
    
    partOne()
    partTwo()
    
steveklabnik/steveklabnik-day04.rs
steveklabnik/steveklabnik-day04.rs
use std::ops::RangeInclusive;
    
    fn solve(input: &str) -> (u32, u32) {
      input.split("\n\n").fold((0, 0), |(a, b), s| {
          let (keys, valid) = s
              .split\_whitespace()
              .map(|s| {
                  let (k, v) = s.split\_at(4);
    
                  match k {
                      "byr:" => (1 << 0, check\_number(v, 4, 1920..=2002)),
                      "iyr:" => (1 << 1, check\_number(v, 4, 2010..=2020)),
                      "eyr:" => (1 << 2, check\_number(v, 4, 2020..=2030)),
                      "hgt:" => {
                          (
                              1 << 3,
                              v.strip\_suffix("cm")
                                  .map(|v| check\_number(v, 3, 150..=193))
                                  .xor(v.strip\_suffix("in").map(|v| check\_number(v, 2, 59..=76)))
                                  .unwrap\_or(false)
                          )
                      }
                      "hcl:" => {
                          (
                              1 << 4,
                              v.strip\_prefix("\#").map\_or(false, |v| {
                                  v.len() == 6 && u32::from\_str\_radix(&v[1..], 16).is\_ok()
                              })
                          )
                      }
                      "ecl:" => {
                          (
                              1 << 5,
                              matches!(v, "amb" | "blu" | "brn" | "gry" | "grn" | "hzl" | "oth")
                          )
                      }
                      "pid:" => (1 << 6, v.len() == 9 && v.parse::<u32>().is\_ok()),
                      "cid:" => (0, true),
                      \_ => (0, false)
                  }
              })
              .fold((0, true), |(keys, valid), (k, v)| (keys | k, valid && v));
    
          let all\_fields = keys == 0b0111\_1111;
    
          (
              a + u32::from(all\_fields),
              b + u32::from(all\_fields && valid)
          )
      })
    }
    
    fn check\_number(s: &str, len: usize, range: RangeInclusive<u32>) -> bool {
      s.len() == len && s.parse().map\_or(false, |i| range.contains(&i))
    }
    

Tags: