[py] Port 2017_02
This commit is contained in:
parent
21f671daed
commit
7ac215b493
4 changed files with 44 additions and 48 deletions
|
|
@ -1,44 +0,0 @@
|
|||
import sys
|
||||
|
||||
def load_rows(filename):
|
||||
rows = []
|
||||
with open(filename, "r") as f:
|
||||
for line in f:
|
||||
digits = line[:-1].split("\t")
|
||||
row = list(map(int, digits))
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
# PART 1
|
||||
|
||||
def checksum(rows):
|
||||
total = 0
|
||||
for row in rows:
|
||||
total += max(row) - min(row)
|
||||
return total
|
||||
|
||||
# PART 2
|
||||
|
||||
def divide_evenly(row):
|
||||
for a in row:
|
||||
for b in row:
|
||||
if a != b and a % b == 0:
|
||||
return a // b
|
||||
|
||||
def checksum_even(rows):
|
||||
total = 0
|
||||
for row in rows:
|
||||
total += divide_evenly(row)
|
||||
return total
|
||||
|
||||
def main(filename):
|
||||
rows = load_rows(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
check = checksum(rows)
|
||||
print(f"Part 1: {check}")
|
||||
check_even = checksum_even(rows)
|
||||
print(f"Part 2: {check_even}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
5 9 2 8
|
||||
9 4 7 3
|
||||
3 8 6 5
|
||||
|
|
@ -2,7 +2,7 @@ import sys
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from .y2017 import d01
|
||||
from .y2017 import d01, d02
|
||||
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
||||
from .y2020 import d10
|
||||
from .y2021 import d14
|
||||
|
|
@ -10,6 +10,7 @@ from .y2022 import d01, d02, d03, d04, d05, d06
|
|||
|
||||
DAYS = {
|
||||
"2017_01": y2017.d01.solve,
|
||||
"2017_02": y2017.d02.solve,
|
||||
"2018_01": y2018.d01.solve,
|
||||
"2018_02": y2018.d02.solve,
|
||||
"2018_03": y2018.d03.solve,
|
||||
|
|
|
|||
42
py/aoc/y2017/d02.py
Normal file
42
py/aoc/y2017/d02.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
def load_rows(inputstr):
|
||||
rows = []
|
||||
for line in inputstr.splitlines():
|
||||
digits = line.split("\t")
|
||||
row = list(map(int, digits))
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
|
||||
# PART 1
|
||||
|
||||
|
||||
def checksum(rows):
|
||||
total = 0
|
||||
for row in rows:
|
||||
total += max(row) - min(row)
|
||||
return total
|
||||
|
||||
|
||||
# PART 2
|
||||
|
||||
|
||||
def divide_evenly(row):
|
||||
for a in row:
|
||||
for b in row:
|
||||
if a != b and a % b == 0:
|
||||
return a // b
|
||||
|
||||
|
||||
def checksum_even(rows):
|
||||
total = 0
|
||||
for row in rows:
|
||||
total += divide_evenly(row)
|
||||
return total
|
||||
|
||||
|
||||
def solve(inputstr):
|
||||
rows = load_rows(inputstr)
|
||||
check = checksum(rows)
|
||||
print(f"Part 1: {check}")
|
||||
check_even = checksum_even(rows)
|
||||
print(f"Part 2: {check_even}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue