Advent of Code: Day 1

Source

Part 1: Given a string of () characters controlling a simulated elevator, where ( means ‘go up’ and ) means ‘go down’, what floor do you end up on?

data = sys.stdin.read()
print(data.count('(') - data.count(')'))

Basically, count the number of up, subtract the number of down. Nothing much more to say for this one.

Part 2: How far do you make it in the instructions before the current floor is negative?

floor = 0
for index, char in enumerate(sys.stdin.read()):
floor += (1 if char == '(' else -1)
if floor < 0:
print(index)
sys.exit(0)

There might be a more elegant way to do this one, but this is clean enough so far as I’m concerned.

comments powered by Disqus