DEV Community

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

Posted on

2 1

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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