#Day 04 Discussion

119 messages · Page 1 of 1 (latest)

subtle galleon
#

Spoiler free

rose umbra
#

new to golang, any reason why this didn't work until I set the scanner buffer to a higher value?
||```go
func getwordsearch(f string) [][]byte {
file, err := os.Open(f)
check(err)
defer file.Close()

scanner:= bufio.NewScanner(file)

// Need to increase the capacity since default somehow breaks it
const maxCapacity = 512*1024  
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)

var wordsearch [][]byte
for scanner.Scan() {
    fmt.Println(scanner.Text())
    wordsearch = append(wordsearch, scanner.Bytes())
}

return wordsearch

}

#

somehow the input it got was wrong in a mysterious way i can't describe cause removing the newline at the EOF caused the part1 value to decrease even though it shouldn't

tawdry pecan
#

copying scanner.Bytes() into a new slice before appending that slice is what i did

upbeat edge
#

Oh I hate this kind of challenge I suck at index manipulation and array handling

subtle galleon
#

We've had some practice previous years so was able to think of something

slim acorn
#

gophercrying day 4 is not looking good for me

upbeat edge
#

I had some idea and got the right solution for part 1 but it took a few seconds to compute dead

languid yew
#

I just brute forced sad

subtle galleon
#

I looked for ||the start of a word|| and then || traversed in each direction to check for valid words ||

#

hint ||directions are|| are just:
||

for dx := -1; dx <= 1; dx++ {
    for dy := -1; dy <= 1; dy++ {

||

bitter heron
#

i haven’t had time to actually sit down and implement yet but am i on the right track with thinking ||iterate through input for X’s, do dfs on each X to find all matches|| for part 1?

upbeat edge
bitter heron
subtle galleon
#

That is what I did as well

somber parrot
#

lol so many spoilers:) but good job, I was too tired yesterday evening to check the day’s problem, will look soon

somber parrot
#

my matrix is square, everyone else's too?
140 x 140

#

I wonder if they'd have done conway style a taurus, would have been simpler

somber parrot
#

part2 seems easier

somber parrot
#

to read you just split by \n and then split by "" (and trim last \n or skip last empty line)

somber parrot
keen plover
#

since it may not be a multiple of the line length

#

but why does copying scanner.Bytes() into another slice work lol

somber parrot
#

by setting a buffer you're forcing scanner to reuse your buffer instead of having its own; then you also reuse the same buffer in the loop so ... it's a mess (non scientific definition)

#

let defaults work until they do not; ie you don't need any maxsize etc... just use scanner.Text() or .Byte() without anything else

keen plover
#

i didnt set any buffer, this is howi was reading:

wordSearch = append(wordSearch, scanner.Bytes())
#

and it didnt work properly

keen plover
#

after which it worked

somber parrot
#

how do you create wordSearch (as mentioned you were missing , 0, but you should just have var wordSearch [][]byte (you edited your code since my initial comment so hard to follow now)

keen plover
#

wordSearch := [][]byte{}

#
scanner := bufio.NewScanner(input)
wordSearch := [][]byte{}

for scanner.Scan() {
    wordSearch = append(
        wordSearch, scanner.Bytes(),
    )
}
#

@rose umbra in fact created their own buffer since the default wasnt working

somber parrot
#

if it "wasn't working" it's a different bug

slim fiber
#

this weeks puzzle input is giving me a migraine

somber parrot
#

the default max is 64k and the lines are 140 long

keen plover
somber parrot
#

prove it 🙂 because I'm 100% sure you can read the input no problem with just the default scanner (even if personally I just used ReadAll)

keen plover
#

okay

somber parrot
#

I can also put comments on stuff that doesn't make it the case 🙂

keen plover
#

this is my answer when directly appending scanner.Bytes():

and this is my answer when copying to a new slice first and appending the new slice

#

im not changing anything else

somber parrot
#

paste or post just the reading of the data (wordSearch := [][]byte{} for instance is wrong as it should start nil)

keen plover
#

makes sense now

somber parrot
#

if you use .Text() you don't have that issue (no reuse of buffer)

#

and yes I see why that large buffer "fixes" it... because it doesn't have to recycle and reads the whole thing into the first buffer

keen plover
#

yes

keen plover
somber parrot
#

it's fine to use bytes if you want to (technically I guess they're supposed to be runes, but they didn't trick us with utf8 input anyway) but then know the limitations of https://pkg.go.dev/bufio#Scanner.Bytes

The underlying array may point to data that will be overwritten by a subsequent call to Scan.

keen plover
#

.Text() too does re-use the buffer, its just that a copy (in the form of a string) is being made anyway

somber parrot
#

strings are unmutable so making a copy is mandatory

#

if you want to use bytes I guess the most efficient is to leave the input as is and just know there is a \n on each line and calculate coordinates taking that into account, something like

func ([]byte state) At(x,y) byte {state[y*(size+1)+x]}
ancient dagger
#

Hey ya'll, I made a thing

#

it's to download input from Advent of Code site --using the command line-- from any day and any year without having to go to it and copy paste

#

I'm a go noobie so it certainly can be cleaned up i'm sure but I had fun making it and I used it for today's input!

somber parrot
#

nice! I would skip the cmd/ dir as it's essentially what your module provides, and I don't think you need godotenv either

#

you may want to tell folks how to look in their "inspect" of the browser to get the session cookie

ancient dagger
#

yeaaa I was trying to find best practices for go projects, i had just a repo with main.go in it but i tried to make it more along the lines of the community repo layout

#

godotenv I'm using so it will read from an .env file

#

i like having a .env file with my vars in there

next creek
#

Don't try to make a big architecture if the project is quite simple

somber parrot
next creek
#

Even middle projects like discordgo put everything at the root folder

#

As long as your files describe well what it handles you're good

ancient dagger
#

updated and pushed, ty for the advice

next creek
#

I don't understand what is the purpose of it

somber parrot
#

gj on goreleaser, for go folks you can tell them also to just

go install github.com/Blovio/sleigh@latest
next creek
#

Oh nvm godotenv are there for a reason

ancient dagger
next creek
#

Maybe handle the error no ?

somber parrot
#

well... almost:

$ go install github.com/Blovio/sleigh@latest
go: downloading github.com/Blovio/sleigh v0.1.2
go: github.com/Blovio/sleigh@latest: version constraints conflict:
        github.com/Blovio/[email protected]: parsing go.mod:
        module declares its path as: github.com/jacktrusler/sleigh
                but was required as: github.com/Blovio/sleigh
ancient dagger
ancient dagger
somber parrot
#

fix up your go.mod 🙂

ancient dagger
#

ok try now

next creek
ancient dagger
#

I didn't want an error message if the user didn't have the variable in their .env file

somber parrot
ancient dagger
#

ok just did 🫠

#

this is mildly painful

#

one sec... pls hold

somber parrot
#

worked

$ go install github.com/Blovio/[email protected]
go: downloading github.com/Blovio/sleigh v0.1.3
go: downloading github.com/joho/godotenv v1.5.1

though that last download is... unfortunate 🙂

ancient dagger
#

not as cool for sure... I think there's probably a better way to handle saved variables

#

but i'm used to using .env files for everything

somber parrot
#

I mean its ok if you can't do source .env; sleigh but I wouldn't add a dependencies to do basic env stuff

#

windows?

ancient dagger
#

no mac, i use direnv so it's really a nonissue for me

#

i was just making it nice for people who just wanna drop a .env file in their directory

#

maybe i will delete it... to make it clean

next creek
#

Direnv, isn't this a thing that nixos use ?

#

I don't think direnv supports jetbrains IDEs

ancient dagger
#

i'm not sure, but it's really nice

somber parrot
#

given it's a session cookie it probably expires every day (or it doesn't?)

ancient dagger
#

no it's a 30 day expiry i think

#

2025-12-30T21:20:55.348Z

#

1 year!

somber parrot
#

ah, good. but still, probably folks have a terminal open and change directory every day ( I do mkdir day4; cd day4 etc...) so the .env is pointless imo (but I have a big bias against deps that aren't my own)

ancient dagger
#

I really appreciate the advice, i'm going to remove it and update the README

somber parrot
#

you should make a post in #community-showcase-archived after that (and adding how to get the cookie)

#

people might miss it here

ancient dagger
#

i have an input/ folder, i just dropped my .env in there so i thought

#

i'd add that for people that want to do it without having to source or whatever but i think you're right that most people have something that autoloads their .env

#

i'd caution againt people putting cookies in their .zshrc etc incase they upload their dotfiles

placid hill
#

i keep getting the same number from multiple attempts at part 2 and AoC tells me it's someone else's correct answer but i never changed accounts

#

and i recopied my input and everything and it still tells me that

#

also solves the smaller test case correctly ofc

#

the number is ||1875|| for anyone wondering

placid hill
placid hill
somber parrot
placid hill
#

If you’re using a vpn or signing into that account from another device that could be the reason