summaryrefslogtreecommitdiff
path: root/2025/day09.py
blob: 3887c1682f1758c65236eba3c4c34c5544366f79 (plain)
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
78
79
80
81
82
83
84
#!/usr/bin/env python3
from functools import reduce
from re import findall
from copy import deepcopy
import sys
# import numpy as np
import png
from itertools import chain

filename = sys.argv[1] if len(sys.argv) == 2 \
    else "in/" + sys.argv[0].split('/')[-1].rstrip(".py") + ".pzl"
data = open(filename).read()
lines = data.rstrip('\n').split('\n')


points = []
for line in lines:
    x, y = [int(i) for i in line.split(',')]
    points.append((x, y))


A = []
for i, p0 in enumerate(points):
    for p1 in points[i+1:]:
        x0, y0 = p0
        x1, y1 = p1
        a = (abs(x1 - x0)+1) * (abs(y1 - y0)+1)
        A.append((a, p0, p1))

A.sort(reverse=True)
res1 = A[0][0]


res2 = 0

counter = 0
B = set()
for p0, p1 in chain(zip(points, points[1:]), [(points[-1], points[0])]):
    x0, y0 = p0
    x1, y1 = p1
    BB = set()
    if x0 == x1:
        if y0 < y1:
            for y in range(y0, y1+1):
                B.add((x0, y))
                BB.add((x0, y))
        else:
            for y in range(y1, y0+1):
                B.add((x0, y))
                BB.add((x0, y))

    elif y0 == y1:
        if x0 < x1:
            for x in range(x0, x1+1):
                B.add((x, y0))
                BB.add((x, y0))
        else:
            for x in range(x1, x0+1):
                B.add((x, y0))
                BB.add((x, y0))
    else:
        assert False

    # n = 1000
    # img = [[0 for _ in range(n)] for _ in range(n)]
    # for x,y in BB:
    #     img[y//100][x//100] = 1
    # writer = png.Writer(width=n, height=n, bitdepth=1, greyscale=True)
    # with open(f'pic/{counter:04d}.png', 'wb') as f:
    #     writer.write(f, img)
    # counter += 1

# n = 1000
# img = [[0 for _ in range(n)] for _ in range(n)]
# for x,y in B:
#     img[y//100][x//100] = 1
# writer = png.Writer(width=n, height=n, bitdepth=1, greyscale=True)
# with open('out.png', 'wb') as f:
#     writer.write(f, img)


print('res1:', res1)
print('res2:', res2)