Skip to content

Commit 997eb81

Browse files
committed
Print reports and end at 8:10
1 parent 365c78b commit 997eb81

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

main.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,37 @@ func main() {
1919
tubeStations = LoadTubeStations()
2020

2121
ids := []int{5937, 6043}
22-
done := make(chan int)
22+
report, done := make(chan string), make(chan int)
2323

2424
for _, id := range ids {
25-
go Dispatch(Robot{id, LatLong{0, 0}}, done)
25+
go Dispatch(Robot{id, LatLong{0, 0}}, report, done)
2626
}
2727

28+
go PrintReports(report)
29+
2830
for i := len(ids); i > 0; {
2931
<-done
3032
i--
3133
}
3234
}
3335

34-
func Dispatch(r Robot, done chan int) {
35-
fmt.Println("Dispatch")
36+
func Dispatch(r Robot, report chan string, done chan int) {
3637
ch := make(chan Instruction, 10) // Only allow 10 instructions in queue at a time
3738
go ReadInstructions(r.ID, ch)
3839

3940
for instruction := range ch {
40-
r.ReceiveInstruction(instruction)
41-
fmt.Println(r.ID, instruction)
41+
hour, min, _ := instruction.Time.Clock()
42+
if hour < 8 || (hour == 8 && min < 10) { // End at 8:10
43+
r.Instruct(instruction, report)
44+
} else {
45+
break
46+
}
4247
}
4348

4449
done <- 1
4550
}
4651

4752
func ReadInstructions(id int, ch chan Instruction) {
48-
fmt.Println("ReadInstructions")
4953
file, _ := os.Open(fmt.Sprintf("%v.csv", id))
5054
defer file.Close()
5155

@@ -66,3 +70,9 @@ func ReadInstructions(id int, ch chan Instruction) {
6670

6771
close(ch)
6872
}
73+
74+
func PrintReports(report chan string) {
75+
for {
76+
fmt.Println(<-report)
77+
}
78+
}

robot.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"math/rand"
45
"time"
56
"fmt"
67
)
@@ -10,20 +11,41 @@ type Robot struct{
1011
Location LatLong
1112
}
1213

13-
func (r *Robot) ReceiveInstruction(instruction Instruction) {
14+
func (r *Robot) Instruct(instruction Instruction, report chan string) {
1415
r.Move(instruction.Location)
16+
if r.IsNearTheTube() {
17+
report <- fmt.Sprintf("%v, %v, %v, %v", r.ID, instruction.Time.Format("15:04:05"), r.Location, traffic())
18+
}
1519
}
1620

17-
const speed int64 = 100000 // Meters per hour (100kph)
21+
const speed int64 = 500000 // Meters per hour (500kph), pretty quick
1822

1923
func (r *Robot) Move(newLocation LatLong) {
24+
// Robots move at constant speed in a staright line
2025
startingLocation := LatLong{0, 0}
2126
if r.Location != startingLocation { // Assume it takes no time to move to starting location
2227
time.Sleep(time.Duration(int64(r.Location.DistanceFrom(newLocation)) * int64(time.Hour) / speed))
2328
}
2429
r.Location = newLocation
2530
}
2631

27-
func (r *Robot) IsNearTheTube(tubeStations []TubeStation) bool {
28-
return true
32+
func (r *Robot) IsNearTheTube() bool {
33+
for _, station := range tubeStations { // Seems like a dreadful way to search for a nearby station
34+
if r.Location.DistanceFrom(station.Location) <= 350.0 {
35+
return true
36+
}
37+
}
38+
return false
39+
}
40+
41+
func traffic() string {
42+
switch rand.Intn(3) {
43+
case 0:
44+
return "HEAVY"
45+
case 1:
46+
return "MODERATE"
47+
case 2:
48+
return "LIGHT"
49+
}
50+
return ""
2951
}

0 commit comments

Comments
 (0)