Native mobile development skills are rare. Being able to point to apps in the store and say “I made those” can really help you stand out. And you can learn how — at half price, if you hurry!
raywenderlich.com is the premier mobile development tutorial subscription site, and right now, everything in their store is 50% off!
Ultimate Beginner plan ($12.42/month on sale, normally 29.99/month):
Access beginner courses
Access beginner books
iOS & Android learning paths
Stream via iOS/Android app
Hands-on challenges
Download source code
Forum membership
Ultimate Professional plan ($24.92/month on sale, normally 49.99/month):
Access every book in our library
Access advanced ‘Pro’ courses
Download & watch courses offline
Access beginner courses
iOS & Android learning paths
Stream via iOS/Android app
Hands-on challenges
Download source code
Forum membership
These deals last only until Monday, November 30th, so if you want to save some money and pick up or sharpen your mobile dev skills, you should act quickly!
I learned iOS development through RayWenderlich.com’s book, iOS Apprentice, back when Objective-C was the only option, and the iPhone 4S was the hot new device. I became a regular reader of the site, and over time, got to know the team and even joined, going on to write articles, a video, and even co-author a book! I can’t think of a better place or a better group of people to learn from.
As I wrote in the previous post, the Advent of Code is happening soon — it start on Tuesday, December 1st and runs all the way to December 25th. If you want to give your programming skills a good workout or test, you’ll want to try out Advent of Code’s challenges!
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven’t determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
While the problems in the Advent of Code are the same for every participant, the data for each participant is different (there’s a sign-up process, which gives you an account, your own progress tracker, and your own data). This prevents participants from simply sharing the solution.
Here are the module masses that were provided for my account:
I used the Python REPL for my Python-based solution. For my Swift-based solution, I used the closest analogue: an Xcode Playground.
Swift, like Python uses the three quotes to denote multiline strings. I used them to define a multiline string constant, rawInput, into which I pasted the data:
With rawInput defined, it’s time to convert it from a multiline string into an array of strings, with each line getting turned into its own array element. The String class’ split method does this quite easily, and the result was the splitInput string array:
let splitInput = rawInput.split(separator: "\n")
The next step was to convert splitInput’s numbers-in-string-form into actual numbers. This process would involve applying the same function — the Int struct’s “init from string” method — to all the elements in an array, which is exactly what the map method is for:
let masses = splitInput.map {Int($0)!}
Swift’s map method takes a closure containing a function as its argument and applies that function to every item in the given array, creating a new array as its result.
In this case, the function in question is:
Int($0)!
Parameters passed into the closure begin with the $ character, which is then followed by a number specifying which parameter it is. The first parameter is $0, followed by the second parameter, $1, followed by the third parameter, $2, and so on.
Only one parameter is passed to the closure: $0, which represents the current element of the splitInput array. It’s fed into the init method of Int that takes a string and attempt to produce an integer. Since it’s possible that this method will be given a string that can’t be converted into an integer, the method’s return type is the optional type Int?.
Since I’m quite certain that all the strings in the splitInput array convert to integers, I used the ! operator to force unwrap the resulting Int? values.
The end result is masses, an array of integers. Each element in the array represents the mass of a component in the ship, and we need to calculate the fuel necessary to propel each component to the final destination.
This calculation involves applying a function to every element in masses, and that function is:
Divide the mass by 3, rounding down.
Subtracting 2 from the result above.
Once again, I used map:
let fuelRequirements = masses.map { mass in
mass / 3 - 2
}
In the function above, mass and 3 are both integers, so mass / 3 is an integer division, which automatically rounds down.
The result of this mapping is fuelRequirements, an array of integers containing the fuel requirements for each module.
The result is the sum of all the values in fuelRequirements. Unfortunately, Swift doesn’t have a built in method for getting the sum of an array, so we’ll need to roll our own:
let totalFuel = fuelRequirements.reduce(0, +)
For my data, the result was 3454942. This turned out to be correct, so it was time to tackle part two.
Day one challenge, part two
Part two involved recalculating the fuel requirements when also taking into account the mass of the added fuel:
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module – take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)
This called for a recursive function, the Swift code for which is below:
func fuelRequired(mass: Int) -> Int {
let result = mass / 3 - 2
if result <= 0 {
return 0
} else {
return result + fuelRequired(mass: result)
}
}
I used this function to map the values in the masses array from part one onto a new array, updatedFuelRequirements…
let updatedFuelRequirements = masses.map { fuelRequired(mass: $0) }
…and the sum of its the elements was the answer for part two:
let updatedTotalFuel = updatedFuelRequirements.reduce(0, +)
We’re only a few days from December, which means it will soon be time for the great programming exercise known as the Advent of Code!
Think of it as an Advent calendar, but chocolates (or cheese, or wine), you’re presented with a new programming puzzle every day between the start of December and Christmas Day, in which you try to save Santa’s mission. You can use whatever programming language you want, and you don’t need to be an expert — as the site says, “just a little programming knowledge and some problem solving skills will get you pretty far.”
Not only do I plan on participating in this year’s Advent of Code, but I might even use a couple of the challenges in the Python class I’m currently teaching on behalf of Computer Coach.
Solving Advent of Code 2019’s day one challenge
Here’s the premise of the 2019 Advent of Code’s challenges: Santa is stuck at the edge of the solar system, and you have to rescue him. Each day’s challenge, which has two parts, gets you closer to bringing him home and saving Christmas.
The Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven’t determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
While the problems in the Advent of Code are the same for every participant, the data for each participant is different (there’s a sign-up process, which gives you an account, your own progress tracker, and your own data). This prevents participants from simply sharing the solution.
Here are the module masses that were provided for my account:
Now that I had the data in a string, I could split the string into an array, using the newline character as the delimiter. I named the array split_input:
split_input is an array of strings which needed to be converted into integer values.
In many other languages, I’d do this by using the map function to apply a “convert a string to its integer value” function to every item in the array, creating a resulting array called masses. Here’s the Python version of that approach:
It works, but from a Python programming point of view, it just doesn’t feel right.
The Pythonic approach would involve using a list comprehension instead of map (and then using the resulting iterator into a list). It just seems more readable:
Once I had the masses, I could then calculate the fuel requirements for each mass. This may be the only time I’ve ever made use of Python’s floor division operator, which performs an integer division, rounding down:
I entered the value for total_fuel into the solution text field, and Advent of Code immediately told me that I had solved part one of the day one challenge! So far, so good.
Day one challenge, part two
Part two of the challenge was a refinement of part one:
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module – take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)
Upon reading this, my first thought was:
The trick to writing recursive functions is to solve the “escape” case first — that is, the case where you stop the recursion and just return a value.
For this problem, the “escape” case is when the repeated fuel calculation gives a result of 0 or less:
if result <= 0:
return 0
Otherwise, take the result, and apply the fuel calculation to it again. That’s what gives us the recursive part. Here’s the resulting if statement:
if result <= 0:
return 0
else:
return result + fuel_required(result)
And finally, we have to handle the initial calculation. The end result is the fuel_required function:
def fuel_required(mass):
result = mass // 3 - 2
if result <= 0:
return 0
else:
return result + fuel_required(result)
Now that we have the fuel_required function, we can apply it to every item in the masses array from part one:
.NET 5 is the 5th major version of the .NET framework, the bedrock of modern Microsoft platform development, and it’s out today!
(For the official details, check out the announcement on the .NET Blog: Announcing .NET 5.0.)
In addition to being the replacement for .NET Framework 4.8 as well as .NET Core 3.1, .NET 5 unifies the various flavors of .NET — “.NET Framework”, .Net Core, Mono, the various Xamarins, Unity, and Universal Windows Platform — into a more cohesive whole.
With this new version of .NET come new versions of its programming languages: C# 9 and F#5, as well as ASP.NET Core and EF Core.
Once again, it’s this Saturday, it’s online, and it’s free-as-in-beer to attend. REGISTER HERE.
The conference will be made of bite-size (15 minutes or shorter!) presentations by Tampa Bay techies and demos of capstone projects by Suncoast Developers Guild alums.
I’ll be presenting on PyGame!
I’ll be doing a presentation in the afternoon, on the topic of…
…PyGame! I try to keep my topics both technical and fun, and what’s more technical and fun than Python and game development?
There will be coding, and I’ll also provide source code and a free book.
What was the last Suncoast Developers Conference like?
The last conference took place in the summer, and was also free and online. You can watch the entire thing below…
…and if you want to jump to a specific presentation, use these links:
In another life, I was a developer evangelist who travelled across North America and I saw tech scenes from Palo Alto to Peoria. I can tell you that one of the signs of a healthy tech community in a small- to medium-sized city is a coding school that acts as a social/technical/gathering place. If your city had one, things were looking up for local techies. If not, it was a safe bet that the place was experiencing a brain drain.
Here in Tampa Bay, Suncoast Developers Guild fills that vital role, and it does so spectacularly. They’re a key part of the heart and soul of tech in the area, and it shows in their efforts, such as events like this.