DEV Community

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

Posted on • Updated on

DAY 6 - Advent of Code 2020 w/ GoLang

DAY6:
Ooooohh the loops. So, I'm not sure if there are built-in functions or packages in Go that would've been helpful with some of this set-evaluation. If there is, let me know! But, I don't believe there is since I also had to write my own slice.contains() method since that doesn't seem to be a built-in.

I found it helpful to make a rune:int map per group in part 2 for tracking the answers.

package days

import (
    "fmt"
    "strings"

    inputs "../inputs"
)

// Six : advent of code, day six part1 and 2
func Six() {
    input := inputs.Day6

    groups := strings.Split(input, "\n\n")

    totalUniqueYesP1 := 0
    totalUniqueYesP2 := 0

    for group := range groups {
        people := strings.Split(groups[group], "\n")

        uniqueYesInGroup := []rune{}

        groupAlphaMap := map[rune]int{
            'a': 0,
            'b': 0,
            'c': 0,
            'd': 0,
            'e': 0,
            'f': 0,
            'g': 0,
            'h': 0,
            'i': 0,
            'j': 0,
            'k': 0,
            'l': 0,
            'm': 0,
            'n': 0,
            'o': 0,
            'p': 0,
            'q': 0,
            'r': 0,
            's': 0,
            't': 0,
            'u': 0,
            'v': 0,
            'w': 0,
            'x': 0,
            'y': 0,
            'z': 0,
        }

        for person := range people {
            personToRune := []rune(people[person])

            for answer := range personToRune {
                groupAlphaMap[personToRune[answer]]++

                if !runeSliceContains(uniqueYesInGroup, personToRune[answer]) {
                    uniqueYesInGroup = append(uniqueYesInGroup, personToRune[answer])
                }
            }
        }

        allSaidYes := 0

        for k := range groupAlphaMap {
            if groupAlphaMap[k] == len(people) {
                allSaidYes++
            }
        }

        totalUniqueYesP1 = totalUniqueYesP1 + len(uniqueYesInGroup)
        totalUniqueYesP2 = totalUniqueYesP2 + allSaidYes
    }

    fmt.Println("(Part1) Sum of unique answers from each group: ")
    fmt.Println(totalUniqueYesP1)
    fmt.Println("")
    fmt.Println("(Part2) Sum of unique answers each person in group answered YES: ")
    fmt.Println(totalUniqueYesP2)
}

func runeSliceContains(s []rune, r rune) bool {
    for _, a := range s {
        if a == r {
            return true
        }
    }
    return false
}
Enter fullscreen mode Exit fullscreen mode

Link to Github source file

Discussion (6)

Collapse
kais_blog profile image
Kai

Nice! I've also used bitmasks for my "overengineered" solution. However, in TypeScript it was way easier if you don't use them. So, I didn't use bitmasks for my tutorial series.

Collapse
dizveloper profile image
Edvin Author

The tutorial is very thorough. Nice work!

Collapse
dizveloper profile image
Edvin Author

Nice! Yeah I started last years AOC in Go but just didn’t follow through with finishing, lol.

Bitmasks is definitely more efficient. My way was just what made sense to me. I try to write the code like I think, and that is DEFINITELY not the most efficient way πŸ˜‚

Collapse
kais_blog profile image
Kai

I'd say, just do what works for you. Don't optimize as long as it is not necessary. Also, you'll spend way more time reading and trying to understand code than writing it. That alone is reason enough to make it as simple and comprehensible as possible. Often, the shortest or most clever solution can be hard to grasp.