Skip to content

Commit

Permalink
2017 day 14 solution
Browse files Browse the repository at this point in the history
Signed-off-by: Lance-Drane <ldraneutk@gmail.com>
  • Loading branch information
Lance-Drane committed Jan 15, 2024
1 parent 32c8da3 commit ed03786
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
5 changes: 1 addition & 4 deletions 2017/10/2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
current_position = (forward + skip_size) % LIST_SIZE
skip_size += 1

hex_arr = [format(reduce(lambda x, y: x ^ y, nums[idx : idx + 16]), 'x') for idx in range(0, 256, 16)]
for idx, item in enumerate(hex_arr):
if len(item) == 1:
hex_arr[idx] = f'0{item}'
hex_arr = [format(reduce(lambda x, y: x ^ y, nums[idx : idx + 16]), 'x').zfill(2) for idx in range(0, 256, 16)]

print(''.join(hex_arr))
30 changes: 30 additions & 0 deletions 2017/14/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from functools import reduce

LIST_SIZE = 256

base = f'{input()}-'

used_squares = 0
for row in range(128):
nums = list(range(LIST_SIZE))
current_position = 0
skip_size = 0
lengths = [ord(x) for x in f'{base}{row}'] + [17, 31, 73, 47, 23]
for _ in range(64):
for length in lengths:
forward = current_position + length
if forward > LIST_SIZE:
last_idx = forward % LIST_SIZE
sublist = nums[current_position:] + nums[:last_idx]
sublist = sublist[::-1]
sublist_breakpoint = (len(sublist) - last_idx) % len(sublist)
nums = sublist[sublist_breakpoint:] + nums[last_idx:current_position] + sublist[:sublist_breakpoint]
else:
nums = nums[:current_position] + nums[current_position:forward][::-1] + nums[forward:]
current_position = (forward + skip_size) % LIST_SIZE
skip_size += 1

bin_arr = [format(reduce(lambda x, y: x ^ y, nums[idx : idx + 16]), 'b').zfill(8) for idx in range(0, 256, 16)]
used_squares += sum(x.count('1') for x in bin_arr)

print(used_squares)
1 change: 1 addition & 0 deletions 2017/14/1_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8316
58 changes: 58 additions & 0 deletions 2017/14/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from functools import reduce

LIST_SIZE = 256

base = f'{input()}-'

grid = []
for row in range(128):
nums = list(range(LIST_SIZE))
current_position = 0
skip_size = 0
lengths = [ord(x) for x in f'{base}{row}'] + [17, 31, 73, 47, 23]
for _ in range(64):
for length in lengths:
forward = current_position + length
if forward > LIST_SIZE:
last_idx = forward % LIST_SIZE
sublist = nums[current_position:] + nums[:last_idx]
sublist = sublist[::-1]
sublist_breakpoint = (len(sublist) - last_idx) % len(sublist)
nums = sublist[sublist_breakpoint:] + nums[last_idx:current_position] + sublist[:sublist_breakpoint]
else:
nums = nums[:current_position] + nums[current_position:forward][::-1] + nums[forward:]
current_position = (forward + skip_size) % LIST_SIZE
skip_size += 1

bin_arr = [format(reduce(lambda x, y: x ^ y, nums[idx : idx + 16]), 'b').zfill(8) for idx in range(0, 256, 16)]
grid.append(list(''.join(bin_arr)))

groups = 0
for y in range(128):
for x in range(128):
if grid[y][x] == '0':
continue
grid[y][x] = '0'
stack = [(y, x)]
while stack:
curr_y, curr_x = stack.pop()
# top
if curr_y > 0 and grid[curr_y - 1][curr_x] == '1':
grid[curr_y - 1][curr_x] = '0'
stack.append((curr_y - 1, curr_x))
# right
if curr_x < 127 and grid[curr_y][curr_x + 1] == '1':
grid[curr_y][curr_x + 1] = '0'
stack.append((curr_y, curr_x + 1))
# bottom
if curr_y < 127 and grid[curr_y + 1][curr_x] == '1':
grid[curr_y + 1][curr_x] = '0'
stack.append((curr_y + 1, curr_x))
# left
if curr_x > 0 and grid[curr_y][curr_x - 1] == '1':
grid[curr_y][curr_x - 1] = '0'
stack.append((curr_y, curr_x - 1))

groups += 1

print(groups)
1 change: 1 addition & 0 deletions 2017/14/2_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1074
1 change: 1 addition & 0 deletions 2017/14/in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ljoxqyyw

0 comments on commit ed03786

Please sign in to comment.