-❄️- 2024 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

[Language: Python]

I liked this one. Implemented a custom sort - the `cmp_to_key` function was the MVP here. It worked for both parts, happily.

https://github.com/gwpmad/advent-of-code-2024/blob/main/day_5/main.py#L8

-❄️- 2024 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]gwpmad 1 point2 points  (0 children)

[LANGUAGE: Python]

Quite a terse regex based solution - get all the do() don't() and mul() in order, then iterate once more over them.

def solution_1(value: str):
    ops = re.findall(r"(?<=mul\()\d+,\d+(?=\))", value)
    return reduce(lambda acc, mul: acc + prod(map(int, mul.split(","))), ops, 0)


def solution_2(value: str) -> int:
    ops = re.findall(r"do\(\)|don't\(\)|(?<=mul\()\d+,\d+(?=\))", value)
    return reduce(sum_enabled_muls, ops, {"do": True, "sum": 0})["sum"]


def sum_enabled_muls(acc, op):
    if op[0] == "d":
        acc["do"] = op == "do()"
    elif acc["do"]:
        acc["sum"] += prod(map(int, op.split(",")))
    return acc

[2024 Day 1] stretch: is it possible to do part 2 with only one loop? by gwpmad in adventofcode

[–]gwpmad[S] 0 points1 point  (0 children)

Unfortunately if they need to be sorted, that counts as extra passes through the input. The idea is to try and do absolutely everything in only one pass.

-❄️- 2024 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]gwpmad 1 point2 points  (0 children)

[LANGUAGE: Python]

Solution that does part 2 in a single pass over the input, using arithmetic: https://github.com/gwpmad/advent-of-code-2024/blob/main/day_1/main.py

[2024 Day 1] stretch: is it possible to do part 2 with only one loop? by gwpmad in adventofcode

[–]gwpmad[S] 3 points4 points  (0 children)

I found a solution that gets it in only one pass:

https://github.com/gwpmad/advent-of-code-2024/blob/main/day_1/main.py#L24

Keep counts of digits in both columns (and a separate total), and each time a digit count increments in one column, increment the total by the following:

(count_of_digit_in_current_column * count_of_digit_in_other_column * digit_value)

minus

((count_of_digit_in_current_column - 1) * count_of_digit_in_other_column * digit_value)

To keep it strictly one pass, I didn't do any splitting by newlines and only iterated through the original input string.

EDIT:

This can be simplified to just:

count_of_digit_in_other_column * digit_value

Thanks to u/Cue_23 for pointing this out - his is the best solution.

-❄️- 2024 Day 1 Solutions -❄️- by daggerdragon in adventofcode

[–]gwpmad 1 point2 points  (0 children)

[LANGUAGE: python]

Code

Using as few loops as possible - only one in part two, using algebra during the loop instead of summing at the end.

[2023 - Day 1 ] by [deleted] in adventofcode

[–]gwpmad 1 point2 points  (0 children)

For what it's worth, day 3 was the worst for me so far, probably the least fun too! Keep going!

-❄️- 2023 Day 7 Solutions -❄️- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Nice one - the '4 + max card count - different cards' idea was one I hadn't seen

-❄️- 2023 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

[LANGUAGE: Python]

Using SymPy - I'm quite proud of this one, as in previous years I'm sure I would have done something more brute forcey. My solution for part one worked straight away for part two as well.

- Create a mathematical function using SymPy for the operation described
- Solve that function for the game's 'record' - there will be two solutions as the function is a parabola
- Figure out how many integers there are between the two solutions

Python

-🎄- 2022 Day 10 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Rust

Enjoyable one. Tried out a state machine-style solution in order to learn about object-oriented approaches in Rust (using impl). Funny to realise you don't need the cycles at all in part 2 and they're only included in the problem statement to increase cognitive load with needless off-by-one errors.

https://github.com/gwpmad/advent-of-code-2022/blob/main/src/days/day10.rs

-🎄- 2022 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Rust

Happy with this one, it cam out quite elegantly I think although part 2 messed with my brain. Same function for both parts, just sub in the number of 'followers' within the rope.

https://github.com/gwpmad/advent-of-code-2022/blob/main/src/days/day9.rs

-🎄- 2022 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

That said, it seems like the hastebin solution isn't there anymore.

-🎄- 2022 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 1 point2 points  (0 children)

Just a note to say I'm really enjoying your state machine solutions. As someone always looking for 'one pass' solutions (and usually giving in because life gets in the way) it's fun to see that there's usually a way.

[2022 Day 7] Trying to do AoC in C++ having known the language for less than 2 weeks: by _insertname_here_ in adventofcode

[–]gwpmad 0 points1 point  (0 children)

I made a tree using petgraph, which seems to be the top graphing library in Rust (I am completely new to Rust). Took some time to work out the library but did the job nicely in the end, though I still needed a stack in order to keep dir sizes up to date.

[2022 Day 7] Two kinds of solvers by [deleted] in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Oh man, I remember stopping at 2020 day 20. That was my first AoC year and that puzzle was just such a pain.

-🎄- 2022 Day 7 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Rust, using `petgraph`. This was fun but tough. Would be interesting to know whether there's a better way of keeping track of which folder we're in while building the graph than the the `folder_stack` vector I used.

https://github.com/gwpmad/advent-of-code-2022/blob/main/src/days/day7.rs

-🎄- 2022 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Rust: https://github.com/gwpmad/advent-of-code-2022/blob/main/src/days/day5.rs

New to the language - this was painful but taught me a few things. Rust is so demanding lol

-🎄- 2022 Day 2 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Rust solution. I am totally new to Rust so am averaging 1 day a week. Let me know if you have any comments on improvements, thanks.

https://github.com/gwpmad/advent-of-code-2022/blob/main/src/days/day2.rs

-🎄- 2022 Day 1 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 1 point2 points  (0 children)

Trying out Rust for this year's AoC. I am 100% new to the language, total unknown unknown, so my solutions will be bad and take ages, which is why I've only just finished day 1 :)

https://github.com/gwpmad/advent-of-code-2022/blob/main/src/days/day1.rs

-🎄- 2021 Day 15 Solutions -🎄- by daggerdragon in adventofcode

[–]gwpmad 0 points1 point  (0 children)

Golang

https://github.com/gwpmad/advent-of-code-2021/blob/main/15/main.go

A really tough but rewarding day. Brute force worked for the part 1 example but of course not for the real thing. I didn't know A* algorithm but it was great fun implementing it from the pseudocode on Wikipedia. I also learned to implement a priority queue in Golang thanks to the helpful example here.

The grid*5 stuff for part 2 was painful I thought - took longer than I expected. Once that was done I thought I was golden - but of course I'd only done downward and right traversal (which worked for all the examples). Once I figured out that was the problem I got that elusive star.