This commit is contained in:
2022-12-07 13:40:01 +01:00
parent 81a58c6266
commit b8f6763a3b
3 changed files with 20 additions and 20 deletions

View File

@@ -12,9 +12,9 @@ declare floor
function adj() { function adj() {
local -i r="$1" n="$2" c="$3" count=0 local -i r="$1" n="$2" c="$3" count=0
local -a prow=(${rows[r-1]}) local -a prow=("${rows[r-1]}")
local -a row=(${rows[r]}) local -a row=("${rows[r]}")
local -a nrow=(${rows[r+1]}) local -a nrow=("${rows[r+1]}")
#echo #echo
#echo p="${prow[*]}" #echo p="${prow[*]}"
#echo r="${row[*]}" #echo r="${row[*]}"
@@ -51,7 +51,7 @@ function run() {
changed=0 changed=0
seated=0 seated=0
for ((r=1; r<=NROWS; ++r)); do for ((r=1; r<=NROWS; ++r)); do
row=(${rows[r]}) row=("${rows[r]}")
newrow=(0) newrow=(0)
for ((c=1; c<=RLENGTH; ++c)); do for ((c=1; c<=RLENGTH; ++c)); do
newrow+=("${row[c]}") newrow+=("${row[c]}")
@@ -61,13 +61,13 @@ function run() {
case ${row[c]} in case ${row[c]} in
0) continue 0) continue
;; ;;
1) if (( $(adj $r $c 2) == 0 )); then 1) if (( $(adj "$r" "$c" 2) == 0 )); then
((++changed)) ((++changed))
newrow[c]=2 newrow[c]=2
fi fi
#printf "[%d][%d]: %s %s %d\n" $r $c "${row[c]}" "${newrow[c]}" $(adj $r $c 2) #printf "[%d][%d]: %s %s %d\n" $r $c "${row[c]}" "${newrow[c]}" $(adj $r $c 2)
;; ;;
2) if (( $(adj $r $c 2) >= 4 )); then 2) if (( $(adj "$r" "$c" 2) >= 4 )); then
((++changed)) ((++changed))
newrow[c]=1 newrow[c]=1
fi fi

View File

@@ -1,6 +1,6 @@
/* debug.c - debug/log management /* debug.c - debug/log management
* *
* Copyright (C) 2021 Bruno Raoult ("br") * Copyright (C) 2021-2022 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -22,8 +22,9 @@
#include "bits.h" #include "bits.h"
#include "debug.h" #include "debug.h"
#define NANOSEC 1000000000 /* nano sec in sec */ #define NS_SEC 1000000000 /* nano sec in sec */
#define MILLISEC 1000000 /* milli sec in sec */ #define MS_SEC 1000000 /* microsec in sec */
#define NS_MS 1000 /* nano in micro */
static long long timer_start; /* in nanosecond */ static long long timer_start; /* in nanosecond */
static u32 debug_level=0; static u32 debug_level=0;
@@ -41,7 +42,7 @@ void debug_init(u32 level)
debug_level_set(level); debug_level_set(level);
if (!clock_gettime(CLOCK_MONOTONIC, &timer)) { if (!clock_gettime(CLOCK_MONOTONIC, &timer)) {
timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec; timer_start = timer.tv_sec * NS_SEC + timer.tv_nsec;
} }
else { else {
timer_start = 0; timer_start = 0;
@@ -54,10 +55,9 @@ inline static long long timer_elapsed()
struct timespec timer; struct timespec timer;
clock_gettime(CLOCK_MONOTONIC, &timer); clock_gettime(CLOCK_MONOTONIC, &timer);
return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start; return (timer.tv_sec * NS_SEC + timer.tv_nsec) - timer_start;
} }
/* void debug - log function /* void debug - log function
* @timestamp : boolean * @timestamp : boolean
* @indent : indent level (2 spaces each) * @indent : indent level (2 spaces each)
@@ -77,8 +77,8 @@ void debug(u32 level, bool timestamp, u32 indent, const char *src,
if (timestamp) { if (timestamp) {
long long diff = timer_elapsed(); long long diff = timer_elapsed();
printf("%lld.%03lld ", diff/NANOSEC, (diff/1000000)%1000); printf("%lld.%03lld ", diff / NS_SEC, (diff % NS_SEC) / NS_MS);
printf("%010lld ", diff); printf("%010lldμs ", diff / NS_MS);
} }
if (src) { if (src) {

View File

@@ -5,17 +5,17 @@
+++++++++++++++++ part 1 +++++++++++++++++ part 1
aoc.bash: res=66719 aoc.bash: res=66719
time: 0:00.03 real, 0.02 user, 0.00 sys time: 0:00.03 real, 0.02 user, 0.00 sys
context-switch: 0+1, page-faults: 0+274 context-switch: 0+1, page-faults: 0+257
aoc-c: res=66719 aoc-c: res=66719
time: 0:00.00 real, 0.00 user, 0.00 sys time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+89 context-switch: 0+1, page-faults: 0+88
+++++++++++++++++ part 2 +++++++++++++++++ part 2
aoc.bash: res=198551 aoc.bash: res=198551
time: 0:00.03 real, 0.03 user, 0.00 sys time: 0:00.03 real, 0.03 user, 0.00 sys
context-switch: 2+1, page-faults: 0+283 context-switch: 2+1, page-faults: 0+259
aoc-c: res=198551 aoc-c: res=198551
time: 0:00.00 real, 0.00 user, 0.00 sys time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 5+1, page-faults: 0+87 context-switch: 0+1, page-faults: 0+87