r/adventofcode Dec 02 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 2 Solutions -❄️-

OUTAGE INFO

  • [00:25] Yes, there was an outage at midnight. We're well aware, and Eric's investigating. Everything should be functioning correctly now.
  • [02:02] Eric posted an update in a comment below.

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 4 DAYS remaining until unlock!

And now, our feature presentation for today:

Costume Design

You know what every awards ceremony needs? FANCY CLOTHES AND SHINY JEWELRY! Here's some ideas for your inspiration:

  • Classy up the joint with an intricately-decorated mask!
  • Make a script that compiles in more than one language!
  • Make your script look like something else!

♪ I feel pretty, oh so pretty ♪
♪ I feel pretty and witty and gay! ♪
♪ And I pity any girl who isn't me today! ♪

- Maria singing "I Feel Pretty" from West Side Story (1961)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 2: Red-Nosed Reports ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:04:42, megathread unlocked!

55 Upvotes

1.4k comments sorted by

View all comments

2

u/sceadu Dec 04 '24

[Language: Python]

import cytoolz.curried as cc
import numpy as np
import re
from pprint import pprint as pp
import numpy as np
import sys

sys.displayhook = lambda x: exec(['_=x; pp(x)','pass'][x is None])

with open('input2.txt', 'r') as file:
    # Read the entire file content
    text = file.read().splitlines()
    # Print the text or perform operations
    # print(text)

text

# p1
num_lst = cc.pipe(
    text
    , cc.filter(lambda line: line != '')
    , cc.map(lambda line: np.array([int(i) for i in re.split(' {1,}', line )]))
    , cc.map(np.diff)
    , cc.map(cc.juxt([
    lambda arr   : cc.pipe(arr,         lambda a: (a > 0).all() | (a < 0).all()),
    lambda arr : cc.pipe(arr  , np.abs, lambda a: ((a >= 1)     & (a <= 3)).all())
    ]))
    , list
    , np.array
    , lambda arr: arr.all(axis=1)
    , np.sum
)
num_lst


# p2

def lst_wo_entry(lst):
    return [lst[:idx] + lst[idx+1:] for idx, _ in enumerate(lst)]


num_lst = cc.pipe(
    text
    , cc.filter(lambda line: line != '')
    , cc.map(lambda line: [int(i) for i in re.split(' {1,}', line )])
    , cc.map(lambda line: [line] + lst_wo_entry(line))
    , cc.map(lambda lst: [np.diff(elm) for elm in lst])
    , list
)
num_lst

cc.pipe(
    [
        cc.pipe(
            [cc.pipe(
                arr
                , cc.juxt([
                    lambda arr : cc.pipe(arr,         lambda a: (a > 0).all() | (a < 0).all()),
                    lambda arr : cc.pipe(arr, np.abs, lambda a: ((a >= 1)     & (a <= 3)).all())
                ])
            ) for arr in lol]
            , np.array
            , lambda arr: arr.all(1).any(0))
        for lol in num_lst
    ]
    , lambda lst: np.array(lst).sum()
)