diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2016-08-22 22:43:59 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2016-08-22 22:43:59 +0200 |
commit | 6000340883ce93af385891913b479597ddf5200d (patch) | |
tree | bcc904768eca6b857485c55770ebea70af1e392e | |
parent | 8eea0d085fd5352496328aa87c120df4e58dd7c8 (diff) |
-rw-r--r-- | main.go | 33 |
1 files changed, 19 insertions, 14 deletions
@@ -10,29 +10,32 @@ import ( "strings" ) -type List map[int]int +type List struct { + Map map[int]int + Homo int +} -func (l List) Insert(n int) { - l[n]++ +func (l *List) Insert(n int) { + if l.Map[n]++; l.Map[n] > 1 { + l.Homo++ + } } -func (l List) Delete(n int) { - if l[n]--; l[n] <= 0 { - delete(l, n) +func (l *List) Delete(n int) { + if l.Map[n] > 1 { + l.Homo-- + } + if l.Map[n]--; l.Map[n] <= 0 { + delete(l.Map, n) } } func (l List) IsHomo() bool { - for _, v := range l { - if v > 1 { - return true - } - } - return false + return l.Homo > 0 } func (l List) IsHetero() bool { - return len(l) > 1 + return len(l.Map) > 1 } func Split(s string) (string, int) { @@ -58,7 +61,9 @@ func (l List) Kind() string { func Homo(r io.Reader, w io.Writer) { scanner := bufio.NewScanner(r) scanner.Scan() // eat # of cases - l := make(List) + l := List{ + Map: make(map[int]int), + } for scanner.Scan() { s, n := Split(scanner.Text()) switch s { |