# Day01 AdventofCode 2018
###### tags: `aoc2018`

> Picture from [4chan/g/](http://boards.4channel.org/g/thread/68732433#q68732433)
Any suggestions and discussion are welcome through [Issues](https://github.com/felixshai/python-aoc-2018/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.
```python=
for line in open("input.txt"):
print(line)
```
And then remove [Newline(\n)](https://en.wikipedia.org/wiki/Newline) by calling `str.strip()` function.
```python=
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()`
```python=
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`
```python=
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](https://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python) if you want to know more about it.
Finally, print out the result by calling `print()`
```python=
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](https://stackoverflow.com/questions/12354515/what-is-the-difference-between-sets-and-lists-in-python) if you want to know the differences between list and set.
```python=
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 [color=red]
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.
```python=
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](https://en.wikipedia.org/wiki/Hash_table)
### 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](https://www.reddit.com/r/adventofcode/comments/a20646/2018_day_1_solutions/eaukxu5/). Check out his solution if you are interested in the detail.