Skip to content

Instantly share code, notes, and snippets.

@jacktose
Last active December 7, 2022 19:49
Show Gist options
  • Save jacktose/535f2b33285f74e814b63d54d000a6b4 to your computer and use it in GitHub Desktop.
Save jacktose/535f2b33285f74e814b63d54d000a6b4 to your computer and use it in GitHub Desktop.
u/nthistle's rolling window solution for Advent of Code 2022/6, and u/P1h3r1e3d13's variations
# https://www.reddit.com/r/adventofcode/comments/zdw0u6/2022_day_6_solutions/iz3mu21/
# https://topaz.github.io/paste/#XQAAAQBAAwAAAAAAAAA7mkrvHeIvDZUizuO2LI0KXEPUFLLgeIPRqbv/z+nmNU7MYNHuAOqKaD9UwePzSwp6PCMKtAsVAgxfdclBU/ECGwhPZKq9BUR7o/Zmu+RIMPXoNzknSVbDlqPcEWb4XUG+o730qLQo1wzmnszMJK+r6NrEozSGFECms/XleM5xyuUD53rlAg75i0dcTOehtb1WruFsmuFwg2mVsqUGkAarZ91Z0S5nIRNpNzt4nm4Sd+ZspUOwMi1Jy+UlCSFTPNIQLfYwl6ASa24CiiwiY4ATuPdNdXtyCjLqxfqKFgfPqJLyeVkuHoC+PaTNlr2uRgIvqUXu69Xj3qp4HrDsdRhdSc5p9uEWfh3h9Ho1p/Jm/CQ5JN04MvoCgTXNe5MLr1WkhxA7Ms/bLsFdK6YyolhP9yXU8Y3WEWxhYavVBje+Lxs5P0MPZgRCwB/cUu3+cy7u4exW0KcagX5ibrMPumzB8hv+pNC2yo+b3ZXKxqXd7jKBvFQnlwRzusStU+uP/wTSg/TeaOv/8fwG+A==
with open("input.txt") as f:
s = f.read().strip()
# alternate approach for part 2
cur_window = {}
# mjqjpqmgbljsphdztnvjfqwrcgsmlb
# XXXXXXXXXXXXXX
# to roll the window over one
# XXXXXXXXXXXXXX (remove m, add d)
# first we need to populate cur_window with the first 14
for i in range(14):
if s[i] not in cur_window:
cur_window[s[i]] = 0
cur_window[s[i]] += 1
# roll until cur_window has 14 unique elements
next_idx = 14
while len(cur_window.keys()) < 14:
# roll the window by 1
# adding next_idx
if s[next_idx] not in cur_window:
cur_window[s[next_idx]] = 0
cur_window[s[next_idx]] += 1
# delete next_idx - 14
cur_window[s[next_idx - 14]] -= 1
if cur_window[s[next_idx - 14]] == 0:
del cur_window[s[next_idx - 14]]
next_idx += 1
print(next_idx)
# Minimally adjust the original to use collections.Counter
from collections import Counter
with open("input.txt") as f:
s = f.read().strip()
cur_window = Counter()
# mjqjpqmgbljsphdztnvjfqwrcgsmlb
# XXXXXXXXXXXXXX
# to roll the window over one
# XXXXXXXXXXXXXX (remove m, add d)
# first we need to populate cur_window with the first 14
for i in range(14):
#if s[i] not in cur_window:
# cur_window[s[i]] = 0
cur_window[s[i]] += 1
# roll until cur_window has 14 unique elements
next_idx = 14
while len(cur_window.keys()) < 14:
# roll the window by 1
# adding next_idx
#if s[next_idx] not in cur_window:
# cur_window[s[next_idx]] = 0
cur_window[s[next_idx]] += 1
# delete next_idx - 14
cur_window[s[next_idx - 14]] -= 1
if cur_window[s[next_idx - 14]] == 0:
del cur_window[s[next_idx - 14]]
#or cur_window = +cur_window
next_idx += 1
print(next_idx)
# My version, with a little cleanup
from collections import Counter
win_size = 14
with open("input.txt") as f:
s = f.read().strip()
cur_window = Counter()
# first we need to populate cur_window with the first 14
for i in range(win_size):
cur_window[s[i]] += 1
# roll until cur_window has 14 unique elements
next_idx = win_size
while len(cur_window) < win_size:
# roll the window by 1
old_idx = next_idx - win_size
# adding next_idx
cur_window[s[next_idx]] += 1
# delete old_idx
cur_window[s[old_idx]] -= 1
if cur_window[s[old_idx]] == 0:
del cur_window[s[old_idx]]
#or cur_window = +cur_window
next_idx += 1
print(next_idx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment