aoc2018
Picture from 4chan/g/
Any suggestions and discussion are welcome through Issues.
In part1, we learned how to read text file line by line.
And then remove Newline(\n) by calling str.strip()
function.
Since each line contains a number, we convert str
to int
by calling int()
. And put those numbers into a list for later use. For adding new element into list. Use list.append()
Initialize the varibale and add through all givin numbers by calling for-loop
You could use current = current + change
in last line. They are doing the same thing. In general, those two declaration are different. See this stackOverFlow question if you want to know more about it.
Finally, print out the result by calling print()
Continue from code in part1. We need to record result after each adding and check the result is appeared twice or not. Here we used if-else
statement and current in record
to accomplish it. Notice that we declare record as a set
, not a list
. And we add new element to a set by calling set.add()
. See this stackOverFlow question if you want to know the differences between list and set.
As part2's description,
Might need to repeat its list of frequency changes many times before a duplicate frequency is found
We need to put our code in while-loop
and leave the loop when duplicate value is appeared. Here we used bool
variable to fulfill later requirement.
Finally print out the answer. At this point we finished day01. Good job.
set
not list
in part2?You can try using list
, and soon find out that computer takes lots of time run it. For using set
, it takes <0.2s. If you are interested in the reason of the result, see Hash Table-Wikipedia
long_iterate.txt
take lots of time to compute?Here is the input in long_iterate.txt
This input is provided by Reddit/u/VikeStep. Check out his solution if you are interested in the detail.