Solution to Day 1 Advent of Code challenge in Swift

How to complete all 25 day challenges in Swift. The quick and elaborate ways.

Pavan Kataria
3 min readDec 4, 2020
Day 1. Advent of Code in Swift

The Advent of Code is an Advent calendar consisting of 50 small programming puzzles released every year on December divided over 25 days through to Christmas.

You can complete the challenges in any language aimed to test a variety of skill sets and skill levels. Developers all around the world participate using it as an opportunity for interview prep; to practise new problems; explore new programming languages; or to simply challenge each other.

We promise ourselves as developers that this will be the year we follow through to complete every daily challenge only to fail just like breaking yet another New Year’s gym resolution! But not this time!

Stick around and we will go through solving these challenges exploring quick and dirty to more elaborate, optimum, and sometimes even insane solutions to these challenges.

Challenge Format

Take a look at the challenge presented for day 1. The format is as follows, you are presented with part 1 of the day’s coding challenge. You are given a text document unique to you containing an array of input which you need to consume and process an output. You enter this output in the input field on the webpage and are told whether you have answered correctly. Answering correctly reveals the second challenge where the process repeat one last time for the day.

Parsing the input

I’m using playgrounds to solve these challenges. First you’ll want to drag your input file into the resources folder like so

input.txt file shown placed inside the Resources folder in a playground page

Then we’ll need a hand helper method to help grab the contents of the input file ready for consumption. We can wrap this up in amethod and store it in the parent page, named AOC2020 in this example, inside its respective Source file so it’s accessible to all the other playground sub-pages.

Then in our Day 1 playground sub-page we can use the method and process the array of strings to morph the document into the input we desire for our algorithmic solutions.

In todays challenges here’s how to process the input:

And that’s it. Now we’re ready for write the actual solution and passing in input to the parameter of the algorithmic solution.

Day 1, part 1: Finding 2 numbers that sum up to 2020

2 for loops used to traverse input array for 2 numbers summing up to 2020

This solution simply traverses through the input two integers at the same time and completes when two numbers are found that sum up to 2020 and returns the multiplied value for answer submission.

Day 1, Part 2: Finding 3 numbers that sum up to 2020

The problem now extends to finding 3 numbers and in the quickest fashion involves adding another loop. Here’s a simple solution for this second challenge.

That’s it for the challenges of Day 1. A straight forward day to get the juices flowing.

To stay tuned for Day 2 challenges follow me here on Medium.

If you enjoyed reading this post please consider liking and sharing so others can find it as well.

You can check me out on Github and Twitter too.

--

--

Pavan Kataria

Engineer Pavan, First of His name, the Unburnt, Lord of the iOS Realm, Bug Breaker, Space Indenter, and New writer — otherwise known as PK