Skip to content

Commit

Permalink
Merge pull request #22 from vaskoz/day10
Browse files Browse the repository at this point in the history
Day10
  • Loading branch information
vaskoz committed Aug 31, 2018
2 parents 6f3a498 + d1b9ecf commit 9b4be61
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ problems from
* [Day 7](https://github.com/vaskoz/dailycodingproblem-go/issues/15)
* [Day 8](https://github.com/vaskoz/dailycodingproblem-go/issues/17)
* [Day 9](https://github.com/vaskoz/dailycodingproblem-go/issues/19)
* [Day 10](https://github.com/vaskoz/dailycodingproblem-go/issues/21)
60 changes: 60 additions & 0 deletions day10/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package day10

import (
"fmt"
"log"
"sync"
"time"
)

// Scheduler represents a job scheduler.
type Scheduler struct {
queue chan job
wg sync.WaitGroup
startTime time.Time
}

type job struct {
f func()
n uint
jobStartTime time.Time
}

// NewScheduler returns a new instance of a running job scheduler.
func NewScheduler() *Scheduler {
s := &Scheduler{queue: make(chan job), startTime: time.Now()}
s.wg.Add(1)
go func() {
for j := range s.queue {
s.wg.Add(1)
go func(j job) {
ms, _ := time.ParseDuration(fmt.Sprintf("%dms", j.n))
time.Sleep(ms)
log.Printf("starting job at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds()))
j.f()
log.Printf("completed job at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds()))
s.wg.Done()
}(j)
}
}()
return s
}

// Schedule schedules a new job to be run.
func (s *Scheduler) Schedule(f func(), millis uint) {
current := convertNanoToMillis(time.Since(s.startTime).Nanoseconds())
log.Printf("Received at %dms scheduled for %dms", current, current+int64(millis))
s.queue <- job{f, millis, time.Now()}
}

// Shutdown prevents new jobs from being scheduled and allows the scheduler to terminate.
func (s *Scheduler) Shutdown() {
close(s.queue)
s.wg.Done()
s.wg.Wait()
log.Printf("Scheduler shutdown complete at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds()))
}

func convertNanoToMillis(duration int64) int64 {
return duration / int64(1000000)
}
30 changes: 30 additions & 0 deletions day10/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day10

import (
"fmt"
"testing"
"time"
)

func TestScheduler(t *testing.T) {
t.Parallel()
sched := NewScheduler()
sched.Schedule(func() { fmt.Println("hi there") }, 10)
sched.Schedule(func() { fmt.Println("hello there") }, 20)
sched.Schedule(func() { fmt.Println("so long") }, 30)
sched.Schedule(func() { time.Sleep(1 * time.Second); fmt.Println("i fell asleep") }, 1)
sched.Shutdown()
}

func TestSchedulerParallel(t *testing.T) {
t.Parallel()
sched := NewScheduler()
sched.Schedule(func() { fmt.Println("hi there") }, 10)
sched.Schedule(func() { fmt.Println("hello there") }, 20)
sched.Schedule(func() { fmt.Println("so long") }, 30)
sched.Schedule(func() { time.Sleep(2 * time.Second); fmt.Println("i fell asleep again") }, 1)
time.Sleep(2 * time.Second)
sched.Schedule(func() { fmt.Println("later jobs") }, 1)
sched.Schedule(func() { fmt.Println("later still") }, 10)
sched.Shutdown()
}
1 change: 1 addition & 0 deletions day7/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var testcases = []struct {
}

func TestNumberOfDecodings(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := NumberOfDecodings(tc.input); result != tc.expected {
t.Errorf("For input %v, expected %v but got %v", tc.input, tc.expected, result)
Expand Down
1 change: 1 addition & 0 deletions day8/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package day8
import "testing"

func TestCountUnivalSubtrees(t *testing.T) {
t.Parallel()
root := &BinaryTree{value: 0,
left: &BinaryTree{value: 1},
right: &BinaryTree{value: 0,
Expand Down
1 change: 1 addition & 0 deletions day9/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var testcases = []struct {
}

func TestMaximumNonAdjacentSum(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := MaximumNonAdjacentSum(tc.input); result != tc.expected {
t.Errorf("Expected %v, but got %v", tc.expected, result)
Expand Down

0 comments on commit 9b4be61

Please sign in to comment.