Files
advent2022/solutions/day25/day25.go
2022-12-25 23:09:33 -08:00

77 lines
1.1 KiB
Go

package day25
import (
"bufio"
"fmt"
"os"
)
func valueOf(r rune) int {
switch r {
case '2':
return 2
case '1':
return 1
case '0':
return 0
case '-':
return -1
case '=':
return -2
default:
fmt.Println("PANIC PANIC")
return 23489
}
}
func Run(filename string) {
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
runningTotal := 0
for scanner.Scan() {
line := []rune(scanner.Text())
current := 0
place := 1
for x := len(line) - 1; x >= 0; x-- {
r := line[x]
current += (place * valueOf(r))
place *= 5
}
fmt.Println("Coverted", line, "==>", current)
runningTotal += current
}
file.Close()
fmt.Println("Running total", runningTotal)
output := ""
for runningTotal != 0 {
runningTotal += 2
digit := runningTotal % 5
runningTotal /= 5
switch digit {
case 4:
output = "2" + output
case 3:
output = "1" + output
case 2:
output = "0" + output
case 1:
output = "-" + output
case 0:
output = "=" + output
}
}
fmt.Println("Which is", output, "in SNAFU.")
}