create account

Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018 by steempytutorials

View this thread on: hive.blogpeakd.comecency.com
· @steempytutorials · (edited)
$22.85
Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018
![banner.png](https://www.digifloor.com/wp-content/uploads/2016/07/python-banner.jpg)

---

#### Repository
https://github.com/python

#### What will I learn

- .strip()
- Counting characters
- True == 1 and False == 0
- Nested loop
- .pop()


#### Requirements

- Python 3.7.2
- [Pipenv](https://pypi.org/project/pipenv/)
- git

#### Difficulty

- basic

---

### Tutorial

#### Preface

This tutorial is part of a course where solutions to puzzles from [Advent of Code 2018](https://adventofcode.com/2018/) are discussed to explain programming techniques using Python. Each puzzle contains two parts and comes with user specific inputs. It is recommended to try out the puzzle first before going over the tutorial. Puzzles are a great and fun way to learn about programming.

While the puzzles start of relatively easy and get more difficult over time. Basic programming understanding is required. Such as the different type of variables, lists, dicts, functions and loops etc. A course like [Learn Python2 from Codeacademy](https://www.codecademy.com/learn/learn-python) should be sufficient. Even though the course is for Python two, while Python 3 is used.

This tutorial will look at [puzzle 2](https://adventofcode.com/2018/day/2)

#### Setup

Download the files from Github and install the virtual environment

```
$ cd ~/
$ git clone https://github.com/Juless89/tutorials-aoc
$ cd tutorials-aoc
$ pipenv install
$ pipenv shell
$ cd part_2
```

#### Part one

> To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.

To solve this puzzle the frequency of each character inside the string has to be counted. Then those characters that appear 2 or 3 times their counts have to counted as well.

#### .strip()

First the data has to be imported and formatted in the correct form. A list of strings is the most efficient. 

The text file is many lines of strings:
```
lsrivfotzgdxpkefaqmuiygchj
lsrivfotzqdxpkeraqmewygchj
lsrivfotzbdepkenarjuwygchj
lsrivfotwbdxpkeoaqmunygchj
```

Reading these lines from the file once again adds a `\n` to each line. This time the lines will be used in a string format. `.strip()` can be used to remove the newlines. Doing this inside a list comprehension returns a list.

```
# read input file and convert to list
# remove `\n`
with open('input.txt', 'r') as file:
    lines = [x.strip() for x in file]
```
<br>
`[x.strip() for x in file]` is equal to:

```
list = []

for x in file:
    list.append(x.strip())
```

#### Counting characters

A string is basically a list and can be used in a similar way. This means accessing a character at a specific index:

```
string = 'test'

print(string[0])

t
```

And iterating over each character inside a string with a for loop. To store the count for each character a dict can be used. Where the character is the `key`. If the `key` is already inside the dict the value can be increased by 1, if not the `key` can be added and set to 1.


```
# go over each line
for line in lines:
    # store each character's count
    count = {}

    # check if c already in count
    for c in line:
        if c in count:
            count[c] += 1
        else:
            count[c] = 1
```

#### True == 1 and False == 0

`Count` now contains the frequency of each character.

```
{'l': 1, 's': 1, 'r': 1, 'i': 1, 'v': 2, 'f': 1, 'o': 1, 't': 1, 'z': 1, 'b': 1, 'd': 1, 'x': 1, 'p': 1, 'h': 2, 'e': 1, 'n': 1, 'a': 1, 'q': 1, 'm': 1, 'u': 1, 'w': 1, 'y': 1, 'g': 1, 'j': 1}
```

Only the values are needed. These can be extracted with `.values()`. 

```
dict_values([1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
```

When adding a Boolean statement `True` equals 1 and `False` equals 0. 

```
# store results
results = {'2': 0, '3': 0}

# if 2/3 in values, statement is true
# increment with 1 
results['2'] += 2 in count.values()
results['3'] += 3 in count.values()

# return amount of 2s * 3s
return results['2'] * results['3']
```

<center>![Apr-06-2019 21-40-13.gif](https://cdn.steemitimages.com/DQmZWKC9zPiBpVHQhE3jjBi7Li6rWbYqdCe12hLsVCtzPfE/Apr-06-2019%2021-40-13.gif)</center>

#### Part two

> The boxes will have IDs which differ by exactly one character at the same position in both strings. What letters are common between the two correct box IDs?

To solve this puzzle all lines have to be compared with each other.

#### Nested loop
The straight forward way would be to create a nested loop where each line gets compared to each line.

```
for line1 in lines:
    for line2 in lines:
        # compare line1 and line2
```

However this would mean that lines will be checked against each other and themselves multiple times. When dealing with a lot lines can increase the run time significantly.


#### .pop()
Another solution is to remove the line from the list with `.pop()` and compare that with the remainder of the list. This way each comparison will only be made once.

```
# loop until lines is empty or answer
# is found
while len(lines) > 0:
    # remove line from list
    line1 = lines.pop(0)

    # loop through remaining list
    for x in range(0, len(lines)):
        line2 = lines[x]

        # compare line1 and line2
```

The lines are compared on a character by character basing, this guarantees the same order. For each character that is different 1 gets added to difference. When the difference is 1 the line is returned

```
difference = 0
# compare each character, for each
# character that is different add 1
# to difference
for c in range(0, len(line1)):
    if line1[c] != line2[c]:
        difference += 1

# if difference is 1 return id
if difference == 1:
    return line1
```

<center>![Apr-06-2019 21-40-40.gif](https://cdn.steemitimages.com/DQmRyKdmyXZtqtpdMAndCnXWzCEGUrGEiKSGhZNUBJEweTP/Apr-06-2019%2021-40-40.gif)</center>

#### Running the code


```
if __name__ == "__main__":
    # read input file and convert to list
    # remove `\n`
    with open('input.txt', 'r') as file:
        lines = [x.strip() for x in file]

    print(f'Part 1 Answer: {part_1(lines)}')
    print(f'Part 2 Answer: {part_2(lines)}')
```

Run the code with:

`python main.py`


<center>![Screenshot 2019-04-06 at 21.41.58.png](https://cdn.steemitimages.com/DQmfWaWgWAQH6W8W9ngFQsvJRoedHE7XVfJ1Ks9dKM6UnkC/Screenshot%202019-04-06%20at%2021.41.58.png)</center>


#### Curriculum
[Part 1](https://steemit.com/utopian-io/@steempytutorials/learn-how-to-program-with-python-1---solving-puzzles-from-advent-of-code-2018)

---

The code for this tutorial can be found on [Github](https://github.com/Juless89/tutorials-aoc)!

This tutorial was written by @juliank.
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 125 others
properties (23)
authorsteempytutorials
permlinklearn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","python","course","programming"],"app":"steemit/0.1","users":["juliank"],"image":["https://www.digifloor.com/wp-content/uploads/2016/07/python-banner.jpg","https://cdn.steemitimages.com/DQmZWKC9zPiBpVHQhE3jjBi7Li6rWbYqdCe12hLsVCtzPfE/Apr-06-2019%2021-40-13.gif","https://cdn.steemitimages.com/DQmRyKdmyXZtqtpdMAndCnXWzCEGUrGEiKSGhZNUBJEweTP/Apr-06-2019%2021-40-40.gif","https://cdn.steemitimages.com/DQmfWaWgWAQH6W8W9ngFQsvJRoedHE7XVfJ1Ks9dKM6UnkC/Screenshot%202019-04-06%20at%2021.41.58.png"],"links":["https://github.com/python","https://pypi.org/project/pipenv/","https://adventofcode.com/2018/","https://www.codecademy.com/learn/learn-python","https://adventofcode.com/2018/day/2","https://steemit.com/utopian-io/@steempytutorials/learn-how-to-program-with-python-1---solving-puzzles-from-advent-of-code-2018","https://github.com/Juless89/tutorials-aoc"],"format":"markdown"}
created2019-04-06 19:48:33
last_update2019-04-06 19:54:27
depth0
children7
last_payout2019-04-13 19:48:33
cashout_time1969-12-31 23:59:59
total_payout_value17.170 HBD
curator_payout_value5.684 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,866
author_reputation31,094,047,689,691
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id82,594,187
net_rshares38,675,957,356,223
author_curate_reward""
vote details (189)
@athconnect ·
Excellent blog post on the world of programming. πŸ‘
properties (22)
authorathconnect
permlinkre-steempytutorials-learn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018-20190406t195123981z
categoryutopian-io
json_metadata{}
created2019-04-06 19:51:24
last_update2019-04-06 19:51:24
depth1
children0
last_payout2019-04-13 19:51:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length50
author_reputation29,422,719,219
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,594,354
net_rshares0
@portugalcoin ·
$10.32
Thank you for your contribution @steempytutorials.
After reviewing your tutorial we suggest the following points listed below:

- We suggest that you don't repeat information that you put in the previous tutorials. For example, the <b>Setup</b> section.

- Thanks for following our suggestions to improve your tutorials.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/2-1-3-1-1-3-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorportugalcoin
permlinkre-steempytutorials-learn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018-20190407t123739689z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["steempytutorials"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/2-1-3-1-1-3-1-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-04-07 12:37:39
last_update2019-04-07 12:37:39
depth1
children2
last_payout2019-04-14 12:37:39
cashout_time1969-12-31 23:59:59
total_payout_value7.852 HBD
curator_payout_value2.464 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length817
author_reputation598,828,312,571,988
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,633,790
net_rshares16,577,753,854,653
author_curate_reward""
vote details (22)
@steempytutorials ·
Thanks for the feedback!
properties (22)
authorsteempytutorials
permlinkre-portugalcoin-re-steempytutorials-learn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018-20190407t141442801z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2019-04-07 14:14:42
last_update2019-04-07 14:14:42
depth2
children0
last_payout2019-04-14 14:14:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length24
author_reputation31,094,047,689,691
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,638,120
net_rshares0
@utopian-io ·
Thank you for your review, @portugalcoin! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-steempytutorials-learn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018-20190407t123739689z-20190410t033553z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-04-10 03:35:54
last_update2019-04-10 03:35:54
depth2
children0
last_payout2019-04-17 03:35:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation152,955,367,999,756
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,782,964
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @steempytutorials!

You just got a **0.26%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinklearn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018---vote-steemplus
categoryutopian-io
json_metadata{}
created2019-04-07 14:21:54
last_update2019-04-07 14:21:54
depth1
children0
last_payout2019-04-14 14:21:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length443
author_reputation247,952,188,232,400
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,638,520
net_rshares0
@steem-ua ·
#### Hi @steempytutorials!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-learn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018-20190407t131939z
categoryutopian-io
json_metadata"{"app": "beem/0.20.19"}"
created2019-04-07 13:19:39
last_update2019-04-07 13:19:39
depth1
children0
last_payout2019-04-14 13:19:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length295
author_reputation23,214,230,978,060
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,635,570
net_rshares0
@utopian-io ·
Hey, @steempytutorials!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-learn-how-to-program-with-python-2---solving-puzzles-from-advent-of-code-2018-20190408t002014z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-04-08 00:20:15
last_update2019-04-08 00:20:15
depth1
children0
last_payout2019-04-15 00:20:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length598
author_reputation152,955,367,999,756
root_title"Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,663,777
net_rshares0