-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (69 loc) · 1.7 KB
/
main.go
File metadata and controls
86 lines (69 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bufio"
"context"
"encoding/json"
"log"
"math"
"net"
"github.com/tlgs/protohackers/internal/protohackers"
)
type PrimeTime struct{ *protohackers.Config }
func (PrimeTime) Setup() context.Context { return context.TODO() }
type Request struct {
Method string `json:"method"`
Number *float64 `json:"number"`
}
type Response struct {
Method string `json:"method"`
Prime bool `json:"prime"`
}
func isPrime(n int) bool {
if n <= 3 {
return n > 1
} else if n%2 == 0 || n%3 == 0 {
return false
}
for i := 5; i*i <= n; i += 6 {
if n%i == 0 || n%(i+2) == 0 {
return false
}
}
return true
}
func (PrimeTime) Handle(_ context.Context, conn net.Conn) {
addr := conn.RemoteAddr()
log.Printf("accepted connection: %v", addr)
defer func() {
conn.Close()
log.Printf("closed connection: %v", addr)
}()
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
in := scanner.Bytes()
var out []byte
var req Request
err := json.Unmarshal(in, &req)
if err != nil || req.Method != "isPrime" || req.Number == nil {
out = []byte("💩")
} else {
// this is not robust enough and would fail if a float
// such as `7.0` (which is not an integer) was passed.
// luckily, the challenge input does not have such a test case. :)
f := *req.Number
primality := f == math.Trunc(f) && isPrime(int(f))
out, _ = json.Marshal(Response{"isPrime", primality})
}
out = append(out, byte('\n'))
if _, err = conn.Write(out); err != nil {
log.Printf("%v (%v)", err, addr)
} else {
log.Printf("%#q ⇒ %#q (%v)", in, out[:len(out)-1], addr)
}
}
}
func main() {
cfg := protohackers.NewConfig(10001)
cfg.ParseFlags()
protohackers.RunTCP(PrimeTime{cfg})
}