-
Notifications
You must be signed in to change notification settings - Fork 0
/
elections.go
124 lines (99 loc) · 2.87 KB
/
elections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright 2012 Borys Piddubnyi <zhu@zhu.org.ua>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"path"
"strconv"
)
type config struct {
verbose bool
help bool
precision int
path string
}
var cfg config
const (
defaultPrecision = 1
defaultPrePath = "results"
)
func main() {
// Flag parsing
defaultPath := path.Join(defaultPrePath, "<precision>")
flag.BoolVar(&cfg.verbose, "verbose", false, "verbose mode")
flag.BoolVar(&cfg.help, "help", false, "print this help")
flag.IntVar(&cfg.precision, "precision", defaultPrecision, "calculation precision (decimal places)")
flag.StringVar(&cfg.path, "path", defaultPath, "path where to save results")
flag.Parse()
if cfg.help {
fmt.Printf("Usage: ./elections [options]\n")
fmt.Printf("Options:\n")
flag.PrintDefaults()
return
}
if cfg.precision < 0 {
fmt.Printf("Precision should be greater or equal zero\n")
return
}
if cfg.path == defaultPath {
cfg.path = path.Join(defaultPrePath, strconv.Itoa(cfg.precision))
}
// Receiving the info
regions, err := parseRegions()
if err != nil {
fmt.Printf("Failed to get regions info: %v\n", err)
return
}
resultMap := make(map[string]map[string]map[float64]float64)
// Calculations
countryPartyMap := make(map[string]map[float64]float64)
resultMap["Україна"] = countryPartyMap
for _, region := range regions {
regionPartyMap := make(map[string]map[float64]float64)
resultMap[region.Name] = regionPartyMap
for _, dist := range region.Districts {
for _, prec := range dist.Precincts {
// Omitting few precincts with no voters voted
if prec.VotersVoted == 0 {
continue
}
for party, result := range prec.Parties {
rpM := regionPartyMap[party]
cpM := countryPartyMap[party]
if rpM == nil {
b := make(map[float64]float64)
rpM = b
regionPartyMap[party] = rpM
}
if cpM == nil {
b := make(map[float64]float64)
cpM = b
countryPartyMap[party] = cpM
}
resultRound := round(result, cfg.precision)
rpM[resultRound]++
cpM[resultRound]++
}
}
}
}
// Save results
for region, regionMap := range resultMap {
for party, partyMap := range regionMap {
partyMapToPlot(partyMap, party, region, &cfg)
partyMapToCsv(partyMap, party, region, &cfg)
}
}
}