refactor: massive refactor to have only one binary to call
This commit is contained in:
79
internal/2020/DayFive/code.go
Normal file
79
internal/2020/DayFive/code.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package dayfive
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Register("2020D5", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
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 ParseInput(filepath string) []string {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
return strings.Split(string(content), "\n")
|
||||
}
|
||||
|
||||
func PartOne(input []string) int {
|
||||
maxSeatID := 0
|
||||
for _, pass := range input {
|
||||
seatID := calculateSeatID(pass)
|
||||
if seatID > maxSeatID {
|
||||
maxSeatID = seatID
|
||||
}
|
||||
}
|
||||
return maxSeatID
|
||||
}
|
||||
|
||||
func PartTwo(input []string) int {
|
||||
seatIDs := make(map[int]bool)
|
||||
minSeatID := 1000
|
||||
maxSeatID := 0
|
||||
|
||||
for _, pass := range input {
|
||||
if len(pass) < 10 {
|
||||
continue
|
||||
}
|
||||
seatID := calculateSeatID(pass)
|
||||
seatIDs[seatID] = true
|
||||
if seatID < minSeatID {
|
||||
minSeatID = seatID
|
||||
}
|
||||
if seatID > maxSeatID {
|
||||
maxSeatID = seatID
|
||||
}
|
||||
}
|
||||
|
||||
for seatID := minSeatID + 1; seatID < maxSeatID; seatID++ {
|
||||
if !seatIDs[seatID] && seatIDs[seatID-1] && seatIDs[seatID+1] {
|
||||
return seatID
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
35
internal/2020/DayFive/code_test.go
Normal file
35
internal/2020/DayFive/code_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package dayfive
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
input := []string{
|
||||
"FBFBBFFRLR",
|
||||
"BFFFBBFRRR",
|
||||
"FFFBBBFRRR",
|
||||
"BBFFBBFRLL",
|
||||
}
|
||||
expected := 820
|
||||
got := PartOne(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
input := []string{
|
||||
"FFFFFFFLLL",
|
||||
"FFFFFFFLLR",
|
||||
"FFFFFFFLRL",
|
||||
"FFFFFFFLRR",
|
||||
"FFFFFFFRLR",
|
||||
"FFFFFFFRRL",
|
||||
"FFFFFFFRRR",
|
||||
}
|
||||
expected := 4
|
||||
got := PartTwo(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user