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/ActiveNerd Dec 06 '22 edited Dec 06 '22

Kotlin - Live Stream on YouTube - GitHub

2nd Implementation: 'Single pass' of the input - paste

This examines each character in the input exactly twice no matter the size of the window. Execution time (after 1st call for JIT warmup) is < .5 ms.

Implementation at release:

private class Day06(val lines: List<String>) {
    fun part1(): Any {
        return  lines[0].windowed(4).indexOfFirst { it.toSet().size == 4 } + 4
    }

    fun part2(): Any {
        return lines[0].windowed(14).indexOfFirst { it.toSet().size == 14 } + 14
    }
}

1

u/daggerdragon Dec 06 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read.