Last active
August 29, 2015 14:19
-
-
Save DeedleFake/5de867bac4face7b6c18 to your computer and use it in GitHub Desktop.
Counts how many times an outfit tag appears.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"container/heap" | |
"fmt" | |
"io" | |
"os" | |
"regexp" | |
) | |
type Outfit struct { | |
Tag string | |
Done int | |
} | |
func (o Outfit) String() string { | |
var buf bytes.Buffer | |
switch o.Tag { | |
case "": | |
buf.WriteString("Unaffiliated ") | |
default: | |
buf.WriteByte('[') | |
buf.WriteString(o.Tag) | |
buf.WriteString("] ") | |
} | |
buf.WriteByte('(') | |
fmt.Fprintf(&buf, "%v", o.Done) | |
buf.WriteByte(')') | |
return buf.String() | |
} | |
type OutfitHeap []Outfit | |
func (oh OutfitHeap) Len() int { | |
return len(oh) | |
} | |
func (oh OutfitHeap) Swap(i1, i2 int) { | |
oh[i1], oh[i2] = oh[i2], oh[i1] | |
} | |
func (oh OutfitHeap) Less(i1, i2 int) bool { | |
return oh[i1].Done > oh[i2].Done | |
} | |
func (oh *OutfitHeap) Push(v interface{}) { | |
*oh = append(*oh, v.(Outfit)) | |
} | |
func (oh *OutfitHeap) Pop() (r interface{}) { | |
r = (*oh)[len(*oh)-1] | |
*oh = (*oh)[:len(*oh)-1] | |
return | |
} | |
func main() { | |
outfits := make(map[string]int) | |
re := regexp.MustCompile(`\[([a-zA-Z0-9]+)\]`) | |
r := bufio.NewReader(os.Stdin) | |
for { | |
line, err := r.ReadString('\n') | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
panic(err) | |
} | |
matches := re.FindStringSubmatch(line) | |
if len(matches) < 2 { | |
outfits[""]++ | |
continue | |
} | |
outfits[matches[1]]++ | |
} | |
oh := make(OutfitHeap, 0, len(outfits)) | |
for o, r := range outfits { | |
heap.Push(&oh, Outfit{ | |
Tag: o, | |
Done: r, | |
}) | |
} | |
fmt.Printf("Total: %v outfits (and unaffiliated)\n", oh.Len()) | |
prev := 0 | |
for i := 1; oh.Len() > 0; i++ { | |
o := heap.Pop(&oh).(Outfit) | |
if o.Done == prev { | |
i-- | |
} | |
fmt.Printf("#%v: %v\n", i, o) | |
prev = o.Done | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Total: 130 outfits (and unaffiliated) | |
#1: Unaffiliated (36) | |
#2: [LON3] (11) | |
#2: [0Hhh] (11) | |
#3: [MCY] (10) | |
#4: [SHOK] (9) | |
#5: [TIW] (8) | |
#5: [ABTF] (8) | |
#5: [QRY] (8) | |
#6: [xPWR] (6) | |
#6: [KAIN] (6) | |
#7: [WASP] (5) | |
#7: [OO] (5) | |
#7: [BAX] (5) | |
#7: [MAIR] (5) | |
#7: [TD42] (5) | |
#7: [BLHR] (5) | |
#7: [PREY] (5) | |
#7: [252V] (5) | |
#7: [PONO] (5) | |
#8: [JEST] (4) | |
#8: [RX8l] (4) | |
#8: [CNOH] (4) | |
#8: [HYUN] (4) | |
#8: [ORBS] (4) | |
#8: [F1S7] (4) | |
#9: [MACS] (3) | |
#9: [RSNC] (3) | |
#9: [FHM] (3) | |
#9: [I3FS] (3) | |
#9: [SWAG] (3) | |
#9: [11C] (3) | |
#9: [R3K] (3) | |
#10: [RX81] (2) | |
#10: [XVK] (2) | |
#10: [WTAC] (2) | |
#10: [B4ND] (2) | |
#10: [Lacy] (2) | |
#10: [UKIP] (2) | |
#10: [FFS] (2) | |
#10: [IB] (2) | |
#10: [AE] (2) | |
#10: [SG] (2) | |
#10: [C15O] (2) | |
#10: [666] (2) | |
#10: [PXEL] (2) | |
#10: [JNJ] (2) | |
#10: [NRI] (2) | |
#10: [1RPC] (2) | |
#10: [S4LT] (2) | |
#10: [REBR] (2) | |
#10: [X] (2) | |
#10: [RCN6] (2) | |
#11: [ANGC] (1) | |
#11: [rU5H] (1) | |
#11: [3KDC] (1) | |
#11: [3DC3] (1) | |
#11: [GUT] (1) | |
#11: [UhOh] (1) | |
#11: [GBX] (1) | |
#11: [RAP1] (1) | |
#11: [JEWS] (1) | |
#11: [iHoP] (1) | |
#11: [SAvg] (1) | |
#11: [RATL] (1) | |
#11: [DIVI] (1) | |
#11: [GMYT] (1) | |
#11: [COBR] (1) | |
#11: [7FC] (1) | |
#11: [qp] (1) | |
#11: [RE4] (1) | |
#11: [AXTM] (1) | |
#11: [BL] (1) | |
#11: [Mako] (1) | |
#11: [TG] (1) | |
#11: [F1W5] (1) | |
#11: [D117] (1) | |
#11: [M0M] (1) | |
#11: [SHT] (1) | |
#11: [C00K] (1) | |
#11: [SKWL] (1) | |
#11: [CTAB] (1) | |
#11: [ADK] (1) | |
#11: [SBW] (1) | |
#11: [NC1S] (1) | |
#11: [UxB] (1) | |
#11: [GE7R] (1) | |
#11: [VALM] (1) | |
#11: [TIP] (1) | |
#11: [RlP] (1) | |
#11: [AVIA] (1) | |
#11: [PSHU] (1) | |
#11: [PL13] (1) | |
#11: [ZMAJ] (1) | |
#11: [L0L0] (1) | |
#11: [DIQ] (1) | |
#11: [709] (1) | |
#11: [CLOG] (1) | |
#11: [CSTB] (1) | |
#11: [DREV] (1) | |
#11: [SCRM] (1) | |
#11: [BADN] (1) | |
#11: [CASH] (1) | |
#11: [GCOA] (1) | |
#11: [GGUD] (1) | |
#11: [MB0] (1) | |
#11: [ACR0] (1) | |
#11: [MCB] (1) | |
#11: [0SEF] (1) | |
#11: [RHoA] (1) | |
#11: [LYB] (1) | |
#11: [BRCK] (1) | |
#11: [TFL] (1) | |
#11: [NCIv] (1) | |
#11: [NFMS] (1) | |
#11: [TAS] (1) | |
#11: [4ACE] (1) | |
#11: [1TRY] (1) | |
#11: [XEPD] (1) | |
#11: [418] (1) | |
#11: [PotP] (1) | |
#11: [NEWS] (1) | |
#11: [KORE] (1) | |
#11: [BO0T] (1) | |
#11: [GNwW] (1) | |
#11: [WGB] (1) | |
#11: [ALAY] (1) | |
#11: [CNCR] (1) | |
#11: [T0T] (1) | |
#11: [Fite] (1) | |
#11: [PAFF] (1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment