DEV Community

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

DAY 6 - Advent of Code 2020 w/ GoLang

dizveloper profile image Edvin Updated on ・2 min read

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 (

    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 {

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

        allSaidYes := 0

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

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

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

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)

Editor guide
davidjones418 profile image
David Jones • Edited

I’m learning Go for this AoC as well! I did this one with bitmasks so I haven’t used maps much yet (I think your solution is much easier to follow!) but I think you can avoid initiating the keys in the map, and rely on values defaulting to 0 when you try to increment a key for the first time. Then I think you can call len() on the map to count unique keys.

kais_blog profile image

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.

dizveloper profile image
Edvin Author

The tutorial is very thorough. Nice work!

davidjones418 profile image
David Jones

Cool, that’s a great explanation!

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 😂

kais_blog profile image

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.