This commit is contained in:
David 2022-12-25 20:42:52 -07:00
parent a97522988a
commit 205ecb4acc

77
25/main.go Normal file
View File

@ -0,0 +1,77 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"time"
)
func mustAtoi(line string) int {
i, _ := strconv.Atoi(line)
return i
}
func main() {
start := time.Now()
partOne()
duration := time.Since(start)
fmt.Printf("p1: %s\n", duration)
}
func makeScanner(test bool) *bufio.Scanner {
var f *os.File
if test {
f, _ = os.Open("inputs/testinput")
} else {
f, _ = os.Open("inputs/input")
}
reader := bufio.NewReader(f)
return bufio.NewScanner(reader)
}
func parseSNAFU(str string) int {
num := 0
place := 1
for i := len(str) - 1; i >= 0; i-- {
switch str[i] {
case '2':
num += place * 2
case '1':
num += place
case '0':
// noop
case '-':
num -= place
case '=':
num -= place * 2
default:
panic("oh no")
}
place *= 5
}
return num
}
func toSNAFU(normal int) string {
num := ""
digits := []string{"0", "1", "2", "=", "-"}
for {
num = digits[normal%5] + num
if normal == 0 {
return num
}
normal = (normal + 2) / 5
}
}
func partOne() {
scanner := makeScanner(false)
sum := 0
for scanner.Scan() {
line := scanner.Text()
sum += parseSNAFU(line)
}
fmt.Println(toSNAFU(sum)[1:])
}