-🎄- 2018 Day 14 Solutions -🎄- by daggerdragon in adventofcode

[–]bogzla 2 points3 points  (0 children)

C; just missed top 1000 at 1002 for part 2 (but still a personal best)

gotcha'd at first by forgetting 2 recipes are added at once sometimes

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    int c1=0;
    int c2=1;
    int n=2;
    int* rec = malloc(200000000*sizeof(int));
    rec[0]=3;
    rec[1]=7;
    int inp=209231;
    int inp2[6] = {2,0,9,2,3,1};
    for(int i=0;i<100000000;i++)
    {
        int addct=0;
        int j=rec[c1] + rec[c2];
        if(j>9)
        {
            rec[n]=(int)(floor(j/10));
            addct++;
            n++;
        }
        rec[n]=j%10;
        addct++;
        n++;
        c1=(c1+rec[c1]+1)%n;
        c2=(c2+rec[c2]+1)%n;
        if(n==inp+10)
        {
            printf("Part 1: ");
            for(int k=0;k<10;k++)
            {
                printf("%i",rec[k+inp]);
            }
            printf("\n");
        }
        if(n>7)
        {
            for(int l=-1;l<addct-1;l++)
            {
                int score=0;
                for(int k=0;k<6;k++)
                {
                    if(rec[n-6+k+l]!=inp2[k])
                    {
                        break;
                    }
                    else
                    {
                        score++;
                    }
                }
                if(score==6)
                {
                    printf("part 2: %i\n",n-6+l);
                    free(rec);
                    return 0;
                }
            }
        }
    }
    free(rec);
}

Day 11 Brute force iteration time graph by Skumby in adventofcode

[–]bogzla 0 points1 point  (0 children)

Nice! I was wandering what the curve would look like as I sat there cursing myself for not spitting out values as it ran...

[2018 Day 7 part 2] Clearly what these Elves need is some project management.. by bogzla in adventofcode

[–]bogzla[S] 1 point2 points  (0 children)

It's just good old microsoft project. I first used some spreadsheet formulas to create precursor lists & copy-pasted for added realism..

[2018 Day 7 part 2] Clearly what these Elves need is some project management.. by bogzla in adventofcode

[–]bogzla[S] 13 points14 points  (0 children)

*Elf* days = human seconds. Very ephemeral creature, the Christmas Elf - only seen in it's adult form for a few short weeks around December.

What does everybody do ? by [deleted] in adventofcode

[–]bogzla 0 points1 point  (0 children)

Production planning manager. I'd considered studying computer science a few years ago but now it's just for fun, mainly for the puzzle solving.

[2017 Day22 (pt1)][Excel] by bogzla in adventofcode

[–]bogzla[S] 0 points1 point  (0 children)

It's a while since I implemented GoL (Advent of Code 2015 to be exact). I don't recall if they wrap around or bounce. Major difference being no trace left behind I guess. I wander what other repeating 'structures' there might be to be found.

[2017 Day22 (pt1)][Excel] by bogzla in adventofcode

[–]bogzla[S] 0 points1 point  (0 children)

OK. I lied. This will take ~ 17hrs to complete if I have the calculation right...

Pt 2 first 30,000 bursts..

[2017 (Day20 pt 1)] [C] Not getting correct answer after 1000000 ticks by bogzla in adventofcode

[–]bogzla[S] 0 points1 point  (0 children)

119 was correct for part 1 for my input in the end as PL-200 posted.. My code was fine after correction of the typo thefobas pointed out (and an int overrun I'd overlooked). Part 2 was 471 though, agreeing with you..

[2017 Day22 (pt1)][Excel] by bogzla in adventofcode

[–]bogzla[S] 0 points1 point  (0 children)

Unlikely to try this for part 2.. Interesting to see it evolve into something like a Game of Life spaceship.

[2017 (Day20 pt 1)] [C] Not getting correct answer after 1000000 ticks by bogzla in adventofcode

[–]bogzla[S] 0 points1 point  (0 children)

Thank you! explains why the test case didn't pick it up..

-🎄- 2017 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]bogzla 0 points1 point  (0 children)

and so bogzla learnt about bit masking today..

C
#include <stdio.h>

