diff --git a/2022/day04/README.org b/2022/day04/README.org index 76fa3b6..27b7c65 100644 --- a/2022/day04/README.org +++ b/2022/day04/README.org @@ -65,10 +65,26 @@ pairs. /In how many assignment pairs does one range fully contain the other?/ -To begin, [[file:4/input][get your puzzle input]]. +Your puzzle answer was =444=. + +The first half of this puzzle is complete! It provides one gold star: * + +** --- Part Two --- +It seems like there is still quite a bit of duplicate work planned. +Instead, the Elves would like to know the number of pairs that /overlap +at all/. + +In the above example, the first two pairs (=2-4,6-8= and =2-3,4-5=) +don't overlap, while the remaining four pairs (=5-7,7-9=, =2-8,3-7=, +=6-6,4-6=, and =2-6,4-8=) do overlap: + +- =5-7,7-9= overlaps in a single section, =7=. +- =2-8,3-7= overlaps all of the sections =3= through =7=. +- =6-6,4-6= overlaps in a single section, =6=. +- =2-6,4-8= overlaps in sections =4=, =5=, and =6=. + +So, in this example, the number of overlapping assignment pairs is =4=. + +/In how many assignment pairs do the ranges overlap?/ Answer: - -You can also [Shareon -[[https://twitter.com/intent/tweet?text=%22Camp+Cleanup%22+%2D+Day+4+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F4&related=ericwastl&hashtags=AdventOfCode][Twitter]] -[[javascript:void(0);][Mastodon]]] this puzzle. diff --git a/2022/day04/aoc.bash b/2022/day04/aoc.bash new file mode 100755 index 0000000..d2f5bc2 --- /dev/null +++ b/2022/day04/aoc.bash @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +# +# aoc.bash: Advent of Code 2022, day 3 +# +# Copyright (C) 2022 Bruno Raoult ("br") +# Licensed under the GNU General Public License v3.0 or later. +# Some rights reserved. See COPYING. +# +# You should have received a copy of the GNU General Public License along with this +# program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later + +. common.bash + +export LANG=C +declare -a sections + +parse() { + local -a _arr + local -i _tmp + + while IFS=-, read -ra _arr; do + # arrange the two sections so that the lowest is the first + printf "_arr=%s\n" "${_arr[*]}" + if ((_arr[0] > _arr[2])); then + ((_tmp=_arr[0], _arr[0]=_arr[2], _arr[2]=_tmp)) + ((_tmp=_arr[1], _arr[1]=_arr[3], _arr[3]=_tmp)) + printf "\t->%s\n" "${_arr[*]}" + fi + sections+=("${_arr[*]}") + done + #readarray -t input +} + +part1() { + declare -ig res=0 + local -a _sect + + for line in "${sections[@]}"; do + # shellcheck disable=SC2206 + _sect=($line) + (( _sect[1] >= _sect[3] || + (_sect[0] == _sect[2] && _sect[3] >= _sect[1]) )) && (( res++ )) + done +} + +solve() { + if (($1 == 1)); then + part1 + else + part2 + fi +} + +main "$@" +exit 0