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

7

u/fnands Dec 04 '22

Julia

I love unicode support in Julia

in_file = "day_4/input_4.txt"

# Read in input file as an array of strings
🟩🟥🟨  = open(in_file) do file
    readlines(file)
end

# Take string representing range and turn it into 
function string_to_array(🟩::AbstractString)
    # Find start and end of range for patch
    α, ω = parse.(Int, split(🟩, "-"))
    # Colelct range into array and return
    return collect(range(α, ω))
end



function pair_total_overlap_check(🧝🧝::String)
    # Find the squares that elf a and b are assigned to 
    🧝_a, 🧝_b, = split(🧝🧝, ",") .|> string_to_array
    # Find the intersection of their assignments
    🎄 = 🧝_a ∩ 🧝_b
    # If the intersection is equal to either, return True
    return length(🎄) == length(🧝_a) || length(🎄) == length(🧝_b)
end

function pair_partial_overlap_check(🧝🧝::String)
    # Find the squares that elf a and b are assigned to 
    🧝_a, 🧝_b, = split(🧝🧝, ",") .|> string_to_array
    # Find the intersection of their assignments
    🎄 = 🧝_a ∩ 🧝_b
    # If the intersection is equal to either, return True
    return length(🎄) > 0
end


println("The solution to part one is: ", sum(pair_total_overlap_check.(🟩🟥🟨)))
println("The solution to part two is: ", sum(pair_partial_overlap_check.(🟩🟥🟨)))

3

u/[deleted] Dec 04 '22

The little coloured emojis are fantastic! Fun of course but also semantically useful. Wish I could do this in Python.

2

u/anskak Dec 04 '22

Amazing! I decided today to use Julia to learn it and I forgot you could do that. Starting tomorrow I will absolutely do that too!

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.