adventofcode/2015/day02/main.go

73 lines
1.2 KiB
Go
Raw Permalink Normal View History

2016-12-16 22:21:15 +00:00
package main
import (
"fmt"
"io"
"log"
"strconv"
"strings"
)
func main() {
var input []string
var readInp string
for {
_, err := fmt.Scan(&readInp)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
input = append(input, readInp)
}
var totalPaper, totalRibbon int
for _, k := range input {
l, w, h := parseInputString(k)
totalPaper += calcPaperForPresent(l, w, h)
totalRibbon += calcRibbonForPresent(l, w, h)
}
fmt.Printf("The elves need %d sq ft of wrapping paper\n", totalPaper)
fmt.Printf("and %d ft of ribbon.\n", totalRibbon)
}
func parseInputString(inp string) (int, int, int) {
dim := strings.Split(inp, "x")
if len(dim) == 3 {
pt1, _ := strconv.Atoi(dim[0])
pt2, _ := strconv.Atoi(dim[1])
pt3, _ := strconv.Atoi(dim[2])
return pt1, pt2, pt3
}
return 0, 0, 0
}
func calcPaperForPresent(l, w, h int) int {
pt1 := l * w
pt2 := w * h
pt3 := h * l
pt4 := pt3
if pt1 < pt2 && pt1 < pt3 {
pt4 = pt1
} else if pt2 < pt3 {
pt4 = pt2
}
return pt1*2 + pt2*2 + pt3*2 + pt4
}
func calcRibbonForPresent(l, w, h int) int {
var low1, low2 int
low2 = h
if l < w {
low1 = l
if w < h {
low2 = w
}
} else {
low1 = w
if l < h {
low2 = l
}
}
return low1*2 + low2*2 + (l * w * h)
}