72 lines
1.4 KiB
Python
72 lines
1.4 KiB
Python
from functools import reduce
|
|
|
|
def lines_with_sequence(char):
|
|
def with_char(length):
|
|
sequence = char * length
|
|
|
|
def with_length(doc):
|
|
return reduce(lambda count, line: count + int(sequence in line), doc.split("\n"), 0)
|
|
|
|
return with_length
|
|
return with_char
|
|
|
|
|
|
|
|
part1 = lines_with_sequence('C')
|
|
line_match_counter = part1(4)
|
|
doc = """
|
|
hello
|
|
world
|
|
CCCC CCCC
|
|
CCCC
|
|
"""
|
|
result = line_match_counter(doc)
|
|
print(result)
|
|
|
|
######
|
|
|
|
def count_matching_lines(doc: str, needle: str) -> int:
|
|
"""Returns the number of lines where needle is in doc."""
|
|
return sum(1 for line in doc.splitlines() if needle in line)
|
|
|
|
|
|
def lines_with_sequence(char):
|
|
def with_char(length):
|
|
return lambda doc: count_matching_lines(doc, char * length)
|
|
return with_char
|
|
|
|
part1 = lines_with_sequence('C')
|
|
line_match_counter = part1(4)
|
|
doc = """
|
|
hello
|
|
world
|
|
CCCC CCCC
|
|
CCCC
|
|
"""
|
|
result = line_match_counter(doc)
|
|
print(result)
|
|
|
|
|
|
#####
|
|
|
|
from functools import partial
|
|
|
|
def count_matching_lines(char: str, length: int, doc: str) -> int:
|
|
needle = char * length
|
|
return sum(1 for line in doc.splitlines() if needle in line)
|
|
# lines_with_sequence
|
|
# part1 = lines_with_sequence('C')
|
|
# line_match_counter = part1(4)
|
|
part1 = partial(count_matching_lines, 'C')
|
|
line_match_counter = partial(part1, 4)
|
|
|
|
doc = """
|
|
hello
|
|
world
|
|
CCCC CCCC
|
|
CCCC
|
|
"""
|
|
result = line_match_counter(doc)
|
|
print(result)
|
|
|