Unable to load image

Advent of Code day 6

was preparing my butthole after the last one but ended up being the fastest one i've done :marseyexcited:

with open("day6input.txt") as f:
    input = f.read()

# part 1

for i in range(len(input)):
    marker = set(input[i:i+4])
    if len(marker) == 4:
        answer = i + 4
        break

print(answer)

# part 2 

for i in range(len(input)):
    marker = set(input[i:i+14])
    if len(marker) == 14:
        answer = i + 14
        break

print(answer)
28
Jump in the discussion.

No email address required.

guaranteed O(n) solution, if only javascript was a real language and had proper hashmaps with size tracking

const l = 14; // change for code length
const h: any = [];
let d = 0;
console.log(fs.readFileSync(process.argv[2], 'utf-8')
  .split('').map(ch => ch.charCodeAt(0)).reduce((p, c, i, s,) => p
     ((h[c] ??= 0)  1) && ++h[c] && (h[c] === 2 ? ++d : 1) && (i >= l-1) && !d && i+1
    || (i >= l-1) && (h[s[i-(l-1)]]--) && (h[s[i-(l-1)]] === 1 ? d-- : 1) && 0
    || 0
  , 0));
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.