about summary refs log tree commit diff
path: root/users/Profpatsch/advent-of-code/2020/02/main.py
blob: e3b27c382a211376defedfe471e273c43bb4a3e6 (plain) (blame)
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
74
75
76
77
import sys

def parse(line):
    a = line.split(sep=" ", maxsplit=1)
    assert len(a) == 2
    fromto = a[0].split(sep="-")
    assert len(fromto) == 2
    (from_, to) = (int(fromto[0]), int(fromto[1]))
    charpass = a[1].split(sep=": ")
    assert len(charpass) == 2
    char = charpass[0]
    assert len(char) == 1
    pass_ = charpass[1]
    assert pass_.endswith("\n")
    pass_ = pass_[:-1]
    return {
        "from": from_,
        "to": to,
        "char": char,
        "pass": pass_
    }

def char_in_pass(char, pass_):
    return pass_.count(char)

def validate_01(entry):
    no = char_in_pass(entry["char"], entry["pass"])
    if no < entry["from"]:
        return { "too-small": entry }
    elif no > entry["to"]:
        return { "too-big": entry }
    else:
        return { "ok": entry }

def char_at_pos(char, pos, pass_):
    assert pos <= len(pass_)
    return pass_[pos-1] == char

def validate_02(entry):
    one = char_at_pos(entry["char"], entry["from"], entry["pass"])
    two = char_at_pos(entry["char"], entry["to"], entry["pass"])
    if one and two:
        return { "both": entry }
    elif one:
        return { "one": entry }
    elif two:
        return { "two": entry }
    else:
        return { "none": entry }


res01 = []
res02 = []
with open("./input", 'r') as f:
    for line in f:
        p = parse(line)
        res01.append(validate_01(p))
        res02.append(validate_02(p))

count01=0
for r in res01:
    print(r)
    if r.get("ok", False):
        count01=count01+1

count02=0
for r in res02:
    print(r)
    if r.get("one", False):
        count02=count02+1
    elif r.get("two", False):
        count02=count02+1
    else:
        pass

print("count 1: {}".format(count01))
print("count 2: {}".format(count02))