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 package main
import "fmt" import (
"fmt"
/** "math"
* Auto-generated code below aims at helping you parse "os"
* the standard input according to the problem statement. )
**/
func main() { func main() {
var L, H int var L, H int
fmt.Scan(&L, &H) 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++ { for i := 0; i < H; i++ {
var numeral string var numeral string
fmt.Scan(&numeral) 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++ { mayaToDigits := make(map[string]int)
var num1Line string for number, mayaDigit := range digitsToMaya {
fmt.Scan(&num1Line) mayaToDigits[mayaDigit] = number
} }
var S2 int
fmt.Scan(&S2)
for i := 0; i < S2; i++ { firstOperand := extractDigit(H, mayaToDigits)
var num2Line string secondOperand := extractDigit(H, mayaToDigits)
fmt.Scan(&num2Line)
}
var operation string var operation string
fmt.Scan(&operation) fmt.Scan(&operation)
var result int
if operation == "+" {
// fmt.Fprintln(os.Stderr, "Debug messages...") result = firstOperand + secondOperand
fmt.Println("result")// Write answer to stdout } 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
} }