From fcc61d7d53b551e8ae152a9ce4c3786949c17101 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 22 Aug 2016 21:56:01 +0200 Subject: Simplify --- main.go | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index bfbdc7c..d475ac3 100644 --- a/main.go +++ b/main.go @@ -6,39 +6,25 @@ import ( "bufio" "io" "os" - "runtime/pprof" "strconv" "strings" ) -type List struct { - Data []int - Map map[int]int -} +type List map[int]int -func (l *List) Insert(n int) { - l.Data = append(l.Data, n) - l.Map[n]++ +func (l List) Insert(n int) { + l[n]++ } -func (l *List) Delete(n int) { - for i, v := range l.Data { - if v == n { - l.Data = append(l.Data[:i], l.Data[i+1:]...) - switch l.Map[n] { - case 1: - delete(l.Map, n) - default: - l.Map[n]-- - } - return - } +func (l List) Delete(n int) { + if l[n]--; l[n] <= 0 { + delete(l, n) } } func (l List) IsHomo() bool { var top int - for _, v := range l.Map { + for _, v := range l { if v > top { top = v } @@ -47,7 +33,7 @@ func (l List) IsHomo() bool { } func (l List) IsHetero() bool { - return len(l.Map) > 1 + return len(l) > 1 } func Split(s string) (string, int) { @@ -72,13 +58,9 @@ func (l List) String() string { func Homo(r io.Reader, w io.Writer) { scanner := bufio.NewScanner(r) - scanner.Scan() - n, _ := strconv.Atoi(scanner.Text()) + scanner.Scan() // eat # of cases - l := List{ - Data: make([]int, 0, n), - Map: make(map[int]int), - } + l := make(List) for scanner.Scan() { s, n := Split(scanner.Text()) @@ -94,8 +76,5 @@ func Homo(r io.Reader, w io.Writer) { } func main() { - f, _ := os.Create("cpu.out") - pprof.StartCPUProfile(f) - defer pprof.StopCPUProfile() Homo(os.Stdin, os.Stdout) } -- cgit v1.2.3