-
Notifications
You must be signed in to change notification settings - Fork 0
/
incomplete_day06.sed
executable file
·124 lines (112 loc) · 2.14 KB
/
incomplete_day06.sed
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
#!/bin/sed -nf
# NOTE: this is way too slow! It would take at least 12 hours to run
# on part 1, and thus I haven't checked if it's correct.
#
# There is only one line, which makes things easy.
# Convert numbers to tallies, and tabs to a space to save us \t messes, and put
# a space at the beginning for consistency (i.e. we will use space as the start
# of a number)
s/\t/ /g
s/^/ /
:starttally
s/\(^\| \)\(|*\)0/\1\2x/g
s/\(^\| \)\(|*\)1/\1\2x|/g
s/\(^\| \)\(|*\)2/\1\2x||/g
s/\(^\| \)\(|*\)3/\1\2x|||/g
s/\(^\| \)\(|*\)4/\1\2x||||/g
s/\(^\| \)\(|*\)5/\1\2x|||||/g
s/\(^\| \)\(|*\)6/\1\2x||||||/g
s/\(^\| \)\(|*\)7/\1\2x|||||||/g
s/\(^\| \)\(|*\)8/\1\2x||||||||/g
s/\(^\| \)\(|*\)9/\1\2x|||||||||/g
s/x//g
s/\(^\| \)\(|*\)\([0-9]\)/\1\2\2\2\2\2\2\2\2\2\2\3/g
/[0-9]/ b starttally
h
:mainloop
# Find the largest grouping
:startlargest
s/\([ a]\)|/\1A/g
s/A/a/g
# If there are multiple groups with tallies, and not all just one, keep going.
/||[^|]\+|/ b startlargest
/|[^|]\+||/ b startlargest
/|[^|]\+|/ {
# If it's a tie, grab the first one and use that.
s/|/+/
s/|/a/g
s/+/|/
}
:startconvert
s/a|/||/
t startconvert
s/|/+/g
s/a/|/g
# Redistribute those +s.
:distribute0
:distribute1
s/+ \(\(|*x \)*\)\(|*\)\( \|$\)/ \1\3x\4/
t distribute1
:distribute2
s/^\(\( |*x\)*\) \(|*\)\(\( |*\)*\) \(a*\)+/\1 \3x\4 \6/
t distribute2
s/x\( \|$\)/|\1/g
s/+/a/g
t distribute0
s/a/|/g
# There shouldn't be more +s.
/+/ {
p
s/^.*$/FAIL/
p
b
}
# Hold the new line, and check if there are dupes
H
x
# Store as a number to keep hold space smaller.
s/$/)/
:startcompress
s/||||||||||/+/g
s/|||||||||/9/
s/||||||||/8/
s/|||||||/7/
s/||||||/6/
s/|||||/5/
s/||||/4/
s/|||/3/
s/||/2/
s/|/1/
s/\(+\|^\))/\10)/
s/\([0-9]\))/)\1/
s/+/|/g
/|/ b startcompress
s/)//
/\(^\|\n\)\([^\n]*\)\n\(.*\n\)\?\2\($\|\n\)/ b end
x
p
b mainloop
:end
# Delete non-newlines, newlines to tallies, add 1 (for trailing newline)
s/[^\n]//g
s/\n/|/g
# Now convert to a number.
s/$/)/
:startprint
s/||||||||||/+/g
s/|||||||||/9/
s/||||||||/8/
s/|||||||/7/
s/||||||/6/
s/|||||/5/
s/||||/4/
s/|||/3/
s/||/2/
s/|/1/
s/\(+\|^\))/\10)/
s/\([0-9]\))/)\1/
s/+/|/g
/|/ b startprint
s/)//
# And print!
p