Avent of Code - Day 06 - Tuning Trouble

in adventofcode •  2 years ago 

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

Day 06 - Tuning Trouble

https://adventofcode.com/2022/day/5

image.png

Q1

import sys
from collections import defaultdict, deque

file1 = open(sys.argv[1], "r")

while True:
        line = file1.readline()
        if not line:
                break
        s = line.strip()
        break

n = len(s)
i = 4
print(s)
while i < n:
        if len(s[i-4:i]) == len(set(s[i-4:i])):
                print(i)
                break
        else:
                i += 1

Q2

import sys
from collections import defaultdict, deque

file1 = open(sys.argv[1], "r")

while True:
        line = file1.readline()
        if not line:
                break
        s = line.strip()
        break

n = len(s)
x = 14
i = x
print(s)
while i < n:
        if len(s[i-x:i]) == len(set(s[i-x:i])):
                print(i)
                break
        else:
                i += 1

Q1 and Q2 are basically the same - except one is looking for 4 characters and another for 14 characters. Checking String contains unique characters: See How to Check if a Given String has Unique Characters?


Steem to the Moon!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!