-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.go
177 lines (158 loc) · 3.26 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"github.com/dergeberl/aoc/utils"
"os"
"strconv"
"strings"
)
type point struct {
x, y int
}
type paper [][]bool
type fold struct {
axis string
value int
}
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay13Part1(string(input)))
fmt.Printf("Part 2: \n%v\n", SolveDay13Part2(string(input)))
}
//SolveDay13Part1 returns the number of dots after the first fold
func SolveDay13Part1(input string) int {
p, ins := parseInput(input)
if ins[0].axis == "x" {
p = foldX(p, ins[0].value)
}
if ins[0].axis == "y" {
p = foldY(p, ins[0].value)
}
return p.countDots()
}
//SolveDay13Part2 returns the dots as readable string
func SolveDay13Part2(input string) string {
p, ins := parseInput(input)
for i := range ins {
if ins[i].axis == "x" {
p = foldX(p, ins[i].value)
}
if ins[i].axis == "y" {
p = foldY(p, ins[i].value)
}
}
return p.toSting()
}
//foldX fold on the x-axis on a number and returns new paper
func foldX(p paper, v int) paper {
for x := 0; x < v; x++ {
fromX := (v * 2) - x
for y := 0; y < len(p[x]); y++ {
if fromX > len(p)-1 {
continue
}
if p[fromX][y] {
p[x][y] = true
}
}
}
return p[:v]
}
//foldY fold on the y-axis on a number and returns new paper
func foldY(p paper, v int) paper {
for y := 0; y < v; y++ {
fromY := (v * 2) - y
for x := 0; x < len(p); x++ {
if fromY > len(p[x])-1 {
continue
}
if p[x][fromY] {
p[x][y] = true
}
}
}
for x := range p {
p[x] = p[x][:v]
}
return p
}
//parseInput returns an initial paper and a list of folds for an input
func parseInput(input string) (paper, []fold) {
pointsInstruction := strings.Split(input, "\n\n")
if len(pointsInstruction) != 2 {
panic("wrong input")
}
points, _ := utils.InputToSlice(pointsInstruction[0])
poi := make([]point, len(points))
for i := range points {
tmpPoints := strings.Split(points[i], ",")
if len(tmpPoints) != 2 {
panic("wrong input")
}
x, _ := strconv.Atoi(tmpPoints[0])
y, _ := strconv.Atoi(tmpPoints[1])
poi[i] = point{x: x, y: y}
}
var sizeX, sizeY int
for i := range poi {
if poi[i].x > sizeX {
sizeX = poi[i].x
}
if poi[i].y > sizeY {
sizeY = poi[i].y
}
}
p := make([][]bool, sizeX+1)
for i := 0; i <= sizeX; i++ {
p[i] = make([]bool, sizeY+1)
}
for i := range poi {
p[poi[i].x][poi[i].y] = true
}
instructions, _ := utils.InputToSlice(pointsInstruction[1])
ins := make([]fold, len(instructions))
for i := range instructions {
tmpInstructions := strings.Split(instructions[i], "=")
if len(tmpInstructions) != 2 {
panic("wrong input")
}
value, _ := strconv.Atoi(tmpInstructions[1])
axis := "y"
if strings.HasSuffix(tmpInstructions[0], "x") {
axis = "x"
}
ins[i].value = value
ins[i].axis = axis
}
return p, ins
}
//countDots returns the number of dots in a paper
func (p paper) countDots() int {
var sum int
for x := range p {
for y := range p[x] {
if p[x][y] {
sum++
}
}
}
return sum
}
//toSting returns a printable string from a paper
func (p paper) toSting() string {
var out string
for y := range p[0] {
for x := range p {
if p[x][y] {
out += "#"
continue
}
out += "."
}
out += fmt.Sprintln("")
}
return out
}