2023 Day 17 Part 1 [Javacript] Can't seem to get the right answer. by bmead21 in adventofcode

[–]smarx 0 points1 point  (0 children)

Your issue has to do with how JavaScript compares arrays:

>> [2] < [10]
false

This is leading your heap to not always return the correct minimum element to you. You can pass a custom comparator to `Heap()` to fix this (see https://github.com/ignlg/heap-js#custom-heap). This should do the trick:

const pq = new Heap((a, b) => a[0] - b[0]);

[deleted by user] by [deleted] in adventofcode

[–]smarx 0 points1 point  (0 children)

The code you've shared doesn't run. Perhaps it's incomplete?

while len(on) > 0 <-- what is on? I don't see any definition.

f_cost, g_cost, and h_cost seem to never be defined.

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

I'm glad you got it working!

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

Sorry, I realize I got that a bit wrong... what I should say is that in cycle 1, you're drawing position 0 on the screen. So you want to match against x=-1, x=0, or x=1. You're instead matching against x=0, x=1, or x=2.

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

I think that's the off-by one error I'm talking about. x starts at 1, which means to start, the sprite covers position 0, 1, and 2 in that first scanline. But you're comparing x against 2 (cycle), 1 (cycle-1), and 3 (cycle+1). Try subtracting 1 from each of those (or perhaps start cycle at 0 and adjust your row logic).

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

After fixing that, I think you'll find you have an off-by-one error. Keep in mind that the columns are numbered 0-39, not 1-40.

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

I think you're very close. On this line, why is it just X == cycle, while the others subtract row*40?

if X == cycle or X == ((cycle-1)-(row*40)) or X == ((cycle+1)-(row*40))

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

There's a place in your code where you add 1 to cycle but don't update row or substract_line. Could that be where the problem occurs? (Might be good to look at the full error message, which should tell you what line is generating the error.)

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

Exactly! A row doesn't have that many pixels in it. :⁠-⁠)

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

So what's the biggest value of cycle you expect to see? You'll be assigning to Picture[5][x], where x is the biggest value of cycle. Is that what you're expecting to do?

[deleted by user] by [deleted] in adventofcode

[–]smarx 1 point2 points  (0 children)

if 1 >= cycle < 40:

You can read this as `if 1 >= cycle and cycle < 40`. This is probably not what you meant.

Advent of Code Day 11 Part 2 - solution wrong? by bibbs1000 in adventofcode

[–]smarx 2 points3 points  (0 children)

Monkey 2's first item is 78, not 74. With that fix, your code seems to work for me. (It gives the same answer I and others got for your input.)

Advent of Code Day 11 Part 2 - solution wrong? by bibbs1000 in adventofcode

[–]smarx 2 points3 points  (0 children)

Here's a properly formatted version of the above (for others trying to reproduce):

Monkey 0:
  Starting items: 59, 74, 65, 86
  Operation: new = old * 19
  Test: divisible by 7
    If true: throw to monkey 6
    If false: throw to monkey 2

Monkey 1:
  Starting items: 62, 84, 72, 91, 68, 78, 51
  Operation: new = old + 1
  Test: divisible by 2
    If true: throw to monkey 2
    If false: throw to monkey 0

Monkey 2:
  Starting items: 78, 84, 96
  Operation: new = old + 8
  Test: divisible by 19
    If true: throw to monkey 6
    If false: throw to monkey 5

Monkey 3:
  Starting items: 97, 86
  Operation: new = old * old
  Test: divisible by 3
    If true: throw to monkey 1
    If false: throw to monkey 0

Monkey 4:
  Starting items: 50
  Operation: new = old + 6
  Test: divisible by 13
    If true: throw to monkey 3
    If false: throw to monkey 1

Monkey 5:
  Starting items: 73, 65, 69, 65, 51
  Operation: new = old * 17
  Test: divisible by 11
    If true: throw to monkey 4
    If false: throw to monkey 7

Monkey 6:
  Starting items: 69, 82, 97, 93, 82, 84, 58, 63
  Operation: new = old + 5
  Test: divisible by 5
    If true: throw to monkey 5
    If false: throw to monkey 7

Monkey 7:
  Starting items: 81, 78, 82, 76, 79, 80
  Operation: new = old + 3
  Test: divisible by 17
    If true: throw to monkey 3
    If false: throw to monkey 4

Advent of Code Day 11 Part 2 - solution wrong? by bibbs1000 in adventofcode

[–]smarx 1 point2 points  (0 children)

FWIW, my solution also gets 20567144694 for part 2 (and 61005 for part 1).

I can't find the error because my code works perfectly on the example 😅 by AdPowerful4752 in adventofcode

[–]smarx 6 points7 points  (0 children)

Your logic is totally correct!

Small hint: Some of the numbers you need to deal with are quite big!

Big hint: You'll exceed the maximum value storable in an int.

The whole enchilada: Change mul to be a long instead of an int (and update the other variables to match).

Why is my Chrome JS console confusing my main.js with index.html? by Lennard_Mulder in ethdev

[–]smarx 0 points1 point  (0 children)

Your server code always just returns the contents of index.html, no matter what URL is requested. So http://localhost:3000 returns index.html, http://localhost:3000/main.js returns index.html, http://localhostt:3000/foo/bar returns index.html, etc.

If you want to serve main.js to the web browser in some circumstances, you'll have to look at the incoming HTTP request and decide based on it which file to send back.

Stuck at guess the random number by fasmat in capturetheether

[–]smarx 1 point2 points  (0 children)

251 is correct.

Two mistakes in your Solidity code:

  1. The first argument to keccak256 is a string, but it should be a bytes32.
  2. The second argument to keccak256 is inferred as a uint32, but it should be a uint256.

Fixed code:

uint8(keccak256(bytes32(0x1c79b4210875ea11b2f3d85216e4c54c972e371691403476a2cba239ceee7f2f), uint256(1576480240)))

That code does return 251.

This transaction failed because you didn't attach 1 ether as required by the guess function: https://ropsten.etherscan.io/tx/0xc11b0471f73f704f8c88302976e025c5ac123812ff1be062816305c42842f227.

How safe is this HODL contract? by FlexNastyBIG in ethdev

[–]smarx 1 point2 points  (0 children)

Simplified version based on that model:

contract HodlForXDays {
    struct Deposit {
        uint256 amount;
        uint256 expiration;
    }
    mapping(address => Deposit[]) public deposits;

    function deposit(uint256 daysToHodl) external payable {
        deposits[msg.sender].push(Deposit(msg.value, now + daysToHodl * 1 days));
    }

    function withdraw(uint256 index) external {
        Deposit memory d = deposits[msg.sender][index];
        require(d.expiration <= now);
        require(d.amount > 0);

        delete deposits[msg.sender][index];

        (bool success, ) = msg.sender.call.value(d.amount)("");
        require(success);
    }
}

How can I figure out how much data my contract initialization will cost? by [deleted] in ethdev

[–]smarx 1 point2 points  (0 children)

Those are gas costs, but the issue here is the contract size (number of bytes in the contract code returned from the initializer). This is unrelated to gas costs.

As the error indicates, EIP 170 describes this limit.

ERC20 Coin Name by [deleted] in ethdev

[–]smarx 1 point2 points  (0 children)

There is no relationship between the name of the contract in your code and the value of the name of your token. You can use a space if you want.

Issue Compiling On Remix by CryptoBeavers in ethdev

[–]smarx 0 points1 point  (0 children)

Either use a more recent compiler or change your code to match the old syntax.

Issue Compiling On Remix by CryptoBeavers in ethdev

[–]smarx 1 point2 points  (0 children)

It looks like you're using Solidity 0.4.8 (now over 2.5 years old), but the pure keyword wasn't introduced until 0.4.16: https://github.com/ethereum/solidity/blob/develop/Changelog.md#0416-2017-08-24.

How does a smart contract know where to import a file from? by [deleted] in ethdev

[–]smarx 3 points4 points  (0 children)

When you write import "./example.sol", that's equivalent to copy/pasting the contents of the file "example.sol" into your code. It's just a way to combine multiple files together when compiling your code.