mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
2020 day5 - ugh off by one w/ funky binary search
This commit is contained in:
+81
-19
@@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -14,38 +14,100 @@ func main() {
|
||||
flag.Parse()
|
||||
fmt.Println("Running part", part)
|
||||
|
||||
bytes, err := ioutil.ReadFile("input.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("Reading file: %s", err)
|
||||
}
|
||||
|
||||
if part == 1 {
|
||||
ans := part1(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
ans := part1(string(bytes))
|
||||
fmt.Println("Output:", ans)
|
||||
} else {
|
||||
ans := part2(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
ans := part2(string(bytes))
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
}
|
||||
|
||||
func part1(input string) int {
|
||||
parsed := parseInput(input)
|
||||
_ = parsed
|
||||
var best int
|
||||
for _, seatDetails := range strings.Split(input, "\n") {
|
||||
if len(seatDetails) == 0 {
|
||||
continue
|
||||
}
|
||||
rowLeft, rowRight := 0, 127
|
||||
for _, char := range seatDetails[0:6] {
|
||||
if char == 'F' {
|
||||
rowRight -= (rowRight - rowLeft + 1) / 2
|
||||
} else {
|
||||
rowLeft += (rowRight - rowLeft + 1) / 2
|
||||
}
|
||||
}
|
||||
if seatDetails[6] == 'B' {
|
||||
rowLeft = rowRight
|
||||
}
|
||||
|
||||
return 0
|
||||
colLeft, colRight := 0, 7
|
||||
for _, col := range seatDetails[7:9] {
|
||||
if col == 'L' {
|
||||
colRight -= (colRight - colLeft + 1) / 2
|
||||
} else {
|
||||
colLeft += (colRight - colLeft + 1) / 2
|
||||
}
|
||||
}
|
||||
|
||||
if seatDetails[9] == 'R' {
|
||||
colLeft = colRight
|
||||
}
|
||||
|
||||
id := rowLeft*8 + colLeft
|
||||
if id > best {
|
||||
best = id
|
||||
}
|
||||
}
|
||||
|
||||
return best
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
parsed := parseInput(input)
|
||||
_ = parsed
|
||||
allSeatIDs := make(map[int]bool)
|
||||
|
||||
return 0
|
||||
}
|
||||
for _, seatDetails := range strings.Split(input, "\n") {
|
||||
rowLeft, rowRight := 0, 127
|
||||
for _, char := range seatDetails[0:6] {
|
||||
if char == 'F' {
|
||||
rowRight -= (rowRight - rowLeft + 1) / 2
|
||||
} else {
|
||||
rowLeft += (rowRight - rowLeft + 1) / 2
|
||||
}
|
||||
}
|
||||
if seatDetails[6] == 'B' {
|
||||
rowLeft = rowRight
|
||||
}
|
||||
|
||||
func parseInput(input string) []int {
|
||||
var ans []int
|
||||
colLeft, colRight := 0, 7
|
||||
for _, col := range seatDetails[7:9] {
|
||||
if col == 'L' {
|
||||
colRight -= (colRight - colLeft + 1) / 2
|
||||
} else {
|
||||
colLeft += (colRight - colLeft + 1) / 2
|
||||
}
|
||||
}
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, l := range lines {
|
||||
ans = append(ans, util.StrToInt(l))
|
||||
if seatDetails[9] == 'R' {
|
||||
colLeft = colRight
|
||||
}
|
||||
|
||||
id := rowLeft*8 + colLeft
|
||||
allSeatIDs[id] = true
|
||||
}
|
||||
|
||||
return ans
|
||||
var mySeatID int
|
||||
for id := range allSeatIDs {
|
||||
if allSeatIDs[id] && allSeatIDs[id+2] && !allSeatIDs[id+1] {
|
||||
mySeatID = id + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return mySeatID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user