-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
96 lines (84 loc) · 1.53 KB
/
main.go
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
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"bufio"
"io"
)
func fs1(input io.Reader, rows int) int {
scanner := bufio.NewScanner(input)
scanner.Scan()
s := scanner.Text()
cols := len(s)
grid := make([][]bool, rows)
for row := 0; row < rows; row++ {
grid[row] = make([]bool, cols)
}
sum := 0
for col := 0; col < cols; col++ {
if s[col] == '.' {
grid[0][col] = true
sum++
}
}
for row := 1; row < rows; row++ {
for col := 0; col < cols; col++ {
v := trap(grid[row-1], col)
if !v {
grid[row][col] = true
sum++
}
}
}
return sum
}
func trap(g []bool, col int) bool {
left := true
center := g[col]
right := true
if col > 0 {
left = g[col-1]
}
if col < len(g)-1 {
right = g[col+1]
}
return (!left && !center && right) ||
(!center && !right && left) ||
(!left && center && right) ||
(!right && center && left)
}
func fs2(input io.Reader, rows int) int {
scanner := bufio.NewScanner(input)
scanner.Scan()
s := scanner.Text()
cols := len(s)
grid := make([][]bool, rows)
for row := 0; row < rows; row++ {
grid[row] = make([]bool, cols)
}
sum := 0
for col := 0; col < cols; col++ {
if s[col] == '.' {
grid[0][col] = true
sum++
}
}
for row := 1; row < rows; row++ {
for col := 0; col < cols; col++ {
v := trap(grid[row-1], col)
if !v {
grid[row][col] = true
sum++
}
}
}
//for row := 0; row < rows; row++ {
// for col := 0; col < cols; col++ {
// if grid[row][col] {
// fmt.Print(".")
// } else {
// fmt.Print("^")
// }
// }
// fmt.Println()
//}
return sum
}