summaryrefslogtreecommitdiff
path: root/05/main.py
blob: 876d20c7379ac045191fba1769a8784eb498c138 (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
72
73
#!/usr/bin/python



def solve_part1(lines):
    result = 0

    for line in lines:
        # Rule 1: at least three vowels
        nvowels = 0
        for v in "aeiou":
            nvowels += line.count(v)
        rule1 = nvowels >= 3

        # Rule 2: at least one double letter
        rule2 = False
        for i in range(len(line) - 1):
            if line[i] == line[i + 1]:
                rule2 = True
                break

        # Rule 3: no "ab", "cd", "pq" or "xy"
        rule3 = True
        for s in ["ab", "cd", "pq", "xy"]:
            if line.count(s) > 0:
                rule3 = False
                break

        if rule1 and rule2 and rule3:
            result += 1

    return result



def solve_part2(lines):
    result = 0

    for line in lines:
        # Rule 1: twice-occurring two-letter sequence
        rule1 = False
        for i in range(len(line) - 1):
            s = line[i : i + 2]
            if line.count(s) >= 2:
                rule1 = True
                break

        # Rule 2: ABA-like sequence
        rule2 = False
        for i, c in enumerate(line):
            if i < len(line) - 2 and line[i + 2] == c:
                rule2 = True
                break

        if rule1 and rule2:
            result += 1

    return result



def main():
    # Read list of strings from input text file
    with open("input.txt", "r") as f:
        lines = f.read().splitlines()

    print("Part 1 solution:", solve_part1(lines)) # 258 for me
    print("Part 2 solution:", solve_part2(lines)) #  53 for me



if __name__ == "__main__":
    main()