Days 1 and 2
This commit is contained in:
parent
ac605225d5
commit
a47c950497
30
2021/day1.py
Normal file
30
2021/day1.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
lines = [line.rstrip() for line in sys.stdin]
|
||||||
|
|
||||||
|
# Part 1
|
||||||
|
nums = map(int, lines)
|
||||||
|
prev = 99999
|
||||||
|
incs = 0
|
||||||
|
for n in nums:
|
||||||
|
if n > prev:
|
||||||
|
incs += 1
|
||||||
|
prev = n
|
||||||
|
print(incs)
|
||||||
|
|
||||||
|
# Part 2
|
||||||
|
nums = list(map(int, lines))
|
||||||
|
prev = 99999
|
||||||
|
incs = 0
|
||||||
|
for i in range(2, len(nums)):
|
||||||
|
sus = nums[i] + nums[i - 1] + nums[i - 2]
|
||||||
|
if sus > prev:
|
||||||
|
incs += 1
|
||||||
|
prev = sus
|
||||||
|
print(incs)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
37
2021/day2.py
Normal file
37
2021/day2.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
lines = [line.rstrip() for line in sys.stdin]
|
||||||
|
|
||||||
|
# Part 1
|
||||||
|
hor = 0
|
||||||
|
depth = 0
|
||||||
|
for line in lines:
|
||||||
|
d, n = line.split()
|
||||||
|
if d[0] == "f":
|
||||||
|
hor += int(n)
|
||||||
|
elif d[0] == "d":
|
||||||
|
depth += int(n)
|
||||||
|
elif d[0] == "u":
|
||||||
|
depth -= int(n)
|
||||||
|
print(hor * depth)
|
||||||
|
|
||||||
|
# Part 2
|
||||||
|
aim = 0
|
||||||
|
hor = 0
|
||||||
|
depth = 0
|
||||||
|
for line in lines:
|
||||||
|
d, n = line.split()
|
||||||
|
if d[0] == "f":
|
||||||
|
hor += int(n)
|
||||||
|
depth += int(n) * aim
|
||||||
|
elif d[0] == "d":
|
||||||
|
aim += int(n)
|
||||||
|
elif d[0] == "u":
|
||||||
|
aim -= int(n)
|
||||||
|
print(hor * depth)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -1,7 +1,9 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
lines = [line.rstrip() for line in sys.stdin]
|
lines = [line.rstrip() for line in sys.stdin]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue