refactor: add calculateSeatID helper to simplify code
This commit is contained in:
@@ -15,27 +15,34 @@ func parseInput(file string) []string {
|
|||||||
return strings.Split(string(content), "\n")
|
return strings.Split(string(content), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func calculateSeatID(pass string) int {
|
||||||
|
if len(pass) < 10 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
rowStr := pass[:7]
|
||||||
|
columnStr := pass[7:10]
|
||||||
|
|
||||||
|
row := 0
|
||||||
|
for idx, char := range rowStr {
|
||||||
|
if char == 'B' {
|
||||||
|
row |= 1 << (6 - idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
column := 0
|
||||||
|
for idx, char := range columnStr {
|
||||||
|
if char == 'R' {
|
||||||
|
column |= 1 << (2 - idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return row*8 + column
|
||||||
|
}
|
||||||
|
|
||||||
func PartOne(input []string) int {
|
func PartOne(input []string) int {
|
||||||
maxSeatID := 0
|
maxSeatID := 0
|
||||||
for _, pass := range input {
|
for _, pass := range input {
|
||||||
rowStr := pass[:7]
|
seatID := calculateSeatID(pass)
|
||||||
columnStr := pass[7:10]
|
|
||||||
|
|
||||||
row := 0
|
|
||||||
for idx, char := range rowStr {
|
|
||||||
if char == 'B' {
|
|
||||||
row |= 1 << (6 - idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
column := 0
|
|
||||||
for idx, char := range columnStr {
|
|
||||||
if char == 'R' {
|
|
||||||
column |= 1 << (2 - idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
seatID := row*8 + column
|
|
||||||
if seatID > maxSeatID {
|
if seatID > maxSeatID {
|
||||||
maxSeatID = seatID
|
maxSeatID = seatID
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user