Skip to content

Instantly share code, notes, and snippets.

@spielkind
Last active December 6, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spielkind/535fad935f1ad7caabaef25eb922cf5c to your computer and use it in GitHub Desktop.
Save spielkind/535fad935f1ad7caabaef25eb922cf5c to your computer and use it in GitHub Desktop.
#!/bin/python
class Submarine():
def __init__(self):
self.position={'horizontal': 0, 'depth': 0}
self.aim=0
def forward(self,amount):
self.position['horizontal']+=amount
self.position['depth']+=amount*self.aim
def up(self,amount):
self.aim-=amount
def down(self,amount):
self.aim+=amount
def singleposition(self):
return(self.position['depth']*self.position['horizontal'])
def runcommands(self, commands):
for c in commands:
getattr(self, c['command'])(c['amount'])
commands = []
with open('day2.txt') as f:
for line in f:
command = {}
(c, a) = line.split()
command['command'] = c
command['amount'] = int(a)
commands.append(command)
sub = Submarine()
sub.runcommands(commands)
print('Part Two: ',sub.singleposition())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment