Unable to load image

Advent of Code Day 5, AKA Stacks 101 :marseyinabox:

I actually stayed up to get a semi-decent score for once (being a eurocel for this is suffering). How are you all faring with shifting boxes around?

![](/images/16702184438592093.webp)

26
Jump in the discussion.

No email address required.

I'm about three steps away from just switching to perl or awk or something holy shit

Jump in the discussion.

No email address required.

I give up my code magically shit itself and I don't care to get part two, it sucks dick and balls (and a little cock) anyways. any suggestions welcome.


package five

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strconv"
)

func stackInit(m *map[int]string, rd *bufio.Scanner) {
	const frame_break_len int = 9
	const frame_break string = " 1   2   3   4   5   6   7   8   9 "

	col := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
		if atEOF && len(data) == 0 {
			return 0, nil, nil
		}

		if len(data) >= 4 {
			return 4, data[0:4], nil
		}

		if atEOF {
			return len(data), data, nil
		}

		return
	}

	rd.Split(col)

	var buffer string
	for i := 0; rd.Scan(); i++ {
		word := rd.Text()
		buffer = fmt.Sprintf("%s%s", buffer, word)

		if i%frame_break_len == 0 {
			if bytes.Contains([]byte(buffer), []byte(frame_break)) {
				break
			}
			if bytes.ContainsRune([]byte(buffer), '\n') {
				npos := bytes.IndexRune([]byte(buffer), '\n') + 1
				buffer = buffer[npos:]
			}
		}

		p := i % frame_break_len

		val := bytes.TrimSpace([]byte(word))
		if bytes.ContainsRune(val, '[') && len(val) > 0 {
			(*m)[p] = fmt.Sprintf("%s%c", (*m)[p], val[1])
		}
	}
}

func readLine(m *map[int]string, line string, do_rev bool) {
	elem := bytes.Split([]byte(line), []byte(" "))

	rev := func(str []byte) string {
		var reverse []byte

		if len(str) > 1 {
			for i := len(str) - 1; i >= 0; i-- {
				reverse = append(reverse, str[i])
			}

			return string(reverse)
		}

		return string(str)
	}

	var amt, crate, dest int
	for i, v := range elem {
		switch string(v) {
		case "move":
			amt, _ = strconv.Atoi(string(elem[i+1]))
		case "from":
			crate, _ = strconv.Atoi(string(elem[i+1]))
			crate -= 1
		case "to":
			dest, _ = strconv.Atoi(string(elem[i+1]))
			dest -= 1
		default:
		}
	}

	if do_rev {
		(*m)[dest] = fmt.Sprintf("%s%s", rev([]byte((*m)[crate][:amt])), (*m)[dest])
	} else {
		(*m)[dest] = fmt.Sprintf("%s%s", (*m)[dest], (*m)[crate][:amt])
	}
	(*m)[crate] = (*m)[crate][amt:]
}

func One() string {
	var deliver []byte

	fp, _ := os.Open("stack.txt")
	defer fp.Close()

	rd := bufio.NewScanner(fp)

	s := make(map[int]string)

	stackInit(&s, rd)

	lp, _ := os.Open("moves.txt")
	defer lp.Close()

	ld := bufio.NewScanner(lp)
	ld.Split(bufio.ScanLines)

	for ld.Scan() {
		word := ld.Text()
		if len(word) > 0 {
			readLine(&s, word, true)

		}
	}

	for i := range s {
		deliver = append(deliver, s[i][0])
	}

	return string(deliver)
}

func Two() string {
	var deliver []byte

	//fp, _ := os.Open("stack.txt")
	//defer fp.Close()

	//rd := bufio.NewScanner(fp)

	s := make(map[int]string)

	//magically stopped working I'm not fixing it
	//stackInit(&s, rd)
	s[0] = "SPHVFG"
	s[1] = "MZDVBFJG"
	s[2] = "NJLMG"
	s[3] = "PWDVZGN"
	s[4] = "BCRV"
	s[5] = "ZLWPMSRV"
	s[6] = "PHT"
	s[7] = "VZHCNSRQ"
	s[8] = "JQVPGLF"

	lp, _ := os.Open("moves.txt")
	defer lp.Close()

	ld := bufio.NewScanner(lp)
	ld.Split(bufio.ScanLines)

	for ld.Scan() {
		word := ld.Text()
		if len(word) > 0 {
			readLine(&s, word, false)

		}
	}

	for i := range s {
		deliver = append(deliver, s[i][0])
	}

	return string(deliver)
}
Jump in the discussion.

No email address required.

Your pulitzer's in the mail

Jump in the discussion.

No email address required.



Now playing: Bad Boss Boogie (DKC).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.