Live data from Hacker News

Advent of Code 2023 is nigh

adventofcode.com

291–300 of 319 posts

Re: Advent of Code 2023 is nigh

#291

I challenge myself to do it in bash one liners. I came up with a clever and shockingly simple solution to part2 using expansion and substitution. cat 1.txt | sed -E 's/(one)/\11\1/g; s/(two)/\12\1/g; s/(three)/\13\1/g; s/(four)/\14\1/g; s/(five)/\15\1/g; s/(six)/\16\1/g; s/(seven)/\17\1/g; s/(eight)/\18\1/g; s/(nine)/\19\1/g;' | sed -e 's/[^0-9]//g' | awk '{print substr($0,1,1) substr($0,length,1)}' | tr '\n' '+' | s…

"I challenge myself to do it with bash one liners."

Is bash required.

For example, this also works in dash, NetBSD sh, pdksh, tcsh, etc.

Re: Advent of Code 2023 is nigh

#292
post #101

Earlier quoted context omitted.

Same here. I would've really like if the spec specifically mentioned the possibility of that one edge case ahead of time instead of having to sift through the 1000 lines of input. No hate on AOC though, I really respect all the hard work that goes into it.

Unless the question has been edited recently, it did. There are multiple lines in the second example input that show the overlap: > eightwothree > 4nineeightseven2 > zoneight234 I test my AoC solutions incrementally by printing output, so I found that I was failing to produce the correct list of numbers in a line right away. I suppose if you're taking a faster approach and just trying to extract the first and last nu…

I'm not certain, but I _feel_ like those are new values. There's a very real chance (majority chance) I'm wrong and oblivious though.

Re: Advent of Code 2023 is nigh

#293
post #290

Earlier quoted context omitted.

It's a Regular Language, in the mathematical sense. All you need is the greedy match anything sequence, I don't know why so many people jumped to non-Regular regex extensions.

I'm sorry I'm not familiar with the mathematics behind regular expressions. Can you give an example of what your approach would look like?

    first = line.match(  /(?:(0|zero)|(1|one) ... (9|nine))/)
    last  = line.match(/.*(?:(0|zero)|(1|one) ... (9|nine))/)

    indexOfGroupMatched = 
        ([_, ...groups]) => groups.findIndex(x => x != undefined)

    num = +[first, last].map(indexOfGroupMatched).join('')

Re: Advent of Code 2023 is nigh

#294
First time trying a code golf solution, managed to do Part 2 in 231 characters.

  p="one two three four five six seven eight nine".split();sum(int(x[0]+x[-1])for x in["".join([["",s[0]][s[0].isdigit()],str(p.index(w)+1)][s.startswith(w)]for s in[l[i:]for i in range(len(l))]for w in p)for l in open("input.txt")])

Re: Advent of Code 2023 is nigh

#295
post #173

Earlier quoted context omitted.

For me it was a lack of specific instructions on how to handle overlaps. The edge case that frustrated me for a while was "oneight" at the end of a line. My initial code made it look like this "1ight", when it should have been "18".

When searching for the last number in the line I just reversed the line and scanned through it looking for the reversed strings for the number: one -> eno two -> owt three -> eerht etc It makes the entire solution extremely simple, though a little verbose.

Yeah I was a little confused about all the people saying they've already given up on day 1. Seems like everyone's just really overcomplicating this. Why is everyone jumping to "replace the word strings with the digits" instead of just doing exactly what the problem says and... finding the first and last occurrence?

1. Build a list of values with corresponding string matches: [ 0 => ['zero', '0'], 1 => ['one', '1'], ...]

2. Loop through that and find the index of each within the input string, maintaining the lowest seen index + associated value.

3. When done, return value.

To find the last occurrence... just reverse the input string and all the search strings.

I'm not even sure it's all that verbose. If you exclude the part where I hardcoded an array of ten digits, it was... 11 lines of code, a third of which are closing braces. I'm sure I could cut it in half if I used some builtins for mapping/reducing/etc.

Re: Advent of Code 2023 is nigh

#296
post #173

Earlier quoted context omitted.

For me it was a lack of specific instructions on how to handle overlaps. The edge case that frustrated me for a while was "oneight" at the end of a line. My initial code made it look like this "1ight", when it should have been "18".

When searching for the last number in the line I just reversed the line and scanned through it looking for the reversed strings for the number: one -> eno two -> owt three -> eerht etc It makes the entire solution extremely simple, though a little verbose.

Same here. I'm using regex and I was wondering how to make it go backwards. After a moment of thought, that seems silly, so I just reversed the string and the regex and do the scan.

Re: Advent of Code 2023 is nigh

#297
post #173

Earlier quoted context omitted.

When searching for the last number in the line I just reversed the line and scanned through it looking for the reversed strings for the number: one -> eno two -> owt three -> eerht etc It makes the entire solution extremely simple, though a little verbose.

Yeah I was a little confused about all the people saying they've already given up on day 1. Seems like everyone's just really overcomplicating this. Why is everyone jumping to "replace the word strings with the digits" instead of just doing exactly what the problem says and... finding the first and last occurrence? 1. Build a list of values with corresponding string matches: [ 0 => ['zero', '0'], 1 => ['one', '1'], .…

> I'm not even sure it's all that verbose

Mine is. But that's because I don't bother DRYing it and making it more clever (yanking and pasting is faster than thinking)

Re: Advent of Code 2023 is nigh

#298
post #201

Earlier quoted context omitted.

I don't get the amount of effort people out into the replacement-strategy, I did perfectly fine without it and the code is about as complex as the examples I've seen. https://github.com/codr7/swift-interpreter/blob/main/part10/...

Ahh, people are trying to do a replacement before finding tokens. I wondered why so many people were saying this was difficult. My head went straight to token parsing, which given the limited set of tokens made it trivial. Thought I was missing something

I think people with understanding of compilers always want to model their code as DSL, and that makes it easier to go for backwards scanning.

Re: Advent of Code 2023 is nigh

#299

As others have said, part 2 of today's was really difficult. I finally solved it using Python regex `overlapped=true`, but it was very tricky. The irritation of having all of the test cases passing, but it failing for my challenge input! I hope it doesn't scare off newcomers, but I already know a few who have given up on part 2.

The inputs are actually not as tough as they could be. It’s possible to beat this puzzle with brittle regex that would fail on something like oneightwone.

Re: Advent of Code 2023 is nigh

#300
In times past, it was Pascal that was my language of choice[1]. This year, I'm going to use a virtual BitGrid[2]. Here's the repo[3] Hopefully I can finish the 2023 problems before next November, I have nothing but the simulator. Nothing to convert text to code, do I/O, etc.

Oh boy.... Day 1 A... and I have to figure out how to feed text into a bitgrid, and get it out the other side... before I can even think about parsing it, making integers, and adding them, then converting back to text.

BitGrid - a sea of LUTs with latches, completely parallel down to the bit processing. Each LUT has 4 bits of input, and 4 independent bits out to each cardinal direction. Clocking in 2 phases, like colors on a chess board, to eliminate undefined behavior or timing issues.

[1] https://github.com/mikewarot/Advent_of_Code_in_Pascal

[2] https://github.com/mikewarot/Bitgrid

[3] https://github.com/mikewarot/Advent_of_Code_in_BitGrid

Post reply on HN