-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.go
52 lines (40 loc) · 1.22 KB
/
day05.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
package day05
import (
"fmt"
"github.com/doniacld/adventofcode/internal/filereader"
"github.com/doniacld/adventofcode/puzzles/2020/05/seat"
"github.com/doniacld/adventofcode/puzzles/solver"
)
func New(fileName string) solver.Solver {
return day05{fileName: fileName}
}
type day05 struct {
fileName string
}
func (d day05) Solve() (string, error) {
seats, err := filereader.ExtractString(d.fileName)
if err != nil {
return "", err
}
maxSeatId, missingSeatId := retrieveMaxAndMissingSeatIds(seats)
out := fmt.Sprintf("max seatId: %d & missing seatId: %d", maxSeatId, missingSeatId)
return out, nil
}
// retrieveMaxAndMissingSeatIds browses all seatsId and retrieves the max seatId value
// and the missing seatId
func retrieveMaxAndMissingSeatIds(seats []string) (int, int) {
var max, seatId int
seenSeatIds := make(map[int]struct{}, 0)
for _, s := range seats {
bp := seat.RetrieveBoardingPass(s)
seatId = seat.ComputeSeatId(bp)
// fill the map with all seatIds
seenSeatIds[seatId] = struct{}{}
// update the max
max = seat.MaxSeatId(max, seatId)
}
// retrieve the missingSeatId
allSeatIds := seat.CreateAllSeatIds()
missingSeatId := seat.GetMissingSeatId(allSeatIds, seenSeatIds)
return max, missingSeatId
}