day 10 attempt with numpy
This commit is contained in:
parent
0abdabad64
commit
e21df10751
32
day10/sol2.py
Normal file
32
day10/sol2.py
Normal file
@ -0,0 +1,32 @@
|
||||
import numpy, numpy.linalg
|
||||
def solve(input):
|
||||
less = 0
|
||||
greater = 0
|
||||
for line in open(input):
|
||||
parts = line.split()[1:]
|
||||
idxs = [delist(p) for p in parts[:-1]]
|
||||
target = delist(parts[-1])
|
||||
if len(idxs) != len(target) and 0:
|
||||
if len(idxs) > len(target):
|
||||
greater += 1
|
||||
if len(idxs) < len(target):
|
||||
less += 1
|
||||
#print(line.strip())
|
||||
else:
|
||||
#assert len(idxs) == len(target)
|
||||
N = len(target)
|
||||
matrix = [[int(i in row) for i in range(N)] for row in idxs]
|
||||
|
||||
#print(matrix, target)
|
||||
arr = numpy.array(matrix)
|
||||
print(arr)
|
||||
try:
|
||||
print(target, numpy.linalg.lstsq(arr.T, target))
|
||||
except numpy.linalg.LinAlgError as ex:
|
||||
print(target, ex)
|
||||
print(less, greater)
|
||||
|
||||
def delist(s):
|
||||
return [int(x) for x in s.strip("{}()").split(',')]
|
||||
|
||||
solve("input")
|
||||
Loading…
x
Reference in New Issue
Block a user