View Single Post
[Python]AoC luke 4:
SPOILER ALERT! Vis spoiler

Kode

# Part 1
from collections import Counter

count = 0
with open('4.txt') as f:
    for line in f:
        if Counter(line.split()).most_common(1)[0][-1] == 1:
            count += 1

print(count)

Kode

# Part 2
from itertools import combinations

anagrams = 0
with open('4.txt') as f:
    for line_nr,line in enumerate(f, 1):
        for a, b in combinations(line.split(), 2):
            if sorted(a) == sorted(b):
                anagrams += 1
                break

print(line_nr - anagrams)
Sist endret av snippsat; 4. desember 2017 kl. 13:32.