-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (64 loc) · 1.24 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
package main
import (
"fmt"
"log"
"os"
"slices"
"strings"
)
func off(grid1, grid2 []string) int {
off := 0
A:
for i := 0; i < len(grid1); i++ {
for j := 0; j < len(grid1[i]); j++ {
if grid1[i][j] != grid2[i][j] {
off++
if off == 2 {
break A
}
}
}
}
return off
}
func pointOfIncidence(grid []string, smudge int) int {
for row := 1; row < len(grid); row++ {
above := slices.Clone(grid[:row])
slices.Reverse(above)
below := grid[row:]
k := min(len(above), len(below))
above = above[:k]
if off(above, below) == smudge {
return row
}
}
return 0
}
func transpose(grid []string) []string {
tr := make([]string, len(grid[0]))
for i := 0; i < len(grid[0]); i++ {
s := make([]byte, len(grid))
for j := 0; j < len(grid); j++ {
s[j] = grid[j][i]
}
tr[i] = string(s)
}
return tr
}
func solveWith(smudge int) {
raw, err := os.ReadFile("input")
if err != nil {
log.Fatal(err)
}
chunks := strings.Split(strings.TrimSpace(string(raw)), "\n\n")
sum := 0
for _, chunk := range chunks {
grid := strings.Split(chunk, "\n")
sum += 100*pointOfIncidence(grid, smudge) + pointOfIncidence(transpose(grid), smudge)
}
fmt.Println(sum)
}
func main() {
solveWith(0) // 35210
solveWith(1) // 31974
}