package main

import (
	"fmt"
	"sync"
)

type Job struct {
	ID    int
	Value int
}

type Result struct {
	ID    int
	Value int
}

func runPool(input []int, workerCount int) []int {
	if workerCount < 1 {
		workerCount = 1
	}

	jobs := make(chan Job)
	results := make(chan Result)
	var workers sync.WaitGroup

	for range workerCount {
		workers.Go(func() {
			for job := range jobs {
				results <- Result{
					ID:    job.ID,
					Value: job.Value * job.Value,
				}
			}
		})
	}

	go func() {
		defer close(jobs)
		for id, value := range input {
			jobs <- Job{ID: id, Value: value}
		}
	}()

	go func() {
		workers.Wait()
		close(results)
	}()

	output := make([]int, len(input))
	for result := range results {
		output[result.ID] = result.Value
	}
	return output
}

func main() {
	fmt.Println(runPool([]int{2, 3, 4, 5}, 3))
}
