summaryrefslogtreecommitdiffstats
path: root/solutions/py/d16.py
blob: d08c8562ca2429bcf58a7174554c9a057eda8d6f (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
def do(input, phases=100, repeats=1):
    nums = []
    for i in range(repeats):
        nums += [int(x) for x in input[0].strip()]

    for phase in range(phases):
        print(phase)
        new_list = []
        for i in range(len(nums)):  # position of number to calculate
            new_num = 0
            for k in range(len(nums)):
                mod = (k+1) % (4*(i+1))
                if mod >= i + 1 and mod < 2*i + 2:
                    new_num += nums[k]
                elif mod >= 3*i + 3:
                    new_num -= nums[k]
            new_list.append(abs(new_num) % 10)
        nums = new_list
    return "".join([str(n) for n in nums[:8]])

def pt1(input):
    return do(input, phases=100, repeats=1)

def pt2(input, phases=100, repeats=10000):
    offset = int(input[0][:7])
    nums = []
    for i in range(repeats):
        nums += [int(x) for x in input[0].strip()]
    nums = nums[offset:]
    for phase in range(phases):
        print(phase)
        nums = nums[::-1]
        new_nums = []
        n = sum(nums)
        while len(nums) != 0:
            new_nums.append(abs(n) % 10)
            n -= nums.pop()
        nums = new_nums
    return "".join([str(n) for n in nums[:8]])

def square(size):
    s = ""
    for y in range(size):
        for x in range(size):
            mod = (x+1) % (4*(y+1))
            if mod < y+1:
                s += "0"
            elif mod < 2*y + 2:
                s += "+"
            elif mod < 3*y + 3:
                s += "0"
            else:
                s += "-"
            s += " "
        s += "\n"
    return s

if __name__ == "__main__":
    input = open("../input/16", "r").readlines()
    ans1 = pt1(input)
    ans2 = pt2(input)
    print(ans1)
    print(ans2)
    #print(square(8))
    #print(square(20))
    #print(square(62))