Passed
Push — main ( 83fd9b...e71399 )
by Yohann
57s
created

main.get_priority()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import pathlib
2
3
4
def get_priority(items):
5
    item = set.intersection(*map(set, items)).pop()
6
    i = ord(item)  # magic done here :3
7
    return (i | 32) - 70 - (26 * (i & 32) >> 5)
8
9
10
def get_compartments_priorities_sum(rucksacks):
11
    def cut_line(line):
12
        half = len(line) // 2
13
        return map(set, (line[:half], line[half:]))
14
15
    return sum(map(get_priority, map(cut_line, rucksacks)))
16
17
18
def get_badge_priorities_sum(rucksacks):
19
    def chunk(lst, size):
20
        return [lst[i:i + size] for i in range(0, len(lst), size)]
21
22
    return sum(map(get_priority, chunk(rucksacks, 3)))
23
24
25
def main():
26
    content = pathlib.Path('./input.txt').read_text()
27
    lines = content.splitlines()
28
29
    print("Part 1:", get_compartments_priorities_sum(lines))
30
    print("Part 2:", get_badge_priorities_sum(lines))
31
32
33
if __name__ == '__main__':
34
    main()
35