-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.go
118 lines (108 loc) · 2.51 KB
/
day13.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
package main
import (
"fmt"
)
func day13() {
lines := getLines("input/13.txt")
lineCount, lineCount2 := 0, 0
start := 0
for i := 0; i < len(lines); i++ {
if len(lines[i]) == 0 {
pattern := lines[start:i]
i++
start = i
v, vok := verticalReflection(pattern, false)
h, hok := horizontalReflection(pattern, false)
if vok {
lineCount += v
}
if hok {
lineCount += 100 * h
}
v2, vok2 := verticalReflection(pattern, true)
h2, hok2 := horizontalReflection(pattern, true)
if vok2 {
lineCount2 += v2
}
if hok2 {
lineCount2 += 100 * h2
}
}
}
var result = lineCount
var result2 = lineCount2
fmt.Println("Day 13 Part 1 Result: ", result)
fmt.Println("Day 13 Part 2 Result: ", result2)
}
func horizontalReflection(pattern []string, part2 bool) (int, bool) {
for row := 0; row < len(pattern)-1; row++ {
found := true
smudges := 0
for col := 0; col < len(pattern[0]); col++ {
if pattern[row][col] != pattern[row+1][col] {
smudges++
if smudges > 1 {
found = false
break
}
}
}
if found {
ok, alt := checkHorizontal(pattern, row, 1-smudges)
if (!part2 && ok && !alt) || (part2 && alt) {
return row + 1, true
}
}
}
return 0, false
}
func checkHorizontal(pattern []string, horLine, maxSmudge int) (bool, bool) {
smudges := 0
for row := horLine - 1; row >= 0 && (horLine+(horLine-row)+1) < len(pattern); row-- {
for col := 0; col < len(pattern[0]); col++ {
if pattern[row][col] != pattern[horLine+(horLine-row)+1][col] {
smudges++
if smudges > maxSmudge {
return false, false
}
}
}
}
return true, smudges == maxSmudge
}
func verticalReflection(pattern []string, part2 bool) (int, bool) {
for col := 0; col < len(pattern[0])-1; col++ {
found := true
smudges := 0
for row := 0; row < len(pattern); row++ {
if pattern[row][col] != pattern[row][col+1] {
smudges++
if smudges > 1 {
found = false
break
}
}
}
if found {
ok, alt := checkVertical(pattern, col, 1-smudges)
if (!part2 && ok && !alt) || (part2 && alt) {
return col + 1, true
}
}
}
return 0, false
}
func checkVertical(pattern []string, verLine, maxSmudge int) (bool, bool) {
smudges := 0
for col := verLine - 1; col >= 0 && (verLine+(verLine-col)+1) < len(pattern[0]); col-- {
for row := 0; row < len(pattern); row++ {
if pattern[row][col] != pattern[row][verLine+(verLine-col)+1] {
smudges++
if smudges > maxSmudge {
return false, false
}
}
}
}
return true, smudges == maxSmudge
}