more string parsing! and custom sorts in go

This commit is contained in:
David 2020-12-05 12:04:37 -05:00
parent d0a11045fd
commit 2828ad3af3
2 changed files with 907 additions and 0 deletions

799
05/input Normal file
View File

@ -0,0 +1,799 @@
FBBBFFFLRL
BFFBBBBLRL
FBFFFBBRRR
FBFBBFFLRL
FFFBFBBRLR
FBFBBBFRLL
BFBFFBFRLR
BFBFFFBRRL
BFBBFFBRLR
FBBFBBBRLL
FFFBBFFRRR
FFBBBFBRLR
BBFFBFBLLL
BBFFBBFRRR
BBFBFFBLLR
FFFBBBFLRR
BBFBBFFRLR
BBFFFBFRLL
FBFFBFBLLR
FBBFFFBRLR
BFFFFBFLRR
BBFFBBBLRL
FBFBBFFRRL
BBFFFFFLRR
BFBBBFBRRL
BBFFBBBRLR
FFBFBFBRLL
BFBFFBFLLR
FBBFBBFRLR
BBFFFBBRLR
BBFFBBFLLR
FFBBBBBRRL
BFFFBFBRRL
FFBFFFBRRL
FBBBBFBLLL
FBBFFBFLRR
BBFFFFBRRL
FFBFBFBLRR
FFBBFBBLLR
BFBFBBFRLL
BBFFFBFLLL
FBBBBFFRRR
BBFBFFFRLL
FFFBBFBRRR
FFBBFFFLRR
BFBBBFFRRR
BFFFFBFRLL
BFFFFBBRLR
FBFBBFBRRL
BFFBFFFLLL
FBFFBFBRLR
BFFFBBFRLR
BFBFBBBRLR
BFFBBBFLLR
BFBBBBFRRL
BFFFFFBLLL
BBFFFFFLRL
FBFFBBBRRL
FBBBFFFLLL
FBFBFBFRLL
BBFBFBBLRR
BBFBFBFLRR
FBFFBBFRRR
FBBBFBBLRR
FBFFBFFRLR
BFBBFFFLRL
FFBFFBFRLR
FBFBFFBRLL
BFFBBFFLLL
FBBFBFBLLR
FFBBFFBLLR
BFFBBFFRRR
FFFBBBFLLR
FFBBBBBLLR
FFBFFBBLLR
FFBFFFBRLR
BFBFBFFLLL
BBFFFFBRRR
BFBFFFFRLL
FBFBFFFRRL
FBFFFFBLRR
FFBFFBFLRR
FBBBBFBRLR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BRLL
BFFFBBBRRL
BBFFFBFRLR
BFBBFBFLRL
BFFFBFBLRL
FBBFBFFRLR
BBFBFFBRLL
BBFFBFBRRR
BFBBBFBRLR
BFFBFFFRRL
BFBBBFBLRL
BFBBFFFRRR
FBFFFBFLRL
FFBFBBFRRL
BFFFFFFRLR
BFFBFBBLRR
FFFBBFBLRR
BFBBBFBRLL
FBFFFFFLLL
BFFBBBFLRL
FBFBFFBLRR
FBFFBBBLRL
FBFBBFBRLL
FFBFBBFLRR
BFBBFFBLLL
BBFBBBFRRR
FBFBBBBLRR
BFBFBFFLRR
FBBFFBFRLL
FBFBBBFLRR
FFFBFBBLLR
BBFBFFBRRR
FBFFBFFLLL
BBFFBFFRRL
FFBFBFFLLL
BFFBBBFRRL
FBBFBFFLLR
BFBBFFFRLR
BFBFBFBLLL
BFFBFFBRLR
FBBBBBFLRL
BFBBBFFLLR
FFFBFBBLRR
BBFBBFFLLL
FBBBFFBLLL
BBFFFFBLRL
FFBBFBBRRL
FFFBBBBLLR
FBFBFBFLLL
FBFBFBBLRR
BFFFFFFRRL
FFBFFBBRRR
FFFBBFFLLR
BFFFFBFRLR
FBFFBFBLLL
BFFFFBBLRR
BFBFFBBRRR
BBFFBBFLLL
FBBFFFBLLL
FBFFFFBLLL
FBFFFBBLLR
BBFFBBFLRR
FFBBBFBLRR
BFFFFBFRRR
BBFFBFBLRR

108
05/main.go Normal file
View File

@ -0,0 +1,108 @@
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func main() {
partOne()
partTwo()
}
type boardingPass struct {
passID string
row int
seat int
id int
}
func parsePassID(s string) (int, int) {
bottom := 0
top := 127
left := 0
right := 7
for _, r := range s {
switch {
case r == 'F':
top = ((top - bottom - 1) / 2) + bottom
case r == 'B':
bottom = (top + bottom + 1) / 2
case r == 'L':
right = ((right - left - 1) / 2) + left
case r == 'R':
left = (left + right + 1) / 2
}
}
return top, left
}
// Instead of zones or groups, this airline uses binary space partitioning to seat people.
// A seat might be specified like FBFBBFFRLR, where F means "front", B means "back", L means "left", and R means "right".
// Every seat also has a unique seat ID: multiply the row by 8, then add the column.
// Here are some other boarding passes:
// BFFFBBFRRR: row 70, column 7, seat ID 567.
// FFFBBBFRRR: row 14, column 7, seat ID 119.
// BBFFBBFRLL: row 102, column 4, seat ID 820.
// As a sanity check, look through your list of boarding passes. What is the highest seat ID on a boarding pass?
func partOne() {
f, _ := os.Open("input")
reader := bufio.NewReader(f)
scanner := bufio.NewScanner(reader)
passes := []boardingPass{}
maxID := 0
for scanner.Scan() {
line := scanner.Text()
row, column := parsePassID(line)
passes = append(passes, boardingPass{
passID: line,
row: row,
seat: column,
id: (row * 8) + column,
})
if (row*8)+column > maxID {
maxID = (row * 8) + column
}
}
fmt.Println(maxID)
}
// It's a completely full flight, so your seat should be the only missing boarding pass in your list.
// However, there's a catch: some of the seats at the very front and back of the plane don't exist on this aircraft, so they'll be missing from your list as well.
// Your seat wasn't at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list.
func partTwo() {
f, _ := os.Open("input")
reader := bufio.NewReader(f)
scanner := bufio.NewScanner(reader)
passes := []boardingPass{}
for scanner.Scan() {
line := scanner.Text()
row, column := parsePassID(line)
passes = append(passes, boardingPass{
passID: line,
row: row,
seat: column,
id: (row * 8) + column,
})
}
sort.Slice(passes, func(i, j int) bool {
return passes[i].id < passes[j].id
})
for i, s := range passes {
if i != 0 && passes[i-1].id != s.id-1 {
fmt.Println(s.id - 1)
}
}
}