> - [Return to challenge instructions](https://developer.ansys.com/pybake-24/challenge-instructions)
> - [Download the Level 1 Challenge Files](https://github.com/ansys/DevRelPublic/blob/main/Events/PyBake2024/Level1Challenges.zip)
> - [Submit Level 1 Answers Here](https://developer.ansys.com/form/steel-city-pybake-level-1-answer)

# Preamble

In programming we assign values to variables, which we can then pass around the program. We can parcel up part of the program into a re-usable block as a "function" which can be called on variables. "functions" are written as words ending with a pair of parentheses in Python, like `input()` which requests user keyboard input.

In programming there are a few basic "types" of value. The three most common are as follows.

- strings - `'this is a string'` - collections of characters
- integers - `23` - whole numbers
- floats - `0.5` - decimal (or "floating point") numbers

... And the most useful function for beginners, which you will use more than any other, is the `print()` function.

You can print objects to screen using the built-in `print` keyword function.

Try this out.

```python
print('Hello World!')

```

## Challenge 1 - Hello World

Write a short program that, when run, asks for the user's name and then gives them a random personal affirmation! This personal affirmation should be one of 6 strings

## FINAL ANSWER

Each challenge will have a single, "Final Answer", which you will need to enter correctly in the submission form. You can (try to) submit the form as many times as you like with no penalty. It will only work if all of your answers are correct, otherwise the form will tell you which are wrong.

> - [Submit Level 1 Answers Here](https://developer.ansys.com/form/steel-city-pybake-level-1-answer)

This first challenge is just a warm up and you can have the final answer for Challenge 1 for free! It is:

```
OneAnsys

```

## Challenge 2

In the digital age it is very common to receive text files in emails containing data. A lot of the time this will be in Excel, but sometimes it can also be in Word, or similar. But how do you deal with it? Well, opening files in Python is actually quite simple!

Once opened they can be worked with in a variety of ways to extract all kinds of useful information.

```Python
with open('Challenge2.txt', encoding='utf8') as f:
    data = f.read()

```

This reads in the contents of `Challenge2.txt` into a string variable called `data`.

This test file is real, and is included in the same directory as the challenge notebook. It contains the entire text of the book Don Quixote by Miguel de Cervantes. Your challenge is to write a script that calculates 3 values from this text.

1. How many times does the name `"Don Quixote"` appear in the file (as written)
2. How many times does the upper case version of the name appear? (i.e. `"DON QUIXOTE"`)
3. How many times does `"Don Quixote"` appear in the file case-**insensitive**?

## FINAL ANSWER

The final answer for this challenge is the sum of parts 1, 2 and 3.

## Challenge 3 - Not again Bob

We're getting into much more realistic territory with this next challenge. Recently you're having a look at the company budget and notice it looks like Bob's been paying an awful lot to this one vendor, some "HADOOP" and you're suspicious that he might going over his (already generous) budget of 300000 units of money per calendar year. So you decide to do some basic calculations to look at how much he's been spending.

You have a year's worth of expenses to various companies (see the challenge 3 files) that look like this. This is February.

```csv
PAYEE,AMOUNT
SWIRLIX,7812
BISHARP,9
LITWICK,2550
SPHEAL,12211
HELIOLISK,7000
ELGYEM,1000
CINCCINO,60
HADOOP,8799
HYDREIGON,77777
GOREBYSS,876
KOMMO-O,1
DUOSION,54662
WATCHOG,75687
ARCHEOPS,10000
BLACEPHALON,3
GIBLE,899
WOOLOO,90

```

Write a Python script that adds up the total expenses paid by Bob to his vendor over the previous calendar year and tells you the amount he spent on HADOOP.

What about the totals for swirlix and wooloo? Use the **total** amount of all three as your final answer.

## FINAL ANSWER

Your final answer to is the amount spent on HADOOP, SWIRLIX, and WOOLOO in total.

## Challenge 4 - Cloakroom Chaos

You're helping out with your friend's party, and you've agreed to help out with the cloakroom! Only he lives in a big house with 7 quite small cloakrooms, which is all you have to work with. They can hold 23, 13, 31, 37, 31, 43, and 17 respectively. You decide to fill up each cloakroom in turn as people come in with their coats. The cloakrooms are all named after National Parks in Utah (taken from [utah.com](https://www.utah.com/destinations/national-parks/)):

1. Arches - 23
2. Bryce Canyon - 13
3. Canyonlands - 31
4. Capitol Reef - 37
5. Grand Canyon - 31
6. Great Basin - 43
7. Mesa Verde - 17

You decide to fill up the cloakrooms in this order. The guests arrive (according to the `Challenge4.txt` file) and you put their cloaks away.

### Part 1

Some hours later, people start to leave. The following five guests approach you.

- Erwin Murray
- Jim Miller
- Christine York
- Dorothy Araujo
- Ruth Battle

Which cloakrooms are their cloaks in?

Names were generated using this package: [names](https://pypi.org/project/names/).

### Part 2

Unfortunately these 5 guests report to you that their coats aren't the right ones! It turns out that quite a few guests didn't have coats at all but the system counted them as if they did. Looking at the guest list you realise a pattern. Somehow, after the first 7 guests have arrived, the 5th guest after that didn't have a coat, and the 5th guest after THAT and so on. With this in mind you go back to your system and find the correct cloaks for the 5 guests, which rooms are they in? Did all 5 guests even come with coats?

For example, if the following guests arrived in this order, the first 7 have coats

```
Roman Thompson - coat
Otis Sahota - coat
Marcie Case - coat
Angela Floyd - coat
Erwin Murray - coat
Richard Murphy - coat
Harold Snell - coat
-----------

Now, the 5th guest of each group of 5 is coat-less!

Lillian Fox - coat
Marjorie Dunn - coat
Amy Lamoureux - coat
Burt Klein - coat
Daniel Thomas - NO COAT
-----------
Melvin Gomez - coat
Frederick Gordon - coat
Karen Sawyer - coat
Julia Kennedy - coat
Joyce Watkins - NO COAT
-----------
etc.

```

### FINAL ANSWER

To construct the final answer to this challenge you must take the names of the cloakrooms that the 5 guests' coats were in (if they had no coat, they should be left out) and concatenate the **cloakroom** names into a single string with spaces removed.

For example, if the 5 had put their coats in `Arches`, `Mesa Verde`, `Arches`, `Capitol Reef`, respectively, and the 5th had no coat, your final answer would be:

> `ArchesMasaVerdeArchesCapitolReef`

## Challenge 5 - Decoding Messages

You have been learning programming with a friend, and he's just sent you a strange message (`Challenge5.txt`)

He tells you it can be decoded by printing a character `*` at each position denoted by the numbers. This line wraps around to the next line after the **274th** character. Why can't he just be normal?

For example if we consider the following message, which wraps around after the 12th character:

```
1 6 10 13 17 19 22 25 29 31 34 37 38 39 42 46 47 48

```

It would mean that we print a `*` as the first character

```
*

```

Then we print spaces (or `.` in this case) until we reach the next `*`, which will be the *6*th character

```
*....*

```

Then we do the same for the next: the 10th character.

```
*....*...*

```

The next character is 13, but we wrap around after the 12th character, so it looks like this:

```
*....*...*..
*

```

If we keep going to the end of the second line it looks like this

```
*....*...*..
*...*.*..*..

```

If we keep going to the end of the message we get this pattern

```
*....*...*..
*...*.*..*..
*...*.*..*..
***..*...***

```

har har.

#### Having trouble opening the input file?

You may have some issues opening the accompanying input file in some text editors. It may appear as a list of non-numeric characters. This is an encoding issue and can be solved by opening the file in another text editor. Opening the file in VS Code or Word, or just Python, for example, should fix it. The problem has been seen in Notepad so far.

What does your friend's message say? It is case-sensitive so make sure that any `w` aren't actually `W`!

### FINAL ANSWER

Your final answer is the series of characters that your friend's message spells out.

Your friend tells you it's from YouTube and if you paste this url:

- `https://www.youtube.com/watch?v=`

into your browser and add the code to the end you'll see what he was trying to tell you.

## Challenge 6 - Tournament Arc

Rock Paper Scissors... the greatest game ever devised... Or so says your friend who loves RPS. He's challenged you to a duel but the rules are a bit different to what you're used to.

- You're going to play 100 times
- Whoever wins more than 50% of the time, wins outright!
- You have to decide all your plays ahead of time

However, your friend makes a mistake and accidentally shares all of his plays with you ahead of time (see file `Challenge6.txt`).

### Part 1

You decide to capitalise on this and devise your own list of plays to beat him utterly and win every single game. How many times must you play Rock, Paper, and Scissors, **each**, to beat him?

For example, suppose the game was only 3 hands long and his planned list was Rock -&gt; Rock -&gt; Paper.

```
R
R
P

```

Then the winning set would be Paper -&gt; Paper -&gt; Scissors!

```
H   A
R < P -> You Win!
R < P -> You Win!
P < S -> You Win!

```

Your answer would then be `0R2P1S` as you played no rocks, two papers and one scissors to win.

### Part 2

After thinking about it a bit you realise that beating him 100% might lock a bit suspicious. You only need to win more than 50% of the time to win outright, so you come up with a simple rule to choose when you win and when to lose.

- To get a 50% win rate you decide to deliberately **lose every other hand**, starting with the second hand.
- But to win outright you decide to **win the last 3 hands** to make sure you get over 50%.

How many times must you play Rock, Paper, and Scissors this time?

### FINAL ANSWER

Combine your answers from Parts 1 and 2. What is the total number of times you play Rock, Paper and Scissors? For example, if your Part 1 answer was `3R7P5S` and your Part 2 answer was `7R5P5S` then your final answer would be `10R12P10S`.

---

## Challenge 7 - Now You're Thinking With Portals!

You have a 2D ball moving in 2D space. This ball is moving through with velocity `v` (in units per second) along a horizontal line in 2D space at `y = 0`. At the start of the scenario (`t = 0`), it is at position `x = 0`, `y = 0` and is travelling in the positive `x` direction. In its path is a portal, when the ball's centre passes through the portal it is shifted 1 unit upwards (positive y-direction) and reverses its velocity. If it encounters another portal it is shifted up again and its velocity reversed again, and so on. This carries on for 600 seconds. You have to find out the ball's y-position after 600 seconds have elapsed. Remember, not all portals will necessarily be used!

Your puzzle input is a list of portal coordinates, as `x, y` coordinate pairs.

### Expressions you will need

velocity is equal to distance divided by time, so time elapsed is equal to distance covered divided by velocity, and the distance travelled is equal to velocity multiplied by time elapsed.

```
v = d/t
t = d/v
d = v*t

```

### Example

Suppose the ball is moving at `v = 3` and your list of portal coords looks like this.

```
3, 0
4, 1
-3, 1
0, 2

```

This means the following events will occur:

1. The ball starts at `(0., 0.)` travelling rightwards with velocity `3.` units per second
2. The ball enters its first portal at coordinate `(3., 0.)` after 3. units of distance has been traversed, and thus time elapsed = `3./3. = 1.` seconds
3. The ball is now at `(3., 1.)` travelling with velocity `-3.` (leftwards)
4. The ball travels 6. units to the left reaching its second portal at coordinates `(-3., 1.)`. The time elapsed since it entered the first portal is `6./3. = 2.` seconds. The total time elapsed is `1.` + `2.` = `3.` seconds. The ball passes through the second portal.
5. The ball is now at `(-3., 2.)` travelling rightwards with velocity `+3.` units per second
6. The ball travels 3 more units to enter its third portal at `(0., 2.)`. The time elapsed since it last travelled through a portal is `3./3. = 1.` second. The total time elapsed is `1.` + `3.` = `4.` seconds.
7. The ball is now at `(0., 3.)` and travelling with velocity `-3.` units per second (leftwards).
8. There are no more portals in the ball's path so the final answer is the y coordinate: **`3.`**.

A visualisation of the example scenario is below. Portal entries are in orange and their exits are in blue.

**Note: the ball's distance travelled, or time elapsed when entering a portal, can be a floating point number. Integers have been used in the example for ease. However, all the answers are y-coordinates and y-values can only be integers. The Final Answer must be in integer format.**

![](https://developer.ansys.com/sites/default/files/inline-images/portal_example2.png)

### Part 1

Given the portal coords in `Challenge7.txt` and a velocity of `v = 5`, what is the final y coordinate of the ball?

### Part 2

In this instance, the ball starts at a much faster velocity of `v = 500`, but every time it goes through a portal the magnitude of the velocity halves (and the direction still changes). What is the new final y coordinate of the ball after `600` seconds?

### Part 3

In this final scenario you are given the power to avoid portals if you want! The ball can ignore the portal and carry on as if it wasn't there. Given this new dynamic, what is the largest y-coordinate you can reach? This time there is no time limit and no change in velocity magnitude.

### Tips And Tricks

- All solutions are valid if you solved the problem yourself and got the right answer!
- Part 3 
    - You should think about which values you actually need to keep track of and which you don't.
    - If there are only 3 portals in the `y = 3` plane, then there are only 3 possible routes the ball can take. This is true at all heights.

> - [Submit Level 1 Answers Here](https://developer.ansys.com/form/steel-city-pybake-level-1-answer)