-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (100 loc) · 2.03 KB
/
main.go
File metadata and controls
128 lines (100 loc) · 2.03 KB
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
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) {
dots := 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)
}
dots[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 dots, folds
}
func solve(r io.Reader) {
dots, folds := parse(r)
performFold(dots, folds[0])
fmt.Println(len(dots))
}
func getFunc(x bool) func(pos) int {
return func(p pos) int {
if x {
return p.x
}
return p.y
}
}
func setFunc(x bool) func(*pos, int) {
return func(p *pos, value int) {
if x {
p.x = value
} else {
p.y = value
}
}
}
func performFold(dots map[pos]bool, f fold) {
get, set := getFunc(f.isX), setFunc(f.isX)
for dot := range dots {
changingValue := get(dot)
// the fold is either left or up, and the dot is already to the left or above the fold line, so it would not move
if changingValue < f.value {
continue
}
// remove old dot
delete(dots, dot)
// update dot position
set(&dot, 2*f.value-changingValue)
// add new dot
dots[dot] = 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())
}