DAY 9 - Advent of Code 2020 w/ GoLang

dizveloper

Edvin

Posted on December 9, 2020

DAY 9 - Advent of Code 2020 w/ GoLang

DAY9:

This one felt good, for me. There are many loops, as you'll see. That is mainly because I had to write some utility functions for typical slice evaluation that Go doesn't have by default like other languages (not complaining, just pointing out.).

I do advise you split out the logic into helper functions. It could've gotten really messy if I did all of the work in the main function.

package days

import (
    "fmt"

    inputs "../inputs"
)

// https://adventofcode.com/2020/day/9
// Nine : advent of code, day nine part1 and 2
func Nine() {
    inputSlice := inputs.Day9

    part1 := getRuleBreaker(inputSlice, 25)
    fmt.Print("(Part 1) - Value that doesn't follow preamble rule: ")
    fmt.Println(part1)

    part2 := contiguousCheckSum(inputSlice, part1)
    fmt.Print("(Part 2) - Contiguous series sum (series max+min): ")
    fmt.Println(part2)
}

func getRuleBreaker(inputSlice []int, preambleSize int) int {
    for i := preambleSize; i < len(inputSlice); i++ {
        preamble := inputSlice[i-preambleSize : i]
        if !preambleEquator(preamble, inputSlice[i]) {
            return inputSlice[i]
        }
    }

    return 0
}

func contiguousCheckSum(inputSlice []int, toSum int) int {
    maxMinSum := 0

    for i := 0; i < len(inputSlice); i++ {
        for j := i + 1; j < len(inputSlice); j++ {
            if toSum != inputSlice[i] && toSum != inputSlice[j] {
                contiguousSeries := inputSlice[i:j]

                contSum := 0
                for c := 0; c < len(contiguousSeries); c++ {
                    contSum = contSum + contiguousSeries[c]
                }

                if contSum == toSum {
                    maxMinSum = maxMinSumation(contiguousSeries)
                }
            }
        }
    }

    return maxMinSum
}

func maxMinSumation(contiguousSeries []int) int {
    min := contiguousSeries[0]
    for v := 0; v < len(contiguousSeries); v++ {
        if contiguousSeries[v] < min {
            min = contiguousSeries[v]
        }
    }

    max := contiguousSeries[0]
    for v := 0; v < len(contiguousSeries); v++ {
        if contiguousSeries[v] > max {
            max = contiguousSeries[v]
        }
    }

    return min + max
}

func preambleEquator(preamble []int, current int) bool {
    worked := false
    for j := 0; j < len(preamble); j++ {
        for x := j + 1; x < len(preamble); x++ {
            if preamble[j]+preamble[x] == current {
                worked = true
                break
            }
        }
    }
    return worked
}
Enter fullscreen mode Exit fullscreen mode

Link to Github source file

💖 💪 🙅 🚩
dizveloper
Edvin

Posted on December 9, 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