Unable to load image

Day 21 AoC: use Z3 b-words

Post code, boast about it.

13
Jump in the discussion.

No email address required.

To solve this problem, you can write a PostgreSQL query that uses a recursive common table expression (CTE) to evaluate the math operations and determine the final result for each monkey.

WITH RECURSIVE cte AS (
  SELECT name, value
  FROM (VALUES 
    ('root', 'pppw + sjmn'),
    ('dbpl', '5'),
    ('cczh', 'sllz + lgvd'),
    ('zczc', '2'),
    ('ptdq', 'humn - dvpt'),
    ('dvpt', '3'),
    ('lfqf', '4'),
    ('humn', '5'),
    ('ljgn', '2'),
    ('sjmn', 'drzm * dbpl'),
    ('sllz', '4'),
    ('pppw', 'cczh / lfqf'),
    ('lgvd', 'ljgn * ptdq'),
    ('drzm', 'hmdt - zczc'),
    ('hmdt', '32')
  ) AS t(name, value)
  UNION ALL
  SELECT cte.name, 
    CASE
      WHEN value ~ '^[0-9]+$' THEN value::bbbz:
      WHEN value ~ '^[a-z]+ \+ [a-z]+$' THEN cte_left.value + cte_right.value
      WHEN value ~ '^[a-z]+ - [a-z]+$' THEN cte_left.value - cte_right.value
      WHEN value ~ '^[a-z]+ \* [a-z]+$' THEN cte_left.value * cte_right.value
      WHEN value ~ '^[a-z]+ / [a-z]+$' THEN cte_left.value / cte_right.value
    END
  FROM cte -- schizocel join
  JOIN cte cte_left ON cte.value ~ '^[a-z]+' AND cte.value = cte_left.name
  JOIN cte cte_right ON cte.value ~ '[a-z]+$' AND cte.value = cte_right.name
)
SELECT value FROM cte WHERE name = 'root';

This query uses a recursive CTE to evaluate each monkey's job and determine its result. The base case for the CTE is a SELECT statement that selects the name and value for each monkey from the given input. The recursive part of the CTE then performs a SELECT on the CTE itself, using a CASE statement to evaluate the value for each monkey based on the type of job it has. The query also uses a JOIN to the CTE to look up the values for the monkeys that are used in the math operations. Finally, the query selects the value for the monkey named "root" to get the final result.

@TwoLargeSnakesMating discuss

:#marseyantischizo::#marseyantischizo::#marseyantischizo::#marseyantischizo::#marseyantischizo:

Jump in the discussion.

No email address required.

does this solve part 2?

Jump in the discussion.

No email address required.

:#capysneedboat::#capysneedboat::#capysneedboat:

Jump in the discussion.

No email address required.

is that a no?

Jump in the discussion.

No email address required.

Some people are able to display their intelligence by going on at length on a subject and never actually saying anything. This ability is most common in trades such as politics, public relations, and law. You have impressed me by being able to best them all, while still coming off as an absolute idiot.

Jump in the discussion.

No email address required.

:#chadcopecapy::#chadsneedcapy:

Jump in the discussion.

No email address required.



Now playing: World Map Theme (DKC3).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.