Skip to content

Commit da719c9

Browse files
committed
(simulation) object & exact score computation & we can run w/o gui
1 parent 7ab1e46 commit da719c9

12 files changed

Lines changed: 160 additions & 59 deletions

File tree

config/config.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ type uiConfig struct {
2020
VehicleDefaultColor color.RGBA
2121
}
2222

23-
type simulationConfig struct {
24-
Bonus int
25-
}
26-
2723
type config struct {
28-
UI uiConfig
29-
Simulation simulationConfig
24+
UI uiConfig
3025
}
3126

3227
var Config config
@@ -39,6 +34,4 @@ func init() {
3934
Config.UI.VehicleDefaultColor = colornames.Red
4035
Config.UI.GridColor = colornames.Gray
4136
Config.UI.WindowTitle = "Google Hashcode 2018 - Simulator!"
42-
43-
Config.Simulation.Bonus = 2
4437
}

ghashcode/trip.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func (t *Trip) SomeoneIsOnIt() {
107107
// StartTrip takes the current step as parameter
108108
// if we start it on time we earn the bonus points
109109
// a different color is assigned if we start it on time or not
110-
func (t *Trip) StartTrip(step int) {
110+
func (t *Trip) StartTrip(step int, bonus int16) {
111111
if step == int(t.EarliestStart) {
112-
t.Bonus += config.Config.Simulation.Bonus
112+
t.Bonus += int(bonus)
113113
t.Color = colornames.Gold
114114
return
115115
}
@@ -121,6 +121,7 @@ func (t *Trip) StartTrip(step int) {
121121
// a different is assigned if we end it on time or not
122122
func (t *Trip) Finish(step int32) int {
123123
failed := false
124+
// the vehicle does this even if the arrival step is later than the latest finish
124125
if step > t.LatestFinish {
125126
failed = true
126127
}
@@ -130,6 +131,7 @@ func (t *Trip) Finish(step int32) int {
130131
t.Color = colornames.Green
131132
return t.Distance + t.Bonus
132133
}
134+
// but no points are earned by such a ride
133135
t.Color = colornames.Red
134136
return 0
135137
}

ghashcode/vehicle.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,32 +43,43 @@ func (v *Vehicle) AddToImd(imd *imdraw.IMDraw) {
4343
imd.Line(config.Config.UI.VehicleSize)
4444
}
4545

46-
func (v *Vehicle) Drive(allTrips []*Trip, step int) (score int) {
47-
// it has done every of his trips
46+
func (v *Vehicle) Drive(allTrips []*Trip, step int, bonus int16) (score int) {
47+
// until the vehicle handles all scheduled rides
4848
if v.CurrentTrip >= len(v.Trips) {
4949
return
5050
}
5151

5252
tripToGoTo := allTrips[v.Trips[v.CurrentTrip]]
53+
tripToGoTo.SomeoneIsOnIt()
54+
55+
// first, the vehicle drives from its current intersection ([0,0] at the beginning of the simulation) to the
56+
// start intersection of the next ride (unless the vehicle is already in this intersection)
57+
if !v.OnRide {
58+
v.DriveTo(tripToGoTo.Start.X, tripToGoTo.Start.Y)
59+
}
5360

5461
if v.OnRide {
55-
tripToGoTo.StartTrip(step)
62+
tripToGoTo.StartTrip(step, bonus)
63+
// then, if the current step is earlier than the earliest start of the next ride,
64+
// the vehicle waits until that step
5665
if int32(step) < tripToGoTo.EarliestStart {
5766
tripToGoTo.WarnEarly()
5867
return
5968
}
69+
70+
// then, the vehicle drives to the finish intersection
6071
v.DriveOnTrip(tripToGoTo.End.X, tripToGoTo.End.Y)
6172
currentX, currentY := v.GetPosition()
73+
6274
if currentX == tripToGoTo.End.X && currentY == tripToGoTo.End.Y {
6375
score += tripToGoTo.Finish(int32(step))
76+
// then, the process repeats for the next assigned ride,
6477
v.NextTrip()
6578
return
6679
}
6780
return
6881
}
6982

70-
tripToGoTo.SomeoneIsOnIt()
71-
v.DriveTo(tripToGoTo.Start.X, tripToGoTo.Start.Y)
7283
return
7384
}
7485

@@ -107,6 +118,10 @@ func (v *Vehicle) DriveOnTrip(destinationX, destinationY int32) {
107118
func (v *Vehicle) DriveTo(destinationX, destinationY int32) {
108119
// retrieve current position
109120
currentX, currentY := v.GetPosition()
121+
if destinationX == currentX && destinationY == currentY {
122+
v.OnRide = true
123+
return
124+
}
110125

111126
// calculate the absolute difference between the positions
112127
dx := math.Abs(float64(currentX - destinationX))

main.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,33 @@ var (
2323
frames = 0
2424
second = time.Tick(time.Second)
2525

26+
noGuiFlag *bool
27+
2628
outputFile *string
2729
inputFile *string
2830

2931
camPos = pixel.ZV
3032
camSpeed = 500.0
3133
camZoom = 1.0
3234
camZoomSpeed = 1.2
35+
36+
simulation *simulator.Simulation
3337
)
3438

3539
func init() {
40+
simulation = simulator.NewSimulation()
41+
3642
outputFile = flag.String("o", "", "Path to your result")
3743
inputFile = flag.String("i", "", "Path to the exercice input")
44+
noGuiFlag = flag.Bool("noGui", false, "Run the simulation without a GUI")
45+
3846
flag.Parse()
47+
48+
simulation.ParseOutputFile(*outputFile)
49+
simulation.ParseInputFile(*inputFile)
3950
}
4051

4152
func run() {
42-
vehicles := simulator.ParseOutputFile(*outputFile)
43-
trips := simulator.ParseInputFile(*inputFile)
44-
4553
cfg := pixelgl.WindowConfig{
4654
Title: config.Config.UI.WindowTitle,
4755
Bounds: pixel.R(0, 0, 1024, 720),
@@ -53,19 +61,20 @@ func run() {
5361

5462
tick := time.Tick(6 * time.Millisecond)
5563

56-
score := 0
57-
step := 0
58-
lastStep := 0
5964
imd := imdraw.New(nil)
6065
last := time.Now()
61-
for !win.Closed() {
66+
67+
for !win.Closed() && !simulation.Ended {
6268
imd.Clear()
6369
frames++
6470
dt := time.Since(last).Seconds()
6571
last = time.Now()
6672

6773
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
6874
win.SetMatrix(cam)
75+
if win.JustPressed(pixelgl.KeySpace) {
76+
simulation.Toggle()
77+
}
6978
if win.Pressed(pixelgl.KeyLeft) {
7079
camPos.X -= camSpeed * dt
7180
}
@@ -80,41 +89,42 @@ func run() {
8089
}
8190
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
8291

83-
step++
8492
select {
8593
case <-tick:
8694
win.Clear(colornames.Black)
87-
for _, trip := range trips {
88-
trip.AddToImd(imd)
89-
}
90-
for _, vehicle := range vehicles {
91-
if !vehicle.Enabled {
92-
continue
93-
}
94-
if lastStep != step {
95-
score += vehicle.Drive(trips, step)
96-
}
97-
vehicle.AddToImd(imd)
98-
}
95+
96+
simulation.Run(imd)
97+
9998
imd.Draw(win)
10099

101-
if lastStep != step {
102-
lastStep = step
103-
}
104100
case <-second:
105101
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
106102
frames = 0
107103
}
108104

109105
win.SetMatrix(pixel.IM)
110-
ui.DrawStepNumber(win, step)
111-
ui.DrawScore(win, score)
112-
ui.DrawNumberOfVehicles(win, len(vehicles))
113-
ui.DrawNumberOfTrips(win, len(trips))
106+
ui.DrawStepNumber(win, simulation.Step)
107+
ui.DrawScore(win, simulation.Score)
108+
ui.DrawNumberOfVehicles(win, len(simulation.Vehicles))
109+
ui.DrawNumberOfTrips(win, len(simulation.Trips))
114110
win.Update()
115111
}
112+
113+
fmt.Printf("Score: %d\n", simulation.Score)
114+
}
115+
116+
func noGui() {
117+
simulation.Start()
118+
for !simulation.Ended {
119+
simulation.Run(nil)
120+
}
121+
fmt.Printf("Score: %d in %d steps\n", simulation.Score, simulation.Step)
116122
}
117123

118124
func main() {
125+
if *noGuiFlag == true {
126+
noGui()
127+
return
128+
}
119129
pixelgl.Run(run)
120130
}

screenshots/v1.0-a.png

9.08 KB
Loading

screenshots/v1.0-b.png

10.1 KB
Loading

screenshots/v1.0-c.png

9.74 KB
Loading

screenshots/v1.0-d.png

9.63 KB
Loading

screenshots/v1.0-e.png

9.59 KB
Loading

screenshots/v1.0.png

21.3 KB
Loading

0 commit comments

Comments
 (0)