# Solving Advent of Code Puzzles with GitHub Copilot: Day 1

- 8 mins## WARNING: Spoilers for Advent of Code below

This year, I decided to try my hand at solving Advent of Code puzzles, mostly out of curiosity since I know this advent calendar has been around for several years, and I wanted to see what it was all about.

If you’re not familiar with Advent of Code, essentially it’s an advent calendar that has programming puzzles each day that you’re meant to solve, with a web page to which you submit answers. New puzzles (two) come out each day at midnight (EST / UTC-5), and there’s also a leaderboard where you can score points if you submit your answers sooner than other folks.

I wrote up my solution for the first day of puzzles (both) in Python (3.8) below:

```
from typing import List
def count_num_increased_measurements(data: List[int]) -> int:
num_increased = 0
# loop over every element, then check if the previous
# element is smaller than the current element.
# if true, increment the count of increased measurements
for i in range(0, len(data)):
if i == 0:
continue
if data[i] > data[i-1]:
num_increased += 1
return num_increased
def count_num_increased_measurement_sums(data: List[int]) -> int:
num_increased = 0
for i in range(0, len(data)):
if i + 3 > len(data):
break
first_three = data[i:i+3]
next_three = data[i+1:i+4]
if sum(first_three) < sum(next_three):
num_increased += 1
return num_increased
if __name__ == '__main__':
data = []
with open('adventofcode-day1-input.txt', 'r') as f:
for line in f:
data.append(int(line))
print(f"# of increased measurements: {count_num_increased_measurements(data)}")
print(f"# of increased measurement sums: {count_num_increased_measurement_sums(data)}")
```

However, when I was thinking about what puzzles might look like in the future, a few thoughts came to mind:

- How can I come up with solutions to puzzles more quickly?
- What can I learn from solving these puzzles? a. Maybe I can learn more about a programming language I’m less familiar with? b. Or maybe there are tools that can help me solve these puzzles?
- How can I stay motivated working with this advent calendar (and have fun)?

## In comes GitHub Copilot

I remembered that I had an invite to the preview of GitHub Copilot, an AI-based pair programming tool that you can install in Visual Studio Code.

I then installed the preview (after updating VSCode), and opened up a new file called `copilot_solution.py`

.

When you use Copilot, you can begin by typing code comments or method names that explain what problem you’re trying to solve / what code you want to write, and then Copilot can essentially translate those comments into code.

## Puzzle One

So I started by copying a piece of the advent day 1 puzzle description (the first puzzle):

```
# count the number of times a depth measurement increases from the previous measurement.
```

And then began typing:

```
def get
```

and Copilot instantly suggested (correct) code for counting the depth measurement changes. I’ve embedded a video below:

And here’s the code Copilot came up with in text format:

```
# count the number of times a depth measurement increases from the previous measurement.
def get_depth_changes(depth_list):
"""
Count the number of times a depth measurement increases from the previous measurement.
"""
depth_changes = 0
for i in range(1, len(depth_list)):
if depth_list[i] > depth_list[i-1]:
depth_changes += 1
return depth_changes
def get_depth_input():
"""
Get the depth measurements from the input file.
"""
depth_list = []
with open('adventofcode-day1-input.txt') as f:
for line in f:
depth_list.append(int(line))
return depth_list
if __name__ == '__main__':
depth_list = get_depth_input()
print(get_depth_changes(depth_list))
```

One particular thing I noticed was that I needed to fully type out `def get_depth_input`

in order to give Copilot the correct context, but it managed to produce working code just from that function signature, and I only wrote and ran this program once.

## Puzzle Two

I tried this out with the second puzzle for day 1 as well, which is a generalization of the first puzzle that involves summing up sliding windows of measurements and comparing them.

You can find the YouTube video for the second puzzle solved by Copilot below:

A couple of points on what I noticed about the code completion:

- Copilot was able to come up with a generalized method for counting sums of increasing measurements with a
`window_size`

parameter, without me specifying the size of the sliding window in the code comments - When creating the entrypoint to the program, Copilot somehow assumed the window length for the method call I wanted would be
`3`

, without having the context (as far as I can tell) from the user (me) - GitHub Copilot has a list of possible solutions to code completions, so I wonder if it’s just luck that the suggested solution was the correct one

Here’s the code written by Copilot:

```
# Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum
def count_number_of_increases_in_sliding_window(array, window_size):
"""
Counts the number of times the sum of measurements in this sliding window increases from the previous sum
:param array: array of measurements
:param window_size: size of the sliding window
:return: number of times the sum of measurements in this sliding window increases from the previous sum
"""
if len(array) < window_size:
return 0
else:
current_sum = sum(array[:window_size])
count = 0
for i in range(window_size, len(array)):
if sum(array[i - window_size + 1:i + 1]) > current_sum:
count += 1
current_sum = sum(array[i - window_size + 1:i + 1])
return count
if __name__ == '__main__':
with open('adventofcode-day1-input.txt', 'r') as f:
array = [int(x) for x in f.read().splitlines()]
print(count_number_of_increases_in_sliding_window(array, 3))
```

I thought this was a fun experiment, so I’m curious to see how far Copilot can go and what it’s limitations are as more puzzles come out each day. I’ll post an update on day 2 or day 3 of the Advent of Code and also try to use more of the puzzle description rather than sentences I think are the most useful for the Copilot program.

Thanks for reading!

**UPDATE:** You can find Day 2 up now.