Esempio n. 1
0
def defrag(key):
    hashes = []
    for number in range(128):
        row_key = key + "-%d"%number
        hash = knot_hash(row_key)
        hashes.append(hash)
    return hashes
Esempio n. 2
0
def day14b(data):
    used_spaces = set()
    for i in range(128):
        row = data + '-' + str(i)
        hash = knot_hash(row)
        bin_string = bin(int(hash, 16))[2:]
        while len(bin_string) < 128:
            bin_string = '0' + bin_string
        for ch in range(len(bin_string)):
            if bin_string[ch] == '1':
                used_spaces.add((i, ch))

    regions = 0
    while used_spaces:
        first = used_spaces.pop(
        )  # Start with an arbitrary elt by removing it...
        used_spaces.add(first)  #... but put it back!
        fringe = [first]
        visited = set()
        in_region = set()
        while fringe:
            current = fringe.pop()
            visited.add(current)
            if current in used_spaces:
                in_region.add(current)
                for n in neighbors(current):
                    if n not in visited and n not in fringe and n not in in_region and n in used_spaces:
                        fringe.append(n)

        used_spaces -= in_region
        #print(len(in_region))
        regions += 1

    return regions
Esempio n. 3
0
def main():
    pos = 0
    skip = 0

    nums = list(range(256))
    if part_1:
        nums = knot_hash.knot_hash(instructions, pos, skip, nums, False)[2]
        print(nums[0] * nums[1])
    else:
        for i in range(64):
            pos, skip, nums, = knot_hash.knot_hash(instructions, pos, skip,
                                                   nums, True)

        # nums = [65, 27, 9, 1, 4, 3, 40, 50, 91, 7, 6, 0, 2, 5, 68, 22]
        # nums *= 16

        hash_str = knot_hash.knot_hash_to_hex_str(nums)
        print(hash_str)
Esempio n. 4
0
def day14a(data):
    used = 0
    for i in range(128):
        row = data + '-' + str(i)
        hash = knot_hash(row)
        bin_string = bin(int(hash, 16))[2:]
        used += bin_string.count('1')

    return used
Esempio n. 5
0
def build_grid(input):
    hashes = ['%s-%d' % (input, x) for x in range(0, 128)]
    grid = []

    for hash in hashes:
        knot_hash_output = knot_hash(hash)

        # from https://stackoverflow.com/questions/1425493/convert-hex-to-binary
        binary = ''.join(
            [bin(int(x, 16))[2:].zfill(NUM_BITS) for x in knot_hash_output])
        grid.append(list(binary))

    return grid
Esempio n. 6
0
def defrag(p1):
    square_count = 0

    grid = []

    for row in range(128):
        instructions = fileStr + '-' + str(row)
        pos = 0
        skip = 0
        nums = list(range(256))
        for i in range(64):
            pos, skip, nums = knot_hash.knot_hash(instructions, pos, skip,
                                                  nums, True)

        hash_str = knot_hash.knot_hash_to_hex_str(nums)
        binary_str = hex_to_binary(hash_str)

        for c in binary_str:
            if c == '1':
                square_count += 1

        for x in binary_str:
            grid.append(int(x))

    if p1:
        print(square_count)
    else:
        print(grid, len(grid))
        island_count = 0
        for x in range(128):
            for y in range(128):
                if grid[x + y * 128] == 1:
                    island_count += 1
                    grid = flood(x, y, grid)

        print("island count:", island_count)
Esempio n. 7
0
 def test_empty_length_list(self):
     self.assertEqual(knot_hash([]), 0)
Esempio n. 8
0
 def test_skip_length_increases(self):
     # [0], 1, 2, 3 | 3
     # 2, 1, 0, [3] | 2 + 1
     # 3, 1, [0], 2 | 2 + 2
     # 3, 1, [2], 0 | 3 + 3
     self.assertEqual(knot_hash([3, 2, 2, 3], max_value=3), 2)
Esempio n. 9
0
 def test_wraps_around(self):
     # 0, 1, 2, 3 | 3
     # 2, 1, 0, [3] | 2
     # 3, 1, 0, 2
     self.assertEqual(knot_hash([3, 2], max_value=3), 3)
Esempio n. 10
0
 def test_length_changes_pos(self):
     # 2, 1, [0]
     self.assertEqual(knot_hash([3, 2]), 2)
