some progress on advent of code
This commit is contained in:
25
day2_1.py
Executable file
25
day2_1.py
Executable file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys, utils
|
||||||
|
|
||||||
|
def where_are_we_going(l: list):
|
||||||
|
h, v = 0, 0
|
||||||
|
for d in l:
|
||||||
|
d = d.split()
|
||||||
|
if d[0] == 'forward':
|
||||||
|
h += int(d[1])
|
||||||
|
elif d[0] == 'down':
|
||||||
|
v += int(d[1])
|
||||||
|
elif d[0] == 'up':
|
||||||
|
v -= int(d[1])
|
||||||
|
return h * v
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("please supply input file")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
f = utils.File(sys.argv[1])
|
||||||
|
|
||||||
|
print(where_are_we_going(f.get_strings()))
|
||||||
|
|
||||||
|
|
28
day2_2.py
Executable file
28
day2_2.py
Executable file
@@ -0,0 +1,28 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys, utils
|
||||||
|
|
||||||
|
def where_are_we_going2(l: list):
|
||||||
|
h, v = 0, 0
|
||||||
|
aim = 0
|
||||||
|
for d in l:
|
||||||
|
d = d.split()
|
||||||
|
n = int(d[1])
|
||||||
|
if d[0] == 'forward':
|
||||||
|
h += n
|
||||||
|
v += aim * n
|
||||||
|
elif d[0] == 'down':
|
||||||
|
aim += n
|
||||||
|
elif d[0] == 'up':
|
||||||
|
aim -= n
|
||||||
|
return h * v
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("please supply input file")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
f = utils.File(sys.argv[1])
|
||||||
|
|
||||||
|
print(where_are_we_going2(f.get_strings()))
|
||||||
|
|
||||||
|
|
1000
day2_input.txt
Normal file
1000
day2_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
37
day3_1.py
Executable file
37
day3_1.py
Executable file
@@ -0,0 +1,37 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys, utils
|
||||||
|
|
||||||
|
def power_consumption(l: list):
|
||||||
|
most, least = calculate_common(l)
|
||||||
|
return int(most, 2) * int(least, 2)
|
||||||
|
|
||||||
|
def calculate_common(l: list):
|
||||||
|
counter = [0 for _ in range(len(l[0]))]
|
||||||
|
for line in l:
|
||||||
|
line = line.rstrip('\n')
|
||||||
|
for i in range(len(line)):
|
||||||
|
if line[i] == '1':
|
||||||
|
counter[i] += 1
|
||||||
|
|
||||||
|
half_len = len(l) / 2
|
||||||
|
most_common, least_common = '', ''
|
||||||
|
|
||||||
|
for val in counter:
|
||||||
|
if val >= half_len:
|
||||||
|
most_common += '1'
|
||||||
|
least_common += '0'
|
||||||
|
else:
|
||||||
|
most_common += '0'
|
||||||
|
least_common += '1'
|
||||||
|
return most_common, least_common
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("please supply input file")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
f = utils.File(sys.argv[1])
|
||||||
|
|
||||||
|
print(power_consumption(f.get_strings()))
|
||||||
|
|
||||||
|
|
1000
day3_input.txt
Normal file
1000
day3_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
5
utils.py
5
utils.py
@@ -7,7 +7,7 @@ class File(object):
|
|||||||
def _get_lines(self):
|
def _get_lines(self):
|
||||||
with open(self.filename, 'r') as f:
|
with open(self.filename, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
self.lines.append(line)
|
self.lines.append(line.rstrip('\n'))
|
||||||
|
|
||||||
def get_strings(self) -> list:
|
def get_strings(self) -> list:
|
||||||
if not self.lines:
|
if not self.lines:
|
||||||
@@ -19,6 +19,9 @@ class File(object):
|
|||||||
return []
|
return []
|
||||||
return [int(n) for n in self.lines]
|
return [int(n) for n in self.lines]
|
||||||
|
|
||||||
|
def get_line(self, n) -> str:
|
||||||
|
return self.lines[n]
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.lines = []
|
self.lines = []
|
||||||
self.filename = ''
|
self.filename = ''
|
||||||
|
Reference in New Issue
Block a user