aboutsummaryrefslogtreecommitdiff
path: root/2022/08/solve.py
blob: b9f5947b8441be951f41ad269eb8074aa674bb88 (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
data = [list(dat.strip()) for dat in open("input").readlines() if dat != '']

def part1():
    res = 0
    for i in range(1,len(data)-1):
        for j in range(1,len(data[0])-1):
            sight = [True,True,True,True]

            for l in range(0, i):
                if data[l][j] >= data[i][j]:
                    sight[0] = False
            for l in range(0, j):
                if data[i][l] >= data[i][j]:
                    sight[1] = False
            for l in range(len(data[0])-1,j,-1):
                if data[i][l] >= data[i][j]:
                    sight[2] = False
            for l in range(len(data)-1,i,-1):
                if data[l][j] >= data[i][j]:
                    sight[3] = False
            if any(sight):
                res += 1

    return res + ((len(data)+len(data[0]))*2-4)

def part2():
    res = []
    for i in range(1,len(data)-1):
        for j in range(1,len(data[0])-1):
            sight = [0,0,0,0]

            for l in range(i-1, -1, -1):
                sight[0] += 1
                if data[l][j] >= data[i][j]:
                    break
            for l in range(j-1, -1, -1):
                sight[1] += 1
                if data[i][l] >= data[i][j]:
                    break
            for l in range(j+1, len(data[0])):
                sight[2] += 1
                if data[i][l] >= data[i][j]:
                    break
            for l in range(i+1, len(data)):
                sight[3] += 1
                if data[l][j] >= data[i][j]:
                    break
            res.append([x:=1,[x:=x*s for s in sight]][-1][-1]) # Woah!

    return max(res)

print(f"Part 1: {part1()}")
print(f"Part 2: {part2()}")