Esempio n. 11
0
#!/usr/bin/env python

from knot_hash import knot_hash

puzzle_input = "hxtvlmkl"
#puzzle_input = "flqrgnkx"

# Construct table

table = [
    list("{:0128b}".format(int(knot_hash(puzzle_input + "-" + str(i)), 16)))
    for i in range(128)
]


def floodfill(fill, x, y):
    if table[x][y] == "1":
        table[x][y] = fill
        if x >= 1:
            floodfill(fill, x - 1, y)
        if y < 127:
            floodfill(fill, x, y + 1)
        if y >= 1:
            floodfill(fill, x, y - 1)
        if x < 127:
            floodfill(fill, x + 1, y)


filler = 1  # Starts off at 1 to avoid conflict with existing "1"s, remember to subtract 1 later before return answer
for x in range(len(table)):
    for y in range(len(table[x])):
Esempio n. 12
0
def test_knot_hash_step(hashed_list, input_lengths, expected_hashed_list):
    knot_hash.knot_hash(input_lengths, hashed_list)

    assert hashed_list == expected_hashed_list
Esempio n. 13
0
from knot_hash import knot_hash
# def knot_hash(k):
#     return "a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5"

key = "vbqugkhl"
# key = "flqrgnkx"
used = 0
grid = []

for i in range(128):
    k = "{}-{}".format(key, i)
    h = knot_hash(k)
    s = "{0:b}".format(int(h, 16))
    n = [int(b) for b in s]
    used += sum(n)
    while len(n) < 128:
        n = [0] + n
    grid.append(n)

print used

print len(grid), len(grid[0])

import numpy as np
labels = np.zeros((128, 128), int)
max_label = 0

to_merge = {}

to_remove = set([])
change_to = {}
 def test_2(self):
     self.assertEqual(knot_hash("AoC 2017"),
                      "33efeb34ea91902bb2f59c9920caa6cd")
 def test_1(self):
     self.assertEqual(knot_hash(""), "a2582a3a0e66e6e86e3812dcb672a272")
 def test_4(self):
     self.assertEqual(knot_hash("1,2,4"),
                      "63960835bcdc130f0b66d7ff4f6a5a8e")
 def test_3(self):
     self.assertEqual(knot_hash("1,2,3"),
                      "3efbe78a8d82f29979031a4aa0b16a9d")
Esempio n. 18
0
#!/usr/bin/env python

from knot_hash import knot_hash

#puzzle_input = "flqrgnkx"
puzzle_input = "hxtvlmkl"

tally = ""
for i in range(128):
    print(puzzle_input + "-" + str(i))
    tally += bin(int(knot_hash(puzzle_input + "-" + str(i)), 16))

sum = 0
for char in tally:
    if char == "1":
        sum += 1

print(sum)
Esempio n. 19
0
# Advent of Code
# Alex Johnson
# Day 14

import knot_hash

input_14 = "hwlqcszp"

print("[PART 1]")

# compute raw hashes
hashes_raw = []
for i in range(128):
    hashes_raw.append(knot_hash.knot_hash(input_14 + "-" + str(i)))

print("Hashes computed.")

# convert to binary
binary_hashes = []
for hash_hex in hashes_raw:
    hash_binary = str(bin(int(hash_hex, 16))[2:])
    while len(hash_binary) < 128:
        hash_binary = "0" + hash_binary
    binary_hashes.append(hash_binary)

print("Hashes converted to binary.")

# count number of squares used
used = 0
for h in binary_hashes:
    for char in h:
Esempio n. 20
0
 def test_zero_length(self):
     self.assertEqual(knot_hash([0]), 0)
Esempio n. 21
0
from knot_hash import knot_hash


def count_bits(n):
    count = 0
    while n:
        count += n & 1
        n = n >> 1
    return count


key_string = 'oundnydw'
num_used = 0

for i in range(128):
    row_hash = knot_hash('{}-{}'.format(key_string, i))
    num_used += count_bits(int(row_hash, 16))

print num_used
Esempio n. 22
0
def purge_region(coord):
    grid[coord[0]][coord[1]] = '0'
    squares_to_check_next = [
        (coord[0], coord[1] - 1), (coord[0], coord[1] + 1),
        (coord[0] - 1, coord[1]), (coord[0] + 1, coord[1])
    ]
    for square in squares_to_check_next:
        if square[1] >= 0 and square[1] < len(
                grid) and square[0] >= 0 and square[0] < len(
                    grid[0]) and grid[square[0]][square[1]] == '1':
            purge_region(square)


for i in range(128):
    plaintext = salt + '-' + str(i)
    hashtext = knot_hash(plaintext)
    binary = ''.join([str(int_to_bin(int(c, 16))) for c in hashtext])
    used_count += binary.count('1')
    grid.append(list(binary))

regions = 0
for x in range(128):
    for y in range(128):
        if grid[x][y] == '1':
            purge_region((x, y))
            regions += 1
            print(regions)

print('part 1: ' + str(used_count))
print('part 2: ' + str(regions))
Esempio n. 23
0
#!/usr/bin/env python

from knot_hash import knot_hash

INPUT = 'nbysizxe'
NUM_BITS = 4

hashes = ['%s-%d' % (INPUT, x) for x in range(0, 128)]
total = 0

for hash in hashes:
    knot_hash_output = knot_hash(hash)

    # from https://stackoverflow.com/questions/1425493/convert-hex-to-binary
    binary = ''.join(
        [bin(int(x, 16))[2:].zfill(NUM_BITS) for x in knot_hash_output])
    total += binary.count('1')

print(total)
Esempio n. 24
0
# pylint: disable=invalid-name
import sys
import os
from knot_hash import knot
from knot_hash import knot_hash

path = os.path.join(sys.path[0], "day10_input")
file = open(path, "r")

input = file.readline()
lengths = [int(l) for l in input.split(",")]

numbers = list(range(256))

# Part 1
knot(numbers, lengths, (0, 0))
print(numbers[0] * numbers[1])

# Part 2

print(knot_hash(input))
from knot_hash import knot_hash

entry = "ljoxqyyw"
total = 0
for i in range(128):
    hashed = bin(int(knot_hash(f"{entry}-{i}"), 16))[2:]
    total += hashed.count('1')

print(total)
Esempio n. 26
0
square_count = 0


def hex_to_bin(hex):
    return bin(int(hex, 16))[2:].zfill(4)


def bin_to_symbol(bin):
    if bin:
        return "#"
    else:
        return "."


for i in range(128):
    hash = knot_hash(input + "-" + str(i))
    row = [int(d) for d in "".join([hex_to_bin(c) for c in hash])]
    square_count += reduce(lambda a, b: a + b, row)
    rows.append(row)

# Part 1
print(square_count)

grid = []

for row in rows:
    grid_row = []
    for b in row:
        grid_row.append(bin_to_symbol(b))
    grid.append(grid_row)
Esempio n. 27
0
from knot_hash import knot_hash, dense_hash

p = '31,2,85,1,80,109,35,63,98,255,0,13,105,254,128,33'

# Part 1: product of first two values of knot hash
p1, _, _ = knot_hash(list(range(256)), list(map(int, p.split(','))))
print(p1[0] * p1[1])

# Part 2: Hex dense hash of the ASCII length values
print(dense_hash(p))

Esempio n. 28
0
import sys
sys.path.append('../10')
from knot_hash import knot_hash

input = "xlqgujun"

grid = []
for i in range(128):
    grid.append(format(int(knot_hash("%s-%d" % (input, i)), 16), '0128b'))

print "%d squares are used in the grid" % sum(n.count('1') for n in grid)

# by god
neighbors = {(line, char): [
    (x, y) for x, y in ((line, char - 1), (line - 1, char), (line, char + 1),
                        (line + 1, char))
    if x >= 0 and y >= 0 and x < 128 and y < 128 and grid[x][y] == '1'
]
             for char in range(128) for line in range(128)
             if grid[line][char] == '1'}

# What follows is basically my day 12 solution,
# a depth-first search
visited = set()


def find_neighbors(square):
    if square in visited:
        return
    if grid[square[0]][square[1]] == '0':
        return
Esempio n. 29
0
File: 10_2.py Progetto: robsws/aoc
import sys
from knot_hash import knot_hash

input_filename = sys.argv[1]
file = open(input_filename,'r')
plaintext = file.read().rstrip()

print('part2: '+knot_hash(plaintext))
Esempio n. 30
0
 def test_length_reverses(self):
     self.assertEqual(knot_hash([3]), 2)