AnnouncementsFunnyVideosMusicAncapsTechnologyEconomicsPrivacyGIFSCringeAnarchyFilmPicsThemesIdeas4MatrixAskMatrixHelpTop Subs
4

https://adventofcode.com/2024/day/1

This is the start of the real Advent of Code for this year. If there is any one to try it's this one. Cheating is welcome and encouraged if that's your level (AI). Give it a shot.

Comment preview
[-]DV1(+1|0)

Neat. Is there a coding language preferred for these?

By the sounds of the day 1 challenge you could spreadsheet it with Excel or similar.

[-]x0x7
0(+0|0)

You could. There isn't a preferred language. In fact if you hit a language others haven't that's a plus.

Lol. I guess you can do it in excel. I'd be interested in seeing the formulas. In fact excel is encouraged. I'd love to see how far someone can get.

[-]happytoes0(+0|0)

I wondered how it would work. My first guess was that one submits code and the competition website runs it. But where was the list of programming languages that were allowed?

My second guess was to cut and paste the example into my source file as a string constant

;;; I'll guess that one uses cut and past
;;; to get the input from the webpage
(defparameter example-input
"3 4
4 3
2 5
1 3
3 9
3 3")

So I wrote my code. Since I'm old and weird, I used a weird old language. There is some parsing stuff
and the actual computation is

(defun compute(list-of-lists)
;;start by unzipping and sorting
(let ((left (sort (mapcar #'first list-of-lists) #'<))
(right (sort (mapcar #'second list-of-lists) #'<)))
(reduce #'+
(mapcar #'(lambda(x y)(abs (- x y)))
left
right))))

That worked, so I clicked the Reddit authentication (I'm so old that I have an account from when Reddit was
a good website). Then I clicked to get my input, which appeared in a new webpage.

I used cut and paste, not realizing that there were a thousand lines on the page. Nothing broke :-)

But it turns out to be text file. I think the idea is that you save it with your browsers Save function. Then your code reads the file as an ordinary text file, just ASCII codes for numerals. Compute the answer, and type it into the box to win a gold star!

[-]x0x7
0(+0|0)

Yep. There are a few strategies you could use for reading the input. Both reading the file and copy pasting in your code both seem reasonable. I’m saving both the initial example and the longer on to short.txt and long.txt and then using a symbolic link to input.txt to change which one I’m reading.

But I should probably just switch that to reading a command line argument.

It’s been a while since I’ve touched proper lisp, but I have touched some clojure about a year ago.

There’s multiple generations in between that I’d probably misidentify as CL. So for all I know that was one any of those.

[-]LarrySwinger
0(+0|0)

I'm so old that I have an account from when Reddit was a good website

It was never a good website. Same.

[-]x0x7
0(+0|0)
[-]LarrySwinger
0(+0|0)

Check out my answer, I'm doing it in bash. I must say I'm having fun with it, can you pick out all the nuances in my code? Part 2 to follow soon.

lol, i dunno bash! but i'd love to help however i can! I'd say to readability you should use carbon.now.sh - that converts the code in an image that you can then put on gvid.tv or catbox.moe and it makes it easier to read... sometimes! anyways, its great that you are joining! The problems aren't that bad, and its earning me extra clout on twitter. double lol.

[-]LarrySwinger
0(+0|0)

Is Twitter making u rich?

You got the rest of the problems done yet? they aren't that hard.

[-]LarrySwinger
0(+0|0)

Part 1

#!/bin/bash
left=( $(cat input.txt | cut -d\  -f 1 | sort) )
right=( $(cat input.txt | cut -d\  -f 4 | sort) )
total=0

for i in ${!left[@]}
do
        let "total += $(echo "sqrt ( (${left[$i]} - ${right[$i]})^2)" | bc)"
done

echo $total
[-]LarrySwinger
0(+0|0)

Pt. 1-2 combined

#!/bin/bash
left=( $(cat input.txt | cut -d\  -f 1 | sort) )
right=( $(cat input.txt | cut -d\  -f 4 | sort) )
distance=0
similarity=0

function calcDistance() {
for i in ${!left[@]}
do
        let "distance += $(echo "sqrt ( (${left[$i]} - ${right[$i]})^2)" | bc)"
done
echo $distance
}

function calcSimilarity() {
        for i in ${left[@]}
        do
                weight=$(grep -o $i <<< ${right[*]} | wc -l)
                let "similarity += $i * $weight"
        done
        echo $similarity
}
[-]x0x7
0(+0|0)

Woah. That's cool.

[-]LarrySwinger
0(+0|0)

Is it anything special? I thought it'd be some kind of meme to do the whole thing in bash but it actually feels quite natural. I'm not even sure why I had this view of bash that it's inadequate. Instead of libraries you have cli tools.

[-]x0x7
0(+0|0)

True. And if you ever really need to you can implement something in another language and use it.

There are a few of the later ones where bash probably won't work but I wouldn't count bash out to be used for more than it should.

[-]LarrySwinger
0(+0|0)

So yeah. I used a single space as delimiter and simply picked the fourth field for the right column. And I didn't know if bc (basic calculator) has an absolute value feature so I implemented it manually by taking the square root of the squared value.

[-]x0x7
0(+0|0)

That's funny because I have in the past ditched bc for a programming language even for inline math in bash mostly because bc doesn't even want to handle floating point numbers. How funny would that be if you ended up launching node or python anyway.

[-]LarrySwinger
0(+0|0)

You have to set the scale, which is a fixed number of significant points behind the comma that it uses. Try echo "scale=3; 2469/200" | bc.