← run

go-04-map-concurrent

0.000
0/1 tests· concurrency

Proposed solution

```go
package challenge

import (
	"sync"
)

func MapConcurrent(inputs []int, workers int, fn func(int) int) []int {
	if len(inputs) == 0 {
		return []int{}
	}

	// Cap workers to the number of inputs
	if workers > len(inputs) {
		workers = len(inputs)
	}

	// Create a channel to send work items
	work := make(chan int, len(inputs))
	
	// Create a channel to receive results
	results := make(chan struct {
		index int
		value int
	}, len(inputs))

	var wg sync.WaitGroup

	// Start workers
	for i := 0; i < workers; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for index := range work {
				value := fn(index)
				results <- struct {
					index int
					value int
				}{index: index, value: value}
			}
		}()
	}

	// Send all work items
	for i, input := range inputs {
		work <- i // We send the index to avoid capturing the loop variable
	}
	close(work)

	// Collect results
	finalResults := make([]int, len(inputs))
	
	// Wait for all workers to finish
	go func() {
		wg.Wait()
		close(results)
	}()

	// Collect results in order
	for result := range results {
		finalResults[result.index] = result.value
	}

	return finalResults
}
```

Test output (stdout)

{"ImportPath":"challenge [challenge.test]","Action":"build-output","Output":"# challenge [challenge.test]\n"}
{"ImportPath":"challenge [challenge.test]","Action":"build-output","Output":"./solution.go:44:9: declared and not used: input\n"}
{"ImportPath":"challenge [challenge.test]","Action":"build-fail"}
{"Time":"2026-06-30T06:25:40.461799284+02:00","Action":"start","Package":"challenge"}
{"Time":"2026-06-30T06:25:40.461861984+02:00","Action":"output","Package":"challenge","Output":"FAIL\tchallenge [build failed]\n"}
{"Time":"2026-06-30T06:25:40.461866544+02:00","Action":"fail","Package":"challenge","Elapsed":0,"FailedBuild":"challenge [challenge.test]"}
Self-repair attempts
[
  {
    "total": 1,
    "answer": "```go\npackage challenge\n\nimport (\n\t\"sync\"\n)\n\nfunc MapConcurrent(inputs []int, workers int, fn func(int) int) []int {\n\tif len(inputs) == 0 {\n\t\treturn []int{}\n\t}\n\n\t// Cap workers to the number of inputs\n\tif workers > len(inputs) {\n\t\tworkers = len(inputs)\n\t}\n\n\t// Create a channel to send work items\n\twork := make(chan int, len(inputs))\n\t\n\t// Create a channel to receive results\n\tresults := make(chan struct {\n\t\tindex int\n\t\tvalue int\n\t}, len(inputs))\n\n\tvar wg sync.WaitGroup\n\n\t// Start workers\n\tfor i := 0; i < workers; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\tfor index := range work {\n\t\t\t\tvalue := fn(index)\n\t\t\t\tresults <- struct {\n\t\t\t\t\tindex int\n\t\t\t\t\tvalue int\n\t\t\t\t}{index: index, value: value}\n\t\t\t}\n\t\t}()\n\t}\n\n\t// Send all work items\n\tfor i, input := range inputs {\n\t\twork <- i // We send the index to avoid capturing the loop variable\n\t}\n\tclose(work)\n\n\t// Collect results\n\tfinalResults := make([]int, len(inputs))\n\t\n\t// Wait for all workers to finish\n\tgo func() {\n\t\twg.Wait()\n\t\tclose(results)\n\t}()\n\n\t// Collect results in order\n\tfor result := range results {\n\t\tfinalResults[result.index] = result.value\n\t}\n\n\treturn finalResults\n}\n```",
    "passed": 0,
    "test_error": "{\"ImportPath\":\"challenge [challenge.test]\",\"Action\":\"build-output\",\"Output\":\"# challenge [challenge.test]\\n\"}\n{\"ImportPath\":\"challenge [challenge.test]\",\"Action\":\"build-output\",\"Output\":\"./solution.go:44:9: declared and not used: input\\n\"}\n{\"ImportPath\":\"challenge [challenge.test]\",\"Action\":\"build-fail\"}\n{\"Time\":\"2026-06-30T06:25:40.461799284+02:00\",\"Action\":\"start\",\"Package\":\"challenge\"}\n{\"Time\":\"2026-06-30T06:25:40.461861984+02:00\",\"Action\":\"output\",\"Package\":\"challenge\",\"Output\":\"FAIL\\tchallenge [build failed]\\n\"}\n{\"Time\":\"2026-06-30T06:25:40.461866544+02:00\",\"Action\":\"fail\",\"Package\":\"challenge\",\"Elapsed\":0,\"FailedBuild\":\"challenge [challenge.test]\"}"
  },
  {
    "total": 5,
    "answer": "Looking at the error, there's a variable `input` that's declared but not used. Let me analyze the code more carefully and fix it.\n\nThe issue is in my implementation - I'm sending indices to the work channel but then trying to use the actual input values. I need to properly handle the mapping between indices and values.\n\nLet me rewrite this correctly:\n\n```go\npackage challenge\n\nimport (\n\t\"sync\"\n)\n\nfunc MapConcurrent(inputs []int, workers int, fn func(int) int) []int {\n\tif len(inputs) == 0 {\n\t\treturn []int{}\n\t}\n\n\t// Cap workers to the number of inputs\n\tif workers > len(inputs) {\n\t\tworkers = len(inputs)\n\t}\n\n\t// Create a channel to send work items (index and value)\n\twork := make(chan struct {\n\t\tindex int\n\t\tvalue int\n\t}, len(inputs))\n\t\n\t// Create a channel to receive results\n\tresults := make(chan struct {\n\t\tindex int\n\t\tvalue int\n\t}, len(inputs))\n\n\tvar wg sync.WaitGroup\n\n\t// Start workers\n\tfor i := 0; i < workers; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\tfor workItem := range work {\n\t\t\t\tvalue := fn(workItem.value)\n\t\t\t\tresults <- struct {\n\t\t\t\t\tindex int\n\t\t\t\t\tvalue int\n\t\t\t\t}{index: workItem.index, value: value}\n\t\t\t}\n\t\t}()\n\t}\n\n\t// Send all work items\n\tfor i, input := range inputs {\n\t\twork <- struct {\n\t\t\tindex int\n\t\t\tvalue int\n\t\t}{index: i, value: input}\n\t}\n\tclose(work)\n\n\t// Collect results\n\tfinalResults := make([]int, len(inputs))\n\t\n\t// Wait for all workers to finish\n\tgo func() {\n\t\twg.Wait()\n\t\tclose(results)\n\t}()\n\n\t// Collect results in order\n\tfor result := range results {\n\t\tfinalResults[result.index] = result.value\n\t}\n\n\treturn finalResults\n}\n```",
    "passed": 5,
    "test_error": ""
  }
]
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.