add 2025 day 4 part 2
This commit is contained in:
183
2025/day-04/part-two.py
Normal file
183
2025/day-04/part-two.py
Normal file
@@ -0,0 +1,183 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
# Logger setup
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.INFO)
|
||||
stream_handler = logging.StreamHandler()
|
||||
logger.addHandler(stream_handler)
|
||||
|
||||
def main():
|
||||
with open("input.txt", "r") as input:
|
||||
output = 0
|
||||
matrix = getMatrixFromString(input)
|
||||
(output_matrix, count) = getRollsFewerThan(matrix, 4)
|
||||
output += count
|
||||
print("Count = " + str(count))
|
||||
print(isEverythingFalse(output_matrix))
|
||||
while not isEverythingFalse(output_matrix):
|
||||
matrix = removeFoundRolls(matrix, output_matrix)
|
||||
(output_matrix, count) = getRollsFewerThan(matrix, 4)
|
||||
print("Count = " + str(count))
|
||||
output += count
|
||||
logger.info("Output: " + str(output))
|
||||
|
||||
def getMatrixFromString(data):
|
||||
output = []
|
||||
for line in data:
|
||||
line = line.strip()
|
||||
line_output = []
|
||||
for i in range(len(line)):
|
||||
char = line[i]
|
||||
if char == "@":
|
||||
line_output.append(True)
|
||||
elif char == ".":
|
||||
line_output.append(False)
|
||||
else:
|
||||
logger.error("Unexpected char: " + char)
|
||||
output.append(line_output)
|
||||
return output
|
||||
|
||||
def getRollsFewerThan(matrix, n):
|
||||
output = []
|
||||
count = 0
|
||||
for i in range(len(matrix)):
|
||||
line_output = []
|
||||
for j in range(len(matrix[i])):
|
||||
if not matrix[i][j]:
|
||||
line_output.append(False)
|
||||
continue
|
||||
adj = 0
|
||||
# Si on est a la première ligne
|
||||
if(i == 0):
|
||||
# Si on est a la première colonne
|
||||
if(j == 0):
|
||||
if(matrix[i][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j + 1]):
|
||||
adj += 1
|
||||
# Si on est a la dernière colonne
|
||||
elif(j == (len(matrix[i]) - 1)):
|
||||
if(matrix[i][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j]):
|
||||
adj += 1
|
||||
else:
|
||||
if(matrix[i][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j + 1]):
|
||||
adj += 1
|
||||
|
||||
# Si on est a la dernière ligne
|
||||
elif(i == (len(matrix) - 1)):
|
||||
# Si on est a la première colonne
|
||||
if(j == 0):
|
||||
if(matrix[i - 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j + 1]):
|
||||
adj += 1
|
||||
# Si on est a la dernière colonne
|
||||
elif(j == (len(matrix[i]) - 1)):
|
||||
if(matrix[i - 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i][j - 1]):
|
||||
adj += 1
|
||||
else:
|
||||
if(matrix[i - 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j + 1]):
|
||||
adj += 1
|
||||
else:
|
||||
# Si on est a la première colonne
|
||||
if(j == 0):
|
||||
if(matrix[i - 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j + 1]):
|
||||
adj += 1
|
||||
# Si on est a la dernière colonne
|
||||
elif(j == (len(matrix[i]) - 1)):
|
||||
if(matrix[i - 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j]):
|
||||
adj += 1
|
||||
else:
|
||||
if(matrix[i - 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i - 1][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i][j + 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j - 1]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j]):
|
||||
adj += 1
|
||||
if(matrix[i + 1][j + 1]):
|
||||
adj += 1
|
||||
|
||||
print("adj[" + str(i) + "][" + str(j) + "]=" + str(adj))
|
||||
if(adj < n):
|
||||
print("Found one at [" + str(i) + "][" + str(j) + "]")
|
||||
line_output.append(True)
|
||||
count += 1
|
||||
else:
|
||||
line_output.append(False)
|
||||
output.append(line_output)
|
||||
return (output, count)
|
||||
|
||||
def isEverythingFalse(matrix):
|
||||
for i in range(len(matrix)):
|
||||
for j in range(len(matrix[i])):
|
||||
if(matrix[i][j]):
|
||||
return False
|
||||
return True
|
||||
|
||||
def removeFoundRolls(matrix, rolls_matrix):
|
||||
output = []
|
||||
for i in range(len(matrix)):
|
||||
line_output = []
|
||||
for j in range(len(matrix[i])):
|
||||
if(rolls_matrix[i][j]):
|
||||
line_output.append(False)
|
||||
else:
|
||||
line_output.append(matrix[i][j])
|
||||
output.append(line_output)
|
||||
return output
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user