Simplify 2023 day 13
This commit is contained in:
parent
7ba400e86d
commit
dc7f9bb764
@ -12,64 +12,13 @@ def read_sample():
|
|||||||
sample = [ [[k for k in j] for j in i.split('\n') if j != ''] for i in sample if i != '' ]
|
sample = [ [[k for k in j] for j in i.split('\n') if j != ''] for i in sample if i != '' ]
|
||||||
return sample
|
return sample
|
||||||
|
|
||||||
def eq(sample, i, j, transp=False):
|
def diff(line1, line2):
|
||||||
if not transp:
|
|
||||||
if sample[i] == sample[j]:
|
|
||||||
return 0
|
|
||||||
return sum([1 for k in range(len(sample[0])) if sample[i][k] != sample[j][k]])
|
|
||||||
sm = 0
|
|
||||||
for k in range(len(sample)):
|
|
||||||
if sample[k][i] != sample[k][j]:
|
|
||||||
sm += 1
|
|
||||||
return sm
|
|
||||||
|
|
||||||
def find_reflections(pattern, smudge=0):
|
|
||||||
reflections = []
|
|
||||||
|
|
||||||
for i in range(0, len(pattern)): # is line i first of refl ?
|
|
||||||
sm = 0
|
|
||||||
for j in range(i+1):
|
|
||||||
if i-j >= 0 and i+j-1 < len(pattern):
|
|
||||||
sm += eq(pattern, i-j, i+j-1)
|
|
||||||
if sm == smudge:
|
|
||||||
reflections.append((0, i))
|
|
||||||
break
|
|
||||||
|
|
||||||
for i in range(0, len(pattern[0])): # is col i first of refl ?
|
|
||||||
sm = 0
|
|
||||||
for j in range(i+1):
|
|
||||||
if i-j >= 0 and i+j-1 < len(pattern[0]):
|
|
||||||
sm += eq(pattern, i-j, i+j-1, transp=True)
|
|
||||||
if sm == smudge:
|
|
||||||
reflections.append((1, i))
|
|
||||||
break
|
|
||||||
|
|
||||||
return reflections
|
|
||||||
|
|
||||||
def score(refls):
|
|
||||||
sum = 0
|
|
||||||
for j in refls:
|
|
||||||
if j[0] == 1:
|
|
||||||
sum += j[1]
|
|
||||||
else:
|
|
||||||
sum += j[1]*100
|
|
||||||
return sum
|
|
||||||
|
|
||||||
|
|
||||||
def part1(sample):
|
|
||||||
"""Partie 1 du défi"""
|
|
||||||
refls = [score(find_reflections(i)) for i in sample]
|
|
||||||
return sum(refls)
|
|
||||||
|
|
||||||
def part2(sample):
|
|
||||||
"""Partie 2 du défi"""
|
|
||||||
def diff(line1, line2):
|
|
||||||
return sum([1 for i in range(len(line1)) if line1[i] != line2[i]])
|
return sum([1 for i in range(len(line1)) if line1[i] != line2[i]])
|
||||||
|
|
||||||
def transpose(pattern):
|
def transpose(pattern):
|
||||||
return [[pattern[j][i] for j in range(len(pattern))] for i in range(len(pattern[0]))]
|
return [[pattern[j][i] for j in range(len(pattern))] for i in range(len(pattern[0]))]
|
||||||
|
|
||||||
def h_reflects(pattern, smudge=1):
|
def h_reflects(pattern, smudge=0):
|
||||||
nb = 0
|
nb = 0
|
||||||
for i in range(len(pattern)):
|
for i in range(len(pattern)):
|
||||||
total_diff = sum((diff(pattern[i-j], pattern[i+j-1]) for j in range(len(pattern[0])) if i-j >= 0 and i+j-1 < len(pattern) and i+j-1 > 0 and i-j < i+j-1))
|
total_diff = sum((diff(pattern[i-j], pattern[i+j-1]) for j in range(len(pattern[0])) if i-j >= 0 and i+j-1 < len(pattern) and i+j-1 > 0 and i-j < i+j-1))
|
||||||
@ -77,8 +26,15 @@ def part2(sample):
|
|||||||
nb += i
|
nb += i
|
||||||
return nb
|
return nb
|
||||||
|
|
||||||
|
|
||||||
|
def part1(sample):
|
||||||
|
"""Partie 1 du défi"""
|
||||||
return sum((h_reflects(pattern) for pattern in sample))*100 + sum((h_reflects(transpose(pattern)) for pattern in sample))
|
return sum((h_reflects(pattern) for pattern in sample))*100 + sum((h_reflects(transpose(pattern)) for pattern in sample))
|
||||||
|
|
||||||
|
def part2(sample):
|
||||||
|
"""Partie 2 du défi"""
|
||||||
|
return sum((h_reflects(pattern, smudge=1) for pattern in sample))*100 + sum((h_reflects(transpose(pattern), smudge=1) for pattern in sample))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Fonction principale"""
|
"""Fonction principale"""
|
||||||
|
Loading…
Reference in New Issue
Block a user