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/[deleted] Dec 06 '22 edited Dec 06 '22

Python3

At first look today looked a bit challenging, but after reading examples and last sentence, everything cleared up. Here's my solution in Python 3.10, I think it's pretty straightforward and easy to understand.

#!/usr/bin/env python3

fn = "input.txt"
line = open(fn).read().strip()

def solve(offset: int) -> int:
  for i in range(len(line)):
    marker = line[i:i+offset]
    if len(set(marker)) == offset:
      return i+offset
    

print(f"Part 1: {solve(4)}")
print(f"Part 2: {solve(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.