this post was submitted on 07 Dec 2025
34 points (97.2% liked)

Advent Of Code

1199 readers
1 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 7: Laboratories

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Camille@lemmy.ml 2 points 2 weeks ago (1 children)

Go

Oh my scheduling God! Part 1 was easy. Then for part 2 I started tracing each particle one by one using goroutines, but spawning billions of goroutines seemed to make my poor laptop sad. So I implemented a whole thread pool with process management and stuff, but nothing worked. So at the end I started doing the unthinkable: using my brain.

It seems we can just reuse the same idea as part 1 one but with a clever counting scheme. Thing works and is as fast as part 1. I'm both happy and deeply sad not to have been able to leverage Go's supposed killer features - which are actually rarely useful when programming things other than servers tbh.

Here we goooooo:

day07.go

package main

import (
	"aoc/utils"
)

func parseStartLine(line string) []bool {
	runes := []rune(line)
	beams := make([]bool, len(runes))

	for idx, char := range runes {
		if char == 'S' {
			beams[idx] = true
		}
	}

	return beams
}

func stepOne(input chan string) (int, error) {
	beams := parseStartLine(<-input)
	splitCount := 0

	for line := range input {
		runes := []rune(line)
		for idx, char := range runes {
			if char == '^' && beams[idx] {
				splitCount++
				if idx > 0 {
					beams[idx-1] = true
				}
				if idx < len(runes)-1 {
					beams[idx+1] = true
				}
				beams[idx] = false
			}
		}
	}

	return splitCount, nil
}

func valueBeams(beams []bool) []int {
	valBeams := make([]int, len(beams))

	for idx, b := range beams {
		val := 0
		if b {
			val = 1
		}
		valBeams[idx] = val
	}

	return valBeams
}

func stepTwo(input chan string) (int, error) {
	beams := valueBeams(parseStartLine(<-input))

	for line := range input {
		runes := []rune(line)
		for idx, char := range runes {
			bc := beams[idx]
			if char == '^' && bc > 0 {
				beams[idx] = 0
				if idx > 0 {
					beams[idx-1] += bc
				}
				if idx < len(runes)-1 {
					beams[idx+1] += bc
				}
			}
		}
	}

	sum := 0
	for _, bc := range beams {
		sum += bc
	}

	return sum, nil
}

func main() {
	inputFile := utils.FilePath("day07.txt")
	utils.RunStep(utils.ONE, inputFile, stepOne)
	utils.RunStep(utils.TWO, inputFile, stepTwo)
}

[โ€“] CameronDev@programming.dev 4 points 2 weeks ago (1 children)

~32B coroutines does sound like it might make any CPU a bit sad :D

[โ€“] Camille@lemmy.ml 3 points 2 weeks ago

Yeah, the process was terminated by Linux in about 2-3 mins x)