-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
71 lines (62 loc) · 1.67 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""
Day 13 of Advent of Code, December 2023
https://adventofcode.com/
@author: gualandi
"""
import argparse
from numpy import matrix, not_equal
def CheckRow(A):
m, _ = A.shape
for i in range(m-1):
d = min(i, m-2-i)
As = [x for x in range(i-d, i+1)]
Bs = [x+1 for x in range(i, i+d+1)]
diff = 0
for a, b in zip(reversed(As), Bs):
diff += not_equal(A[a, :], A[b, :]).sum()
if diff == DELTA:
return i+1
return 0
def CheckCol(A):
_, n = A.shape
for i in range(n-1):
d = min(i, n-2-i)
As = [x for x in range(i-d, i+1)]
Bs = [x+1 for x in range(i, i+d+1)]
diff = 0
for a, b in zip(reversed(As), Bs):
diff += not_equal(A[:, a], A[:, b]).sum()
if diff == DELTA:
return i+1
return 0
def Process(A):
a = CheckCol(A)
if a > 0:
return a
return 100 * CheckRow(A)
def Parser(filename):
fh = open(filename, mode='r', encoding='utf-8')
Ls = []
Rs = []
for row in fh:
row = row.replace('\n','').strip()
if row == '':
Rs.append(Process(matrix(Ls)))
Ls = []
else:
row = list(map(lambda z: 0 if z=='.' else 1, row))
Ls.append(row)
if Ls != []:
Rs.append(Process(matrix(Ls)))
return sum(Rs)
# TEST FROM COMMAND LINE
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', default='./small.txt', type=str, required=False)
args = parser.parse_args()
# Part 1: DELTA = 0
# Part 2: DELTA = 1
DELTA = 0
print('Part 1:', Parser(args.filename))
DELTA = 1
print('Part 2:', Parser(args.filename))