lunarised.com

Advent of Code Start

Well… I guess its that time of year again where I try advent of code and see how long it takes before I want to kill myself… And this year… I am committed to doing it in C89.

Why C89?

C89 is a language I have fought with since I did COSC242 in university. It is a very stripped down version of C, with little to no mercy given to the programmer. Especially in things like code structure. I especially dislike the lack of booleans, meaning I need to use integers to represent basic flags.

I also want to learn it since some things at work are written in C++, and I think it would be a good way to begin thinking about things in a C way. While I could learn C++, there are too many ways to write it and i feel that an individual developer leaves their mark on C++ more so than in C, so learning C++ wouldn’t help me write code that would “Fit in” so to speak

Previous years Advent of Code?

I have attempted Advent of Code every year since 2019, although I have not managed to get very far in any of them. (I somehow got the farthest in my first year of attempts). Last year, I ended up making the repo private after my attempts to do it in rust failed spectacularly. So I hope that doesn’t happen again this year

Day 1

Day 1 was quite daunting as I hadn’t programmed much in C before (Outside COSC242, and some micro controller things…). I was thankful that the first solution was rather easy. I found the importing of the file to be the hardest part of those parts… Which is a theme that would continue. Realistically, I would rate part 1 to be a 1/10 in difficulty, with part 2 being easier, since the piping was already in place.

Day 2

Day 2 was more interesting than Day 1 since the problem wasn’t as simple as reading integers, now I had to read in Command, Value pairs from a file. Looking back, I probably could have used fscanf() instead of fgets() but honestly, I was struggling enough as it was. This was another problem where I found part 2 to be much easier, as I had 99% of the plumbing in place, and just needed to add another variable and change some logic… Easy as. 2/10 for Part 1 and 1/10 for Part 2.

Day 3

Day 3 was where things started getting hard. I ended up doing Part 2 very messily, as going from the most popular bits to the least popular bits, wasn’t as simple as I had intended, and meant that some of the plumbing I wrote in Part 1 wasn’t at all useful… The shame…

Part One was where I started to care more about using memory sparingly, and I spend a great deal of time working out how to not just use array lengths of 100, especially when I know the length. Again, I probably should have worked on this from the start… but oh well. I never got Part 2 running nicely with trimmed array sizes. Too bad I guess. Part 1 was a solid 5/10 and Part 2 was an 8/10 in terms of difficulty.

Day 4

Day 4 and Day 5 were done both on a sunday, in a 6 hour block. So I was fresh as a daisy when I looked at what I would be doing for Day 4. Again, this was more of a problem of piping in the data. I decided to be lazy and use some structs:

struct Box{
    int value;
    int isHit;
}
struct Square{
    struct Box Box[BOARDSIZE][BOARDSIZE];
    int hasWon;
}

This was mostly because I am scared to death of matrices in C… Oh boy how Part 5 played out… But all in all, Part 1 was fairly straight forward. I built a solution for the little test data, and after realizing I need to manually assign values (or they’ll just be noise… ), I was off to the races!

…And then Part 2

Part 2 took me the longest out of any part thus far. And I blame the 4 nested for loops that I built (O(n^4) is fine by the way kids!). Only getting the last boards win state was hell to accomplish. And to make matters worse, I was checking if the winning square had won, instead of the currently looking at square, and then storing the square that I was currently looking at… Meaning the last square would always look like the answer… Ah well. I got there in the end.

Part 1 5/10, Part 2 10/10

Day 5

Finally… I got around to actually using fscanf() for my input which ended up making the input for this problem super easy. Part 1 was super easy, after I worked out something interesting. See, I was developing this solution under cygwins GCC, and I noticed that when I allocated a matrix bigger than ~510 x ~510, I would get a seg fault. This took me over an hour to diagnose! But what was interesting, is that the first run of Part One on the non test code, gave me the correct answer!

Part 2 was super simple once I finally figured out that y = 0 was at the top, and not the bottom… That was about 30 mins of head scratching too. So yeah. Day 5, not as difficult as day 4, but a real headscratcher!

I also got over my fear of matrices… next up is my fear of pointers i guess…

7/10 and 6/10 today

What I’ve learned over the first 5 days

I probably should have picked an easier language for this project. Typescript would have been to easy, but I feel like doing it in something like Java, or C++ would have meant that I could get through it easier. But then again… I walked into this knowing I was throwing myself into a trial by fire.


Keep your realities separate...

lunarised_