AOC2022/day25/day25.go
Karl Spickermann 01ae5e3d11 Day25
2022-12-26 01:48:11 +01:00

80 lines
1.4 KiB
Go

package main
import (
"AOC2022/helper"
"fmt"
"math"
"os"
"strconv"
)
func main() {
args := os.Args[1:]
lines := helper.ReadTextFile(args[0])
sum := 0
for _, line := range lines {
sum += toNormal(line)
}
fmt.Println(sum)
fmt.Println(toSNAFU(sum))
}
func toNormal(SNAFU string) int {
returnValue := float64(0)
for i := 0; i < len(SNAFU); i++ {
currentChar := SNAFU[len(SNAFU)-1-i]
amount := float64(0)
switch currentChar {
case '2':
amount = 2
case '1':
amount = 1
case '-':
amount = -1
case '=':
amount = -2
}
returnValue += amount * math.Pow(float64(5), float64(i))
}
return int(returnValue)
}
func toSNAFU(input int) string {
returnValue := ""
highestFitting := 99
i := 10
for highestFitting == 99 {
i--
amount := int(math.Pow(5, float64(i)))
if amount < input {
highestFitting = i + 1
}
}
fitIns := 0
for input > 0 {
remains := input % 5
remains += fitIns
fitIns = 0
input = input / 5
if remains > 2 {
remainsRemains := remains % 5
fitIns = remains / 5
if remainsRemains > 2 {
fitIns++
switch remainsRemains {
case 3:
returnValue = "=" + returnValue
case 4:
returnValue = "-" + returnValue
}
} else {
returnValue = strconv.Itoa(remainsRemains) + returnValue
}
} else {
returnValue = strconv.Itoa(remains) + returnValue
}
}
return returnValue
}