Skip to main content

Featured

Advent of Code 2020 | Day 4 | List<String>.contains()

Difficulty is increasing as the challenge is continuing and a bit more of thinking will be needed, it's time to start! Im pretty sure theres no way to compare the passports with validity requirements without using List String or array - we need to change the spaces to newlines or any other way as well, because there's no chance of splitting() we could use.  As usual for first ' splitting ' of the String, we use our line scanner , reformatting the passport file is necessary in order to get all the values correctly, there was no order or rule of sorting, now we have it. So if the line is blank we know that the passport we were checking right now is over and we can proceed to another one. Next we check if the  list has all the required values and if it has, valids will increase, don't forget to clear() the array for next passport. At the end we can print out the result, also keep in mind that the scanner must be closed to avoid memory leaks! I actually hadn'

Advent of Code 2020 | Day 1 | for() loop

 #    #              #       ##  ##       #     ### ### ### ### ##           #  
# # ### # # ### ##  ### ###  #  #   ### ### ###   # # #   # # # # #  ## # # ##  
### # # # # ##  # #  #  # # ### #   # # # # ##  ### # # ### # # # # # # ###  #  
# # ###  #  ### # #  ## ###  #  #   ### ### ### #   # # #   # # # # ###   #  #  
# #                         ##   ##             ### ### ### ### ##      ### ### 

Hey reader! Have you been training programming whole year or even more or just coded your first script yesterday and you...maybe love math? Then try this coding challenge!

I and my friend seen this challenge and thought it will be an easy excercise for our Java coding skills [of course you can solve it in any other language like PythonJavascript or even Assembly language (haha)] and also an opportunity for having a nice time.

The first day doesn't show on leaderboards so this "writeup" cannot be considered as a cheat, but spoiler for sure. So lets get started!


Some elves in accounting want you to fix expense report, but something has fugged up...

Long story short: we have to find 2 numbers that sum current year - 2020 and submit their multiplication as an answer. Looks pretty easy to me.

First thing that came into my mind was putting these numbers in array, you can keep these numbers in memory or either paste them in a text file and then split them or loop through the lines.


After a while of coding I had this:

When you have all the numbers in the arraylist then there comes the solving part. You need to try sum all the numbers between themselves so I think best solution for that is a double loop (triple loop in the second part of the challenge) .


Lemme explain the code: we go through the whole arraylist, each index from 0 to end of the list (its length, size() in this case) contains an integer. For later use we save it in a variable I called 'a'. Then we proceed to the second loop to match the integer with every single number in the list. I called the second integer 'b'. Now we just perform the sum and check result with 'if' function. So if the sum gives 2020 we solved this puzzle. After approximately 1 second of runtime we got a result.


This could be easily solved by a few loops and math operands so lets call it a beginner challenge, but I guess next days will be more and more tricky and require higher skill level. Good luck with solving...


Tags: advent of code 2020, advent of code day one my solution, help the elves, merry x mas programmers


Comments

Popular Posts

RSS FEED

Crypto prices