adventofcode/2022/day25/main.go

51 lines
744 B
Go

package main
import (
"fmt"
"math"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
}
func part1(inp []string) {
var v int
for i := range inp {
v = v + SNAFUToDec(inp[i])
}
fmt.Println("# Part 1")
fmt.Println(DecToSNAFU(v))
}
func SNAFUToDec(inp string) int {
var res int
var place float64
for i := len(inp) - 1; i >= 0; i-- {
var v int
if inp[i] == '-' {
v = -1
} else if inp[i] == '=' {
v = -2
} else {
v = int(inp[i] - '0')
}
v = v * int(math.Pow(5, place))
place++
res = res + v
}
return res
}
func DecToSNAFU(v int) string {
var res string
for v > 0 {
res = string("=-012"[(v+2)%5]) + res
v = (v + 2) / 5
}
return res
}