55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
// Functions for operating on polymers from day 5.
|
|
package polymer
|
|
|
|
import (
|
|
"bytes"
|
|
"unicode"
|
|
)
|
|
|
|
func ApplyReactions(data []byte) []byte {
|
|
result := data
|
|
changed := true
|
|
|
|
for changed {
|
|
result, changed = React(result)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// React finds substrings of the form "xX" or "Xx" and removes them, returning
|
|
// the resulting bytes.
|
|
func React(data []byte) ([]byte, bool) {
|
|
result := bytes.Buffer{}
|
|
changed := false
|
|
|
|
for i := 0; i < len(data); i++ {
|
|
if i < len(data)-1 &&
|
|
data[i] != data[i+1] &&
|
|
string(bytes.ToLower([]byte{data[i]})) == string(bytes.ToLower([]byte{data[i+1]})) {
|
|
|
|
// we've found a reduction; skip over it without writing
|
|
// return append(data[:i], data[i+2:]...), true
|
|
i++
|
|
changed = true
|
|
continue
|
|
}
|
|
|
|
result.WriteByte(data[i])
|
|
}
|
|
|
|
// No changes possible.
|
|
return result.Bytes(), changed
|
|
}
|
|
|
|
// StripElement removes all instances of letter or its upper-case counterpart
|
|
// from the input.
|
|
func StripElement(data []byte, strip rune) []byte {
|
|
return bytes.Map(func(r rune) rune {
|
|
if r == strip || r == unicode.ToUpper(strip) {
|
|
return -1
|
|
}
|
|
return r
|
|
}, data)
|
|
}
|