hi @vaporeon_ want to read some disgusting golfy javascript code i just wrote

Advent of Code Day 1, Part 1 spoiler 

@vaporeon_
There is a round safe lock with the numbers 0-99. I have a text file called "inputs.txt" that has a bunch of instructions separated by line terminators. The instructions are like "L38" or "R15" and tell you turn the safe lock left or right by the given number. The safe lock starts at 50, I need to find how many times the safe lock hits the number 0.

let bound = 100, t = 50, hits = 0;
Deno.readTextFileSync("input.txt").split("\r\n").map(s => s.replace("L", "-").replace("R", "")).forEach(n => {
if ((t +=+ n) < 0 ? t = -(-t % bound) : t %= bound); else ++hits;
});
console.log("password:", hits);

re: Advent of Code Day 1, Part 1 spoiler 

@wallhackio Perhaps I'm stupid and misunderstood what's happening, but you are aware that to make sure that modulus produces a positive result even if your numbers are negative but less than bound in absolute value, you can just do t = (t + n + bound) % bound, there's no need case distinction?

re: Advent of Code Day 1, Part 1 spoiler 

@vaporeon_ in javascript the negative numbers do result in negative results with the % operator because the language is bad

re: Advent of Code Day 1, Part 1 spoiler 

@wallhackio That's why the +bound part

E.g. assume t is currently at 5, you want to rotate L95

(5 + -95 + 100) % 100 = 10 % 100 = 10

The left part never gets negative if abs(n) is always less than 100

Meanwhile, if you just did (5 + -95) % 100 = -90 % 100, that would indeed be a problem

re: Advent of Code Day 1, Part 1 spoiler 

@vaporeon_ oh i didn't read it right

uh, you might be right? although the inputs the question give you have turns greater than 100 if im not mistaken, so maybe this wouldn't work

re: Advent of Code Day 1, Part 1 spoiler 

@wallhackio Good point... For part 2, the division with modulus fixes that, but for part 1? Actually I'm now surprised that my solution works...

re: Advent of Code Day 1, Part 1 spoiler 

@wallhackio OK, my solution works because in Sun OpenBoot, -90 100 mod and 10 100 mod both produce the same result

So if I wrote that in C, my solution would indeed be broken

Truly, I am fake programmer :(

Follow

re: Advent of Code Day 1, Part 1 spoiler 

@vaporeon_ reported for lies

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.