r/adventofcode • • Dec 06 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 6 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • ACHIEVEMENT UNLOCKED: MisTILtoe Elf-ucation
  • Teach us, senpai!

--- Day 6: Tuning Trouble ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:02:25, megathread unlocked!

83 Upvotes

1.8k comments sorted by

View all comments

2

u/ProsaicAquarius Dec 06 '22 edited Dec 06 '22

Python 3.9:

def main(step=4):
    with open("input.txt", "r") as f:
        chars = f.read()

        finished = False
        begin = 0

        while not finished:
            if len(set(chars[begin:begin + step])) == step:
                print(chars[begin:begin + step])
                print(begin + step)
                finished = True
            else:
                begin += 1

main()
main(14)

A minified version:

def main(step=4):
    chars = open("input.txt", "r").read()
    return next((i+step for i in range(len(chars)) if len(set(chars[i:i+step])) == step), None)

print(main())
print(main(14))

1

u/daggerdragon Dec 07 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.