Added 2017/14
This commit is contained in:
parent
e4caab3731
commit
9e64b27a26
@ -4,7 +4,7 @@ from pprint import pprint
|
|||||||
from functools import reduce
|
from functools import reduce
|
||||||
from operator import xor
|
from operator import xor
|
||||||
|
|
||||||
input_f = 'input'
|
input_f = 'test3'
|
||||||
|
|
||||||
def list2int(x):
|
def list2int(x):
|
||||||
return list(map(int, x))
|
return list(map(int, x))
|
||||||
@ -90,7 +90,8 @@ if part == 2:
|
|||||||
for i in range(0,16):
|
for i in range(0,16):
|
||||||
t = numbers[i*16:(i*16)+16]
|
t = numbers[i*16:(i*16)+16]
|
||||||
dense.append(XOR(t))
|
dense.append(XOR(t))
|
||||||
|
print(dense)
|
||||||
|
|
||||||
|
|
||||||
for i in dense:
|
for i in dense:
|
||||||
print(format(i, '02x'),end='')
|
print(format(i, '02x'),end='')
|
||||||
|
1
2017/10/test4
Normal file
1
2017/10/test4
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
@ -6,7 +6,7 @@ from fred import list2int, ppprint
|
|||||||
|
|
||||||
input_f = 'input'
|
input_f = 'input'
|
||||||
|
|
||||||
part = 3
|
part = 2
|
||||||
|
|
||||||
#########################################
|
#########################################
|
||||||
# #
|
# #
|
||||||
@ -100,7 +100,7 @@ if part == 1:
|
|||||||
# Part 2 #
|
# Part 2 #
|
||||||
# #
|
# #
|
||||||
#########################################
|
#########################################
|
||||||
if part == 2:
|
if part == 3: #ugly not workng
|
||||||
grid_length = 7
|
grid_length = 7
|
||||||
for i in range(0,grid_length):
|
for i in range(0,grid_length):
|
||||||
grid.append([])
|
grid.append([])
|
||||||
@ -176,7 +176,7 @@ if part == 2:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
if part == 3:
|
if part == 2:
|
||||||
grid_length = 100
|
grid_length = 100
|
||||||
scanners = {}
|
scanners = {}
|
||||||
for i in range(0,grid_length):
|
for i in range(0,grid_length):
|
||||||
|
94
2017/14/14.md
Normal file
94
2017/14/14.md
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
## \-\-- Day 14: Disk Defragmentation \-\--
|
||||||
|
|
||||||
|
Suddenly, a scheduled job activates the system\'s [disk
|
||||||
|
defragmenter](https://en.wikipedia.org/wiki/Defragmentation). Were the
|
||||||
|
situation different, you might [sit and watch it for a
|
||||||
|
while](https://www.youtube.com/watch?v=kPv1gQ5Rs8A&t=37), but today, you
|
||||||
|
just don\'t have that kind of time. It\'s soaking up valuable system
|
||||||
|
resources that are needed elsewhere, and so the only option is to help
|
||||||
|
it finish its task as soon as possible.
|
||||||
|
|
||||||
|
The disk in question consists of a 128x128 grid; each square of the grid
|
||||||
|
is either *free* or *used*. On this disk, the state of the grid is
|
||||||
|
tracked by the bits in a sequence of [knot hashes](10).
|
||||||
|
|
||||||
|
A total of 128 knot hashes are calculated, each corresponding to a
|
||||||
|
single row in the grid; each hash contains 128 bits which correspond to
|
||||||
|
individual grid squares. Each bit of a hash indicates whether that
|
||||||
|
square is *free* (`0`) or *used* (`1`).
|
||||||
|
|
||||||
|
The hash inputs are a key string (your puzzle input), a dash, and a
|
||||||
|
number from `0` to `127` corresponding to the row. For example, if your
|
||||||
|
key string were `flqrgnkx`, then the first row would be given by the
|
||||||
|
bits of the knot hash of `flqrgnkx-0`, the second row from the bits of
|
||||||
|
the knot hash of `flqrgnkx-1`, and so on until the last row,
|
||||||
|
`flqrgnkx-127`.
|
||||||
|
|
||||||
|
The output of a knot hash is traditionally represented by 32 hexadecimal
|
||||||
|
digits; each of these digits correspond to 4 bits, for a total of
|
||||||
|
`4 * 32 = 128` bits. To convert to bits, turn each hexadecimal digit to
|
||||||
|
its equivalent binary value, high-bit first: `0` becomes `0000`, `1`
|
||||||
|
becomes `0001`, `e` becomes `1110`, `f` becomes `1111`, and so on; a
|
||||||
|
hash that begins with `a0c2017...` in hexadecimal would begin with
|
||||||
|
`10100000110000100000000101110000...` in binary.
|
||||||
|
|
||||||
|
Continuing this process, the *first 8 rows and columns* for key
|
||||||
|
`flqrgnkx` appear as follows, using `#` to denote used squares, and `.`
|
||||||
|
to denote free ones:
|
||||||
|
|
||||||
|
##.#.#..-->
|
||||||
|
.#.#.#.#
|
||||||
|
....#.#.
|
||||||
|
#.#.##.#
|
||||||
|
.##.#...
|
||||||
|
##..#..#
|
||||||
|
.#...#..
|
||||||
|
##.#.##.-->
|
||||||
|
| |
|
||||||
|
V V
|
||||||
|
|
||||||
|
In this example, `8108` squares are used across the entire 128x128 grid.
|
||||||
|
|
||||||
|
Given your actual key string, *how many squares are used*?
|
||||||
|
|
||||||
|
Your puzzle answer was `8250`.
|
||||||
|
|
||||||
|
## \-\-- Part Two \-\-- {#part2}
|
||||||
|
|
||||||
|
Now, [all the defragmenter needs to
|
||||||
|
know]{title="This is exactly how it works in real life."} is the number
|
||||||
|
of *regions*. A region is a group of *used* squares that are all
|
||||||
|
*adjacent*, not including diagonals. Every used square is in exactly one
|
||||||
|
region: lone used squares form their own isolated regions, while several
|
||||||
|
adjacent squares all count as a single region.
|
||||||
|
|
||||||
|
In the example above, the following nine regions are visible, each
|
||||||
|
marked with a distinct digit:
|
||||||
|
|
||||||
|
11.2.3..-->
|
||||||
|
.1.2.3.4
|
||||||
|
....5.6.
|
||||||
|
7.8.55.9
|
||||||
|
.88.5...
|
||||||
|
88..5..8
|
||||||
|
.8...8..
|
||||||
|
88.8.88.-->
|
||||||
|
| |
|
||||||
|
V V
|
||||||
|
|
||||||
|
Of particular interest is the region marked `8`; while it does not
|
||||||
|
appear contiguous in this small view, all of the squares marked `8` are
|
||||||
|
connected when considering the whole 128x128 grid. In total, in this
|
||||||
|
example, `1242` regions are present.
|
||||||
|
|
||||||
|
*How many regions* are present given your key string?
|
||||||
|
|
||||||
|
Your puzzle answer was `1113`.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars:
|
||||||
|
\*\*
|
||||||
|
|
||||||
|
At this point, you should [return to your Advent calendar](/2017) and
|
||||||
|
try another puzzle.
|
||||||
|
|
||||||
|
Your puzzle input was `stpzcrnm`{.puzzle-input}.
|
50
2017/14/sol.py
Normal file
50
2017/14/sol.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
grid = [
|
||||||
|
[0,0,0,0,1],
|
||||||
|
[0,0,1,0,1],
|
||||||
|
[0,0,1,1,1],
|
||||||
|
[1,0,0,0,1],
|
||||||
|
[0,0,0,0,0]
|
||||||
|
]
|
||||||
|
|
||||||
|
blob_count = 1
|
||||||
|
|
||||||
|
for idx,i in enumerate(grid):
|
||||||
|
for jdx,j in enumerate(i):
|
||||||
|
print(grid[idx][jdx],end=' ')
|
||||||
|
print()
|
||||||
|
|
||||||
|
def is_valid(x,y):
|
||||||
|
return 0 <= x < rows and 0 <= y < cols and not visited[x][y] and grid[x][y] == '1'
|
||||||
|
|
||||||
|
rows,cols = len(grid), len(grid[0])
|
||||||
|
visited = [[False for _ in range(cols)] for _ in range(rows)]
|
||||||
|
directions = [(0,1),(0,-1),(1,0),(-1,0)]
|
||||||
|
|
||||||
|
def dfs(x, y,blob_count):
|
||||||
|
print(blob_count)
|
||||||
|
visited[x][y] = True
|
||||||
|
for dx, dy in directions:
|
||||||
|
nx, ny = x + dx, y + dy
|
||||||
|
|
||||||
|
if is_valid(nx, ny):
|
||||||
|
|
||||||
|
dfs(nx, ny,blob_count)
|
||||||
|
else:
|
||||||
|
blob_count += 1
|
||||||
|
return blob_count
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(rows):
|
||||||
|
for j in range(cols):
|
||||||
|
if grid[i][j] == 1 and not visited[i][j]:
|
||||||
|
grid[i][j] = blob_count
|
||||||
|
blob_count = dfs(i, j,blob_count)
|
||||||
|
|
||||||
|
|
||||||
|
print(blob_count)
|
||||||
|
|
||||||
|
for idx,i in enumerate(grid):
|
||||||
|
for jdx,j in enumerate(i):
|
||||||
|
print(grid[idx][jdx],end=' ')
|
||||||
|
print()
|
156
2017/14/solution.py
Normal file
156
2017/14/solution.py
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
#!/bin/python3
|
||||||
|
import sys,re
|
||||||
|
from pprint import pprint
|
||||||
|
from functools import reduce
|
||||||
|
from operator import xor
|
||||||
|
|
||||||
|
input_f = 'input'
|
||||||
|
|
||||||
|
def list2int(x):
|
||||||
|
return list(map(int, x))
|
||||||
|
|
||||||
|
def toACSII(x):
|
||||||
|
for idx,i in enumerate(x):
|
||||||
|
x[idx] = ord(i)
|
||||||
|
return x
|
||||||
|
|
||||||
|
def XOR(x):
|
||||||
|
return reduce(xor, map(int, t))
|
||||||
|
|
||||||
|
part = 1
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 1 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
|
||||||
|
if part == 0:
|
||||||
|
|
||||||
|
size = 256
|
||||||
|
lengths = []
|
||||||
|
skip = 0
|
||||||
|
numbers = []
|
||||||
|
pos = 0
|
||||||
|
for i in range(0,size):
|
||||||
|
numbers.append(i)
|
||||||
|
|
||||||
|
with open(input_f) as file:
|
||||||
|
for line in file:
|
||||||
|
lengths = list2int(line.rsplit()[0].split(','))
|
||||||
|
|
||||||
|
for ldx, length in enumerate(lengths):
|
||||||
|
sub = [numbers[(pos + i) % len(numbers)] for i in range(length)]
|
||||||
|
rev = sub[::-1]
|
||||||
|
|
||||||
|
for i in range(length):
|
||||||
|
numbers[(pos + i) % len(numbers)] = rev[i]
|
||||||
|
|
||||||
|
pos += (length+skip)
|
||||||
|
pos = pos % len(numbers)
|
||||||
|
skip += 1
|
||||||
|
print(numbers[0]*numbers[1])
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 2 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
|
||||||
|
grid = []
|
||||||
|
|
||||||
|
if part == 1:
|
||||||
|
|
||||||
|
size = 256
|
||||||
|
lengths = []
|
||||||
|
skip = 0
|
||||||
|
original_numbers = []
|
||||||
|
pos = 0
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
for i in range(0,size):
|
||||||
|
original_numbers.append(i)
|
||||||
|
|
||||||
|
with open(input_f) as file:
|
||||||
|
for line in file:
|
||||||
|
lengths = list(line.rsplit()[0].split()[0])
|
||||||
|
#print(lengths)
|
||||||
|
|
||||||
|
original_lengths = lengths
|
||||||
|
for x in range(0,128):
|
||||||
|
numbers = original_numbers[:]
|
||||||
|
lengths = []
|
||||||
|
skip = 0
|
||||||
|
pos = 0
|
||||||
|
lengths = original_lengths + ['-'] + list(str(x))
|
||||||
|
#print(lengths)
|
||||||
|
#lengths = ['A','o','C',' ','2','0','1','7']
|
||||||
|
|
||||||
|
lengths = toACSII(lengths) + [17, 31, 73, 47, 23]
|
||||||
|
#print('ACSII',lengths)
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(0,64):
|
||||||
|
for ldx, length in enumerate(lengths):
|
||||||
|
sub = [numbers[(pos + i) % len(numbers)] for i in range(length)]
|
||||||
|
rev = sub[::-1]
|
||||||
|
|
||||||
|
for i in range(length):
|
||||||
|
numbers[(pos + i) % len(numbers)] = rev[i]
|
||||||
|
|
||||||
|
pos += (length+skip)
|
||||||
|
pos = pos % len(numbers)
|
||||||
|
skip += 1
|
||||||
|
dense = []
|
||||||
|
for j in range(0,16):
|
||||||
|
t = numbers[j*16:(j*16)+16]
|
||||||
|
dense.append(XOR(t))
|
||||||
|
#print(dense)
|
||||||
|
|
||||||
|
hexi = ''
|
||||||
|
|
||||||
|
for i in dense:
|
||||||
|
hexi += format(i, '02x')
|
||||||
|
|
||||||
|
#print(hexi)
|
||||||
|
binary = ''
|
||||||
|
for i in hexi:
|
||||||
|
binary += bin(int(i,16))[2:].zfill(4)
|
||||||
|
grid.append(list(binary))
|
||||||
|
|
||||||
|
#for i in hexi:
|
||||||
|
# print(' '.join(bin(ord(char))[2:] for char in i))
|
||||||
|
|
||||||
|
#print(binary)
|
||||||
|
total += binary.count('1')
|
||||||
|
print('Part 1:',total)
|
||||||
|
#input()
|
||||||
|
|
||||||
|
for i in range (0,8):
|
||||||
|
for j in range(0,8):
|
||||||
|
print(grid[i][j],end='')
|
||||||
|
print()
|
||||||
|
|
||||||
|
def count_blobs(grid):
|
||||||
|
rows,cols = len(grid), len(grid[0])
|
||||||
|
visited = [[False for _ in range(cols)] for _ in range(rows)]
|
||||||
|
directions = [(0,1),(0,-1),(1,0),(-1,0)]
|
||||||
|
|
||||||
|
def is_valid(x,y):
|
||||||
|
return 0 <= x < rows and 0 <= y < cols and not visited[x][y] and grid[x][y] == '1'
|
||||||
|
|
||||||
|
def dfs(x, y):
|
||||||
|
visited[x][y] = True
|
||||||
|
for dx, dy in directions:
|
||||||
|
nx, ny = x + dx, y + dy
|
||||||
|
if is_valid(nx, ny):
|
||||||
|
dfs(nx, ny)
|
||||||
|
|
||||||
|
blob_count = 0
|
||||||
|
for i in range(rows):
|
||||||
|
for j in range(cols):
|
||||||
|
if grid[i][j] == '1' and not visited[i][j]:
|
||||||
|
blob_count += 1
|
||||||
|
dfs(i, j)
|
||||||
|
return blob_count
|
||||||
|
|
||||||
|
print(count_blobs(grid))
|
1
2017/14/test2
Normal file
1
2017/14/test2
Normal file
@ -0,0 +1 @@
|
|||||||
|
AoC 2017
|
1
2017/14/test3
Normal file
1
2017/14/test3
Normal file
@ -0,0 +1 @@
|
|||||||
|
a0c2017
|
1
2017/14/test4
Normal file
1
2017/14/test4
Normal file
@ -0,0 +1 @@
|
|||||||
|
flqrgnkx
|
Loading…
Reference in New Issue
Block a user