forked from pointfreeco/episode-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
152 lines (135 loc) · 1.99 KB
/
Contents.swift
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
import Parsing
let string = String(repeating: "A", count: 10_000)
let substring = string.dropFirst(10).dropLast(10)
"café"
"\u{00E9}"
"e\u{0301}"
"\u{00E9}" == "e\u{0301}"
"\u{00E9}".utf8
"e\u{0301}".utf8
Array("\u{00E9}".utf8)
Array("e\u{0301}".utf8)
"\u{00E9}".utf8.elementsEqual("e\u{0301}".utf8)
"\u{00E9}".elementsEqual("e\u{0301}")
struct Dot {
let x, y: Int
}
enum Direction: String, CaseIterable {
case x, y
}
struct Fold {
let direction: Direction
let position: Int
}
struct Instructions {
let dots: [Dot]
let folds: [Fold]
}
let dotTuple = ParsePrint {
Digits()
","
Digits()
}
try dotTuple.print((3, 1))
let dot = ParsePrint(.memberwise(Dot.init)) {
Digits()
","
Digits()
}
try dot.print(Dot(x: 3, y: 1))
do {
try dot.parse("6,10")
} catch {
print(error)
}
let dots = Many {
dot
} separator: {
"\n"
}
try dots.parse("""
6,10
0,14
9,10
0,3
""")
try dots.print([
Dot(x: 3, y: 1),
Dot(x: 2, y: 0),
Dot(x: 1, y: 4),
])
// fold ➡️ y=7
let fold = ParsePrint(.memberwise(Fold.init)) {
"fold ➡️ "
Direction.parser()
"="
Digits()
}
do {
try fold.parse("fold ➡️ y=7")
} catch {
print(error)
}
try fold.print(Fold(direction: .x, position: 5))
let folds = Many {
fold
} separator: {
"\n"
}
let instructions = ParsePrint(.memberwise(Instructions.init)) {
dots
"\n\n"
folds
}
do {
let dot = ParsePrint(.memberwise(Dot.init)) {
Digits()
",".utf8
Digits()
}
let dots = Many {
dot
} separator: {
"\n".utf8
}
let fold = ParsePrint(.memberwise(Fold.init)) {
"fold ➡️ ".utf8
Direction.parser()
"=".utf8
Digits()
}
let folds = Many {
fold
} separator: {
"\n".utf8
}
let instructions = ParsePrint(.memberwise(Instructions.init)) {
dots
"\n\n".utf8
folds
}
}
let input = """
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold ➡️ y=7
fold ➡️ x=5
"""
let output = try instructions.parse(input)
try instructions.print(output) == input