This repository has been archived on 2024-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
SD/sd.go

98 lines
1.8 KiB
Go
Raw Permalink Normal View History

2022-03-27 22:35:46 +00:00
package main
import (
2022-03-28 15:20:00 +00:00
"database/sql"
"flag"
2022-03-27 23:09:22 +00:00
"fmt"
2022-03-28 16:16:29 +00:00
"math/rand"
"os"
2022-03-27 23:09:22 +00:00
"os/exec"
2022-03-29 12:46:37 +00:00
"time"
2022-03-28 15:20:00 +00:00
_ "modernc.org/sqlite"
2022-03-27 22:35:46 +00:00
)
2022-03-29 02:20:38 +00:00
var (
verbose = flag.Bool("v", false, "debug output")
file = flag.String("f", "cards", "cards file")
)
2022-03-27 22:35:46 +00:00
func main() {
2022-03-29 12:46:37 +00:00
if !*verbose {
// Seed the RNG
rand.Seed(time.Now().UnixNano())
}
flag.Parse()
2022-03-28 15:20:00 +00:00
db, err := sql.Open("sqlite", *file)
if err != nil {
panic(err)
}
// Get number of cards
var N int
_ = db.QueryRow("SELECT COUNT(*) FROM cards").Scan(&N)
2022-03-28 17:04:13 +00:00
s := segmentTree{N, make([]int, 4*N)}
2022-03-28 15:20:00 +00:00
// Build segment tree
2022-03-28 16:16:29 +00:00
rows, err := db.Query("SELECT weight FROM cards")
2022-03-28 15:20:00 +00:00
if err != nil {
panic(err)
}
s.Build(rows, 0, N-1, 1)
rows.Close()
2022-03-28 15:20:00 +00:00
2022-03-28 16:51:19 +00:00
if *verbose {
2022-03-28 17:04:13 +00:00
fmt.Println(s)
2022-03-28 16:51:19 +00:00
}
2022-03-27 23:09:22 +00:00
// https://stackoverflow.com/questions/14094190/function-similar-to-getchar
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// do not display entered characters on the screen
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
// restore the echoing state when exiting
defer exec.Command("stty", "-F", "/dev/tty", "echo").Run()
2022-03-27 23:02:49 +00:00
2022-03-27 23:09:22 +00:00
for {
2022-03-28 16:16:29 +00:00
// Choose a random card
x := rand.Intn(s.seg[1])
w, i := s.Query(x, 0, N-1, 1)
2022-03-28 16:51:19 +00:00
if *verbose {
2022-03-29 12:43:57 +00:00
fmt.Println(s.seg[1])
2022-03-28 16:51:19 +00:00
fmt.Println(x)
fmt.Println(w)
fmt.Println(i)
2022-03-28 16:51:19 +00:00
}
// Get card contents from database
2022-03-28 16:16:29 +00:00
var key, val string
db.QueryRow("SELECT key, val FROM cards WHERE idx=?", i).Scan(&key, &val)
2022-03-30 14:06:00 +00:00
fmt.Println("> " + key)
// Wait for confirmation
var b []byte = make([]byte, 1)
os.Stdin.Read(b)
2022-03-28 16:16:29 +00:00
fmt.Println(val)
2022-03-27 23:26:22 +00:00
// Read user input
2022-03-27 23:09:22 +00:00
os.Stdin.Read(b)
2022-03-28 15:20:00 +00:00
if b[0] == byte('y') {
2022-03-28 16:16:29 +00:00
w >>= 1
2022-03-28 15:20:00 +00:00
} else if b[0] == byte('n') {
2022-03-31 03:27:55 +00:00
w <<= 3
2022-03-27 23:09:22 +00:00
} else {
return
2022-03-27 23:09:22 +00:00
}
// Update segment tree and database
s.Update(i, w, 0, N-1, 1)
_, err = db.Exec("UPDATE cards SET weight=? WHERE idx=?", w, i)
if err != nil {
panic(err)
}
2022-03-27 23:09:22 +00:00
}
2022-03-27 22:35:46 +00:00
}