-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (75 loc) · 1.57 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
package main
import (
"fmt"
"log"
"strings"
"github.com/jdrst/adventofgo/util"
)
type point struct {
x, y int
}
func main() {
day13(util.ReadFile("input.txt"))
}
func day13(file util.File) {
parts := strings.Split(string(file), util.NewLine()+util.NewLine())
lines := strings.Split(parts[0], util.NewLine())
folds := strings.Split(parts[1], util.NewLine())
points := map[point]bool{}
for _, l := range lines {
crd := util.Line(l).SubSplitWith(",").AsInts()
points[point{crd[0], crd[1]}] = true
}
var axis string
var pos, lastX, lastY int
for i, f := range folds {
fmt.Sscanf(f, "fold along %1s=%d", &axis, &pos)
switch axis {
case "x":
lastX = pos
case "y":
lastY = pos
}
foldAlong(axis, pos, points)
if i == 0 {
fmt.Printf("First part: %v\n", len(points))
}
}
fmt.Println("Second part:")
for i := 0; i < lastY; i++ {
for j := 0; j < lastX; j++ {
if _, exists := points[point{j, i}]; exists {
fmt.Printf("%v", "██")
} else {
fmt.Printf("%v", " ")
}
}
fmt.Printf("\n")
}
}
func foldAlong(axis string, pos int, points map[point]bool) {
switch axis {
case "x":
foldAlongX(pos, points)
case "y":
foldAlongY(pos, points)
default:
log.Fatal("can't fold")
}
}
func foldAlongX(pos int, points map[point]bool) {
for p := range points {
if p.x > pos {
delete(points, p)
points[point{x: pos - (p.x - pos), y: p.y}] = true
}
}
}
func foldAlongY(pos int, points map[point]bool) {
for p := range points {
if p.y > pos {
delete(points, p)
points[point{x: p.x, y: pos - (p.y - pos)}] = true
}
}
}