DEV Community

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

Posted on • Updated on

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

Top comments (0)