~aperezdc/aoc2022

63084cddb11242e1f1778f5c3f3a3d5e987b52ad — Adrian Perez de Castro 2 years ago 21cb718
Day 6, first part
1 files changed, 29 insertions(+), 0 deletions(-)

A day06/day06.py
A day06/day06.py => day06/day06.py +29 -0
@@ 0,0 1,29 @@
#! /usr/bin/env python3
# vim:fenc=utf-8
#
# Copyright © 2022 Adrian Perez de Castro <aperez@igalia.com>
#
# Distributed under terms of the MIT license.

import unittest

def doit(data):
    assert len(data) >= 4
    for i in range(len(data) - 4):
        if len(frozenset(data[i:i+4])) == 4:
            return i + 4

class SelfTest(unittest.TestCase):
    def testDoit(self):
        self.assertEqual(7, doit("mjqjpqmgbljsphdztnvjfqwrcgsmlb"))
        self.assertEqual(5, doit("bvwbjplbgvbhsrlpgdmjqwftvncz"))
        self.assertEqual(6, doit("nppdvjthqldpwncqszvftbrmjlhg"))
        self.assertEqual(10, doit("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"))
        self.assertEqual(11, doit("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"))

if __name__ == "__main__":
    from sys import argv
    if len(argv) == 1:
        unittest.main()
    else:
        [print(doit(data)) for data in argv[1:]]