My solutions to KnowIT code calendar 2020

It is that time of year again, advent time, and every day in Desember there is a new puzzle in the KnowIT Christmas calendar. Last year, I started a little late, but did submit most of the puzzles. My solutions from last year, I have in last years blog post. This year I want to solve all the 24 puzzles and looking forward for some problem solving throughout Desember. I am keeping my solutions in a Github repository.

Puzzle Language Solution Submitted
Day 1: Missingno Go πŸŽ… βœ…
Day 2: Julenissens sinneproblem C# πŸŽ… βœ…
Day 3: Ordjakt C# πŸŽ… βœ…
Day 4: Julebonusen C# πŸŽ… βœ…
Day 5: Musesteg Go πŸŽ… βœ…
Day 6: Julegodt Go πŸŽ… βœ…
Day 7: Julesymmetre C# πŸŽ… βœ…
Day 8: Julenissen kommer Go πŸŽ… βœ…
Day 9: Dei arktiske alvane sine arbitrΓ¦re avstandsregler Go πŸŽ… βœ…
Day 10: Alvelekene Go πŸŽ… βœ…
Day 11: Passordhint Go πŸŽ… βœ…
Day 12: The Parents in the Parentheses Go πŸŽ… βœ…
Day 13: Bokstavkluss Go πŸŽ… βœ…
Day 14: Minus-pluss-sekvens Go πŸŽ… βœ…
Day 15: ? ? ?
Day 16: Rikelig talldifferanse Go πŸŽ… βœ…

When the December 24th was closing in, more and more time was spent on the Advent of Code puzzles. Also there was more of the puzzles in the KnowIT calendar that was updated since they discovered they had an error in the answer, input or problem description. That was a bit annoying when I spent time in the morning to solve it and just had to do minor change or change the input to get the correct answer later in the day. So ended up focusing on the Advent of Code instead this year.

Comments on the solutions

Day 1: Missingno
The first puzzle there was a file with a set of numbers in random order. The numbers was from 1 to 100000, except one was missing. The puzzle was to find the missing one. I solved this using Go, the solution itself is not optimized in any way. I first read the numbers into a slice for then sorting that slice and looping though again to see where the next element not was the previous plus one.

Day 2: Julenissens sinneproblem
Fun puzzle where I could use Sieve of Sundaram algorithm to find the prime numbers up to a number. Then do a binary search to find the closest prim number. Then there was just to loop through numbers from 0 and up to the number of gifts. Check each number if it contains 7, I do not like the way I implemented that check, but it works.

static void Sieve()
{
    var n = MAX;
    var nNew = (int)Math.Sqrt(n);
    var marked = new int[n/2+500];

    for(var i = 1; i <= (nNew - 1) / 2; i++)
        for(var j = (i * (i+1)) << 1;
                        j <= n / 2; j = j + 2 * i + 1)
                        marked[j] = 1;

    primes.Add(2);

    for(var i = 1; i <= n / 2; i++)
        if(marked[i] == 0)
            primes.Add(2*i+1);
}

static int BinarySearch(int left, int right, int n)
{
    if(left <= right)
    {
        var mid = (left + right) / 2;
        if(mid == 0 || mid == primes.Count - 1)
            return (int)primes[mid];

        if((int)primes[mid] == n)
            return (int)primes[mid];

        if((int)primes[mid] < n && (int)primes[mid+1] > n)
            return (int)primes[mid];
        if(n < (int)primes[mid])
            return BinarySearch(left, mid-1,n);
        else
            return BinarySearch(mid + 1, right, n);
    }
           

Day 3: Ordjakt
This was also a fun one where I needed to search a matrix in all directions to find out which words from a given word list that they did not include. It was given that three words in the list was not in the matrix. Then there was time to write a method that could be searching through the matrix. I did this in C#, but it would be nice to try this out in a functional language as well.

Day 4: Julebonusen
Simple little puzzle in the morning. I did this one in C# and tried to keep the solution compact.

Day 5: Musesteg
This is a fun puzzle, calculating the area of a polygon. I remember back to the days in school when I first learned how to calculate the area of a polygon using the coordinates. This puzzle is exactly the same, I will treat the starting point as (0,0 and finding the coordinates of each point where the line is changing direction.

Day 6: Julegodt
This puzzle, I made a solution in the morning and then gave up since I could not get the correct number. In the afternoon when I checked in, the description was changed a little bit since there was something wrong in the puzzle description. Gave it a new shot and I got the correct answer.

Day 7: Julesymmetre
Today was a little fight to get this right. My head was not playing right in the beginning. First I started in Go and the solution was correct for the example, but when I switched to the input in the puzzle for some reason Go did not load anything. Did not have time to investigate, so switch to C# and quick and dirty got the solution.

Day 8: Julenissen kommer
After reading the puzzle description a couple of times to be clear for how I can solve this, it went well. It is good to feel that writing code in Go feels more and more natural. Still a lot I have to practices in Go, but doing this kind of small puzzles helps to be better in Go.

Day 9: Dei arktiske alvane sine arbitrære avstandsregler
This ended up being a fun puzzle. Started solving this using 2D-array of rune in Go, but wanted to just use a simple map of a coordinate type with rune as value. Then also wrote a simple stack functionality. This worked and gave the correct result.

Day 10: Alvelekene
This was so simple and straight forward. Reading the inout file and just parsing and counting in one go.

Day 11: Passordhint
This was an easy puzzle, but for some reason I added a reverse word step. Lots of time lost on that before I read the description again and realized that there was no reversing anywhere. So what I can learn from today is reading the description really well.

Day 12: The Parents in the Parentheses
Today I did not want to sit inside when it finally was nice weather in Bergen, so my solution ended up a straight forward loop where with a counter. First that come to mind and was able to get the solution five minutes after I started.

Day 13: Bokstavkluss
Simple and fast puzzle to solve. Looping through a string and counting how many times they occurred and removed the letters based on some simple rules.

Day 14: Minus-pluss-sekvens
This was an easy puzzle, but I continued to get the wrong value for number of prime numbers in the sequence. I was really sure that the function generating the sequence was correct, but it did not cross my mind at all the numbers that is in the sequence. I was focusing that the index was increasing, so then the numbers was sorted. The search function I used in Go requires that the slice is sorted. When that crossed my mind finally, all was correct. Since a liner search algorithm is dead slow when the slice gets big, I ended up using a map[int]int in Go to keep track of the numbers already in the sequence.

Day 16: Rikelig talldifferanse
The description of this puzzle was really short and did not really give much description on the problem. I had to do some research myself and then I found of what abundant number was. When I read the definition, I can recall this from primary school, but it is a long time ago. When that knowledge was fresh in my mind, the code to calculate the number of values was straight forward to write.

Teis Lindemark

Read more posts by this author.