-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
155 lines (135 loc) · 2.5 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
func fs1(input io.Reader) (int, error) {
const (
nrows = 6
ncols = 50
)
grid := make([][]bool, nrows)
for i := range grid {
grid[i] = make([]bool, ncols)
}
scanner := bufio.NewScanner(input)
for scanner.Scan() {
s := scanner.Text()
if s[1] == 'e' {
// Rectangle
x := strings.Index(s, "x")
cols, err := strconv.Atoi(s[5:x])
if err != nil {
return 0, err
}
rows, err := strconv.Atoi(s[x+1:])
if err != nil {
return 0, err
}
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
grid[row][col] = true
}
}
} else {
idx := indexAll(s, " ")
if s[7] == 'c' {
// Rotate column
x, err := strconv.Atoi(s[16:idx[2]])
if err != nil {
return 0, err
}
by, err := strconv.Atoi(s[idx[3]+1:])
if err != nil {
return 0, err
}
rotateCol(grid, x, by)
} else {
// Rotate row
y, err := strconv.Atoi(s[13:idx[2]])
if err != nil {
return 0, err
}
by, err := strconv.Atoi(s[idx[3]+1:])
if err != nil {
return 0, err
}
rotateRow(grid, y, by)
}
}
}
sum := 0
for row := 0; row < nrows; row++ {
for col := 0; col < ncols; col++ {
if grid[row][col] {
sum++
fmt.Printf("#")
} else {
fmt.Printf(".")
}
}
fmt.Println()
}
return sum, nil
}
func rotateRow(grid [][]bool, y, by int) {
row := copyRow(grid[y])
for i := 0; i < len(row); i++ {
row[i] = grid[y][mod(i-by, len(row))]
}
grid[y] = row
}
func copyRow(s []bool) []bool {
res := make([]bool, len(s))
for i, v := range s {
res[i] = v
}
return res
}
func rotateCol(grid [][]bool, x, by int) {
col := copyCol(x, grid)
for i := 0; i < len(grid); i++ {
col[i] = grid[mod(i-by, len(grid))][x]
}
for i := 0; i < len(grid); i++ {
grid[i][x] = col[i]
}
}
func copyCol(col int, grid [][]bool) []bool {
res := make([]bool, len(grid))
for i := 0; i < len(grid); i++ {
res[i] = grid[i][col]
}
return res
}
func mod(d, m int) int {
res := d % m
if (res < 0 && m > 0) || (res > 0 && m < 0) {
return res + m
}
return res
}
func indexAll(s string, search string) []int {
i := 0
var res []int
for i < len(s) {
index := strings.Index(s[i:], search)
if index == -1 {
return res
}
res = append(res, index+i)
i += index + len(search)
}
return res
}
func fs2(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
_ = line
}
return 42, nil
}