summaryrefslogtreecommitdiff
path: root/11/main.py
blob: 9939d4252c7f66749ff7031d755c6ebc00d2bc12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python



def increment(string):
    last = string[-1]
    if last == "z":
        return increment(string[0:-1]) + "a"
    else:
        return string[0:-1] + chr(ord(last) + 1)



def solve_part1(old):
    valid = False
    while not valid:
        new = increment(old)

        # Rule 2: no "i", "o" or "l"
        # Cheap to check and likely to fail, so we do this first
        rule2 = True
        for c in ["i", "o", "l"]:
            if new.count(c) > 0:
                rule2 = False
                break
        if not rule2:
            old = new
            continue

        # Rule 1: one increasing straight of three letters
        rule1 = False
        ords = [ord(c) for c in new]
        for i in range(len(new) - 2):
            if ords[i + 1] == ords[i] + 1 and ords[i + 2] == ords[i] + 2:
                rule1 = True
                break

        # Rule 3: two different pair of letters
        rule3 = False
        count = 0
        taken = []
        for i in range(len(new) - 1):
            if new[i] not in taken and new[i] == new[i + 1]:
                count += 1
                taken.append(new[i])
        if count >= 2:
            rule3 = True

        old = new
        valid = rule1 and rule3

    return new



def solve_part2(old):
    return solve_part1(solve_part1(old))



def main():
    # My personal input password
    passwd = "vzbxkghb"

    print("Part 1 solution:", solve_part1(passwd)) # "vzbxxyzz" for me
    print("Part 2 solution:", solve_part2(passwd)) # "vzcaabcc" for me



if __name__ == "__main__":
    main()