mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 08:21:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
44
adventofcode/2021/day2/day2.py
Normal file
44
adventofcode/2021/day2/day2.py
Normal file
@@ -0,0 +1,44 @@
|
||||
def part1(inp):
|
||||
horizontal_pos = 0
|
||||
depth = 0
|
||||
for line in inp:
|
||||
command, amount = line.split(" ")
|
||||
if command == "forward":
|
||||
horizontal_pos += int(amount)
|
||||
elif command == "up":
|
||||
depth -= int(amount)
|
||||
elif command == "down":
|
||||
depth += int(amount)
|
||||
#print(f"horizontal {horizontal_pos}, depth {depth}")
|
||||
print("Part 1", horizontal_pos * depth)
|
||||
|
||||
|
||||
def part2(inp):
|
||||
horizontal_pos = 0
|
||||
depth = 0
|
||||
aim = 0
|
||||
for line in inp:
|
||||
command, amount = line.split(" ")
|
||||
if command == "forward":
|
||||
horizontal_pos += int(amount)
|
||||
# It increases your depth by your aim multiplied by X.
|
||||
depth += aim * int(amount)
|
||||
elif command == "up":
|
||||
aim -= int(amount)
|
||||
elif command == "down":
|
||||
aim += int(amount)
|
||||
#print(f"horizontal {horizontal_pos}, depth {depth}")
|
||||
print("Part 2", horizontal_pos * depth)
|
||||
|
||||
|
||||
def main(input_file):
|
||||
with open(input_file) as f:
|
||||
entries = f.readlines()
|
||||
part1(entries)
|
||||
part2(entries)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
infile = sys.argv[1] if len(sys.argv) > 1 else "example.txt"
|
||||
main(infile)
|
Reference in New Issue
Block a user