DEV Community

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

Posted on • Edited on

1

DAY 3 - Advent of Code 2020 w/ GoLang

DAY3:
Modulo or your computer explodes (:

I had a silly for loop initially that would just duplicate and append to the remaining lines once it reached the end. It worked I think but I never let it finish because my laptop fans were on super plus ultra

package days

import (
    "fmt"

    inputs "../inputs"
)

// Three : advent of code, day three part1 and 2.
func Three() {
    inputSlice := inputs.Day3

    slopes := [5][2]int{
        {1, 1},
        {3, 1},
        {5, 1},
        {7, 1},
        {1, 2},
    }

    trees := []int{}

    for slope := 0; slope < len(slopes); slope++ {
        treesAtSlope := treesInSlope(inputSlice, slopes[slope][0], slopes[slope][1])
        trees = append(trees, treesAtSlope)
    }

    multiplied := 1
    for tree := 0; tree < len(trees); tree++ {
        multiplied = multiplied * trees[tree]
    }

    fmt.Print("Trees encountered: ")
    fmt.Println(trees)
    fmt.Print("Trees multiplied: ")
    fmt.Println(multiplied)
}

func treesInSlope(grid []string, right int, down int) int {
    trees := 0
    row := 0
    column := 0

    for {
        if column >= len(grid[0]) {
            column = column % len(grid[0])
        }

        if row >= len(grid) {
            break
        }

        if isItATree(grid[row], column) {
            trees++
        }

        column = column + right
        row = row + down
    }

    return trees
}

func isItATree(row string, columnPos int) bool {
    toRune := []rune(row)
    tree := '#'
    return toRune[columnPos] == tree
}

Enter fullscreen mode Exit fullscreen mode

Link to Github source file

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

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

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay