Advent of Code: Day 1
So this year I’m participating in the Advent of Code, a worldwide event that is only available in Christmas time and draws hundreds of thousands of developers to solve puzzles together. Anyone can register, no matter if you're a professional programmer, an IT student, or someone just learning to code.
Advent of Code starts on December 1 every year and runs through December 25, ending precisely on Christmas Day.
So for Day 1, we were presented with a problem called Sonar Sweep. You can read the puzzle here. In essence, we’re given a list of numbers and our goal is to find how many times the numbers increase from one number to the next.
I decided to use Python to solve this year’s puzzles. It’s a relatively new language to me and I think this is a great way to learn it along with the ride.
Here’s my very rudimentary code to solve the first puzzle:
file = open('input.txt')
lines = file.read().splitlines()
numbers = list(map(int, lines))
increases = 0
for i in range(len(numbers) - 1):
if numbers[i + 1] > numbers[i]:
increases += 1
print(increases)
This script gave me a result of 1665, which I submitted and thankfully it’s the correct answer.
Now the surprise part! Actually, when you’ve successfully solved a puzzle of a day, the second puzzle will show up. This newcomer is often based on the first puzzle, and in this case, it uses the exact same input. The requirement is a little different: We’re to find how many times the collection of 3 adjacent numbers increases from the beginning to the end. So, similar to the first puzzle, only in time we calculate 3 numbers at once.
Here’s my Python script to solve it:
file = open('input.txt')
lines = file.read().splitlines()
numbers = list(map(int, lines))
increases = 0
for i in range(len(numbers) - 3):
if numbers[i + 1] + numbers[i + 2] + numbers[i + 3] > numbers[i] + numbers[i + 1] + numbers[i + 2]:
increases += 1
print(increases)
This time the result was 1702, which got me the second star of the day.
That’s it for Day 1. The challenge is just starting.