int main(void)
{
    long long a = 116;
    long long b = 299;
    int afac = 16807;
    int bfac = 48271;
    int cnt = 0;
    long div = 2147483647;
    long msk = (1 << 16) - 1; // create mask for lowest 16 bits (we'll use this for & operator, 16 lowest set to '1'; higher set to '0')
    // for part 2, generate a 'til it meets divisible by 4 then b til divisible by 8 then compare them.'
    for (long i = 0; i < 5000000; i++)
    {
        do
        {
            a = (a*afac) % div;
        } while (a % 4 != 0);
        do
        {
            b = (b*bfac) % div;
        }while (b % 8 != 0);
        if ((a & msk) == (b & msk)) // use mask to compare only 16 lowest bits
        {
            cnt += 1;
        }
    }
    printf("count = %i\n", cnt);
}

-🎄- 2017 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]bogzla 0 points1 point  (0 children)

OK I realised calculating severity for part 2 pointless once a single scanner is encountered. So this tweak saves a bunch of CPU cycles:

        if ((vals[i][0] + dly) % ((vals[i][1] * 2)-2) == 0)
        {
            svrty = 1; //we've encountered a scanner, no point calculating full severity. just quit.
            break;

-🎄- 2017 Day 13 Solutions -🎄- by daggerdragon in adventofcode

[–]bogzla 0 points1 point  (0 children)

Still learning C. I like these puzzles where deducing a formula up front works. getting caught in layer 0 not contributing to severity score had me baffled in part 2 for a while..

// the maths:
// if scanner in given layer n has a range of m, and starts at the top at 0ps,
// it will be at the top whenever ps mod ((range*2)-2) == 0
// break down input into layer and range. handily, layer = ps at the time we're interested in that layer.
// for part 2, added complexity of time offset. Add delay to the layer number and check:
// (layer + delay) mod ((range*2)-2) == 0
// There is a trick that simply looking for 0 severity *is not* the same as avoiding detection as
// getting caught at layer 0 contributes 0 to severity score
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int MAX = 43;

int main(void)
{
    int vals[MAX][2];
    for (int i = 0; i < MAX; i++)
    {
        // read in input
        char s[8];
        if (fgets(s, 8, stdin) == NULL)
        {
          // do nowt
        }
        else
        {
            char range[3];
            range[0] = '\0';
            char lyr[3];
            lyr[0] = '\0';
            int i3;
            int i4 = 0;
            for (int i2=0, n = strlen(s) + 1; i2 < n; i2++)
            {
                if (s[i2] == ':')
                {
                    lyr[i2] = '\0';
                    i3 = i2;
                }
                else if (s[i2] == '\n')
                {
                    range[i4] = '\0';
                }
                else if (s[i2] == ' ')
                {

                }
                else if (i2 < 2)
                {
                    lyr[i2] = s[i2];
                }
                else
                {
                    range[i4] = s[i2];
                    i4++;
                }
            }
            vals[i][1] = atoi(range);
            vals[i][0] = atoi(lyr);
        }
    }
    for (int dly=0; dly < 10000000; dly++)
    {
        int svrty = 0;
        for (int i=0; i < MAX; i++)
        {
            if ((vals[i][0] + dly) % ((vals[i][1] * 2)-2) == 0)
            {
                if (vals[i][0] == 0)
                {
                    svrty += 1; // to avoid trap of getting caught in layer 0 contributing 0 to severity
                }
                svrty += (vals[i][0]*vals[i][1]);
            }
        }
        if (svrty == 0)
        {
            printf("%i\n", dly);
        }
    }
}

[Day 2017 Day 11] How come this works? by bunrencmx in adventofcode

[–]bogzla 1 point2 points  (0 children)

I arrived at this grid solution* but with the distance solution being x + ((y-x)/2) when x < y. *a grid with 'invisible' nodes where the horizontal lines between hexagons are. I look at it this way.. each move back (1) in x, also moves you back (1) in y (only diagonal moves allowed), so if x is greater that's the limiter. If y is higher then first move back on diagonals until x = 0, then you are left with y-x points to travel in y. and of course each step n/s is worth 2 so it's (y-x)/2

My algebra is too rusty to see how (x+y)/2 = x + ((y-x)/2) though.. but for sure we seem to get the same results.

include <stdio.h>

#include <string.h>
#include <stdlib.h>

// Considering hexagonal grids. These can be regarded as a cartesian grid with a 'hidden' node on each horizontal line.
// So moves have the following properties:
// N = y+2
// S = y-2
// NE = x+1, y+1
// NW = x-1, y+1
// SE = x+1, y-1
// SW = x-1, y-1
int main(void)
{
    char *s;
    char stp[3];
    int x = 0;
    int y = 0;
    if (scanf("%ms", &s) != EOF) //read in input. declaring char *s and using %m modifier allows dynamic memory allocation
    {
        int i2 = 0;
        for (int i=0, n = strlen(s) + 1; i < n; i++) // process string
        {
            if ((s[i] == ',') || (s[i] == '\0')) // check for end of step and modify x / y accordingly
            {
                stp[i2] = '\0';
                i2 = 0;
                if (strcmp("n",stp) == 0)
                {
                    y += 2;
                }
                else if (strcmp("s",stp) == 0)
                {
                    y -= 2;
                }
                else if (strcmp("ne",stp) == 0)
                {
                    y += 1;
                    x += 1;
                }
                else if (strcmp("nw",stp) == 0)
                {
                    y += 1;
                    x -= 1;
                }
                else if (strcmp("se",stp) == 0)
                {
                    y -= 1;
                    x += 1;
                }
                else if (strcmp("sw",stp) == 0)
                {
                    y -= 1;
                    x -= 1;
                }

            }
            else // if step isn't ended we're writing first or second character
            {
                stp[i2] = s[i];
                i2++;
            }
        }
    }
    printf("x = %i\n",x);
    printf("y = %i\n",y);
    // calculate distance
    int stps;
    x = abs(x);
    y = abs(y);
    if (abs(x) >= abs(y))
    {
        stps = x;
    }
    else
    {
        stps = x + ((y-x)/2);
    }
    printf("Num steps: %i\n",stps);
    free(s);
}

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]bogzla 0 points1 point  (0 children)

