-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.um
81 lines (68 loc) · 1.73 KB
/
day4.um
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
import "std.um"
fn check1(m: []str, x, y, dx, dy: int): int {
if m[y][x] == 'S' {
a := m[y+dy][x+dx]
b := m[y+dy*2][x+dx*2]
c := m[y+dy*3][x+dx*3]
return int(a == 'A' && b == 'M' && c == 'X')
} else if m[y][x] == 'X' {
a := m[y+dy][x+dx]
b := m[y+dy*2][x+dx*2]
c := m[y+dy*3][x+dx*3]
return int(a == 'M' && b == 'A' && c == 'S')
}
return 0
}
fn check2(m: []str, x, y: int): int {
if m[y][x] == 'A' {
a := m[y+1][x+1]
b := m[y-1][x-1]
c := m[y-1][x+1]
d := m[y+1][x-1]
return int(
((a == 'M' && b == 'S') || (b == 'M' && a == 'S')) &&
((c == 'M' && d == 'S') || (d == 'M' && c == 'S')))
}
return 0
}
fn main() {
lines := []str{}
for l := ""; scanf("%s", &l) == 1 {
lines = append(lines, l)
}
width, height := len(lines[0]), len(lines)
score := 0
// hor
for x := 0; x <= width-4; x++ {
for y := 0; y < height; y++ {
score += check1(lines, x, y, 1, 0)
}
}
// ver
for x := 0; x < width; x++ {
for y := 0; y <= height-4; y++ {
score += check1(lines, x, y, 0, 1)
}
}
// diag \
for x := 0; x <= width-4; x++ {
for y := 0; y <= height-4; y++ {
score += check1(lines, x, y, 1, 1)
}
}
// diag /
for x := 3; x < width; x++ {
for y := 0; y <= height-4; y++ {
score += check1(lines, x, y, -1, 1)
}
}
mas := 0
// mas
for x := 1; x <= width-2; x++ {
for y := 1; y <= height-2; y++ {
mas += check2(lines, x, y)
}
}
printf("Part 1: %v\n", score)
printf("Part 2: %v\n", mas)
}