adventofcode/2018/day02-2.go

35 lines
891 B
Go
Raw Permalink Normal View History

// This is my second solution, c.f. https://github.com/annabunches/adventofcode/commit/3ca1a1068b749bab4d0626af32549ce2385d57bb
// for the original solution.
//
// This solution is far more elegant; it runs in O(n*m) time, instead of the original implementation's O(n^2 * m) time.
// Thanks to @lizthegrey for (probably unintentionally) prompting me to rewrite this >_>
2018-12-02 05:40:11 +00:00
package main
import (
"fmt"
"internal/util"
)
func main() {
ids := util.ReadInput()
subIDs := make(map[string]struct{})
2018-12-02 05:40:11 +00:00
for _, id := range ids {
// iterate over each letter
for i := range id {
// create a subID with just letter id[i] omitted
subID := id[0:i] + id[i+1:]
// if it's already in our map, we've found the correct output
if _, found := subIDs[subID]; found {
fmt.Println(subID)
2018-12-02 05:40:11 +00:00
return
}
// otherwise, add the substring to our map
subIDs[subID] = struct{}{}
2018-12-02 05:40:11 +00:00
}
}
}