day_4.jl (1387B)
1 #!/usr/bin/env julia 2 # https://adventofcode.com/2022/day/4 3 using AdventOfCode 4 5 example = split(""" 6 2-4,6-8 7 2-3,4-5 8 5-7,7-9 9 2-8,3-7 10 6-6,4-6 11 2-6,4-8""", 12 "\n") 13 input = readlines("data/day_4.txt") 14 15 # Split an input line into an array of arrays 16 function parse_line(inputline::AbstractString) 17 map(x->parse.(Int, split(x, "-")), split(inputline, ",")) 18 end 19 20 # Return true iff one assignment range is entirely contained in another 21 function check_containment(assignments::Array{Array{Int,1},1}) 22 (assignments[1][1] >= assignments[2][1] && assignments[1][2] <= assignments[2][2]) || 23 (assignments[2][1] >= assignments[1][1] && assignments[2][2] <= assignments[1][2]) 24 end 25 26 function part_1(input::Array{<:AbstractString,1}) 27 sum(map(check_containment, map(parse_line, input))) 28 end 29 @assert part_1(example) == 2 30 @info part_1(input) 31 32 # For part 2 I'm simply checking if assignments overlap at all. Not too hard 33 34 # Return true iff the assignments overlap at all 35 function check_overlap(assignments::Array{Array{Int,1},1}) 36 assignments[1][1] <= assignments[2][2] && 37 assignments[1][2] >= assignments[2][1] 38 end 39 40 function part_2(input::Array{<:AbstractString,1}) 41 sum(map(check_overlap, map(parse_line, input))) 42 end 43 @assert part_2(example) == 4 44 @info part_2(input)