started advent of code 2021
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
__pycache__
|
20
day1_1.py
Executable file
20
day1_1.py
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys, utils
|
||||||
|
|
||||||
|
def find_increasing(l: list):
|
||||||
|
prev = 0
|
||||||
|
count = 0
|
||||||
|
for n in l:
|
||||||
|
if n > prev:
|
||||||
|
count += 1
|
||||||
|
prev = n
|
||||||
|
return count - 1 # because first measurement doesn't count
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("please supply input file")
|
||||||
|
sys.exit()
|
||||||
|
f = utils.File(sys.argv[1])
|
||||||
|
print(find_increasing(f.get_ints()))
|
||||||
|
|
||||||
|
|
21
day1_2.py
Executable file
21
day1_2.py
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys, utils
|
||||||
|
|
||||||
|
def find_increasing_window(l: list):
|
||||||
|
win = l[:3]
|
||||||
|
count = 0
|
||||||
|
for n in range(3, len(l)):
|
||||||
|
new_win = [l[n-2], l[n-1], l[n]]
|
||||||
|
if sum(new_win) > sum(win):
|
||||||
|
count += 1
|
||||||
|
win = new_win
|
||||||
|
return count
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("please supply input file")
|
||||||
|
sys.exit()
|
||||||
|
f = utils.File(sys.argv[1])
|
||||||
|
print(find_increasing_window(f.get_ints()))
|
||||||
|
|
||||||
|
|
2000
input1.txt
Normal file
2000
input1.txt
Normal file
File diff suppressed because it is too large
Load Diff
25
utils.py
Normal file
25
utils.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
class File(object):
|
||||||
|
def __init__(self, filename):
|
||||||
|
self.filename = filename
|
||||||
|
self.lines = list()
|
||||||
|
self._get_lines()
|
||||||
|
|
||||||
|
def _get_lines(self):
|
||||||
|
with open(self.filename, 'r') as f:
|
||||||
|
for line in f:
|
||||||
|
self.lines.append(line)
|
||||||
|
|
||||||
|
def get_strings(self) -> list:
|
||||||
|
if not self.lines:
|
||||||
|
return []
|
||||||
|
return self.lines[:]
|
||||||
|
|
||||||
|
def get_ints(self) -> list:
|
||||||
|
if not self.lines:
|
||||||
|
return []
|
||||||
|
return [int(n) for n in self.lines]
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.lines = []
|
||||||
|
self.filename = ''
|
||||||
|
|
Reference in New Issue
Block a user