Skip to content

Commit d07e084

Browse files
committed
Finished up day5 part 2
1 parent f07776b commit d07e084

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

day5.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"sort"
6+
)
47

58
// Day5Solution1 finding the max seat id
69
func Day5Solution1(input []string, done chan string) {
@@ -25,5 +28,25 @@ func Day5Solution1(input []string, done chan string) {
2528

2629
// Day5Solution2 WIP
2730
func Day5Solution2(input []string, done chan string) {
28-
done <- "part2"
31+
var seats []seatAssignment
32+
33+
for _, input := range input {
34+
seats = append(seats, makeSeatAssignment(input))
35+
}
36+
sort.SliceStable(seats, func(i int, j int) bool {
37+
return seats[i].id < seats[j].id
38+
})
39+
40+
lowestID := seats[0].id
41+
sumOfIDsUpToLowest := (lowestID * (lowestID + 1)) / 2
42+
highestID := seats[len(seats)-1].id
43+
sumOfIDsUpToHighest := (highestID * (highestID + 1)) / 2
44+
sumOfIdsShouldBe := sumOfIDsUpToHighest - sumOfIDsUpToLowest
45+
seatIDActualSum := 0
46+
for _, seat := range seats {
47+
seatIDActualSum += seat.id
48+
}
49+
indexOfSeatWithIDOneHigherThanMissingSeat := sumOfIdsShouldBe - seatIDActualSum
50+
51+
done <- fmt.Sprintf("part2: missing seat id %v", seats[indexOfSeatWithIDOneHigherThanMissingSeat].id-1)
2952
}

seats.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
67
)
@@ -12,6 +13,10 @@ type seatAssignment struct {
1213
binarySpacePartition string
1314
}
1415

16+
func (s seatAssignment) String() string {
17+
return fmt.Sprintf("seat row: %v, col: %v, id: %v", s.row, s.column, s.id)
18+
}
19+
1520
func makeBinary(input string, zero rune, one rune) int {
1621
input = strings.ReplaceAll(input, string(zero), "0")
1722
input = strings.ReplaceAll(input, string(one), "1")

0 commit comments

Comments
 (0)