My Python solutions for the advent of code 2020. A nice challenge for everyday learning a coding language.
These are some things I learned along the way.
General
- Thanks to Björn Söderqvist aka cybear for the inspiration to track learning in the README.md file
About Github
- Finally learned (again) to create a public github repo and add my daily solutions to it.
About Python
- I wanted to practice the zip and map functions. I noticed that the [map()] creates a list containing a map object, instead the content of the mapping operation. Instead list(map()) works as I wanted.
About Python
- Learned to apply set operations on a list of sets
- Learned more about the unpacking *
About Github
- Start to get familiar with markdown
Resources
About Visual Studio Code
- Learned to debug with VSC
About Python
- Using os.path.dirname(os.path.realpath(file)) to open a file in the same directory as the script. (KUDOS to @dogmatic69)
General
- Giving variables a meaningful name is an art. I think I changed my vars 5 times each, until it mades sense (to me).
Python
- Deep copy vs shallow copy: I knew about the difference, but used list.copy() without understanding, it is a shallow copy. Then I found the package copy with the deepcopy() function. Nice. Until I refactored a solution, that doesn't need to copy the list at all. Much better performance ...
General
- Reading the assignment is critical (really?). Smallest and biggest number ... I interpreted as lowest and highest index ... Wrong assumption. RTF rules!
General
- Pen and paper is still an excellent tool to understand a problem.
- Breaking down a problem into smaller problems is a very good strategy.
- After having a solution - it is so much easier and fun to refactor the code. I even found a totally different solution in 4 lines. Find a solution that proofs you understand the problem at hand. Then, and only then, optimise your code.
- I notice, that every day during this challenge my code gets nicer to look at. Better structure, better comments (I hope) better variable names.
- Debugging is the only way to find errors in complex situations. My code left out the last "adapter group". I only saw that in the debugger ...
Python
- Removing items from a list ... sometimes the simple things are just gone from the brain ... Thank you Google.
- Using the zip function to calculate the difference of elements in a list. Sooo neat ...
General
- Matrix is a challenge. Rows, coluns, ... diagonal movements ...
- Part 1 was fairly easy but part two took me long to code. Easy on paper though ...
Python
- I get used to the itertools. count was cool to use for one generic function, that can go in all directions through a matrix. I just pass the -1,0,+1 step value as parameter to the function. Inside I use count and the given step.
- Used copy.deepcopy again.
General
- Rotating, coordinations, moving relative, ... a nice mind challenge. Still much easier than yesterday's seating simulation.
- Pen and paper have proven again to be super useful.
- Started with just find "a" solution - with a lot of repeated code. When it worked, I refactored and deduplicated. It's much neater code to maintain - but is it easier to understand? I doubt.
- I learned to include images in github markdown. See below.
Python
- I practiced the zip function. Until I found out during refactoring, I did not need it.
- PIL (pillow) package: I created my first animated gif with pillow:
- os ... refreshed my kowledge on reading files from a directory.
- And I nearly forgot the amazing generators. I used it for the first time to create a function, that yields always the next file name and content (sorted).
Resources
- Create animated gifs with PIL
- Saving gif with PIL
- Color pallet generator
- Github markdown
- Read/write files (Real Python)
- Understanding Generators (Real Python)
Python
- Convert an integer to binary with a defined length (f"{value:036b}) and back to an integer (using int(value, 2))
- Getting used to the zip function. Zipped the mask with the binary value.
General
- Reminder! Pen and paper is essential to develop an algorithm. From intuition to explicit rules (code) it's a long way ...
Python
- Used the profiler package cProfile to find out where the performance is lost. It was the index() function. Obviously the longer the list, the longer it takes ... and 300000000 is very long ...
Resources
General
- My brain needs a rest. Long working days are not good for a night of AoC.
Python
- Working with sets. Good to know, that you can't directly access set elements (as they are unsorted). Bad to find that out, when you need the elements. OMG, I wrote something like this: [{"type"}][0] (don't tell anyone).
General
- I fall behind.
Python
- The unpacking of iterables is not really intuitive yet (for me) - in the context of list comprehensions. More to learn for me.
Python
- I remembered the reduce function from the functools library. Together with a lambda function it multiplies the content of a list very nicely (pythonic).
result = reduce(lambda x, y: x*y, [1,2,3,4])
Python
- re: Named matching groups
(?P<MG1> )
and matching groups, that are not captured:(?: )
- re: it's not possible to count with regular expressions
- I got really frustrated with re (and learned a lot). Finally I used a cheat (hard coded max limit) - recommended by a colleague. I could not find out how to validated the recursive rule:
11: 42 31 | 42 11 31
. I simpified it to42 (42 (42...31) 32) 31
with "+" occurances. But we can't write it like(42)+(31)+
because both rules 42 and 31 need to have the same occurance. How do we find out, if the matches are valid, i.e. matched the same times? I gave the match groups a name(?P<AA>(42)+)(?P<BB>(31)+)
- and accessed it after a match. But I actually did not find out, how many times each of them matched ... if you have a tip - please reach out!
Resources
General
- Need to practice my markdown skills more ... just noticed, that my README looked awfull.
General
- It's getting complex here. Spent the whole day - finally I solved part 1 and got stuck on part 2. How to turn and flip the tiles ...
Python
- The zip function, I practicesd it a lot today. Very handy to access columns of list of lists. But still somewhat ... confuse. Since you need to convert the result from sets to lists to strings ...
General
- The task was challenging - not to code, but to grasp. My approach turned out wrong. (take a food with only one allergene - since only those ingredients can have that allergene; if the allergene then shows up in another food, with different ingredients, it could only be the earlier found ones ... but that is only partly true. What if a new ingredient also includes the same allergene? Now I have two ingredients with soy ... In the end I got a tip - keep the ingredients, that show up in all food with the same allergene. Those are then the candidates to contain the allergen.)
Python
- Working with sets. Is not difficult, but to actually unpacking a remaining element in a set was more tricky than I thought. Especially in a comprehension, as the set elements can't be accessed by []. I converted the set into a list ... but I think, that's not the best way.
- Sorted function on dictionaries - containing a set as value. This was a challenging excercise. I split it finally in two parts. First, convert the values into a list, than use a lambda function in the sorted key= parameter.
General
- It is very exhausting to read the AoC. I missed today a sentence, and my code - obviously - did not finish. Very frustrating - until I saw the code of a colleague, and wondered why she did what she did. Ahhh. Reading the f... instructions. My bad.
Python
- I learned the extend function list a list. It appends multiple values to the list instead of a single one. Nice.
- I need to look into deque - from the collections library. Double ended que ... interesting!
- islice is another function from collections I want to know more about.
Python
- Looked deeper into deque (pronounced deck). Really nice lib to rotate arrays, add left and right of a list without slicing.
General
- Broke today my code into super small chunks. This made the final code super easy to read. And I could optimise, i.e. not have to do same calculations in many places. Reduces the failure risk, and once the function works - one an build on it.
Merry Christmas!