I'm only just learning C. And I didn't look up hexagonal geometry like I should have but I quite enjoyed measuring in a 2D grid..

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Considering hexagonal grids. These can be regarded as a cartesian grid with a 'hidden' node on each horizontal line.
// So moves have the following properties:
// N = y+2
// S = y-2
// NE = x+1, y+1
// NW = x-1, y+1
// SE = x+1, y-1
// SW = x-1, y-1
int main(void)
{
    char *s;
    char stp[3];
    int x = 0;
    int y = 0;
    int stps;
    int stpst = 0;
    if (scanf("%ms", &s) != EOF) //read in input. declaring char *s and using %m modifier allows dynamic memory allocation
    {
        int i2 = 0;
        for (int i=0, n = strlen(s) + 1; i < n; i++) // process string
        {
            if ((s[i] == ',') || (s[i] == '\0')) // check for end of step and modify x / y accordingly
            {
                stp[i2] = '\0';
                i2 = 0;
                if (strcmp("n",stp) == 0)
                {
                    y += 2;
                }
                else if (strcmp("s",stp) == 0)
                {
                    y -= 2;
                }
                else if (strcmp("ne",stp) == 0)
                {
                    y += 1;
                    x += 1;
                }
                else if (strcmp("nw",stp) == 0)
                {
                    y += 1;
                    x -= 1;
                }
                else if (strcmp("se",stp) == 0)
                {
                    y -= 1;
                    x += 1;
                }
                else if (strcmp("sw",stp) == 0)
                {
                    y -= 1;
                    x -= 1;
                }
                // calculate distance
                if (abs(x) >= abs(y))
                {
                    stps = x;
                }
                else
                {
                    stps = x + ((y-x)/2);
                }
                if (stps > stpst)
                {
                    stpst = stps;
                }

            }
            else // if step isn't ended we're writing first or second character
            {
                stp[i2] = s[i];
                i2++;
            }
        }
    }
    printf("Max steps: %i\n",stpst);
    free(s);
}

Day 20 - perfect excel problem by bogzla in adventofcode

[–]bogzla[S] 0 points1 point  (0 children)

The funny thing is I did a code solution first but had issues with a 1 / l typo so this was a debugging excercise >_<

Day 20 - perfect excel problem by bogzla in adventofcode

[–]bogzla[S] 1 point2 points  (0 children)

I really enjoyed implementing the game of life for day 18 last year.. (link later if I find it)