generated from Schroedinger-Hat/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (170 loc) · 4.3 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type proposal struct {
name string
days []int
}
func (i proposal) Title() string { return i.name }
func (i proposal) Description() string {
resultString := ""
for index, value := range i.days {
resultString += strconv.Itoa(value)
if index < len(i.days)-1 {
resultString += ","
}
}
return resultString
}
func (i proposal) FilterValue() string { return i.name }
type preferredDay struct {
day int
excluded []proposal
}
func (i preferredDay) Title() string { return strconv.Itoa(i.day) }
func (i preferredDay) Description() string {
resultString := ""
for index, value := range i.excluded {
resultString += value.name
if index < len(i.excluded)-1 {
resultString += ","
}
}
return resultString
}
func (i preferredDay) FilterValue() string { return strconv.Itoa(i.day) }
type model struct {
list list.Model
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
return docStyle.Render(m.list.View())
}
var docStyle = lipgloss.NewStyle().Margin(1, 2)
func main() {
proposals := parseInputFile("input.txt")
m := model{list: list.New(proposals, list.NewDefaultDelegate(), 0, 0)}
m.list.Title = "List of persons with day proposal"
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
preferredDays := buildPreferredDays(proposals)
m = model{list: list.New(preferredDays, list.NewDefaultDelegate(), 0, 0)}
m.list.Title = "List of preferred day with excluded persons"
p = tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
func buildPreferredDays(items []list.Item) []list.Item {
var preferredDays []list.Item
sortedByKey := sortKeyByValue(countDays(items))
for _, actualDay := range sortedByKey {
preferredDay := *new(preferredDay)
preferredDay.day = actualDay
for _, item := range items {
isIt := isExcluded(actualDay, item.(proposal).days)
if isIt {
preferredDay.excluded = append(preferredDay.excluded, item.(proposal))
}
}
preferredDays = append(preferredDays, preferredDay)
}
return preferredDays
}
func parseInputFile(input string) []list.Item {
file, err := os.Open(input)
if err != nil {
fmt.Println("Error opening file:", err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
var persons []list.Item
for scanner.Scan() {
persons = extractPerson(scanner, persons)
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
return persons
}
func extractPerson(scanner *bufio.Scanner, persons []list.Item) []list.Item {
splitted := strings.SplitN(scanner.Text(), ":", 2)
person := *new(proposal)
person.name = splitted[0]
days := strings.ReplaceAll(splitted[1], " ", "")
for _, rawDay := range strings.Split(days, ",") {
day, _ := strconv.Atoi(rawDay)
person.days = append(person.days, day)
}
persons = append(persons, person)
return persons
}
func sortKeyByValue(input map[int]int) []int {
type KeyValue struct {
Key int
Value int
}
var keyValues []KeyValue
for key, value := range input {
keyValues = append(keyValues, KeyValue{Key: key, Value: value})
}
sort.Slice(keyValues, func(i, j int) bool {
return keyValues[i].Value > keyValues[j].Value
})
var output []int
for _, keyValue := range keyValues {
output = append(output, keyValue.Key)
}
return output
}
func countDays(items []list.Item) map[int]int {
dayCounter := make(map[int]int)
for _, item := range items {
for _, day := range item.(proposal).days {
if dayCounter[day] == 0 {
dayCounter[day] = 1
} else {
dayCounter[day] += 1
}
}
}
return dayCounter
}
func isExcluded(thisDay int, days []int) bool {
for _, day := range days {
if day == thisDay {
return false
}
}
return true
}