diff --git a/2022/day06/Makefile b/2022/day06/Makefile new file mode 100644 index 0000000..e0add74 --- /dev/null +++ b/2022/day06/Makefile @@ -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 . +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + +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 diff --git a/2022/day06/README.org b/2022/day06/README.org new file mode 100644 index 0000000..6e04550 --- /dev/null +++ b/2022/day06/README.org @@ -0,0 +1,92 @@ +** --- Day 6: Tuning Trouble --- +The preparations are finally complete; you and the Elves leave camp on +foot and begin to make your way toward the /star/ fruit grove. + +As you move through the dense undergrowth, one of the Elves gives you a +handheld /device/. He says that it has many fancy features, but the most +important one to set up right now is the /communication system/. + +However, because he's heard you have [[/2016/day/6][significant]] +[[/2016/day/25][experience]] [[/2019/day/7][dealing]] +[[/2019/day/9][with]] [[/2019/day/16][signal-based]] +[[/2021/day/25][systems]], he convinced the other Elves that it would be +okay to give you their one malfunctioning device - surely you'll have no +problem fixing it. + +As if inspired by comedic timing, the device emits a few colorful +sparks. + +To be able to communicate with the Elves, the device needs to /lock on +to their signal/. The signal is a series of seemingly-random characters +that the device receives one at a time. + +To fix the communication system, you need to add a subroutine to the +device that detects a /start-of-packet marker/ in the datastream. In the +protocol being used by the Elves, the start of a packet is indicated by +a sequence of /four characters that are all different/. + +The device will send your subroutine a datastream buffer (your puzzle +input); your subroutine needs to identify the first position where the +four most recently received characters were all different. Specifically, +it needs to report the number of characters from the beginning of the +buffer to the end of the first such four-character marker. + +For example, suppose you receive the following datastream buffer: + +#+begin_example +mjqjpqmgbljsphdztnvjfqwrcgsmlb +#+end_example + +After the first three characters (=mjq=) have been received, there +haven't been enough characters received yet to find the marker. The +first time a marker could occur is after the fourth character is +received, making the most recent four characters =mjqj=. Because =j= is +repeated, this isn't a marker. + +The first time a marker appears is after the /seventh/ character +arrives. Once it does, the last four characters received are =jpqm=, +which are all different. In this case, your subroutine should report the +value =7=, because the first start-of-packet marker is complete after 7 +characters have been processed. + +Here are a few more examples: + +- =bvwbjplbgvbhsrlpgdmjqwftvncz=: first marker after character =5= +- =nppdvjthqldpwncqszvftbrmjlhg=: first marker after character =6= +- =nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg=: first marker after character =10= +- =zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw=: first marker after character =11= + +/How many characters need to be processed before the first +start-of-packet marker is detected?/ + +Your puzzle answer was =1658=. + +The first half of this puzzle is complete! It provides one gold star: * + +** --- Part Two --- +Your device's communication system is correctly detecting packets, but +still isn't working. It looks like it also needs to look for /messages/. + +A /start-of-message marker/ is just like a start-of-packet marker, +except it consists of /14 distinct characters/ rather than 4. + +Here are the first positions of start-of-message markers for all of the +above examples: + +- =mjqjpqmgbljsphdztnvjfqwrcgsmlb=: first marker after character =19= +- =bvwbjplbgvbhsrlpgdmjqwftvncz=: first marker after character =23= +- =nppdvjthqldpwncqszvftbrmjlhg=: first marker after character =23= +- =nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg=: first marker after character =29= +- =zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw=: first marker after character =26= + +/How many characters need to be processed before the first +start-of-message marker is detected?/ + +Answer: + +Although it hasn't changed, you can still [[file:6/input][get your +puzzle input]]. + +You can also [Shareon +[[https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Tuning+Trouble%22+%2D+Day+6+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F6&related=ericwastl&hashtags=AdventOfCode][Twitter]] +[[javascript:void(0);][Mastodon]]] this puzzle. diff --git a/2022/day06/aoc.bash b/2022/day06/aoc.bash new file mode 100755 index 0000000..e5ff241 --- /dev/null +++ b/2022/day06/aoc.bash @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# +# aoc.bash: Advent of Code 2022, day 6 +# +# 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 + +declare -a stacks=() rules=() + +parse() { + local -i _queue _state=1 + local _input + local -a _parse + local cur="" + local -i lcur=0 i + + read -r _input + len=${#_input} + + lcur=0 + for (( i = 0; i < len - 4; ++i )); do + echo + nextc=${_input:i:1} # get next char + printf "next=%s cur=%s lcur=%d\n" "$nextc" "$cur" "$lcur" + for ((j = 0; j < lcur; ++j)); do # compare with previous ones + printf "compare %s with %d:%s\n" "$nextc" "$j" "${cur:j:1}" + if [[ $nextc == "${cur:j:1}" ]]; then + cur=${cur:j+1}$nextc + (( lcur = lcur - (j + 1) + 1)) + printf "\t-> equal cur=%d:%s" "$lcur" "$cur" + continue 2 + fi + done + cur+=$nextc + ((lcur++)) + ((lcur == 4)) && break + done + echo "$cur" $((i + 1)) +} + +solve() { + local -i _part="$1" _i=0 _j=0 _nb _from _to + local -a _rule + + for ((; _i < ${#rules[@]}; ++_i )); do + read -ra _rule <<< "${rules[$_i]}" + (( _nb = _rule[0], _from = _rule[1] - 1, _to = _rule[2] - 1 )) + if ((_part == 1)); then + # move elements one by one + for ((_j = 0; _j < _nb; ++_j)); do + stacks[$_to]="${stacks[$_from]:0:1}${stacks[$_to]}" + stacks[$_from]=${stacks[$_from]:1} + done + else + # move elements by block + stacks[$_to]="${stacks[$_from]:0:_nb}${stacks[$_to]}" + stacks[$_from]=${stacks[$_from]:_nb} + fi + done + printf -v res "%c" "${stacks[@]}" +} + +main "$@" +exit 0 diff --git a/2022/day06/aoc.h b/2022/day06/aoc.h new file mode 100644 index 0000000..2ef8361 --- /dev/null +++ b/2022/day06/aoc.h @@ -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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ +#ifndef _AOC_H_ +#define _AOC_H_ + +extern int parseargs(int ac, char**av); + +#endif /* _AOC_H_ */ diff --git a/2022/day06/common.bash b/2022/day06/common.bash new file mode 100755 index 0000000..5af1e54 --- /dev/null +++ b/2022/day06/common.bash @@ -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 . +# +# SPDX-License-Identifier: GPL-3.0-or-later + +# 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" +} diff --git a/2022/day06/common.c b/2022/day06/common.c new file mode 100644 index 0000000..a3827b6 --- /dev/null +++ b/2022/day06/common.c @@ -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 . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include + +#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; +} diff --git a/2022/day06/input/example.txt b/2022/day06/input/example.txt new file mode 100644 index 0000000..7980a82 --- /dev/null +++ b/2022/day06/input/example.txt @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb diff --git a/2022/day06/input/example2.txt b/2022/day06/input/example2.txt new file mode 100644 index 0000000..19fe247 --- /dev/null +++ b/2022/day06/input/example2.txt @@ -0,0 +1 @@ +bvwbjplbgvbhsrlpgdmjqwftvncz diff --git a/2022/day06/input/example3.txt b/2022/day06/input/example3.txt new file mode 100644 index 0000000..0f12ee2 --- /dev/null +++ b/2022/day06/input/example3.txt @@ -0,0 +1 @@ +nppdvjthqldpwncqszvftbrmjlhg diff --git a/2022/day06/input/example4.txt b/2022/day06/input/example4.txt new file mode 100644 index 0000000..38510e7 --- /dev/null +++ b/2022/day06/input/example4.txt @@ -0,0 +1 @@ +nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg diff --git a/2022/day06/input/example5.txt b/2022/day06/input/example5.txt new file mode 100644 index 0000000..e1d0a43 --- /dev/null +++ b/2022/day06/input/example5.txt @@ -0,0 +1 @@ +zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw diff --git a/2022/day06/input/input.txt b/2022/day06/input/input.txt new file mode 100644 index 0000000..a8f698b --- /dev/null +++ b/2022/day06/input/input.txt @@ -0,0 +1 @@ +cbhbmmqnmmqvqfqgqcqmmtvvvdsvscvssmfmhfmhhgdgcclhlnnvmnvmnnwngnqggqlqhhbqbfbvffcpfcfpcpmcppsgssvcvmmdwmwsmwswnwvnvvtgvgfgqqnlltgllpplhpllrnrwrtrzrlrzllpnlpnplnlwnnzfztzcchczctcbbpwpbbfhhqllgrllnnghnnlnznzzlqqmggqbgbjjlnlnjllwlnwnjnbjnnhhcddgmdmqmllfjllddmgmpggtvvqqwmmhcmmjpjdpdqqnhhqbhhscstsvttbwtwdtwwjffvccrwrvvnqqtwwhgwhwthwthhvshsfsjstshthqqdfdhdqhqrqggfffvhffhbbwtbtvtwvvwgvvjjjvtjvvlmlmtmztmthmhttjwwmqqjffhvhqvvrddbsbfbgffmcfffqqfzzgmggzvggbjjmccftcftfnttqqjzjdzdrrgvgsgvvlqvlqqlplnnjccjqcjcgjgfjgjlgjjzhhjbbbfdbfddgccncppsbbjjsttcqtqgqpqllqggtsthhtvvtrttbthtnhhddvnnbggmtggcsgcscmmvmllttjbjggbzbttrfttgsstwtftdthdththbttcggdldtdlttbvvpgpjpwpwvwcwjwsjsttznztzftzzjpzzrtzrztrrtsrrdtrdtdbdhhdgdwwnjnnvjvsjjrwjjvrrzvrvllrgghgbbgbsssslqlrllbjbttzfztzpzzvtvccszsddtqttjllqgqgpqphptpwttwtmtltljjfnnzwzmzlzssghssqhhphbhfftwwqmwmzzqtzqzqrrndrrmrqrzrszzrcrbccglgcgwgrgdggtvtpptgtfgflglgrrnvrvhrrhlrhhfchfhdhzzwggvbbgfgrrpdrdccfllqflfmlfmlmbbhnbbsblbzbgbqqjppcddcvvsddjfjjlnnwbbflfzzmlmddjssjffcqffwdfdqqphpnhppbttjfjzzdvdjvvddnfffmjfmfmpfmmmdlmlrrhqqcpcmclmmbmppzczjjmlltqlqzzqgqcggdmdzznnnnsjjvbjjwbwhbhfhvvtmmsfmmcgmcmwcwhwnhhpccmbmdbmbgbjbggqqccfllfqqmqwwzbblffthhdnncqnccqppldlpldpllzmmsqqdffnggqlqtllwlglnlnhlhflfssvzzrllbtbnbgnnfvfllwslszswsnnpwwppvspsmmnfnpffrmrhmmctttrjrbjrjpjtpjpssdwdtwtmmtgtsgttlwttfrfvvpjpcjccmtthhldhllzslshhnrnvvfddnllltclcrrhnnjwwwpfpddmtmsmfflslrlmlmwmllgfgmfgmmjttpcpspslsjspsqstqsqqpsprphrrpqqfzqqwcwcddsjjpvvfnfvnnnrncnwcwczzcwzwnwmwffbdfbdfdjjhrhvhhvwvsvhshnhwwzlwlnnphhtjjmljjqzjjsjgsjshhwjjmttvgvsgstggrcrmmrqrhrqrzrmzmggqvqtqgtqqqqltqqwggmmzggjccgmgwgqqmnqnrnprptrrvfrvffnmnfmmnnnwzzldlvlgvlvqqpnphpgglhlshlhzzpcptpssblqvdbpbbcrngctqgptccwpdcpbcjwdmcrzmhwffgzqmmqwpqvplwzmjlzmvmzmbtqjnhzrpppnpntvmjbzmvhjvbgflmmjnwssvqvbnbddfcwqdtvpvddctmpptcjmvwszhbsttcbjlfjvwlljhlqlvvsnzphdjhfswltdhzprltzcszrgcmnmfznmbccstjvjngwpbsfqssfwvpdhhmlmzttzlszsgpvvbcfvzrlsttlqpnqhwtbqfjnbztnwfnhlhfltgdtsrrwrfhlssvsgzpffnnrhgcvclqnjgfhhzswllvcjffpngcmtjphwnbdfrrgjfrfqbnvbwgccsjvvmjgcvqvtggnvgvgnnchbblqnvvdgjtvdmfrzvhvhzcbqzdslphjjqtcwjqptstvvpqgdbgbcmmchjpvjhvbwtlmlnqpldfcbmwvrmpqcsfdhmmwnbvmpglcfbnsgjmljcwpzpwffcqfrbdrztzhqzhzpcrjfdmrctttdrzlmzcqwjftngwjmgqscftnpbjwqrcvqwbwbdhbhvrdcvgrvctjgwjtmdgzwgvdbmsrjpsbhwcqlqzjnzmgpctlwqhfnbmgsprjcqtfjjvzljqpfqgnvrgnjjlmpqgnzpmpsvzjmtvnfgslldjtjdwlzrvqrbvcpspzpcdnpdjmhcdhsbmthmgjqchqwwsnfjnwgpctlwcdpqpmzqcrptscngqnmhzwjdcqwvdmlzntsfbstbtmmnlsspjbjjdspnslgcnlbfnbhjlzjhrrmdbbwtswqpbpwtzhcnldbsfrvndzvcgzjprcrclhfwbvzgthcglfzqrshgtvbflvnznhmlzdtfnrsltnnppqgrtndbsfqmftdnzmqfwdpcrmssldmrrnzchnqpflgbqzqjjrzlfnptqdwzdhmctfbztmfcmbqcwjmgnwgqzqjctphrrthgvpvppztvpzgzhdszfhlzlcnwmfrlsnvftfrlfvbnlwzctphhpfvszntqcvnfmvmwwhsjlnpfhcpjvrncgmsbprwwllvnsbjfhtbtmcvvpzcvqrrqdvgzvllvcvnvvbftngqcqcvvbsqfshmnhwptcsbsnjzltqzmflftrhblqszrzsbfcpgzncvwhlqlcbmgjdpfcmlgdfphsfcgqccthmlzjbdwpsbddwwrtwmsvqwbzwhzmghhrspvbptznqsqvhgnhlwqjnhgnzprrtjddwtqfsshjrvlglhdlstghftmllvzmmfglnscnrtgcwwwzjphzhvqlctdwjlqvbsvlgzrdpvjhhcthgjhlwsgfzlschhfprfcrzfrzmnlwvqsnqlllczwfhswchrlggqszwrvpldrffnzrffbhtfbslgcdrqpjcrbqsrgfrhttcptncfndcbsdbjsqjvgbmnmhlnbltdcgbmllpgjmgljvglrwmrvgthhhzrsmzvgwcfgvcsbzpjcqnhrzhznzjcjghhwqvllrmsvlpzlssrdsqwvzvqwsvfsjqvbgrfbmfzlchqsjgncbljtvzsfdnvmfzcbrlnnvvmjbmgjmqzjtdzpljdwqtvwsrlbslwfvlgbtnmlpcwmngncnmdhqctshmjmnhpqcqzhvlbvgptctrvggdgnpnrlnngrfhsqdfthvcnpwjfjnslcqlfrfvhnctmsqgnqmgpwtlzhtdqhqqrcllzjccnszsqvrzhcffvnfnstgdmljdrqrndljdnfbbjvmpqdnqhtdlnzcvnfjlvzmfzrndhtglvngmbrdbpbnhvfmrbcwqzttnjplgrhchjtfjwfbjfbmzlrnllhzccpfhfhnjpfvzlpbqnhwmpssvwtzhdbtnbtllhpfdcqjjnzgbdrfjjbcltnzdrmtmsmvpjtmdnqszgbqncsvhjbgwswhmghpvstrqbglgrtgbchttbznvvdhppzwnttpgcbdjdhlsqhhtlphjjrjncrmfdtjhdwmjgmpngnbptzwgwdztmhtpglnftwpnnmrmcwfhwnhlsbwsrjnlmdlmffbzsnhsvnbldwtrrhdhfsjdrsnzlgtfzcwwqrfhtrmjhmphqndwtbpczvmfgczmdlqjqdlwmvjjzwmqnpvwzmtjwtprlnbvjdhpjgndrwzcfthnwhnhqpwtcjlhrhdplzsncfmszmhjmgljhnlsrrfwplclcvjjqmtpnwbtsbwdnjdlqntvdnfgwbpwspssprbffjdlcvbwcqlttnwnhwdspfsjhppbhspnrvrsfmzvbwwtjfzmnzwgqddbmcjzzqhqlgrglsvgsjdwlnsbtmqgsnfwwqrjsbcgdlmbgqwvgpqllqwbcplfjrgnzsdtdtvqnrbcrqjhdtqqplvszvtlflgbpwnpzczbvhzfjrslcwcswsgfvvsswzzwhtfjfpsrvcfnrs