advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.go (1039B)

    1 package main
    2 
    3 import (
    4 	"bufio"
    5 	"fmt"
    6 	"os"
    7 )
    8 
    9 func readInput(filename string) (res []string) {
   10 	file, _ := os.Open(filename)
   11 	defer file.Close()
   12 	scanner := bufio.NewScanner(file)
   13 	for scanner.Scan() {
   14 		res = append(res, scanner.Text())
   15 	}
   16 	return
   17 }
   18 
   19 func main() {
   20 	input := readInput("./input.txt")
   21 	// 919
   22 	fmt.Println(part1(input))
   23 	// 642
   24 	fmt.Println(part2(input))
   25 }
   26 
   27 func decodeSeat(line string) int {
   28 	var row, col int
   29 	for i := 0; i < 7; i++ {
   30 		row <<= 1
   31 		if line[i] == 'B' {
   32 			row += 1
   33 		}
   34 	}
   35 	for i := 7; i < 10; i++ {
   36 		col <<= 1
   37 		if line[i] == 'R' {
   38 			col += 1
   39 		}
   40 	}
   41 	return row*8 + col
   42 }
   43 
   44 func part1(input []string) (max_id int) {
   45 	for _, line := range input {
   46 		id := decodeSeat(line)
   47 		if id > max_id {
   48 			max_id = id
   49 		}
   50 	}
   51 	return
   52 }
   53 
   54 func part2(input []string) int {
   55 	listed_id := make(map[int]bool)
   56 	for _, line := range input {
   57 		listed_id[decodeSeat(line)] = true
   58 	}
   59 	max_id := part1(input)
   60 	for i := 0; i < max_id; i++ {
   61 		if !listed_id[i] && listed_id[i-1] && listed_id[i+1] {
   62 			return i
   63 		}
   64 	}
   65 	return 0
   66 }