DEV Community

Cover image for DAY 5 - Advent of Code 2020 w/ GoLang
Edvin
Edvin

Posted on • Edited on

1

DAY 5 - Advent of Code 2020 w/ GoLang

DAY5:
Didn't feel great about this code, but being able to use two iterators on a for loop really helped me out here. 🤷🏼‍♂️

I think the not feeling great was more because I spent waaaay long on a bug I couldn't find than actually making it work 🤦🏼‍♂️

package days

import (
    "fmt"
    "sort"

    inputs "../inputs"
)

// Five : advent of code, day five part1 and 2
func Five() {
    inputSlice := inputs.Day5

    seatIds := partOne(inputSlice)
    fmt.Println("")
    fmt.Println("Your seat ID is: ")
    fmt.Println(partTwo(seatIds))
}

func partOne(inputSlice []string) []int {
    seatIds := []int{}

    for dir := range inputSlice {
        toRune := []rune(inputSlice[dir])
        rowRange := []int{0, 128}
        colRange := []int{0, 8}
        finalRow := 0
        finalColumn := 0
        for i := 0; i < 7; i++ {
            if toRune[i] == 'F' {
                rowRange[1] = rowRange[1] - ((rowRange[1] - rowRange[0]) / 2)
            } else if toRune[i] == 'B' {
                rowRange[0] = rowRange[0] + ((rowRange[1] - rowRange[0]) / 2)
            }

            if i == 6 {
                finalRow = rowRange[0]
            }
        }

        for i := 0; i < 3; i++ {
            if toRune[i+7] == 'L' {
                colRange[1] = colRange[1] - ((colRange[1] - colRange[0]) / 2)
            } else if toRune[i+7] == 'R' {
                colRange[0] = colRange[0] + ((colRange[1] - colRange[0]) / 2)
            }

            if i == 2 {
                finalColumn = colRange[0]
            }
        }

        seatIds = append(seatIds, ((finalRow * 8) + finalColumn))
    }

    highest := 0
    for i := range seatIds {
        if seatIds[i] > highest {
            highest = seatIds[i]
        }
    }

    fmt.Print("Highest Seat ID: ")
    fmt.Println(highest)
    return seatIds
}

func partTwo(seatIds []int) int {
    sort.Ints(seatIds)
    seatsLeft := []int{}

    for i, seat := 0, seatIds[0]; seat < seatIds[len(seatIds)-1]; i, seat = i+1, seat+1 {
        if seat != seatIds[i] {
            seatsLeft = append(seatsLeft, seat)
            seat = seatIds[i]
        }
    }

    return seatsLeft[0]
}

Enter fullscreen mode Exit fullscreen mode

Link to Github source file

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay