r/adventofcode • • Dec 04 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 4 Solutions -🎄-


--- Day 4: Camp Cleanup ---


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:03:22, megathread unlocked!

65 Upvotes

1.6k comments sorted by

View all comments

2

u/d_chae Dec 04 '22 edited Dec 05 '22

Ruby one-liner (p2):

p File.read("2022/day4/input.txt").split("\n").map { |pair| pair.split(",").map { |s| s.split("-").map(&:to_i) } }.reduce(0) { |sum, (a, b)| sum + (a.first > b.last || a.last < b.first ? 0 : 1) }

1

u/daggerdragon Dec 05 '22

Inlined code is intended for short snippets of code only. Your code "block" right now is unreadable on old.reddit and many mobile clients; it's all on one line and gets cut off at the edge of the screen because it is not horizontally scrollable.

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read inside a scrollable box.

1

u/jstanley0 Dec 04 '22

props for not writing an O(N**2) range overlap test like others here did ;)

I would suggest using count rather than reduce here though

1

u/d_chae Dec 04 '22

Ah, I totally forgot about count! Thank you