this post was submitted on 03 Dec 2025
26 points (100.0% liked)

Advent Of Code

1199 readers
9 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 3: Lobby

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 3 points 3 weeks ago

Go

I usually write a little helper library to bootstrap the input reading process and sometimes even the downloading of the input file. So here it is:

utils.go

package utils

import (
	"bufio"
	"os"
	"strings"
)

type Input interface {
	GetLineChannel() (chan string, error)
}

type FilePath string
type InputText string

func (path FilePath) GetLineChannel() (chan string, error) {
	file, err := os.Open(string(path))
	if err != nil {
		return nil, err
	}

	scanner := bufio.NewScanner(file)

	ch := make(chan string, 1024)
	go (func() {
		defer file.Close()

		for scanner.Scan() {
			ch <- scanner.Text()
		}

		close(ch)
	})()

	return ch, nil
}

func (inputText InputText) GetLineChannel() (chan string, error) {
	lines := strings.Split(string(inputText), "\n")
	ch := make(chan string, len(lines))

	go (func() {
		for _, line := range lines {
			ch <- line
		}

		close(ch)
	})()

	return ch, nil
}

And here comes the solution to day 3:

package main

import (
	"aoc/utils"
	"errors"
	"fmt"
	"math"
)

const inputText = `987654321111111
811111111111119
234234234234278
818181911112111`

type bank []int

func (bk bank) largestNDigitJoltage(n int) int {
	digits := make([]int, n)
	count := 0
	idx := 0

	lenbk := len(bk)

	for range n {
		for i := idx; i < lenbk-(n-count-1); i++ {
			val := bk[i]
			if val > digits[count] {
				idx = i + 1
				digits[count] = val
			}
		}
		count++
	}

	sum := 0
	for index, val := range digits {
		sum += val * int(math.Pow10(n-index-1))
	}

	return sum
}

func readBank(line string) (bank, error) {
	runes := []rune(line)
	bk := make(bank, len(runes))
	for idx, c := range runes {
		switch c {
		case '0':
			bk[idx] = 0
		case '1':
			bk[idx] = 1
		case '2':
			bk[idx] = 2
		case '3':
			bk[idx] = 3
		case '4':
			bk[idx] = 4
		case '5':
			bk[idx] = 5
		case '6':
			bk[idx] = 6
		case '7':
			bk[idx] = 7
		case '8':
			bk[idx] = 8
		case '9':
			bk[idx] = 9
		default:
			msg := fmt.Sprintf("not a number: %c", c)
			return bank{}, errors.New(msg)
		}
	}
	return bk, nil
}

func getBankChannel(input chan string) chan bank {
	ch := make(chan bank, cap(input))

	go func() {
		for line := range input {
			bank, err := readBank(line)
			if err != nil {
				fmt.Errorf("error reading line %v: %v\n", line, err)
				close(ch)
				return
			}
			ch <- bank
		}
		close(ch)
	}()

	return ch
}

func stepOne(input chan string) (int, error) {
	ch := getBankChannel(input)
	sum := 0
	for bank := range ch {
		sum += bank.largestNDigitJoltage(2)
	}

	return sum, nil
}

func stepTwo(input chan string) (int, error) {
	ch := getBankChannel(input)
	sum := 0
	for bank := range ch {
		sum += bank.largestNDigitJoltage(12)
	}

	return sum, nil
}

func main() {
	// input2 := utils.InputText(inputText)
	input := utils.FilePath("day03.txt")

	ch, err := input.GetLineChannel()
	if err != nil {
		fmt.Errorf("step one error: %v\n", err)
		return
	}

	var one int
	one, err = stepOne(ch)
	if err != nil {
		fmt.Errorf("step one error: %v\n", err)
		return
	}
	fmt.Printf("Step one result: %v\n", one)

	// input2 := utils.InputText(inputText)
	input2 := utils.FilePath("day03.txt")

	ch, err = input2.GetLineChannel()
	if err != nil {
		fmt.Errorf("step two error: %v\n", err)
		return
	}

	var two int
	two, err = stepTwo(ch)
	if err != nil {
		fmt.Errorf("step two error: %v\n", err)
		return
	}
	fmt.Printf("Step two result: %v\n", two)
}

While I am quite an adaptable person and I learn to program quickly in about all the languages I've tried, I'm still at the beginning of my journey with Go. It does feel like the language is trying to resist me being clever at every corner. I understand the reasons, why not, but damn it does make the development a bit frustrating at times