Day 3: Toboggan Trajectory

problem

Parsing the Input

The input this time is a file consisting of ‘#’ and ‘.’ and we only need a list of each line. We can accomplish this by using the file.readlines() function.

Part 1

The file above is a map of the terrain, which repeats indefinitely horizontally. The goal is to count the amount of trees you bump into when given a slope to travel down the slope we are given is right 3, down 1. We start at the top left of the map of the toboggan track where ‘#’ indicates a tree and ‘.’ indicates an open square. we can find the width of the track by getting the length of a line of the data, then we set x to how far right we are travelling and y to how far down.

We initialise a trees variable which starts at 0 and then initalise a while loop which goes until we get to the bottom of the track. under the while loop we want to increment the trees variable if we are on a tree, then we increment our y and x variables by how far we travel. When we increment x, we need to make sure we do not get an index error, so once we increment it, we have to change x to x mod width.

After counting all the trees, we return the amout we counted.

Part 2

Part 2 asks us to find the amount of trees we bump into for different slopes. This is good because we can reuse the function we wrote for the first part. It gives us slopes and asks us to multiply the outcomes, so all we need to do is iter through the slopes, plug them into the function and multiply the results.

Day 2: Password Philosophy

problem

Parsing the Input

The input is given with each line containing a range, letter, and a password.

I found the easiest way to parse the input was using regular expressions or RegEx

Part 1

In part 1 we are given a character that has to be in the password between a certain amount of times. For example in the line

the character ‘a’ has to be in the password ‘abcde’ between 1 and 3 times inclusive.

we can set this up by iterating through the list of passwords and incrementing the correct count if it is valid. Python has a handy method for strings called str.count which allows you to count th enumber of occurences of a string (in this case the key) in a string (the password). Documentation. If the number of times the key occurs is between the bounds given, the password is valid, and we increment the counter. After we have incremented through the whole list, we return the valid count.

Part 2

Part 2 changes the rule of a valid password. A password is now valid if only one of the positions given by the bounds (starting at 1) is the key. We can iter through the list like before but the condition on when we increment the valid counter changes. We can use Python’s bitwise xor operator, ^, when checking against if the index of the list is the key for both locations to make sure one of them is the key but not both. After iterating through the whole list and incrementing the counter when we come across a valid password, we return the result.

Day 1: Report Repair

problem

The first day of the new years challenges came with a nice relaxing solution.

Parsing the Input

The input is given as a list of integers seperated by line breaks like the following.

This can be parsed in python by reading the file, and creating a set containing the integer of each line. The reason a set is preferred as opposed to a list is because in this problem, there are no repeated numbers and for lookups, sets have on average an O(1) time complexity whereas lists have O(n) time complexity.

Part 1

In part 1, we have to find the only 2 integers in that set that add to 2020 and then multiply them together. this can be accomplished by iterating through the set and seeing if the number that would make the sum total 2020 existed in the set. When found, the product of the 2 numbers is returned.

Part 2

Part 2 is similar to part 1, but we have to find 3 numbers in the set that add to 2020. This can be done in primarily the same way, but we need to iter through the set a second time so we can know if the third number that sums to 2020 is in the set. Once again when we have found the 3 numbers, we multiply them together and return the result.

Integration Problem 3

Integral taken from instagram user @integralsforyou

let u=x^{-1}

Integration problem 2

Integral taken from instagram user @integralsforyou

let u=\cos(x)

Integration problem 1

Integral taken from instagram user @integralsforyou

The Quadratic Formula

The quadratic formula is used to solve for the roots of a quadratic polynomial (the highest power is 2)

given ax^2+bx+c=0, where a\ne0

Proof using completing the square

multiplying both sides by 4a, we get