init day 9
This commit is contained in:
111
2022/day09/Makefile
Normal file
111
2022/day09/Makefile
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
# AOC daily Makefile - GNU make only.
|
||||||
|
#
|
||||||
|
# Copyright (C) 2021-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>
|
||||||
|
#
|
||||||
|
|
||||||
|
INPUT := input/input.txt
|
||||||
|
SHELL := /bin/bash
|
||||||
|
|
||||||
|
CC := gcc
|
||||||
|
BEAR := bear
|
||||||
|
CCLSFILE:= compile_commands.json
|
||||||
|
|
||||||
|
LIB := aoc_$(shell uname -m)
|
||||||
|
INCDIR := ../include
|
||||||
|
LIBDIR := ../lib
|
||||||
|
LDFLAGS := -L$(LIBDIR)
|
||||||
|
#LDLIB := -l$(LIB) -lm
|
||||||
|
LDLIB := -l$(LIB)
|
||||||
|
|
||||||
|
export LD_LIBRARY_PATH = $(LIBDIR)
|
||||||
|
|
||||||
|
CFLAGS += -std=gnu11
|
||||||
|
CFLAGS += -O2
|
||||||
|
CFLAGS += -g
|
||||||
|
# for gprof
|
||||||
|
# CFLAGS += -pg
|
||||||
|
CFLAGS += -Wall
|
||||||
|
CFLAGS += -Wextra
|
||||||
|
CFLAGS += -march=native
|
||||||
|
# Next one may be useful for valgrind (some invalid instructions)
|
||||||
|
# CFLAGS += -mno-tbm
|
||||||
|
CFLAGS += -Wmissing-declarations
|
||||||
|
CFLAGS += -Wno-unused-result
|
||||||
|
|
||||||
|
CFLAGS += -DDEBUG_DEBUG # activate general debug (debug.c)
|
||||||
|
CFLAGS += -DDEBUG_POOL # memory pools management
|
||||||
|
|
||||||
|
VALGRIND := valgrind
|
||||||
|
VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all --track-origins=yes \
|
||||||
|
--sigill-diagnostics=yes --quiet --show-error-list=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 part1 part2 ccls bear org
|
||||||
|
|
||||||
|
all: README.org ccls part1 part2
|
||||||
|
|
||||||
|
memcheck: memcheck1 memcheck2
|
||||||
|
|
||||||
|
memcheck1: aoc-c
|
||||||
|
@$(VALGRIND) $(VALGRINDFLAGS) aoc-c -p 1 < $(INPUT)
|
||||||
|
|
||||||
|
memcheck2: aoc-c
|
||||||
|
@$(VALGRIND) $(VALGRINDFLAGS) aoc-c -p 2 < $(INPUT)
|
||||||
|
@#@valgrind -s --track-origins=yes aoc-c -p 2 < $(INPUT)
|
||||||
|
|
||||||
|
compile: aoc-c
|
||||||
|
|
||||||
|
cpp: aoc-c.i
|
||||||
|
|
||||||
|
assembly: aoc-c.s
|
||||||
|
|
||||||
|
part1: aoc-c
|
||||||
|
@$(TIME) aoc.bash -p 1 < $(INPUT) 2>&1
|
||||||
|
@$(TIME) aoc-c -p 1 < $(INPUT)
|
||||||
|
|
||||||
|
part2: aoc-c
|
||||||
|
@$(TIME) aoc.bash -p 2 < $(INPUT) 2>&1
|
||||||
|
@$(TIME) aoc-c -p 2 < $(INPUT)
|
||||||
|
|
||||||
|
ccls: $(CCLSFILE)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f aoc-c core* vgcore* gmon.out aoc-c.s aoc-c.i README.html compile_commands.json
|
||||||
|
|
||||||
|
aoc-c: aoc-c.c common.c
|
||||||
|
@echo compiling $<
|
||||||
|
$(CC) $(CFLAGS) $(LDFLAGS) -I $(INCDIR) $^ $(LDLIB) -o $@
|
||||||
|
|
||||||
|
# generate pre-processed file (.i) and assembler (.s)
|
||||||
|
%.i: %.c
|
||||||
|
@echo generating $@
|
||||||
|
@$(CC) -E $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||||
|
|
||||||
|
%.s: %.c
|
||||||
|
@echo generating $@
|
||||||
|
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||||
|
|
||||||
|
# generate README.org from README.html (must cleanup !)
|
||||||
|
org: README.org
|
||||||
|
|
||||||
|
%.org: %.html
|
||||||
|
@echo generating $@. Cleanup before commit !
|
||||||
|
@pandoc $< -o $@
|
||||||
|
|
||||||
|
# generate compile_commands.json
|
||||||
|
$(CCLSFILE): aoc-c.c Makefile
|
||||||
|
$(BEAR) -- make clean compile
|
||||||
|
|
||||||
|
bear: clean
|
||||||
|
@touch .ccls-root
|
||||||
|
@$(BEAR) -- make compile
|
285
2022/day09/README.org
Normal file
285
2022/day09/README.org
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
** --- Day 9: Rope Bridge ---
|
||||||
|
This rope bridge creaks as you walk along it. You aren't sure how old it
|
||||||
|
is, or whether it can even support your weight.
|
||||||
|
|
||||||
|
It seems to support the Elves just fine, though. The bridge spans a
|
||||||
|
gorge which was carved out by the massive river far below you.
|
||||||
|
|
||||||
|
You step carefully; as you do, the ropes stretch and twist. You decide
|
||||||
|
to distract yourself by modeling rope physics; maybe you can even figure
|
||||||
|
out where /not/ to step.
|
||||||
|
|
||||||
|
Consider a rope with a knot at each end; these knots mark the /head/ and
|
||||||
|
the /tail/ of the rope. If the head moves far enough away from the tail,
|
||||||
|
the tail is pulled toward the head.
|
||||||
|
|
||||||
|
Due to nebulous reasoning involving
|
||||||
|
[[https://en.wikipedia.org/wiki/Planck_units#Planck_length][Planck
|
||||||
|
lengths]], you should be able to model the positions of the knots on a
|
||||||
|
two-dimensional grid. Then, by following a hypothetical /series of
|
||||||
|
motions/ (your puzzle input) for the head, you can determine how the
|
||||||
|
tail will move.
|
||||||
|
|
||||||
|
Due to the aforementioned Planck lengths, the rope must be quite short;
|
||||||
|
in fact, the head (=H=) and tail (=T=) must /always be touching/
|
||||||
|
(diagonally adjacent and even overlapping both count as touching):
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
....
|
||||||
|
.TH.
|
||||||
|
....
|
||||||
|
|
||||||
|
....
|
||||||
|
.H..
|
||||||
|
..T.
|
||||||
|
....
|
||||||
|
|
||||||
|
...
|
||||||
|
.H. (H covers T)
|
||||||
|
...
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
If the head is ever two steps directly up, down, left, or right from the
|
||||||
|
tail, the tail must also move one step in that direction so it remains
|
||||||
|
close enough:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
..... ..... .....
|
||||||
|
.TH.. -> .T.H. -> ..TH.
|
||||||
|
..... ..... .....
|
||||||
|
|
||||||
|
... ... ...
|
||||||
|
.T. .T. ...
|
||||||
|
.H. -> ... -> .T.
|
||||||
|
... .H. .H.
|
||||||
|
... ... ...
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
Otherwise, if the head and tail aren't touching and aren't in the same
|
||||||
|
row or column, the tail always moves one step diagonally to keep up:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
..... ..... .....
|
||||||
|
..... ..H.. ..H..
|
||||||
|
..H.. -> ..... -> ..T..
|
||||||
|
.T... .T... .....
|
||||||
|
..... ..... .....
|
||||||
|
|
||||||
|
..... ..... .....
|
||||||
|
..... ..... .....
|
||||||
|
..H.. -> ...H. -> ..TH.
|
||||||
|
.T... .T... .....
|
||||||
|
..... ..... .....
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
You just need to work out where the tail goes as the head follows a
|
||||||
|
series of motions. Assume the head and the tail both start at the same
|
||||||
|
position, overlapping.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
R 4
|
||||||
|
U 4
|
||||||
|
L 3
|
||||||
|
D 1
|
||||||
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
This series of motions moves the head /right/ four steps, then /up/ four
|
||||||
|
steps, then /left/ three steps, then /down/ one step, and so on. After
|
||||||
|
each step, you'll need to update the position of the tail if the step
|
||||||
|
means the head is no longer adjacent to the tail. Visually, these
|
||||||
|
motions occur as follows (=s= marks the starting position as a reference
|
||||||
|
point):
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
== Initial State ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
H..... (H covers T, s)
|
||||||
|
|
||||||
|
== R 4 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
TH.... (T covers s)
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
sTH...
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.TH..
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s..TH.
|
||||||
|
|
||||||
|
== U 4 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
....H.
|
||||||
|
s..T..
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
....H.
|
||||||
|
....T.
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
....H.
|
||||||
|
....T.
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
....H.
|
||||||
|
....T.
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
== L 3 ==
|
||||||
|
|
||||||
|
...H..
|
||||||
|
....T.
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
..HT..
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
.HT...
|
||||||
|
......
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
== D 1 ==
|
||||||
|
|
||||||
|
..T...
|
||||||
|
.H....
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
== R 4 ==
|
||||||
|
|
||||||
|
..T...
|
||||||
|
..H...
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
..T...
|
||||||
|
...H..
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
...TH.
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
....TH
|
||||||
|
......
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
== D 1 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
....T.
|
||||||
|
.....H
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
== L 5 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
....T.
|
||||||
|
....H.
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
....T.
|
||||||
|
...H..
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
..HT..
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
.HT...
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
HT....
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
== R 2 ==
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
.H.... (H covers T)
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
|
||||||
|
......
|
||||||
|
......
|
||||||
|
.TH...
|
||||||
|
......
|
||||||
|
s.....
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
After simulating the rope, you can count up all of the positions the
|
||||||
|
/tail visited at least once/. In this diagram, =s= again marks the
|
||||||
|
starting position (which the tail also visited) and =#= marks other
|
||||||
|
positions the tail visited:
|
||||||
|
|
||||||
|
#+begin_example
|
||||||
|
..##..
|
||||||
|
...##.
|
||||||
|
.####.
|
||||||
|
....#.
|
||||||
|
s###..
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
So, there are =13= positions the tail visited at least once.
|
||||||
|
|
||||||
|
Simulate your complete hypothetical series of motions. /How many
|
||||||
|
positions does the tail of the rope visit at least once?/
|
17
2022/day09/aoc.h
Normal file
17
2022/day09/aoc.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/* aoc.c: Advent of Code 2022
|
||||||
|
*
|
||||||
|
* 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>
|
||||||
|
*/
|
||||||
|
#ifndef _AOC_H_
|
||||||
|
#define _AOC_H_
|
||||||
|
|
||||||
|
extern int parseargs(int ac, char**av);
|
||||||
|
|
||||||
|
#endif /* _AOC_H_ */
|
68
2022/day09/common.bash
Executable file
68
2022/day09/common.bash
Executable file
@@ -0,0 +1,68 @@
|
|||||||
|
#!/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 res
|
||||||
|
export LANG=C
|
||||||
|
|
||||||
|
shopt -s extglob
|
||||||
|
set -o noglob
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
printf "usage: %s [-d DEBUG] [-p PART]\n" "$cmdname"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
checkargs() {
|
||||||
|
local part=1
|
||||||
|
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
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
# Now check remaining argument (backup directory)
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
|
(( $# > 1 )) && usage
|
||||||
|
return "$part"
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
local -i part
|
||||||
|
|
||||||
|
checkargs "$@"
|
||||||
|
part=$?
|
||||||
|
parse "$part"
|
||||||
|
solve "$part"
|
||||||
|
printf "%s: res=%s\n" "$cmdname" "$res"
|
||||||
|
}
|
49
2022/day09/common.c
Normal file
49
2022/day09/common.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/* common.c: Advent of Code 2022, common 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>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "aoc.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
|
static int usage(char *prg)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [-d debug_level] [-p part] [-i input]\n", prg);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int parseargs(int ac, char **av)
|
||||||
|
{
|
||||||
|
int opt, part = 1;
|
||||||
|
|
||||||
|
while ((opt = getopt(ac, av, "d:p:")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case 'd':
|
||||||
|
debug_level_set(atoi(optarg));
|
||||||
|
break;
|
||||||
|
case 'p': /* 1 or 2 */
|
||||||
|
part = atoi(optarg);
|
||||||
|
if (part < 1 || part > 2)
|
||||||
|
return usage(*av);
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
|
||||||
|
default:
|
||||||
|
return usage(*av);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (optind < ac)
|
||||||
|
return usage(*av);
|
||||||
|
return part;
|
||||||
|
}
|
8
2022/day09/input/example.txt
Normal file
8
2022/day09/input/example.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
R 4
|
||||||
|
U 4
|
||||||
|
L 3
|
||||||
|
D 1
|
||||||
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2
|
2000
2022/day09/input/input.txt
Normal file
2000
2022/day09/input/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user