Skip to content

Latest commit

 

History

History
74 lines (60 loc) · 1.66 KB

solution.org

File metadata and controls

74 lines (60 loc) · 1.66 KB

Advent of Code Day 4 Solution

Sample input:

cat sample_input
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

Each pair of elves, are assigned sections: b1-e1,b2-e2 In how many assignment pairs does one range fully contain the other?

using DelimitedFiles

filename = get(ARGS, 1, "sample_input")
input = readdlm(filename, ',', String) |> f -> map(x -> map(i -> parse(Int, i), split(x, "-")), f) |> eachrow |> collect
6-element Vector{SubArray{Vector{Int64}, 1, Matrix{Vector{Int64}}, Tuple{Int64, Base.Slice{Base.OneTo{Int64}}}, true}}:
 [[2, 4], [6, 8]]
 [[2, 3], [4, 5]]
 [[5, 7], [7, 9]]
 [[2, 8], [3, 7]]
 [[6, 6], [4, 6]]
 [[2, 6], [4, 8]]
fully_contained = [(x[1][1] in x[2][1]:x[2][2] && x[1][2] in x[2][1]:x[2][2]) || (x[2][1] in x[1][1]:x[1][2] && x[2][2] in x[1][1]:x[1][2]) for x in input]
6-element Vector{Bool}:
 0
 0
 0
 1
 1
 0
println("Part 1: ", sum(fully_contained))
Part 1: 2
overlap = [(x[1][1] in x[2][1]:x[2][2] || x[1][2] in x[2][1]:x[2][2]) || (x[2][1] in x[1][1]:x[1][2] || x[2][2] in x[1][1]:x[1][2]) for x in input]
6-element Vector{Bool}:
 0
 0
 1
 1
 1
 1
println("Part 2: ", sum(overlap))
Part 2: 4