Solve maya

This commit is contained in:
Gabriel Augendre 2021-08-16 08:45:31 +02:00
parent ae116cf138
commit 9ce79c5e8f

View file

@ -1,39 +1,91 @@
package main
import "fmt"
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
import (
"fmt"
"math"
"os"
)
func main() {
var L, H int
fmt.Scan(&L, &H)
mayaDigits := make(map[string]int)
digitsToMaya := make(map[int]string)
for i := 0; i < 20; i++ {
digitsToMaya[i] = ""
}
for i := 0; i < H; i++ {
var numeral string
fmt.Scan(&numeral)
for j := 0; j < 20; j++ {
for k := 0; k < L; k++ {
digitsToMaya[j] += string(numeral[j*L+k])
}
digitsToMaya[j] += "\n"
}
}
var S1 int
fmt.Scan(&S1)
for i := 0; i < S1; i++ {
var num1Line string
fmt.Scan(&num1Line)
mayaToDigits := make(map[string]int)
for number, mayaDigit := range digitsToMaya {
mayaToDigits[mayaDigit] = number
}
var S2 int
fmt.Scan(&S2)
for i := 0; i < S2; i++ {
var num2Line string
fmt.Scan(&num2Line)
}
firstOperand := extractDigit(H, mayaToDigits)
secondOperand := extractDigit(H, mayaToDigits)
var operation string
fmt.Scan(&operation)
// fmt.Fprintln(os.Stderr, "Debug messages...")
fmt.Println("result")// Write answer to stdout
var result int
if operation == "+" {
result = firstOperand + secondOperand
} else if operation == "-" {
result = firstOperand - secondOperand
} else if operation == "*" {
result = firstOperand * secondOperand
} else if operation == "/" {
result = firstOperand / secondOperand
}
fmt.Println(numberToMaya(result, digitsToMaya))
}
func pow(base, exponent int) int {
return int(math.Pow(float64(base), float64(exponent)))
}
func extractDigit(H int, mayaToDigits map[string]int) int {
var S1 int
fmt.Scan(&S1)
s1Sum := 0
for power := S1/H - 1; power >= 0; power-- {
mayaDigit := ""
for i := 0; i < H; i++ {
var line string
fmt.Scan(&line)
mayaDigit += line + "\n"
}
digit, present := mayaToDigits[mayaDigit]
if !present {
panic("Maya digit not found!")
}
s1Sum += digit * pow(20, power)
}
return s1Sum
}
func numberToMaya(number int, digitsToMaya map[int]string) string {
if number < 20 {
return digitsToMaya[number]
}
var maya []string
for number > 0 {
mayaDigit := digitsToMaya[number%20]
maya = append(maya, mayaDigit)
number = number / 20
}
mayaResult := ""
for i := len(maya) - 1; i >= 0; i-- {
mayaResult += maya[i]
}
return mayaResult
}