Advent Of Code: Day 1

This year I'll be doing the Advent of Code in whatever language is most convenient on the day I do the challenge.

For Day 1, the challenge is to sum up the digits of an input which are followed by the same digit (at some given offset). Here is my Python solution:

def part_1(input, offset=1):
    return sum([int(input[i]) for i in range(len(input)) if input[i] == input[(i + offset) % len(input)]])

def part_2(input):
    return part_1(input, len(input) / 2)

The major part is a list comprehension over the indexes of the input string. Python is a great language for small tasks like this.

Advent of Code runs every day up to Christmas, you should join in!.

Show Comments

Get the latest posts delivered right to your inbox.