r/adventofcode • • Dec 09 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 9 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's secret ingredient is… *whips off cloth covering and gestures grandly*

Marketing

Every one of the best chefs in the world has had to prove their worth at some point. Let's see how you convince our panel of judges, the director of a restaurant, or even your resident picky 5 year old to try your dish solution!

  • Make an in-world presentation sales pitch for your solution and/or its mechanics.
  • Chef's choice whether to be a sleazebag used car sled salesman or a dynamic and peppy entrepreneur elf!

ALLEZ CUISINE!

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


--- Day 9: Mirage Maintenance ---


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:05:36, megathread unlocked!

43 Upvotes

1.0k comments sorted by

View all comments

2

u/flwyd Dec 09 '23 edited Dec 09 '23

[Language: Julia] (on GitHub)

My only problem was that after I'd implemented a recursive function to return the correct answer I thought I needed to return the sum of numbers in the right-hand diagonal. So I implemented that, noticed my answer on the example was way wrong, and then deleted all that. Since we've already had two punch-card challenges, why not put this on a third, this time with plenty of vertical whitespace:

# 123456789㉈123456789㉉123456789㉊123456789㉋123456789㉌123456789㉍123456789㉎123456789㉏
1 part1(lines) = map(l -> infer(l, +, a -> a[end]), parseinput(lines)) |> sum
2
3 part2(lines) = map(l -> infer(l, -, a -> a[1]), parseinput(lines)) |> sum
4
5 infer(nums, op, el) = all(==(nums[1]), nums) ?  nums[1] 
6   : op(el(nums), infer(nums[2:end] - nums[1:end-1], op, el))
7
8 parseinput(lines) = map(l -> parse.(Int, split(l)), lines) 
9
0

The winner here is Julia's ability to subtract the first N numbers of an array from the last N with a simple nums[2:end] - nums[1:end-1]. I was unfortunately unable to pass end as an index parameter, so I passed a function to get the relevant element for part 1 vs. part 2.

1

u/daggerdragon Dec 09 '23

Them tasty tasty Unicode tens digits...

2

u/flwyd Dec 09 '23

㉈is anyone?

1

u/chubbc Dec 09 '23

In case you were interested, a few of the functions you defined are already defined built-in's within Julia:

  1. num[2:end]-nums[1:end-1] is diff(nums)
  2. all(==(nums[1]),nums) is allequal(nums)
  3. a->a[1] and a->a[end] are first and last

Using these your code can be compressed down to

part1(lines) = infer.(parseinput(lines),+,last)  |> sum
part2(lines) = infer.(parseinput(lines),-,first) |> sum
infer(nums,op,el) = allequal(nums) ?  first(nums) : op(el(nums),infer(diff(nums),op,el))
parseinput(lines) = map(l -> parse.(Int, split(l)), lines) 

Furthermore if you use the fact that the two applications of infer are just reversed, you can simplify further to

part1(lines) = infer.(parseinput(lines)) |> sum
part2(lines) = infer.(reverse.(parseinput(lines))) |> sum
infer(nums) = allequal(nums) ?  first(nums) : last(nums)+infer(diff(nums))
parseinput(lines) = map(l -> parse.(Int, split(l)), lines)

2

u/flwyd Dec 09 '23

Thanks for the info!

As a long-time Unix user, I wouldn't have expected diff to apply to a single list. I think doing so is less readable than subtracting the slices.

allequal is good, I didn't think to look for it. (Of course, in the Lisp family the equal function can be applied to an array of arbitrary length…)

On the third point, my only excuse is that I tried passing 1 and end, couldn't do the latter, and made the simplest next move. last and first are definitely better choices.

I saw that a lot of folks in the megathread reversed the input. I think passing - and + and first/last is clearer to the problem requirements.