-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (92 loc) · 1.75 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "13", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type fold struct {
value int
isX bool
}
type pos struct {
x, y int
}
func parse(r io.Reader) (map[pos]bool, []fold) {
paper := make(map[pos]bool)
var folds []fold
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if strings.Index(line, ",") != -1 {
parts := strings.Split(line, ",")
x, err := strconv.Atoi(parts[0])
if err != nil {
panic(err)
}
y, err := strconv.Atoi(parts[1])
if err != nil {
panic(err)
}
paper[pos{x: x, y: y}] = true
} else if strings.Index(line, "=") != -1 {
parts := strings.Split(line, "=")
value, err := strconv.Atoi(parts[1])
if err != nil {
panic(err)
}
folds = append(folds, fold{
value: value,
isX: parts[0][len(parts[0])-1] == 'x',
})
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
return paper, folds
}
func solve(r io.Reader) {
paper, folds := parse(r)
performFold(paper, folds[0])
fmt.Println(len(paper))
}
func performFold(paper map[pos]bool, f fold) {
if f.isX {
for p := range paper {
if p.x < f.value {
continue
}
delete(paper, p)
paper[pos{
x: f.value - (p.x - f.value),
y: p.y,
}] = true
}
} else {
for p := range paper {
if p.y < f.value {
continue
}
delete(paper, p)
paper[pos{
x: p.x,
y: f.value - (p.y - f.value),
}] = true
}
}
}
func main() {
solve(strings.NewReader("6,10\n0,14\n9,10\n0,3\n10,4\n4,11\n6,0\n6,12\n4,1\n0,13\n10,12\n3,4\n3,0\n8,4\n1,10\n2,14\n8,10\n9,0\n\nfold along y=7\nfold along x=5"))
solve(input())
}