From 9e64b27a265b10e36a64a7c3142b6d6437445bab Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Mon, 25 Nov 2024 17:42:14 +0100 Subject: [PATCH] Added 2017/14 --- 2017/10/solution.py | 5 +- 2017/10/test4 | 1 + 2017/13/solution.py | 6 +- 2017/14/14.md | 94 ++++++++++++++++++++++++++ 2017/14/sol.py | 50 ++++++++++++++ 2017/14/solution.py | 156 ++++++++++++++++++++++++++++++++++++++++++++ 2017/14/test2 | 1 + 2017/14/test3 | 1 + 2017/14/test4 | 1 + 9 files changed, 310 insertions(+), 5 deletions(-) create mode 100644 2017/10/test4 create mode 100644 2017/14/14.md create mode 100644 2017/14/sol.py create mode 100644 2017/14/solution.py create mode 100644 2017/14/test2 create mode 100644 2017/14/test3 create mode 100644 2017/14/test4 diff --git a/2017/10/solution.py b/2017/10/solution.py index 95efd77..227a11e 100644 --- a/2017/10/solution.py +++ b/2017/10/solution.py @@ -4,7 +4,7 @@ from pprint import pprint from functools import reduce from operator import xor -input_f = 'input' +input_f = 'test3' def list2int(x): return list(map(int, x)) @@ -90,7 +90,8 @@ if part == 2: for i in range(0,16): t = numbers[i*16:(i*16)+16] dense.append(XOR(t)) - + print(dense) + for i in dense: print(format(i, '02x'),end='') diff --git a/2017/10/test4 b/2017/10/test4 new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/2017/10/test4 @@ -0,0 +1 @@ + diff --git a/2017/13/solution.py b/2017/13/solution.py index 9363235..2e2cff8 100644 --- a/2017/13/solution.py +++ b/2017/13/solution.py @@ -6,7 +6,7 @@ from fred import list2int, ppprint input_f = 'input' -part = 3 +part = 2 ######################################### # # @@ -100,7 +100,7 @@ if part == 1: # Part 2 # # # ######################################### -if part == 2: +if part == 3: #ugly not workng grid_length = 7 for i in range(0,grid_length): grid.append([]) @@ -176,7 +176,7 @@ if part == 2: -if part == 3: +if part == 2: grid_length = 100 scanners = {} for i in range(0,grid_length): diff --git a/2017/14/14.md b/2017/14/14.md new file mode 100644 index 0000000..b85a306 --- /dev/null +++ b/2017/14/14.md @@ -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}. diff --git a/2017/14/sol.py b/2017/14/sol.py new file mode 100644 index 0000000..b0ad911 --- /dev/null +++ b/2017/14/sol.py @@ -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() \ No newline at end of file diff --git a/2017/14/solution.py b/2017/14/solution.py new file mode 100644 index 0000000..b3819eb --- /dev/null +++ b/2017/14/solution.py @@ -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)) \ No newline at end of file diff --git a/2017/14/test2 b/2017/14/test2 new file mode 100644 index 0000000..2c0ae89 --- /dev/null +++ b/2017/14/test2 @@ -0,0 +1 @@ +AoC 2017 diff --git a/2017/14/test3 b/2017/14/test3 new file mode 100644 index 0000000..adf2e92 --- /dev/null +++ b/2017/14/test3 @@ -0,0 +1 @@ +a0c2017 diff --git a/2017/14/test4 b/2017/14/test4 new file mode 100644 index 0000000..e0ffae2 --- /dev/null +++ b/2017/14/test4 @@ -0,0 +1 @@ +flqrgnkx