-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.go
More file actions
84 lines (64 loc) · 1.48 KB
/
day13.go
File metadata and controls
84 lines (64 loc) · 1.48 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
package main
import (
"mathutil"
"os"
"panic"
"readers"
"strconv"
"strings"
)
func Day13Part1() int {
file, err := os.Open("assets/day13.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
display := ProcessLines(lines, true)
return len(display)
}
func Day13Part2() int {
file, err := os.Open("assets/day13.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
display := ProcessLines(lines, false)
display.Print()
return len(display)
}
func ProcessLines(lines []string, breakOnFirstFold bool) mathutil.PointMap {
display := make(mathutil.PointMap)
for _, line := range lines {
if line == "" {
continue
}
if strings.Contains(line, ",") {
coordinates := strings.Split(line, ",")
x, err := strconv.Atoi(coordinates[0])
panic.Check(err)
y, err := strconv.Atoi(coordinates[1])
panic.Check(err)
display[mathutil.Point{X: x, Y: y}] = struct{}{}
continue
} else if strings.Contains(line, "fold along x=") {
coordinate := strings.Replace(line, "fold along x=", "", 1)
x, err := strconv.Atoi(coordinate)
panic.Check(err)
display.FoldX(x)
if breakOnFirstFold {
break
} else {
continue
}
} else if strings.Contains(line, "fold along y=") {
coordinate := strings.Replace(line, "fold along y=", "", 1)
y, err := strconv.Atoi(coordinate)
panic.Check(err)
display.FoldY(y)
if breakOnFirstFold {
break
} else {
continue
}
}
}
return display
}