Skip to content

Instantly share code, notes, and snippets.

@8lall0
Last active October 26, 2023 22:48
Show Gist options
  • Save 8lall0/80bfde652581cee6cc153dd272f2b7ff to your computer and use it in GitHub Desktop.
Save 8lall0/80bfde652581cee6cc153dd272f2b7ff to your computer and use it in GitHub Desktop.
Code100Puzzle - Code Block
package main
import (
"encoding/json"
"fmt"
"strings"
)
type CodeCompetition struct {
Columns int `json:"columns"`
PadChar string `json:"padChar"`
Events []struct {
Name string `json:"name"`
Location string `json:"location,omitempty"`
Date string `json:"date"`
} `json:"events"`
}
func main() {
input := `{
"columns": 80,
"padChar": "·",
"events": [
{ "name": "Code 100", "location": "Zagreb, Croatia", "date": "29.11.2023" },
{ "name": "LIVE", "date": "ongoing" },
{ "name": "Coffee With Developers", "location": "various", "date": "ongoing"} ,
{ "name": "World Congress","location": "Berlin, Germany", "date":"17-19.07.2024" }
]
}`
a := new(CodeCompetition)
err := json.Unmarshal([]byte(input), a)
if err != nil {
fmt.Println(err)
return
}
dottedString := strings.Repeat(a.PadChar, a.Columns)
output := dottedString + "\n"
for _, event := range a.Events {
strRune := []rune(dottedString)
if event.Name != "" {
name := fmt.Sprintf(" %s ", event.Name)
ndx := 1
for i, c := range name {
strRune[i+ndx] = c
}
}
if event.Location != "" {
location := fmt.Sprintf(" %s ", event.Location)
ndx := a.Columns/2 - len(location)/2
for i, c := range location {
strRune[i+ndx] = c
}
}
if event.Date != "" {
date := fmt.Sprintf(" %s ", event.Date)
ndx := a.Columns - 1 - len(date)
for i, c := range date {
strRune[i+ndx] = c
}
}
output += string(strRune) + "\n"
}
output += dottedString + "\n"
fmt.Println(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment