Try โ€‚โ€‰HackMD

Day01 AdventofCode 2018

tags: aoc2018

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

Picture from 4chan/g/

Any suggestions and discussion are welcome through Issues.

๐Ÿง  Code Strategy

Part1

  1. Try to read text file line by line.
  2. Run through all givin numbers, add them up.
  3. Print the result.

Part2

  1. Using code in Part1
  2. Also run through all givin numbers, and record results after each adding for checking whether the result is appeared twice.
  3. Checking the result is appeared twice or not. If so, print it and stop doing Step2
  4. Need to run through givin numbers again if duplicate result still not appeared.

๐Ÿ“œ Implementation

Part1

In part1, we learned how to read text file line by line.

for line in open("input.txt"): print(line)

And then remove Newline(\n) by calling str.strip() function.

for line in open("input.txt"): print(line.strip())

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()

change_freq = [] for line in open("input.txt"): change_freq.append(int(line.strip()))

Initialize the varibale and add through all givin numbers by calling for-loop

current = 0 for change in change_freq: current += change

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()

print(current)

Part2

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.

record = {0} for change in change_freq: current += change if current in record: break else: record.add(current)

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.

flag = True while flag: for change in change_freq: current += change if current in record: flag = False break else: record.add(current)

Finally print out the answer. At this point we finished day01. Good job.

๐Ÿค” Some Questions that Worth Thinking

Why using 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

Why input long_iterate.txt take lots of time to compute?

Here is the input in long_iterate.txt

+10000000
-9999999

This input is provided by Reddit/u/VikeStep. Check out his solution if you are interested in the detail.