예제 #1
0
def usedSpace(data):
    used = 0
    for i in xrange(128):
        key = data + '-{0}'.format(i)
        binaryHash = [bin(int(char, 16))[2:].zfill(4) for char in knotHash(key)]
        used += ''.join(binaryHash).count('1')
    return used
예제 #2
0
def part1(filename):
    with open(filename) as f:
        line = f.readline()

    used = 0
    for i in range(0, 128):
        hashHex = knotHash(line + '-' + str(i))
        hashBin = bin(int(hashHex, 16))[2:].zfill(128)
        used += hashBin.count('1')

    print(used)
예제 #3
0
def findRegions(data):
    binaryLists = []
    for i in xrange(128):
        key = data + '-{0}'.format(i)
        binaryHash = ''.join([bin(int(char, 16))[2:].zfill(4) for char in knotHash(key)])
        binaryLists.append([int(numstring) for numstring in binaryHash])
    disk = np.array(binaryLists)
    regions = 0
    for x in range(128):
        for y in range(128):
            if disk[y, x] == 1:
                removeRegion(disk, [y, x])
                regions += 1
    return regions
예제 #4
0
def part2(filename):
    with open(filename) as f:
        line = f.readline()

    grid = []
    for i in range(0, 128):
        hashHex = knotHash(line + '-' + str(i))
        hashBin = bin(int(hashHex, 16))[2:].zfill(128)
        grid.append(list(hashBin))

    def getNeighbors(grid, i, j):
        neighbors = []

        if j < len(grid[i]):
            k = j + 1
            while k < len(grid[i]) and grid[i][k] == '1':
                neighbors.append((i, k))
                k += 1
        if j > 0:
            k = j - 1
            while k >= 0 and grid[i][k] == '1':
                neighbors.append((i, k))
                k -= 1
        if i < len(grid):
            k = i + 1
            while k < len(grid) and grid[k][j] == '1':
                neighbors.append((k, j))
                k += 1
        if i > 0:
            k = i - 1
            while k >= 0 and grid[k][j] == '1':
                neighbors.append((k, j))
                k -= 1

        return neighbors

    def removeGroup(grid, i, j):
        grid[i][j] = 'x'
        for i, j in getNeighbors(grid, i, j):
            removeGroup(grid, i, j)

    groups = 0
    for i in range(len(grid)):
        for j in range(len(grid[i])):
            if grid[i][j] == '1':
                removeGroup(grid, i, j)
                groups += 1

    print(groups)
예제 #5
0
def createGrid(input_key):
    used_squares = 0
    grid = []
    for r in range(128):
        row_list = []
        row_key = input_key + '-' + str(r)
        row_hash = day10.knotHash(row_key)
        binary_hash = "".join(["{0:04b}".format(int(c, 16)) for c in row_hash])
        used_squares += binary_hash.count('1')
        c = 0
        for b in binary_hash:
            if b == '1':
                grid.append((r, c))
            c += 1
    return grid
예제 #6
0
            exploreRegion(grid, row, column - 1, regionNum)
            exploreRegion(grid, row, column + 1, regionNum)
    except IndexError:
        return


if __name__ == "__main__":
    input = helpers.puzzleInput.Input(2017, 14)
    inputText = input.getFirstInputLine()

    # prepare grid
    numUsedSquares = 0
    grid = []
    for row in range(GRID_SIZE):
        rowKey = inputText + '-' + str(row)
        hash = day10.knotHash(rowKey)
        rowDigits = []
        for hexDigit in hash:
            rowDigits += hexDigitToBinDigits(hexDigit)
        numUsedSquares += rowDigits.count(1)
        grid.append(rowDigits)

    print("result1 =", numUsedSquares)

    numRegions = 0
    for row in range(GRID_SIZE):
        for column in range(GRID_SIZE):
            # Are we in a new region?
            if 1 == grid[row][column]:
                numRegions += 1
                # Use negative region number as new region marker in grid replacing the original 1
예제 #7
0
 def __init__(self, key):
     self.grid = []
     for n in range(128):
         line_key = "{k}-{n}".format(k=key, n=n)
         self.grid.append(list(map(int, hexToBin(knotHash(line_key)))))
예제 #8
0
for i in range(128):
    rows.append(inputString + "-" + str(i))
#print(rows)

rBin = []
count = 0
for i in range(128):
    r = []
    for ch in rows[i]:
        if ch == " ":
            continue
        r.append(ord(ch))
    r = r + [17, 31, 73, 47, 23]
    #print(r)

    rKnot = denseHash(knotHash(r))
    #print(rKnot)
    hexList = []
    for n in rKnot:
        hexList.append(hex(n))
    hexString = ""
    for element in hexList:
        if len(element) == 4:
            hexString = hexString + element[2:]
        else:
            hexString = hexString + "0" + element[2]
    #print(hexString)
    binString = ""
    for ch in hexString:
        bStr = str(bin(hexTable[ch])[2:])
        if len(bStr) < 4: