Day 3

Previous: Day 02 | Next: Day 04

THIS DOES NOT SEEM LIKE A SAFE MODE OF TRANSPOOORR-

###AUTO-ACQUIRED DATA FOLLOWS…
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)] == &\#39;\#&\#39;) {
                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]=&\#39;\0&\#39;;for(int             i=0;
    i<l;i++){if(I[i]==&\#39;\n&\#39;){w=i;break         ;}}h=l
    /(w+1);printf("W: %d,H: %d\n"           ,w,h) ;int 
    s[R] [2]={{1,1},{3,1},{5,1},    {7,1},  {1,2}  };long 
    int tot=1;for(int run=0;run<R;   run++){int      x=0;
    int y=0;int n=0;while(y<h){x=(    x+s [run]        [0]
    )%w;y=y+s[run][1];if(I[x+y\*(w+1)   ]==&\#39;\#&\#39;)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] = &\#39;a&\#39;;
        output[1024] = &\#39;\0&\#39;;
        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] = &\#39;\0&\#39;;
        for(int i = 0; i < length; i++){
            if(input[i] == &\#39;\n&\#39;){
                width = i;
                break;
            }
        }
        height = length/(width+1);
    
    
        printf("W: %d, H: %d\n", width,height);
    
        int run\_list\_size = 15;
        int slopevals[2][15] = {{ 2, 3, 4,  6,  8,  9, 12, 16, 18, 24, 32, 36, 48, 54, 64 },
                                { 1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 47 }};
        unsigned long long int running\_mult\_total = 1;
        struct bn total;
        struct bn tmp;
        struct bn tmp2;
        bignum\_from\_int(&total, 1);
        for(int i = 0;i<run\_list\_size;i++){
            for(int j = 0;j<run\_list\_size;j++){
                //printf("Slope %d, %d\n", slopevals[0][i], slopevals[1][j]);
                int xpos = 0;
                int ypos = 0;
                int n\_trees = 0;
                while(ypos < height){
                    xpos = (xpos+slopevals[0][i])%width;
                    ypos = ypos+slopevals[1][j];
                    //We have to use width+1 to account for newline characters
                    if(input[xpos+ypos\*(width+1)] == &\#39;\#&\#39;) 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;
    }
    
deutschanon/deutschanon-day03pbig.c
deutschanon/deutschanon-day03pbig.c
\#include <stdio.h>
    \#include <stdlib.h>
    
    long long bewegen(int y, int x, int hohe, int breite, char\* wald){
        int spalte = 0;
        long long baume = 0;
        for(int i = 0; i < hohe; i = i + y){
            if(wald[i\*breite+(spalte%breite)] == &\#39;\#&\#39;){
                baume++;
            }
            spalte = spalte + x;
        }
        return baume;
    }
    
    int main(int argc,char\*\* argv){
        int breite,hohe,reihe,spalte;
        hohe = 0;
        breite = 0;
        reihe = 0;
        spalte = 0;
        char c = 0;
    
        FILE \*input;
        if(argc > 1){
            input = fopen(argv[1],"r");
        }else{
            input = fopen("input.txt","r");
        }
    
        while((c = fgetc(input))!= EOF){
            if(c == &\#39;\n&\#39;){
                hohe++;
            }
            if(hohe == 0 && c != &\#39;\r&\#39;){
                breite++;
            }
        }
        
        char \*wald;
        wald = malloc(hohe\*breite);
        fseek(input,0L,0);
        while((c = fgetc(input)) != EOF){
            if(c != &\#39;\n&\#39;){
                if(c != &\#39;\r&\#39;){
                    wald[reihe\*breite+(spalte%breite)] = c;
                    spalte++;
                }
            }else{
                reihe++;
            }
        }
        long long int  ret = 0;
        printf("Breite: %d\tHöhe: %d\n",breite,hohe);
        ret =  bewegen(1,1,hohe,breite,wald) \* bewegen(1,3,hohe,breite,wald) \* 
            bewegen(1,5,hohe,breite,wald) \* bewegen(1,7,hohe,breite,wald) \* 
            bewegen(2,1,hohe,breite,wald);
        printf("Bäume Teil 1: %ld\n",bewegen(1,3,hohe,breite,wald));
        printf("Bäume Teil 2: %lld\n",ret);
    
        free(wald);
    
        fclose(input);
        return 0;
    }
    
steveklabnik/steveklabnik-day03.rs
steveklabnik/steveklabnik-day03.rs
use std::iter::successors;
    
    fn solve(input: &str) -> (u32, u64) {
      let width = input.find(&\#39;\n&\#39;).unwrap();
      let height = input.len() / (width + 1);
      debug\_assert!(input.len() % (width + 1) == 0);
    
      let count\_trees = |dx, dy| {
          (0..height)
              .step\_by(dy)
              .zip(successors(Some(0), |x| Some((x + dx) % width)))
              .map(|(y, x)| u32::from(input.as\_bytes()[y \* (width + 1) + x] == b&\#39;\#&\#39;))
              .sum()
      };
    
      let counts = [
          count\_trees(1, 1),
          count\_trees(3, 1),
          count\_trees(5, 1),
          count\_trees(7, 1),
          count\_trees(1, 2)
      ];
    
      (counts[1], counts.iter().copied().map(u64::from).product())
    }
    
wizardanon/wizardanon-day03.cpp
wizardanon/wizardanon-day03.cpp
\#include "stdafx.h"
    \#include "fstream"  
    \#include <iostream>  
    \#include <fstream>  
    \#include <string>  
    using namespace std;  
    
    \#define Modulorulum %  
    \#define Newlinius &\#39;\n&\#39;;  
    \#define Integerius int  
    \#define Openum {  
    \#define Closeth }  
    \#define Nullus 0  
    \#define Incantation  
    \#define ScryYonder getline  
    \#define HearYe return  
    \#define Hithero while  
    \#define Behold(x) std::cout << x << Newlinius  
    \#define Portal std::ifstream  
    \#define Summon(x) f(x)  
    \#define Appendo ++  
    \#define Equilarmius =  
    \#define IsItSo if  
    \#define Scrivening string  
    \#define Scriv char  
    
    Integerius main() Openum
    
        Portal Summon("day3.txt");
        
        Scrivening Forest;
        Scriv WoodenOne = &\#39;\#&\#39;;
        
        Integerius TreesBetwixt = Nullus;
        Integerius Footfalls = Nullus;
        Integerius Terminus = 31;
        Integerius Cockatrice = 3;
    
        IsItSo (f.is\_open()) Openum
            Hithero ( ScryYonder (f, Forest) ) Openum
                Integerius index Equilarmius 
                    (Footfalls Appendo \* Cockatrice) Modulorulum Terminus;
                IsItSo ( Forest.at(index) == WoodenOne ) Openum
                    TreesBetwixt Appendo;
                Closeth
            Closeth
        Closeth
    
        Behold( TreesBetwixt );
    
        HearYe Nullus;
    Closeth
    

Tags: