Compare commits
2 Commits
25d25b399e
...
dfe2207e8e
| Author | SHA1 | Date | |
|---|---|---|---|
| dfe2207e8e | |||
| 31a255a9ac |
@@ -153,7 +153,7 @@ aoc-c: res=7490863
|
|||||||
context-switch: 0+1, page-faults: 0+98
|
context-switch: 0+1, page-faults: 0+98
|
||||||
|
|
||||||
=========================================
|
=========================================
|
||||||
================= day07 =================
|
================= day08 =================
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
+++++++++++++++++ part 1
|
+++++++++++++++++ part 1
|
||||||
@@ -161,7 +161,15 @@ aoc.bash: res=1698
|
|||||||
time: 0:01.10 real, 1.10 user, 0.00 sys
|
time: 0:01.10 real, 1.10 user, 0.00 sys
|
||||||
context-switch: 5+1, page-faults: 0+319
|
context-switch: 5+1, page-faults: 0+319
|
||||||
|
|
||||||
|
aoc-c: res=1698
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 0+1, page-faults: 0+92
|
||||||
|
|
||||||
+++++++++++++++++ part 2
|
+++++++++++++++++ part 2
|
||||||
aoc.bash: res=672280
|
aoc.bash: res=672280
|
||||||
time: 0:03.54 real, 3.53 user, 0.00 sys
|
time: 0:03.54 real, 3.53 user, 0.00 sys
|
||||||
context-switch: 48+1, page-faults: 0+284
|
context-switch: 48+1, page-faults: 0+284
|
||||||
|
|
||||||
|
aoc-c: res=672280
|
||||||
|
time: 0:00.00 real, 0.00 user, 0.00 sys
|
||||||
|
context-switch: 0+1, page-faults: 0+91
|
||||||
|
|||||||
159
2022/day08/aoc-c.c
Normal file
159
2022/day08/aoc-c.c
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/* aoc-c.c: Advent of Code 2022, day 8
|
||||||
|
*
|
||||||
|
* 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 <string.h>
|
||||||
|
|
||||||
|
#include "bits.h"
|
||||||
|
#include "aoc.h"
|
||||||
|
|
||||||
|
typedef struct tree {
|
||||||
|
u16 size;
|
||||||
|
char *h; /* heigts */
|
||||||
|
char *v; /* visible (part 1) */
|
||||||
|
} tree_t;
|
||||||
|
|
||||||
|
#define HEIGHT(t, x, y) (((t)->h[((t)->size * (y)) + (x)]))
|
||||||
|
#define VISIBLE(t, x, y) (((t)->v[((t)->size * (y)) + (x)]))
|
||||||
|
|
||||||
|
static tree_t *parse(tree_t *trees)
|
||||||
|
{
|
||||||
|
size_t alloc = 0;
|
||||||
|
char *buf = NULL;
|
||||||
|
ssize_t buflen;
|
||||||
|
|
||||||
|
buflen = getline(&buf, &alloc, stdin);
|
||||||
|
buf[--buflen] = 0;
|
||||||
|
trees->size = buflen;
|
||||||
|
trees->h = malloc((buflen * buflen) * sizeof(char) + 1);
|
||||||
|
memcpy(trees->h, buf, buflen); /* store first line */
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
for (buf = trees->h + buflen; ; buf += buflen) {
|
||||||
|
if (scanf("%s", buf) <= 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
trees->v = calloc(buflen * buflen, sizeof (char));
|
||||||
|
return trees;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int visible(tree_t *t, int *max, int x, int y)
|
||||||
|
{
|
||||||
|
int h = HEIGHT(t, x, y);
|
||||||
|
if (h > *max) {
|
||||||
|
VISIBLE(t, x, y) = 1;
|
||||||
|
if ((*max = h) == '9') {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int check_tree(tree_t *t, int x, int y)
|
||||||
|
{
|
||||||
|
int i, h = HEIGHT(t, x, y), res = 1, size = t->size, tmp;
|
||||||
|
|
||||||
|
for (tmp = 0, i = x + 1; i < size ; ++i) { /* east */
|
||||||
|
tmp++;
|
||||||
|
if (HEIGHT(t, i, y) >= h)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res *= tmp;
|
||||||
|
for (tmp = 0, i = x - 1; i >= 0; --i) { /* west */
|
||||||
|
tmp++;
|
||||||
|
if (HEIGHT(t, i, y) >= h)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res *= tmp;
|
||||||
|
for (tmp = 0, i = y + 1; i < size; ++i) { /* south */
|
||||||
|
tmp++;
|
||||||
|
if (HEIGHT(t, x, i) >= h)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res *= tmp;
|
||||||
|
for (tmp = 0, i = y - 1; i >= 0; --i) { /* north */
|
||||||
|
tmp++;
|
||||||
|
if (HEIGHT(t, x, i) >= h)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res *= tmp;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int part1(tree_t *t)
|
||||||
|
{
|
||||||
|
int x, y, max, res = 0, size = t->size;
|
||||||
|
|
||||||
|
for (y = 1; y < size -1; ++y) { /* to east */
|
||||||
|
if ((max = HEIGHT(t, 0, y)) < '9') {
|
||||||
|
for (x = 1; x < size -1; ++x) {
|
||||||
|
if (visible (t, &max, x, y))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (y = 1; y < size -1; ++y) { /* to west */
|
||||||
|
if ((max = HEIGHT(t, size - 1, y)) < '9') {
|
||||||
|
for (x = size - 2; x > 0; --x) {
|
||||||
|
if (visible (t, &max, x, y)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (x = 1; x < size -1; ++x) { /* to south */
|
||||||
|
if ((max = HEIGHT(t, x, 0)) < '9') {
|
||||||
|
for (y = 1; y < size -1; ++y) {
|
||||||
|
if (visible (t, &max, x, y))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (x = 1; x < size -1; ++x) { /* to north */
|
||||||
|
if ((max = HEIGHT(t, x, size - 1)) < '9') {
|
||||||
|
for (y = size - 2; y > 0; --y) {
|
||||||
|
if (visible (t, &max, x, y))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (y = 0; y < size; ++y)
|
||||||
|
for (x = 0; x < size; ++x)
|
||||||
|
if (VISIBLE(t, x, y))
|
||||||
|
res++;
|
||||||
|
return res + size * 4 - 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int part2(tree_t *t)
|
||||||
|
{
|
||||||
|
int res = 0, tmp, size = t->size;
|
||||||
|
|
||||||
|
for (int y = 1; y < size - 1; ++y) {
|
||||||
|
for (int x = 1; x < size - 1; ++x) {
|
||||||
|
tmp = check_tree(t, x, y);
|
||||||
|
if (tmp > res)
|
||||||
|
res = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
int part = parseargs(ac, av);
|
||||||
|
tree_t trees;
|
||||||
|
|
||||||
|
parse(&trees);
|
||||||
|
printf("%s: res=%d\n", *av, part == 1? part1(&trees): part2(&trees));
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
@@ -45,30 +45,30 @@ check_visible() {
|
|||||||
|
|
||||||
part1() {
|
part1() {
|
||||||
declare -ig res
|
declare -ig res
|
||||||
local -i x y max c
|
local -i x y max
|
||||||
|
|
||||||
for ((y = 1; y < size -1; ++y)); do # left to right
|
for ((y = 1; y < size -1; ++y)); do # to east
|
||||||
height max 0 "$y"
|
height max 0 "$y"
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((x = 1; x < size -1; ++x)); do
|
for ((x = 1; x < size -1; ++x)); do
|
||||||
check_visible max "$x" "$y" || break
|
check_visible max "$x" "$y" || break
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
for ((y = 1; y < size -1; ++y)); do # right to left
|
for ((y = 1; y < size -1; ++y)); do # to west
|
||||||
height max $((size - 1)) "$y"
|
height max $((size - 1)) "$y"
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((x = size - 2; x > 0; --x)); do
|
for ((x = size - 2; x > 0; --x)); do
|
||||||
check_visible max "$x" "$y" || break
|
check_visible max "$x" "$y" || break
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
for ((x = 1; x < size -1; ++x)); do # top to bottom
|
for ((x = 1; x < size -1; ++x)); do # to south
|
||||||
height max "$x" 0
|
height max "$x" 0
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((y = 1; y < size -1; ++y)); do
|
for ((y = 1; y < size -1; ++y)); do
|
||||||
check_visible max "$x" "$y" || break
|
check_visible max "$x" "$y" || break
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
for ((x = 1; x < size -1; ++x)); do # bottom to top
|
for ((x = 1; x < size -1; ++x)); do # to north
|
||||||
height max "$x" $((size - 1))
|
height max "$x" $((size - 1))
|
||||||
(( max == 9 )) && continue
|
(( max == 9 )) && continue
|
||||||
for ((y = size - 2; y > 0; --y)); do
|
for ((y = size - 2; y > 0; --y)); do
|
||||||
|
|||||||
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