clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

feat/app-decomposition
puzzletea / export / pdf / parse_takuzuplus.go
parse_takuzuplus.go 2.4K
1package pdfexport23import (4 "encoding/json"5 "fmt"6 "strings"7)89type takuzuPlusSave struct {10 Size int `json:"size"`11 State string `json:"state"`12 Provided string `json:"provided"`13 HorizontalRelations string `json:"horizontal_relations"`14 VerticalRelations string `json:"vertical_relations"`15}1617func ParseTakuzuPlusPrintData(saveData []byte) (*TakuzuData, error) {18 if len(strings.TrimSpace(string(saveData))) == 0 {19 return nil, nil20 }2122 var save takuzuPlusSave23 if err := json.Unmarshal(saveData, &save); err != nil {24 return nil, fmt.Errorf("decode takuzu+ save: %w", err)25 }2627 stateRows := splitNormalizedLines(save.State)28 providedRows := splitNormalizedLines(save.Provided)29 horizontalRows := splitNormalizedLines(save.HorizontalRelations)30 verticalRows := splitNormalizedLines(save.VerticalRelations)3132 size := save.Size33 if size <= 0 {34 size = max(max(len(stateRows), len(providedRows)), max(len(horizontalRows), len(verticalRows)+1))35 }36 if size <= 0 {37 return nil, nil38 }3940 givens := make([][]string, size)41 horizontal := make([][]string, size)42 vertical := make([][]string, max(size-1, 0))43 for y := 0; y < size; y++ {44 givens[y] = make([]string, size)45 horizontal[y] = make([]string, max(size-1, 0))4647 var stateRunes []rune48 if y < len(stateRows) {49 stateRunes = []rune(stateRows[y])50 }51 var providedRunes []rune52 if y < len(providedRows) {53 providedRunes = []rune(providedRows[y])54 }55 var relationRunes []rune56 if y < len(horizontalRows) {57 relationRunes = []rune(horizontalRows[y])58 }5960 for x := 0; x < size; x++ {61 if x < size-1 && x < len(relationRunes) && (relationRunes[x] == '=' || relationRunes[x] == 'x') {62 horizontal[y][x] = string(relationRunes[x])63 }64 if x >= len(providedRunes) || providedRunes[x] != '#' || x >= len(stateRunes) {65 continue66 }67 if stateRunes[x] != '0' && stateRunes[x] != '1' {68 continue69 }70 givens[y][x] = string(stateRunes[x])71 }72 }7374 for y := 0; y < size-1; y++ {75 vertical[y] = make([]string, size)76 if y >= len(verticalRows) {77 continue78 }79 relationRunes := []rune(verticalRows[y])80 for x := 0; x < size && x < len(relationRunes); x++ {81 if relationRunes[x] == '=' || relationRunes[x] == 'x' {82 vertical[y][x] = string(relationRunes[x])83 }84 }85 }8687 return &TakuzuData{88 Size: size,89 Givens: givens,90 HorizontalRelations: horizontal,91 VerticalRelations: vertical,92 GroupEveryTwo: true,93 }, nil94}