Skip to content

Commit d52dbbe

Browse files
committed
Day2 solved
1 parent c74ae6d commit d52dbbe

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

Day2/2pt1.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def fetch_input(input_file):
2+
with open(input_file, 'r') as f:
3+
commands = f.readlines()
4+
5+
commands = [command.strip() for command in commands]
6+
return commands
7+
8+
9+
def main():
10+
commands = fetch_input('input_files/2.txt')
11+
destination = {
12+
'horizontal': 0,
13+
'vertical': 0,
14+
}
15+
for command in commands:
16+
direction, increment = command.split(' ')
17+
increment = int(increment)
18+
19+
if (direction == 'forward'):
20+
destination['horizontal'] += increment
21+
elif (direction == 'up'):
22+
destination['vertical'] -= increment
23+
elif (direction == 'down'):
24+
destination['vertical'] += increment
25+
26+
print(destination)
27+
print(destination['horizontal'] * destination['vertical'])
28+
29+
30+
if __name__ == "__main__":
31+
main()

Day2/2pt2.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def fetch_input(input_file):
2+
with open(input_file, 'r') as f:
3+
commands = f.readlines()
4+
5+
commands = [command.strip() for command in commands]
6+
return commands
7+
8+
9+
def main():
10+
commands = fetch_input('input_files/2.txt')
11+
destination = {
12+
'horizontal': 0,
13+
'vertical': 0,
14+
'aim': 0,
15+
}
16+
for command in commands:
17+
direction, increment = command.split(' ')
18+
increment = int(increment)
19+
20+
if (direction == 'forward'):
21+
destination['horizontal'] += increment
22+
destination['vertical'] += (destination['aim'] * increment)
23+
elif (direction == 'up'):
24+
destination['aim'] -= increment
25+
elif (direction == 'down'):
26+
destination['aim'] += increment
27+
28+
print(destination)
29+
print(destination['horizontal'] * destination['vertical'])
30+
31+
32+
if __name__ == "__main__":
33+
main()

Day2/input_files/2_test.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
forward 5
2+
down 5
3+
forward 8
4+
up 3
5+
down 8
6+
forward 2

0 commit comments

Comments
 (0)