Categories
Programming

The Advent of Code is coming in a few days!

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.”

Advent of Code started in 2015, and has been taking place every year ever since. The 2020 edition begins on Tuesday, December 1st at 12:00 midnight Eastern time (UTC-5).

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.

Day one challenge, part one

Here’s the first part of day one’s challenge:

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:

134492
88713
84405
148193
95951
63545
137840
65558
124836
95431
77622
91864
108677
116871
119496
97172
86115
105704
68613
77114
114013
52766
57048
80814
73888
58253
135934
97409
112439
98262
116047
57456
124261
83006
101495
133449
111372
56146
87818
92209
149259
124559
141838
147988
65703
125566
59650
139564
92430
126307
120406
147383
84362
51529
146366
131840
53270
71886
118767
104311
126181
76964
129430
95489
91098
54133
110057
107276
118226
96104
135382
85152
61697
143417
148879
126846
130205
111170
86687
113729
123330
56976
148470
66028
129715
75686
74964
148258
72669
88809
78173
92699
124806
67217
139066
136002
135730
145708
142054
135772

I decided to use the Python REPL to tackle this problem.

My first step was to copy the numbers above, paste them into a triple-quoted string, and assign that string to the variable raw_input:

raw_input = """134492
88713
84405
148193
95951
63545
137840
65558
124836
95431
77622
91864
108677
116871
119496
97172
86115
105704
68613
77114
114013
52766
57048
80814
73888
58253
135934
97409
112439
98262
116047
57456
124261
83006
101495
133449
111372
56146
87818
92209
149259
124559
141838
147988
65703
125566
59650
139564
92430
126307
120406
147383
84362
51529
146366
131840
53270
71886
118767
104311
126181
76964
129430
95489
91098
54133
110057
107276
118226
96104
135382
85152
61697
143417
148879
126846
130205
111170
86687
113729
123330
56976
148470
66028
129715
75686
74964
148258
72669
88809
78173
92699
124806
67217
139066
136002
135730
145708
142054
135772"""

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 = raw_input.split("\n")
>>> split_input
['134492', '88713', '84405', '148193', '95951', '63545', '137840', '65558', '124836', '95431', '77622', '91864', '108677', '116871', '119496', '97172', '86115', '105704', '68613', '77114', '114013', '52766', '57048', '80814', '73888', '58253', '135934', '97409', '112439', '98262', '116047', '57456', '124261', '83006', '101495', '133449', '111372', '56146', '87818', '92209', '149259', '124559', '141838', '147988', '65703', '125566', '59650', '139564', '92430', '126307', '120406', '147383', '84362', '51529', '146366', '131840', '53270', '71886', '118767', '104311', '126181', '76964', '129430', '95489', '91098', '54133', '110057', '107276', '118226', '96104', '135382', '85152', '61697', '143417', '148879', '126846', '130205', '111170', '86687', '113729', '123330', '56976', '148470', '66028', '129715', '75686', '74964', '148258', '72669', '88809', '78173', '92699', '124806', '67217', '139066', '136002', '135730', '145708', '142054', '135772']

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:

>>> masses = list(map(int, split_input))
>>> masses
[134492, 88713, 84405, 148193, 95951, 63545, 137840, 65558, 124836, 95431, 77622, 91864, 108677, 116871, 119496, 97172, 86115, 105704, 68613, 77114, 114013, 52766, 57048, 80814, 73888, 58253, 135934, 97409, 112439, 98262, 116047, 57456, 124261, 83006, 101495, 133449, 111372, 56146, 87818, 92209, 149259, 124559, 141838, 147988, 65703, 125566, 59650, 139564, 92430, 126307, 120406, 147383, 84362, 51529, 146366, 131840, 53270, 71886, 118767, 104311, 126181, 76964, 129430, 95489, 91098, 54133, 110057, 107276, 118226, 96104, 135382, 85152, 61697, 143417, 148879, 126846, 130205, 111170, 86687, 113729, 123330, 56976, 148470, 66028, 129715, 75686, 74964, 148258, 72669, 88809, 78173, 92699, 124806, 67217, 139066, 136002, 135730, 145708, 142054, 135772]

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:

>>> masses = [int(string) for string in split_input]
>>> masses
[134492, 88713, 84405, 148193, 95951, 63545, 137840, 65558, 124836, 95431, 77622, 91864, 108677, 116871, 119496, 97172, 86115, 105704, 68613, 77114, 114013, 52766, 57048, 80814, 73888, 58253, 135934, 97409, 112439, 98262, 116047, 57456, 124261, 83006, 101495, 133449, 111372, 56146, 87818, 92209, 149259, 124559, 141838, 147988, 65703, 125566, 59650, 139564, 92430, 126307, 120406, 147383, 84362, 51529, 146366, 131840, 53270, 71886, 118767, 104311, 126181, 76964, 129430, 95489, 91098, 54133, 110057, 107276, 118226, 96104, 135382, 85152, 61697, 143417, 148879, 126846, 130205, 111170, 86687, 113729, 123330, 56976, 148470, 66028, 129715, 75686, 74964, 148258, 72669, 88809, 78173, 92699, 124806, 67217, 139066, 136002, 135730, 145708, 142054, 135772]

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:

>>> fuel_requirements = list(map(lambda mass: mass // 3 - 2, masses))
>>> fuel_requirements
[44828, 29569, 28133, 49395, 31981, 21179, 45944, 21850, 41610, 31808, 25872, 30619, 36223, 38955, 39830, 32388, 28703, 35232, 22869, 25702, 38002, 17586, 19014, 26936, 24627, 19415, 45309, 32467, 37477, 32752, 38680, 19150, 41418, 27666, 33829, 44481, 37122, 18713, 29270, 30734, 49751, 41517, 47277, 49327, 21899, 41853, 19881, 46519, 30808, 42100, 40133, 49125, 28118, 17174, 48786, 43944, 17754, 23960, 39587, 34768, 42058, 25652, 43141, 31827, 30364, 18042, 36683, 35756, 39406, 32032, 45125, 28382, 20563, 47803, 49624, 42280, 43399, 37054, 28893, 37907, 41108, 18990, 49488, 22007, 43236, 25226, 24986, 49417, 24221, 29601, 26055, 30897, 41600, 22403, 46353, 45332, 45241, 48567, 47349, 45255]

The map/list/lambda approach works just fine, but once again, a list comprehension just seems more elegant to my eye:

>>> fuel_requirements = [mass // 3 - 2 for mass in masses]
>>> fuel_requirements
[44828, 29569, 28133, 49395, 31981, 21179, 45944, 21850, 41610, 31808, 25872, 30619, 36223, 38955, 39830, 32388, 28703, 35232, 22869, 25702, 38002, 17586, 19014, 26936, 24627, 19415, 45309, 32467, 37477, 32752, 38680, 19150, 41418, 27666, 33829, 44481, 37122, 18713, 29270, 30734, 49751, 41517, 47277, 49327, 21899, 41853, 19881, 46519, 30808, 42100, 40133, 49125, 28118, 17174, 48786, 43944, 17754, 23960, 39587, 34768, 42058, 25652, 43141, 31827, 30364, 18042, 36683, 35756, 39406, 32032, 45125, 28382, 20563, 47803, 49624, 42280, 43399, 37054, 28893, 37907, 41108, 18990, 49488, 22007, 43236, 25226, 24986, 49417, 24221, 29601, 26055, 30897, 41600, 22403, 46353, 45332, 45241, 48567, 47349, 45255]

And now that I had the fuel requirements for each module, all I had to do was get their sum:

>>> total_fuel = sum(fuel_requirements)
>>> total_fuel
3454942

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

Christine Darden, one of the “Hidden Figures” mathematicians who helped land the astronauts on the Moon.

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:

>>> updated_fuel_requirements = [fuel_required(mass) for mass in masses]
>>> updated_fuel_requirements
[67212, 44325, 42171, 74062, 47941, 31741, 68888, 32746, 62387, 47682, 38780, 45902, 54306, 58402, 59715, 48553, 43027, 52822, 34277, 38525, 56974, 26354, 28495, 40375, 36913, 29094, 67934, 48670, 56187, 49098, 57991, 28698, 62098, 41470, 50716, 66693, 55654, 28043, 43877, 46073, 74596, 62245, 70886, 73962, 32822, 62751, 29795, 69750, 46184, 63121, 60171, 73658, 42148, 25734, 73150, 65887, 26606, 35910, 59351, 52123, 63058, 38449, 64681, 47710, 45516, 27037, 54994, 53606, 59081, 48018, 67657, 42543, 30817, 71674, 74407, 63393, 65068, 55551, 43311, 56833, 61632, 28459, 74203, 32983, 64824, 37810, 37450, 74096, 36304, 44373, 39054, 46318, 62371, 33576, 69500, 67968, 67832, 72820, 70993, 67853]

This yields the updated fuel requirements for each module. The final step was to get their sum:

>>> updated_total_fuel = sum(updated_fuel_requirements)
>>> updated_total_fuel
5179544

Entering the value of updated_total_fuel into the solution text field completed the day one challenge.

Categories
Programming

Find out more about .NET 5 on the Auth0 Developers Blog

Tap to view at full size.

Graphic: dotNET robot.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.

Piano keyboard showing C# and F# keys

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.

Want to find out more about .NET 5? A good starting point is the article Five Things You Should Know About .NET 5, written by my teammate Andrea Chiarelli on the Auth0 Developers Blog.

Categories
Conferences Current Events Programming Tampa Bay

Suncoast Developers Guild’s Fall 2020 Conference is THIS SATURDAY!

Suncoast Developers Conference this Saturday!

Hey, Tampa Bay! We’ve got another Suncoast Developers Conference happening this Saturday, and you can attend for FREE, from the comfort of your own computer or phone.

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:

  1. 00:00:00 Reticulating Splines…
  2. 00:00:58 Welcome & Introductions
  3. 00:08:15 Jason L Perry: Badges? We don’t need no stinkin’ badges!
  4. 00:22:11 Robert Bieber: Will it Scale?
  5. 00:58:13 Kento Kawakami – Demo: Smash Bros Combo
  6. 01:28:30 Cody Banks & Abtahee Ali – Demo: Evolution X
  7. 01:54:54 Daniel Demerin – The Rubber Duck Pal Program
  8. 02:11:12 Colter Lena – Demo: Furry Friends
  9. 02:24:50 Trent Costa – Demo: Tailgate
  10. 02:41:54 Dylan Attal – Don’t Crash! CSS-Modules in React
  11. 02:59:42 Vincent Tang – How to start your own Coding Podcast 101
  12. 03:30:31 Michele Cynowicz – Pull Requests and the Developers Who Love Them
  13. 03:52:11 Abe Eveland – Demo: Rollerblade Buyers Guide
  14. 04:04:45 Liz Tiller – Post-Bootcamp Reflections
  15. 04:23:20 Joey deVilla – Ren’py
  16. 04:41:20 Michael Traverso – You Do Belong Here
  17. 05:01:46 Rob Mack – Demo: What’s for Dinner?
  18. 05:15:38 Kat Batuigas – A Taste Of Docs As Code

How do you register for the conference?

REGISTER HERE. Once again, it’s this Saturday, it’s online, and it’s free-as-in-beer to attend.

I’ll see you on Saturday!

One more thing: What is Suncoast Developers Guild?

Since opening their doors in the summer of 2018, Suncoast Developers Guild’s coding school has graduated over 100 students, and before that, they taught people to code in their previous incarnation as the Tampa Bay branch of The Iron Yard.

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.

Categories
Programming

Yup.

Categories
Humor Programming

Developer meme of the day

Photo: Chef (labeled “developers”) pouring olive oil from a comically oversized bottle (labeled “JavaScript”) onto a salad (labeled “website”).
Tap to view at full size.

Not pictured: A oil drum-sized barrel of balsamic vinegar labeled “React”, “Vue”, or “Angular”, and a forgotten can of anchovies labeled “jQuery”.

Thanks to Cameron Barrett for the find!

Categories
Hardware Programming

Yes, you can run Visual Studio Code on Raspberry Pi

This is the first in “Cobra Pi”, a series of articles on getting the most out of your Raspberry Pi!

Yes, you can run Visual Studio Code on Raspberry Pi!

You’ve got many options for editing code or other plain text files on your Raspberry Pi. It is, after all, a Linux machine, and you’ve got all the classic command-line editors — vim, emacs, and…

And the windows-and-mouse-based Geany (text editor) and Thonny (beginner-friendly Python IDE) come along with even the bare-bones version of the Raspberry Pi OS setup.

But if you’re like about half the developers who answered the 2019 Stack Overflow survey, your “home” editor is Visual Studio Code. And yes, you can run Visual Studio Code on Raspberry Pi.

How to install Visual Studio Code on Raspberry Pi

If you go to Visual Studio Code’s “downloads” page, you’ll see this:

Tap to view at full size.

For the Raspberry Pi, you want to download the Debian package for systems with ARM processors (click on the ARM button in the .deb row).

Once downloaded, go to your Downloads directory and double-click on the the .deb file you just downloaded. You’ll see greeted with this dialog box:

Click the Install button. You’ll be presented with another dialog box, this time asking for your user password, since it’s required when installing new applications:

Enter the password you use to log into the Raspberry Pi into the Password field and click OK.

Visual Studio Code will be installed on your Pi. Once the process is done, you can launch it by clicking on the Start Menu (the raspberry icon in the upper left-hand corner)…

…and in the menu that appears, select the Programming menu. A sub-menu will appear, and one of the items will be Visual Studio Code. Click that and…

Screenshot: Visual Studio Code on Raspberry Pi
Tap to view at full size.

You’ll be in the Visual Studio Code that you know and love from Windows, macOS, and Linux! And yes, all the plugins that you’ve come to depend on will be available.

Go forth and code!

Categories
Current Events Hardware Programming

Raspberry Pi 400: A lot of computer for as little as $70!

Photo: Raspberry Pi 400, front/top view showing keyboard as seen by the user.
Tap to view at full size.

The Raspberry Pi 400 — a Raspberry Pi 4 board with 4GB RAM built into a compact keyboard — was announced just today, and the base unit (just the computer built into the compact keyboard) retails for $70!

The computer

Photo: Raspberry Pi 400, back/top view showing keyboard and ports.
Tap to view at full size.

The Raspberry Pi 400 is a slightly updated model from last year’s Raspberry Pi 4, and has these specs:

Feature Notes
Processor 1.8 GHz ARM Cortex-A72 CPU
(A little faster than the Raspberry Pi 4’s 1.5 GHz CPU)
RAM 4 GB
Networking
  • 802.11ac wifi
  • Gigabit Ethernet
  • Bluetooth 5.0
Video 2 micro HDMI ports that can each drive 4K/60 Hz video
USB
  • 2 USB 3.0 ports
  • 1 USB 2.0 port (preferably for the mouse)
Power Provided via adapter and USB-C
Additional ports 40-pin GPIO interface

The complete kit

Photo: Raspberry Pi 400 kit, showing the computer, micro HDMI to HDMI cable, The Official Raspberry Pi Beginner’s Guide, mouse, and power supply, as well as the box they came in.
Tap to view at full size.

For an extra $30, you can get the kit, which is the complete “ready to go out of the box” package. It starts with the Raspberry Pi 400 computer-in-a-keyboard unit described above, and it adds:

The kind of computer that hasn’t been seen since the 1980s

Let’s quickly take stock of what you get with just the Raspberry Pi 400, never mind the kit:

  • A fully-equipped computer with a decent processor, decent RAM, wifi/wired/Bluetooth networking with 2 fast USB ports to spare once you’ve plugged a mouse into the slower one.
  • A computer that you can do hardware experiments with, thanks to its GPIO pins, and an abundance of hobbyist-focused expansion kits.
  • A computer that you can plug into your TV.
  • A computer that costs $100.

There hasn’t been a computer like this since the machines pictured below came out…

Photo: ZX Spectrum computer

Photo: Commodore VIC-20.

…and those machines couldn’t hold a candle to the proper desktops of that era.

On the other hand, you’ll find that the Raspberry Pi 400 can easily keep up with the sort of computer that gets issued for standard office work. You could easily use it to do schoolwork or office work, and it’s actually a decent Linux software development machine and retro-style gaming console, too! And with its expansion capabilities, it’s an excellent machine for IoT and sensor projects.

This is the sort of machine that children of the 1980s and early 1990s learned on, many of whom are today’s techies…

…and this machine will probably be the machine that a lot of children of the 2020s will cut their programming teeth on, and who’ll be the techies of the 2040s and 2050s.

Given a choice between a Chromebook and a Raspberry Pi 400, I’d take the Pi, because I can do a lot more with it. In fact, I might be able to do a lot of my new job with it (which is something I might try soon, just to see what happens).

Graphic: “Cobra Pi” logo

By the bye, keep an eye on this blog for a new feature: Cobra Pi, which covers programming on the Raspberry Pi, and whose motto is: “Code hard! Fail fast! No latency!”

It’ll cover all sorts of cool programming tips, tricks, and techniques on the Raspberry Pi, including JavaScript, Python, and even C and ARM assembly language!