DEV Community

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

Posted on • Edited on

1

DAY 8 - Advent of Code 2020 w/ GoLang

Part 1:
I checked which operation needed to happen and enacted it. I kept a running check to see if an index was revisited. If it was, I broke out of the loop and returned the accumulation.

Part 2:

  1. I looked for the next jmp operation
  2. I changed it to a nop
  3. I reused the code from part1 to return the accumulation, BUT ALSO to return the index at which it terminated
  4. If it terminated before the end of the program, I REVERTED the last changed nop back to jmp and started again on the next jmp NOTE: I followed the running changed index with a global slice to start from in the next recursion.
  5. That recursion continued until I had changed only a single jmp and we reached the end of the program.
package days

import (
    "fmt"
    "strconv"
    "strings"

    inputs "../inputs"
)

// https://adventofcode.com/2020/day/8
// Eight : advent of code, day eight part1 and 2
func Eight() {
    inputSlice := inputs.Day8

    part1(inputSlice)
    part2(inputSlice)
}

func part1(inputSlice []string) {
    acc, _ := oppEvaluator(inputSlice)
    fmt.Print("(Part1) - Accumulator prior to infinite loop restarting: ")
    fmt.Println(acc)
}

var whichOpChanged = []int{0, 0}

func part2(inputSlice []string) {
    innerSlice := inputSlice

    for operation := whichOpChanged[0]; operation < len(innerSlice); operation++ {
        temp := strings.Split(innerSlice[operation], " ")

        typeOfOp := temp[0]
        signedNumber, _ := strconv.Atoi(temp[1])

        if typeOfOp == "nop" && whichOpChanged[0] == operation && whichOpChanged[1] == signedNumber {
            innerSlice[whichOpChanged[0]] = "jmp " + strconv.Itoa(whichOpChanged[1])
        }

        if typeOfOp == "jmp" {
            innerSlice[operation] = "nop " + strconv.Itoa(signedNumber)
            whichOpChanged = []int{operation, signedNumber}
            break
        }
    }

    acc, in := oppEvaluator(innerSlice)
    if in < len(inputSlice) {
        part2(innerSlice)
    } else {
        fmt.Print("(Part2) - Accumulator after termination: ")
        fmt.Println(acc)
    }

}

func oppEvaluator(inputSlice []string) (int, int) {
    accumulator := 0
    operationIndex := 0
    indexVisited := []int{0}

    for {
        temp := strings.Split(inputSlice[operationIndex], " ")

        typeOfOp := temp[0]
        signedNumber, _ := strconv.Atoi(temp[1])

        switch typeOfOp {
        case "acc":
            accumulator = accumulator + signedNumber
            operationIndex++
        case "jmp":
            operationIndex = operationIndex + signedNumber
        case "nop":
            operationIndex++
        default:
            fmt.Println("not an opp")
        }

        if intSliceContains(indexVisited, operationIndex) {
            break
        } else {
            indexVisited = append(indexVisited, operationIndex)
        }

        if operationIndex > len(inputSlice)-1 || operationIndex < 0 {
            break
        }
    }

    return accumulator, operationIndex
}

func intSliceContains(s []int, i int) bool {
    for _, a := range s {
        if a == i {
            return true
        }
    }
    return false
}
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

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay