-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
154 lines (134 loc) · 2.65 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
package main
import (
"fmt"
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
paper := toPaper(input)
return paper.folds(false)
}
func (p *Paper) folds(all bool) int {
for _, f := range p.fold {
if f.Col == 0 {
// Horizontal
row := f.Row
for col := 0; col < len(p.grid[0]); col++ {
p.grid[row][col] = false
}
for col := 0; col < len(p.grid[0]); col++ {
for deltaRow := 1; ; deltaRow++ {
upRow := row - deltaRow
if upRow < 0 {
break
}
downRow := row + deltaRow
p.grid[upRow][col] = p.grid[upRow][col] || p.grid[downRow][col]
}
}
p.grid = p.grid[:row]
} else {
// Vertical
col := f.Col
for row := 0; row < len(p.grid); row++ {
p.grid[row][col] = false
}
for row := 0; row < len(p.grid); row++ {
for deltaCol := 1; ; deltaCol++ {
leftCol := col - deltaCol
if leftCol < 0 {
break
}
rightCol := col + deltaCol
p.grid[row][leftCol] = p.grid[row][leftCol] || p.grid[row][rightCol]
}
}
for row := 0; row < len(p.grid); row++ {
p.grid[row] = p.grid[row][:col]
}
}
if !all {
break
}
}
if all {
fmt.Println(p)
}
sum := 0
for row := 0; row < len(p.grid); row++ {
for col := 0; col < len(p.grid[0]); col++ {
if p.grid[row][col] {
sum++
}
}
}
return sum
}
func toPaper(input io.Reader) *Paper {
lines := aoc.ReaderToStrings(input)
groups := aoc.StringGroups(lines)
var dots []aoc.Position
maxRow := aoc.NewMaxer()
maxCol := aoc.NewMaxer()
for _, line := range groups[0] {
del := aoc.NewDelimiter(line, ",")
col := del.GetInt(0)
row := del.GetInt(1)
dots = append(dots, aoc.Position{
Col: col,
Row: row,
})
maxRow.Add(row)
maxCol.Add(col)
}
var fold []aoc.Position
for _, line := range groups[1] {
del := aoc.NewDelimiter(line, " ")
s := del.GetString(2)
v := aoc.StringToInt(s[2:])
if s[0] == 'y' {
fold = append(fold, aoc.Position{
Col: 0,
Row: v,
})
} else {
fold = append(fold, aoc.Position{
Col: v,
Row: 0,
})
}
}
grid := make([][]bool, maxRow.Get()+1)
for row := 0; row <= maxRow.Get(); row++ {
grid[row] = make([]bool, maxCol.Get()+1)
}
for _, dot := range dots {
grid[dot.Row][dot.Col] = true
}
return &Paper{
grid: grid,
fold: fold,
}
}
type Paper struct {
grid [][]bool
fold []aoc.Position
}
func (p *Paper) String() string {
s := ""
for row := 0; row < len(p.grid); row++ {
for col := 0; col < len(p.grid[0]); col++ {
if p.grid[row][col] {
s += "#"
} else {
s += "."
}
}
s += "\n"
}
return s
}
func fs2(input io.Reader) int {
paper := toPaper(input)
return paper.folds(true)
}