Skip to content

Instantly share code, notes, and snippets.

@zoldar
Created December 6, 2022 22:01
Show Gist options
  • Select an option

  • Save zoldar/828f2ba72ff5653c43bbfe7ebe2c3476 to your computer and use it in GitHub Desktop.

Select an option

Save zoldar/828f2ba72ff5653c43bbfe7ebe2c3476 to your computer and use it in GitHub Desktop.
import std/strformat, std/sets
proc findMarker(input: string, length: int): int =
for idx in 0..<input.len:
if input[idx..<idx+length].toHashSet.len == length:
result = idx + length
break
proc findStartOfPacket*(input: string): int =
findMarker(input, 4)
proc findStartOfMessage*(input: string): int =
findMarker(input, 14)
let input = open("day-6/input.txt").readAll
echo fmt"Start of packet marker position: {findStartOfPacket(input)}"
echo fmt"Start of message marker position: {findStartOfMessage(input)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment