2022 day 1: prepare bash common code + bash solutions

This commit is contained in:
2022-12-01 10:28:40 +01:00
parent 0fb4219c92
commit dbff06e5da
6 changed files with 2421 additions and 12 deletions

View File

@@ -10,7 +10,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
#
INPUT := INPUT.txt
INPUT := input/input.txt
SHELL := /bin/bash
CC := gcc
@@ -50,9 +50,9 @@ VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all --track-origins=yes \
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 ex1 ex2 ccls bear org
.PHONY: clean all compile assembly memcheck memcheck1 memcheck2 part1 part2 ccls bear org
all: README.org ccls ex1 ex2
all: README.org ccls part1 part2
memcheck: memcheck1 memcheck2
@@ -69,11 +69,13 @@ cpp: aoc-c.i
assembly: aoc-c.s
ex1: aoc-c
@$(TIME) aoc-c -p 1 < $(INPUT)
part1: aoc-c
@$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1
@#$(TIME) aoc-c -p 1 < $(INPUT)
ex2: aoc-c
@$(TIME) aoc-c -p 2 < $(INPUT)
part2: aoc-c
@$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1
@#$(TIME) aoc-c -p 2 < $(INPUT)
ccls: $(CCLSFILE)

View File

@@ -64,10 +64,26 @@ this is /=24000=/ (carried by the fourth Elf).
Find the Elf carrying the most Calories. /How many total Calories is
that Elf carrying?/
To begin, [[file:1/input][get your puzzle input]].
Your puzzle answer was =66719=.
Answer:
** --- Part Two ---
By the time you calculate the answer to the Elves' question, they've
already realized that the Elf carrying the most Calories of food might
eventually /run out of snacks/.
You can also [Shareon
[[https://twitter.com/intent/tweet?text=%22Calorie+Counting%22+%2D+Day+1+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F1&related=ericwastl&hashtags=AdventOfCode][Twitter]]
[[javascript:void(0);][Mastodon]]] this puzzle.
To avoid this unacceptable situation, the Elves would instead like to
know the total Calories carried by the /top three/ Elves carrying the
most Calories. That way, even if one of those Elves runs out of snacks,
they still have two backups.
In the example above, the top three Elves are the fourth Elf (with
=24000= Calories), then the third Elf (with =11000= Calories), then the
fifth Elf (with =10000= Calories). The sum of the Calories carried by
these three elves is =45000=.
Find the top three Elves carrying the most Calories. /How many Calories
are those Elves carrying in total?/
Your puzzle answer was =198551=.
Both parts of this puzzle are complete! They provide two gold stars: **

56
2022/day01/aoc.bash Executable file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env bash
#
# aoc.bash: Advent of Code 2022, day 1
#
# 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
. common.bash
declare -a total
declare max=0
declare -i res
parse() {
local -i elf=0
while read -r line; do
if [[ -n $line ]]; then
(( total[elf] += line))
else
(( total[elf] > max )) && (( max = total[elf] ))
((elf++))
fi
done
}
part1() {
res=$max
}
part2() {
local -i i elf newbest
res=0
for ((i=0; i<3; ++i)); do
newbest=0
for ((elf=0; elf<${#total[@]}; ++elf)); do
if (( total[elf] > total[newbest] )); then
newbest=$elf
fi
done
(( res+=total[newbest] ))
unset "total[$newbest]" # remove current max
total=("${total[@]}") # pack array
#printf "%d: %s\n" "$i" "${total[*]}"
done
}
main "$@"
printf "%s: res=%s\n" "$cmdname" "$res"
exit 0

66
2022/day01/common.bash Executable file
View File

@@ -0,0 +1,66 @@
#!/usr/bin/env bash
#
# common.bash: Advent of Code 2022, common bash functions
#
# 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
# shellcheck disable=2034
export cmdname=${0##*/}
export debug=0
export part=1
export res=""
shopt -s extglob
set -o noglob
usage() {
printf "usage: %s [-d DEBUG] [-p PART]\n" "$cmdname"
}
checkargs() {
while getopts p:d: todo; do
case "$todo" in
d)
if [[ "$OPTARG" =~ ^[[:digit:]+]$ ]]; then
debug="$OPTARG"
else
printf "%s: illegal [%s] debug level.\n" "$CMD" "$OPTARG"
exit 1
fi
;;
p)
if [[ "$OPTARG" =~ ^[12]$ ]]; then
part="$OPTARG"
else
printf "%s: illegal [%s] part.\n" "$CMD" "$OPTARG"
exit 1
fi
;;
*)
usage
exit 1
;;
esac
done
# Now check remaining argument (backup directory)
shift $((OPTIND - 1))
(( $# > 1 )) && usage
}
main() {
checkargs "$@"
parse
if ((part == 1)); then
part1
else
part2
fi
}

View File

@@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

2255
2022/day01/input/input.txt Normal file

File diff suppressed because it is too large Load Diff