[py] Port 2017_04
This commit is contained in:
parent
d3d8a493f1
commit
0bf180482b
3 changed files with 37 additions and 37 deletions
|
|
@ -1,36 +0,0 @@
|
||||||
import collections
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# PART 1
|
|
||||||
|
|
||||||
def load_phrases(filename):
|
|
||||||
phrases = []
|
|
||||||
with open(filename, "r") as f:
|
|
||||||
for line in f:
|
|
||||||
phrase = line[:-1].split(" ")
|
|
||||||
phrases.append(phrase)
|
|
||||||
return phrases
|
|
||||||
|
|
||||||
def is_valid(phrase):
|
|
||||||
return len(phrase) == len(set(phrase))
|
|
||||||
|
|
||||||
def count(what, when):
|
|
||||||
return len(list(filter(when, what)))
|
|
||||||
|
|
||||||
# PART 2
|
|
||||||
|
|
||||||
def is_valid_2(phrase):
|
|
||||||
phrase = ["".join(sorted(word)) for word in phrase]
|
|
||||||
return len(phrase) == len(set(phrase))
|
|
||||||
|
|
||||||
def main(filename):
|
|
||||||
print(f"Solutions for {filename}")
|
|
||||||
phrases = load_phrases(filename)
|
|
||||||
correct = count(phrases, is_valid)
|
|
||||||
print(f"Part 1: {correct}")
|
|
||||||
correct_2 = count(phrases, is_valid_2)
|
|
||||||
print(f"Part 2: {correct_2}")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
for filename in sys.argv[1:]:
|
|
||||||
main(filename)
|
|
||||||
|
|
@ -2,7 +2,7 @@ import sys
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .y2017 import d01, d02, d03
|
from .y2017 import d01, d02, d03, d04
|
||||||
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
||||||
from .y2020 import d10
|
from .y2020 import d10
|
||||||
from .y2021 import d14
|
from .y2021 import d14
|
||||||
|
|
@ -12,6 +12,7 @@ DAYS = {
|
||||||
"2017_01": y2017.d01.solve,
|
"2017_01": y2017.d01.solve,
|
||||||
"2017_02": y2017.d02.solve,
|
"2017_02": y2017.d02.solve,
|
||||||
"2017_03": y2017.d03.solve,
|
"2017_03": y2017.d03.solve,
|
||||||
|
"2017_04": y2017.d04.solve,
|
||||||
"2018_01": y2018.d01.solve,
|
"2018_01": y2018.d01.solve,
|
||||||
"2018_02": y2018.d02.solve,
|
"2018_02": y2018.d02.solve,
|
||||||
"2018_03": y2018.d03.solve,
|
"2018_03": y2018.d03.solve,
|
||||||
|
|
|
||||||
35
py/aoc/y2017/d04.py
Normal file
35
py/aoc/y2017/d04.py
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
import collections
|
||||||
|
|
||||||
|
# PART 1
|
||||||
|
|
||||||
|
|
||||||
|
def load_phrases(inputstr):
|
||||||
|
phrases = []
|
||||||
|
for line in inputstr.splitlines():
|
||||||
|
phrase = line.split(" ")
|
||||||
|
phrases.append(phrase)
|
||||||
|
return phrases
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid(phrase):
|
||||||
|
return len(phrase) == len(set(phrase))
|
||||||
|
|
||||||
|
|
||||||
|
def count(what, when):
|
||||||
|
return len(list(filter(when, what)))
|
||||||
|
|
||||||
|
|
||||||
|
# PART 2
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_2(phrase):
|
||||||
|
phrase = ["".join(sorted(word)) for word in phrase]
|
||||||
|
return len(phrase) == len(set(phrase))
|
||||||
|
|
||||||
|
|
||||||
|
def solve(inputstr):
|
||||||
|
phrases = load_phrases(inputstr)
|
||||||
|
correct = count(phrases, is_valid)
|
||||||
|
print(f"Part 1: {correct}")
|
||||||
|
correct_2 = count(phrases, is_valid_2)
|
||||||
|
print(f"Part 2: {correct_2}")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue