-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5_x.krk
68 lines (57 loc) · 1.58 KB
/
day5_x.krk
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
#!/usr/bin/env kuroko
'''
Template
'''
import fileio
import kuroko
from collections import defaultdict
let data
with fileio.open(kuroko.argv[1] if len(kuroko.argv) > 1 else kuroko.argv[0].replace('.krk','.txt'),'r') as f:
data = f.readlines()
let lines = [line.strip() for line in data]
def parse_line(l):
let left, right = l.split(' -> ')
let x1,y1 = [int(x) for x in left.split(',')]
let x2,y2 = [int(x) for x in right.split(',')]
return (x1,y1,x2,y2)
let board = {} #defaultdict(lambda: 0)
'''
def paint_line(x1,y1,x2,y2):
if x1 == x2:
if y1 < y2:
for y = y1; y <= y2; y+=1:
board[(x1,y)] += 1
else:
for y = y2; y <= y1; y+=1:
board[(x1,y)] += 1
else if y1 == y2:
if x1 < x2:
for x = x1; x <= x2; x+=1:
board[(x,y1)] += 1
else:
for x = x2; x <= x1; x+=1:
board[(x,y1)] += 1
'''
def paint_line(x1,y1,x2,y2):
let d_x = 1 if x1 < x2 else (-1 if x1 > x2 else 0)
let d_y = 1 if y1 < y2 else (-1 if y1 > y2 else 0)
let t_x = x2 - x1
let t_y = y2 - y1
let x = x1
let y = y1
#print(x1,y1,x2,y2)
while (x != x2 + d_x or not d_x) and (y != y2 + d_y or not d_y):
if not x in board:
board[x] = {}
board[x][y] = board[x].get(y,0) + 1
x += d_x
y += d_y
for line in lines:
let a,b,c,d = parse_line(line)
paint_line(a,b,c,d)
let count = 0
for x_c, row in board.items():
for y_c, cell in row.items():
if cell >= 2:
count++
print(count)