LogoWCF

dalurness's Solution

package main

import (
    "fmt"
    "os"
    "strconv"
    "strings"
)

func absDiffInt(x, y int) int {
    if x < y {
        return y - x
    }
    return x - y
}

func main() {
    dat, err := os.ReadFile("../trees.txt")
    if err != nil {
        panic("failed to read file")
    }

    trees := strings.Split(string(dat[:]), "\n\n")

    totalOffBalanceWeight := 0
    for _, tree := range trees {
        parts := strings.Split(tree, "\n")
        trunkWeight, err := strconv.Atoi(parts[0])
        if err != nil {
            panic("trunc value incorrect")
        }

        leftWeight := 0
        rightWeight := 0
        limbsStrings := strings.Split(parts[1], " ")
        leftLimb, err := strconv.Atoi(limbsStrings[0])
        if err != nil {
            panic("failed converting left limb amount")
        }
        leftWeight = leftWeight + leftLimb
        rightLimb, err := strconv.Atoi(limbsStrings[1])
        if err != nil {
            panic("failed to convert right limb")
        }
        rightWeight = rightWeight + rightLimb

        ornamentsStrings := strings.Split(parts[2], " ")
        lOrn1, err := strconv.Atoi(ornamentsStrings[0])
        if err == nil {
            leftWeight = leftWeight + lOrn1
        }
        lOrn2, err := strconv.Atoi(ornamentsStrings[1])
        if err == nil {
            leftWeight = leftWeight + lOrn2
        }
        rOrn1, err := strconv.Atoi(ornamentsStrings[2])
        if err == nil {
            rightWeight = rightWeight + rOrn1
        }
        rOrn2, err := strconv.Atoi(ornamentsStrings[3])
        if err == nil {
            rightWeight = rightWeight + rOrn2
        }
        if absDiffInt(leftWeight, rightWeight) > 1 {
            totalOffBalanceWeight = totalOffBalanceWeight + leftWeight + rightWeight + trunkWeight
            fmt.Printf("Tree weight: %d\n", leftWeight+rightWeight+trunkWeight)
        }
    }

    fmt.Printf("Total: %d\n", totalOffBalanceWeight)
}

Day 6: Balancing Tree Act

As the elves are busy making Christmas trees Mrs. Santa comes to look over the finished products. She notices that some of the Christmas trees are leaning one way or the other while some are straight as an arrow. She consults with the Head Elf and confirms that there is an issue in the production of Christmas trees. Help them figure out which trees are off balance so they know how much material is going to need to be recycled.

The elves today were making simple trees with only 2 branches and up to 2 ornaments on each branch. Each part of the tree has a specific weight.

Mrs. Santa has noted that the trees only start to lean over if the branches weights are off by more than 1.

The trees are given as input in the following format:

9
6 6
1 2 2 1

8
4 5
2 2 2 2

9
5 6
1 2 2 2

8
6 6
2 2 _ _

Each tree is separated by a blank line. Each tree is defined as such:

Problem: Calculate the total weight of all the trees in trees.txt that are off balance. For the example above the total weight of off balance trees (the last 2) is 51.