DEV Community

Jonathan Chiou
Jonathan Chiou

Posted on • Updated on

Advent of Code 2015 - My Day 1 Solutions


This is my first post on DEV, and my first year attempting to complete the Advent of Code.


Oops! This is from 2015! I had started a Slack channel at work for AOC and used 2015 Day 1 as a warm up for the group. The title has been edited to reflect this.

Day 1 Part 1

I chose to use Python 3, as it's the language that I'm most comfortable with in solving small problems like this. I also write in Java at work, so using Python here is a nice way to stay sharp and continue to build my skills in that language.
import re

input_string = 'input.txt'

with open(input_string, 'r') as input_file:
    for line in input_file:

        open_parens = re.findall(r'\(', line)
        close_parens = re.findall(r'\)',line)

        open_parens_count = len(open_parens)
        close_parens_count = len(close_parens)

        floor = open_parens_count - close_parens_count
        print("Santa, go to floor " + str(floor))


I wanted to use the requests module to get the input text instead of saving to a .txt, but then I would have had to spend time coding a way to sign in and establish a session in my script.

The use of regex is probably unwarranted as the string patterns being sought out are just single chars, but I'm so used to using regex with Python parsing scripts that it's what I gravitated to out of habit.

So I decided to refactor after seeing a colleague's entry and was tickled by the number of resulting parentheses in my script!
input_string = 'input.txt'

with open(input_string, 'r') as input_file:
    for line in input_file:
        print("Floor #: " + str(len(line.split('('))-len((line.split(')')))))

Day 1 Part 2

I had decided to try and refactor a third time, when it was brought to my attention that there was a part two! Luckily, my third version turned out to be going in a direction that suited my needs for this problem by iterating over each character, so I just modified it to then solve for the second problem.

I don't remember how or why I came to use the operator module, other than I was probably looking for something to do a string comparison in a way that Java has .equals?
import operator

input_string = 'input.txt'

floor = 0
char_count = 0
in_basement = False

with open(input_string, 'r') as input_file:
    for line in input_file:
        for char in line:
            if operator.eq('(',char):
                floor += 1
            elif operator.eq(')',char):
                floor -= 1
                print('invalid input')
            char_count += 1
            if(floor < 0):
                if not in_basement:
                    in_basement = True
                    print("Entered basement at char: " + str(char_count))

    print("Floor: " + str(floor))

I had a lot of fun with these puzzles! I'll be doing a write up on Day 2, which I've completed, and (hopefully) each day following as I complete them.

I certainly welcome any and all feedback, as my primary goal in participating and sharing is to be a better programmer.

Thanks for reading!

Top comments (0)