#Day 04 Discussion
119 messages · Page 1 of 1 (latest)
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
copying scanner.Bytes() into a new slice before appending that slice is what i did
Oh I hate this kind of challenge I suck at index manipulation and array handling
We've had some practice previous years so was able to think of something
day 4 is not looking good for me
I had some idea and got the right solution for part 1 but it took a few seconds to compute 
I just brute forced 
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++ {
||
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?
|| yeah that's a good solution (not exactly dfs) ||
||thanks, yeah i wasn’t sure exactly what to call it, just searching in each direction and breaking as soon as its not a match||
That is what I did as well
lol so many spoilers:) but good job, I was too tired yesterday evening to check the day’s problem, will look soon
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
part2 seems easier
whats causing this issue
to read you just split by \n and then split by "" (and trim last \n or skip last empty line)
don't set scanner.Buffer() and for your result, you'd want
buf := make([]byte, 0, initialCapacity)
ie a capacity but len of 0 at start - but then again you don't need that anyway for 140 lines
i dont understand the scanner doesnt read the entire line ?
since it may not be a multiple of the line length
but why does copying scanner.Bytes() into another slice work lol
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
i didnt set any buffer, this is howi was reading:
wordSearch = append(wordSearch, scanner.Bytes())
and it didnt work properly
then i saw this message and copied bytes for every line into a new slice before appending it into the the wordSearch slice of []byte
after which it worked
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)
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
if it "wasn't working" it's a different bug
this weeks puzzle input is giving me a migraine
the default max is 64k and the lines are 140 long
it literally isnt 😭
thats what ed x 7 is referring to as well
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)
okay
I can also put comments on stuff that doesn't make it the case 🙂
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
paste or post just the reading of the data (wordSearch := [][]byte{} for instance is wrong as it should start nil)
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
yes
true but the way ive written my other funcs, its easier to work with bytes
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.
.Text() too does re-use the buffer, its just that a copy (in the form of a string) is being made anyway
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]}
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!
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
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
Don't try to make a big architecture if the project is quite simple
well community doesn't ... all agree that you have to use cmd/ on the contrary 🙂 (you can see my rant about that in https://laurentsv.com/blog/2024/10/19/no-nonsense-go-package-layout.html)
Even middle projects like discordgo put everything at the root folder
As long as your files describe well what it handles you're good
updated and pushed, ty for the advice
Can you remove godotenv as _dl said ?
I don't understand what is the purpose of it
gj on goreleaser, for go folks you can tell them also to just
go install github.com/Blovio/sleigh@latest
Oh nvm godotenv are there for a reason
it's to load variables from .env files
Maybe handle the error no ?
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
if the env file is empty, it's an error so i wasn't sure how to handle that
damn i was trying to be anonymous haha
fix up your go.mod 🙂
ok try now
fmt.Fprintln(os.stderr, "error while trying to load env file. err=%v", err)
os.Exit(1)
I didn't want an error message if the user didn't have the variable in their .env file
same, did you increment the tag/retag?
why ?
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 🙂
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
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?
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
Direnv, isn't this a thing that nixos use ?
I don't think direnv supports jetbrains IDEs
i'm not sure, but it's really nice
given it's a session cookie it probably expires every day (or it doesn't?)
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)
I really appreciate the advice, i'm going to remove it and update the README
you should make a post in #community-showcase-archived after that (and adding how to get the cookie)
people might miss it here
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
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
session cookie doesnt expire every day
nvm my dumb ass just wasted so much time to realize that the entire reason it wasnt working (all my solutions were correct) was because i was iterating over the wrong bounds
yeah usually it expires / gets removed when you close the window which is even less
Not for me. I’ve been using the same session token the whole time
If you’re using a vpn or signing into that account from another device that could be the reason