DAY 10 - Advent of Code 2020 w/ GoLang

dizveloper

Edvin

Posted on December 11, 2020

DAY 10 - Advent of Code 2020 w/ GoLang

DAY10:
Woof. Be warned, part one is DECIEVINGLY simple. Part two had me thinking about implementing a binary tree, which in Go gets terribly verbose. Half-ish way through that approach, I dumped it and started over. Then attempted to recurse while removing indexes after they'd been evaluated. That eventually evolved into implementing a sliding window recursion approach. I slid and evaluated on if the index landed within the acceptable joltage range. ALSO, don't forget the built-ins.

package days

import (
    "fmt"
    "sort"

    inputs "../inputs"
)

// https://adventofcode.com/2020/day/10
// Ten : advent of code, day ten part1 and 2
func Ten() {
    inputSlice := inputs.Day10

    // Adding in built-ins and sorting
    inputSlice = append(inputSlice, 0)
    sort.Ints(inputSlice)
    inputSlice = append(inputSlice, inputSlice[len(inputSlice)-1]+3)

    fmt.Print("(Part1) - result from multipling the 1 and 3 jolt jumps in the total adapter path: ")
    fmt.Println(numberOfOneAndThreeJoltJumpsInAdapterPath(inputSlice))

    fmt.Print("(Part2) - Number of adapter paths that result in the max joltage of is : ")
    fmt.Println(evaluateAdapterPath(inputSlice, 0))
}

func numberOfOneAndThreeJoltJumpsInAdapterPath(adapterSlice []int) int {
    ones := 0
    threes := 0

    for joltage := 0; joltage < len(adapterSlice)-1; joltage++ {
        if adapterSlice[joltage+1]-adapterSlice[joltage] == 1 {
            ones++
        } else if adapterSlice[joltage+1]-adapterSlice[joltage] == 3 {
            threes++
        }
    }
    return ones * threes
}

var adapterPathsTried = make(map[int]int)

func evaluateAdapterPath(adaptersTocheck []int, startAdapter int) int {
    workingAdapterPaths := 0
    nextAdapter := startAdapter + 1
    furthestPossibleAdapter := startAdapter + 3

    if furthestPossibleAdapter >= len(adaptersTocheck) {
        return 1
    } else if _, ok := adapterPathsTried[adaptersTocheck[startAdapter]]; ok {
        return adapterPathsTried[adaptersTocheck[startAdapter]]
    }

    for i := nextAdapter; i <= furthestPossibleAdapter; i++ {
        lower := adaptersTocheck[startAdapter]
        upper := adaptersTocheck[i]

        if (upper-lower) >= 1 && (upper-lower) <= 3 {
            workingAdapterPaths += evaluateAdapterPath(adaptersTocheck, i)
        }
    }

    adapterPathsTried[adaptersTocheck[startAdapter]] = workingAdapterPaths
    return workingAdapterPaths
}

Enter fullscreen mode Exit fullscreen mode

Link to Github source file

💖 💪 🙅 🚩
dizveloper
Edvin

Posted on December 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

AoC Day 9 - Rope Bridge
go AoC Day 9 - Rope Bridge

December 13, 2022

AoC Day 8 - Treetop Tree House
go AoC Day 8 - Treetop Tree House

December 10, 2022

AoC Day 6 - Tuning Trouble
go AoC Day 6 - Tuning Trouble

December 10, 2022

AoC Day 4 - Camp Cleanup
go AoC Day 4 - Camp Cleanup

December 7, 2022