Posts

Advent of Code - 2023 - Day 1

Dec 20, 2023 | 2 minutes to read

Well, dang, it’s that time again - time for another edition of the Advent of Code. If you’re not hip to this thang, it’s an online competition for software developers which happens every year in December. They publish a new puzzle to be solved each day from Dec 1 through 25. Some people get really into this, and compete to see how quickly they can post a solution from the moment they are placed online, each day.

Really not sure how many days I will work on this year, but since they went to the trouble to host another set of puzzles, I can spend a few minutes to try to solve them. They start out easy and get harder with each passing day.

For Day 1 this year, the challenge was to read a list of “inputs”, find the first and last digits on each line, make a new number from them, then sum those numbers. Python rules for this sort of thing, so here’s my solution for Day 1, part 1:

with open('input.txt') as f:

lines = f.readlines()

total = 0

for line in lines:

arrDigits = []

for char in line:

if char.isdigit():

arrDigits.append(char)

if len(arrDigits) > 0:

first = str(arrDigits[0])

last = str(arrDigits[-1])

num = int(first + last)

total = total + num

print ('total is ' + str(total))


You can leave a comment on this post here.