From 11844d1904e45b06f38e78d438adbbe1a693ad20 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Mon, 21 Dec 2020 14:58:15 +0100 Subject: [PATCH] Initial Commit - days 01-12 --- .gitignore | 4 + Makefile | 31 + OUTPUT | 344 ++++++ README | 4 + day01/INPUT.txt | 200 ++++ day01/Makefile | 40 + day01/OUTPUT | 36 + day01/README | 45 + day01/TEST.txt | 6 + day01/ex1-22.cob | 99 ++ day01/ex1-23.cob | 96 ++ day01/ex1-c.c | 29 + day01/ex1-cob.cob | 79 ++ day01/ex1.bash | 23 + day01/ex2-c.c | 31 + day01/ex2-pure-sort-cob.cob | 96 ++ day01/ex2-pure-sort.bash | 55 + day01/ex2-sort-cob.cob | 99 ++ day01/ex2-sort.bash | 28 + day01/ex2.bash | 24 + day02/INPUT.txt | 1000 ++++++++++++++++ day02/Makefile | 26 + day02/OUTPUT | 12 + day02/README | 48 + day02/ex1-c.c | 31 + day02/ex1.bash | 18 + day02/ex2-c.c | 24 + day02/ex2.bash | 20 + day03/INPUT.txt | 323 +++++ day03/Makefile | 26 + day03/OUTPUT | 12 + day03/README | 82 ++ day03/TEST.txt | 11 + day03/ex1-c.c | 38 + day03/ex1.bash | 21 + day03/ex2-c.c | 56 + day03/ex2.bash | 31 + day04/INPUT.txt | 957 +++++++++++++++ day04/Makefile | 25 + day04/OUTPUT | 12 + day04/README | 127 ++ day04/TEST.txt | 13 + day04/ex1-c.c | 146 +++ day04/ex1.bash | 51 + day04/ex2-c.c | 247 ++++ day04/ex2.bash | 125 ++ day05/INPUT.txt | 743 ++++++++++++ day05/Makefile | 25 + day05/OUTPUT | 12 + day05/README | 62 + day05/TEST.txt | 5 + day05/ex1-c.c | 35 + day05/ex1.bash | 33 + day05/ex2-c.c | 53 + day05/ex2.bash | 40 + day06/INPUT.txt | 2199 +++++++++++++++++++++++++++++++++++ day06/Makefile | 41 + day06/OUTPUT | 24 + day06/README | 90 ++ day06/TEST.txt | 15 + day06/bash.sh | 4 + day06/c.sh | 5 + day06/ex1-bis.bash | 73 ++ day06/ex1-c.c | 78 ++ day06/ex1.bash | 55 + day06/ex2-bis.bash | 59 + day06/ex2-c.c | 79 ++ day06/ex2.bash | 58 + day07/INPUT.txt | 594 ++++++++++ day07/Makefile | 26 + day07/OUTPUT | 20 + day07/README | 72 ++ day07/TEST.txt | 9 + day07/TEST2.txt | 7 + day07/ex1-bis.bash | 76 ++ day07/ex1-c.c | 233 ++++ day07/ex1.bash | 78 ++ day07/ex2-c.c | 233 ++++ day07/ex2.bash | 69 ++ day08/INPUT.txt | 656 +++++++++++ day08/Makefile | 28 + day08/OUTPUT | 16 + day08/README | 94 ++ day08/TEST.txt | 9 + day08/ex1-c.c | 143 +++ day08/ex1.bash | 58 + day08/ex2-c.c | 184 +++ day08/ex2.bash | 78 ++ day09/INPUT.txt | 1000 ++++++++++++++++ day09/Makefile | 28 + day09/OUTPUT | 16 + day09/README | 93 ++ day09/TEST.txt | 20 + day09/ex1-c.c | 103 ++ day09/ex1.bash | 51 + day09/ex2-c.c | 134 +++ day09/ex2.bash | 75 ++ day10/INPUT.txt | 97 ++ day10/Makefile | 28 + day10/OUTPUT | 18 + day10/README | 150 +++ day10/TEST.txt | 11 + day10/TEST2.txt | 31 + day10/ex1-c.c | 132 +++ day10/ex1.bash | 31 + day10/ex2-c.c | 146 +++ day10/ex2.bash | 39 + day11/INPUT.txt | 98 ++ day11/Makefile | 30 + day11/OUTPUT | 16 + day11/README | 219 ++++ day11/TEST.txt | 10 + day11/ex1-c.c | 187 +++ day11/ex1-slow.bash | 175 +++ day11/ex1.bash | 103 ++ day11/ex2-c.c | 219 ++++ day11/ex2.bash | 184 +++ day12/INPUT.txt | 769 ++++++++++++ day12/Makefile | 29 + day12/OUTPUT | 16 + day12/README | 76 ++ day12/TEST.txt | 5 + day12/ex1-c.c | 61 + day12/ex1.bash | 53 + day12/ex2-c.c | 58 + day12/ex2.bash | 48 + templates/Makefile | 26 + templates/ex1-c.c | 38 + templates/ex1.bash | 21 + templates/ex2-c.c | 60 + templates/ex2.bash | 34 + 131 files changed, 16060 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 OUTPUT create mode 100644 README create mode 100644 day01/INPUT.txt create mode 100644 day01/Makefile create mode 100644 day01/OUTPUT create mode 100644 day01/README create mode 100644 day01/TEST.txt create mode 100644 day01/ex1-22.cob create mode 100644 day01/ex1-23.cob create mode 100644 day01/ex1-c.c create mode 100644 day01/ex1-cob.cob create mode 100755 day01/ex1.bash create mode 100644 day01/ex2-c.c create mode 100644 day01/ex2-pure-sort-cob.cob create mode 100755 day01/ex2-pure-sort.bash create mode 100644 day01/ex2-sort-cob.cob create mode 100755 day01/ex2-sort.bash create mode 100755 day01/ex2.bash create mode 100644 day02/INPUT.txt create mode 100644 day02/Makefile create mode 100644 day02/OUTPUT create mode 100644 day02/README create mode 100644 day02/ex1-c.c create mode 100755 day02/ex1.bash create mode 100644 day02/ex2-c.c create mode 100755 day02/ex2.bash create mode 100644 day03/INPUT.txt create mode 100644 day03/Makefile create mode 100644 day03/OUTPUT create mode 100644 day03/README create mode 100644 day03/TEST.txt create mode 100644 day03/ex1-c.c create mode 100755 day03/ex1.bash create mode 100644 day03/ex2-c.c create mode 100755 day03/ex2.bash create mode 100644 day04/INPUT.txt create mode 100644 day04/Makefile create mode 100644 day04/OUTPUT create mode 100644 day04/README create mode 100644 day04/TEST.txt create mode 100644 day04/ex1-c.c create mode 100755 day04/ex1.bash create mode 100644 day04/ex2-c.c create mode 100755 day04/ex2.bash create mode 100644 day05/INPUT.txt create mode 100644 day05/Makefile create mode 100644 day05/OUTPUT create mode 100644 day05/README create mode 100644 day05/TEST.txt create mode 100644 day05/ex1-c.c create mode 100755 day05/ex1.bash create mode 100644 day05/ex2-c.c create mode 100755 day05/ex2.bash create mode 100644 day06/INPUT.txt create mode 100644 day06/Makefile create mode 100644 day06/OUTPUT create mode 100644 day06/README create mode 100644 day06/TEST.txt create mode 100755 day06/bash.sh create mode 100755 day06/c.sh create mode 100755 day06/ex1-bis.bash create mode 100644 day06/ex1-c.c create mode 100755 day06/ex1.bash create mode 100755 day06/ex2-bis.bash create mode 100644 day06/ex2-c.c create mode 100755 day06/ex2.bash create mode 100644 day07/INPUT.txt create mode 100644 day07/Makefile create mode 100644 day07/OUTPUT create mode 100644 day07/README create mode 100644 day07/TEST.txt create mode 100644 day07/TEST2.txt create mode 100755 day07/ex1-bis.bash create mode 100644 day07/ex1-c.c create mode 100755 day07/ex1.bash create mode 100644 day07/ex2-c.c create mode 100755 day07/ex2.bash create mode 100644 day08/INPUT.txt create mode 100644 day08/Makefile create mode 100644 day08/OUTPUT create mode 100644 day08/README create mode 100644 day08/TEST.txt create mode 100644 day08/ex1-c.c create mode 100755 day08/ex1.bash create mode 100644 day08/ex2-c.c create mode 100755 day08/ex2.bash create mode 100644 day09/INPUT.txt create mode 100644 day09/Makefile create mode 100644 day09/OUTPUT create mode 100644 day09/README create mode 100644 day09/TEST.txt create mode 100644 day09/ex1-c.c create mode 100755 day09/ex1.bash create mode 100644 day09/ex2-c.c create mode 100755 day09/ex2.bash create mode 100644 day10/INPUT.txt create mode 100644 day10/Makefile create mode 100644 day10/OUTPUT create mode 100644 day10/README create mode 100644 day10/TEST.txt create mode 100644 day10/TEST2.txt create mode 100644 day10/ex1-c.c create mode 100755 day10/ex1.bash create mode 100644 day10/ex2-c.c create mode 100755 day10/ex2.bash create mode 100644 day11/INPUT.txt create mode 100644 day11/Makefile create mode 100644 day11/OUTPUT create mode 100644 day11/README create mode 100644 day11/TEST.txt create mode 100644 day11/ex1-c.c create mode 100755 day11/ex1-slow.bash create mode 100755 day11/ex1.bash create mode 100644 day11/ex2-c.c create mode 100755 day11/ex2.bash create mode 100644 day12/INPUT.txt create mode 100644 day12/Makefile create mode 100644 day12/OUTPUT create mode 100644 day12/README create mode 100644 day12/TEST.txt create mode 100644 day12/ex1-c.c create mode 100755 day12/ex1.bash create mode 100644 day12/ex2-c.c create mode 100755 day12/ex2.bash create mode 100644 templates/Makefile create mode 100644 templates/ex1-c.c create mode 100755 templates/ex1.bash create mode 100644 templates/ex2-c.c create mode 100755 templates/ex2.bash diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9af0d5e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +ex*-c +core +ex*-cob +day13 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6a2a384 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +SUBDIRS := $(shell echo day??) +INSTALLDIR := ~/dev/www/tk.bodiccea/advent-2020 +DEST := arwen:www/tk.bodiccea/advent-2020 + +EXCLUDE := --exclude 'cob-01/' + +.PHONY: clean deploy $(SUBDIRS) + +clean: + for dir in $(SUBDIRS) ; do \ + make -C $$dir clean ; \ + done + +deploy: clean + rsync -aHixv $(EXCLUDE) --delete --delete-excluded ./ $(INSTALLDIR) + rsync -aHixv $(EXCLUDE) ./ $(DEST) --delete --delete-excluded + +all: $(SUBDIRS) + +$(SUBDIRS): + @echo "=========================================" + @echo "================= $@ =================" + @echo "=========================================" + @echo + @echo "+++++++++++++++++ ex1" + @$(MAKE) --no-print-directory -C $@ ex1 2>&1 + @echo "+++++++++++++++++ ex2" + @$(MAKE) --no-print-directory -C $@ ex2 2>&1 + +output: + @$(MAKE) --no-print-directory all >OUTPUT 2>&1 diff --git a/OUTPUT b/OUTPUT new file mode 100644 index 0000000..5880eec --- /dev/null +++ b/OUTPUT @@ -0,0 +1,344 @@ +========================================= +================= day01 ================= +========================================= + ++++++++++++++++++ ex1 +ex1.bash : 33:1701 180:319 sum=2020 mul=542619 + time: 0:00.06 real, 0.06 user, 0.00 sys + context-switch: 8+4, page-faults: 0+303 + +ex1-cob : res= 542619 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+171 + +ex1-c : 33:1701 180:319 sum=2020 mul=542619 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 2+1, page-faults: 0+75 + ++++++++++++++++++ ex2 +ex2.bash : 80:1450 94:43 185:527 sum=2020 mul=32858450 + time: 0:10.10 real, 9.98 user, 0.03 sys + context-switch: 1845+4, page-faults: 0+305 + +ex2-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 1+7, page-faults: 0+476 + +ex2-pure-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450 + time: 0:00.61 real, 0.60 user, 0.00 sys + context-switch: 136+4, page-faults: 0+309 + +ex2-sort-cob : res= 32858450 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 5+1, page-faults: 0+173 + +ex2-pure-sort-cob : res= 32858450 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 2+1, page-faults: 0+173 + +ex2-c : 80:1450 94:43 185:527 sum=2020 mul=32858450 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+74 + +========================================= +================= day02 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : lines: 1000 matched:607 + time: 0:00.07 real, 0.03 user, 0.00 sys + context-switch: 5+3, page-faults: 0+154 + +ex1-c : lines: 1000 matched:607 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+75 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : lines: 1000 matched:321 + time: 0:00.03 real, 0.03 user, 0.00 sys + context-switch: 10+2, page-faults: 0+152 + +ex2-c : lines: 1000 matched:321 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 2+1, page-faults: 0+74 + +========================================= +================= day03 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : lines:322 pos=966 found:169 + time: 0:00.03 real, 0.01 user, 0.00 sys + context-switch: 2+3, page-faults: 0+152 + +ex1-c : lines:322 pos:966 found:169 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : lines=323 res=6847128288 + time: 0:00.05 real, 0.04 user, 0.00 sys + context-switch: 6+2, page-faults: 0+156 + +ex2-c : lines=323 res=6847128288 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+74 + +========================================= +================= day04 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : valid=182/251 + time: 0:00.08 real, 0.07 user, 0.00 sys + context-switch: 6+3, page-faults: 0+160 + +ex1-c : valid=182/251 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+74 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : valid=109/251 + time: 0:00.20 real, 0.16 user, 0.00 sys + context-switch: 52+2, page-faults: 0+193 + +ex2-c : valid=109/251 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+74 + +========================================= +================= day05 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : lines=743 max=838 + time: 0:01.04 real, 0.70 user, 0.39 sys + context-switch: 72+4435, page-faults: 0+150958 + +ex1-c : lines=743 max=838 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+72 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : lines=743 seat=714 + time: 0:01.19 real, 0.77 user, 0.50 sys + context-switch: 80+4426, page-faults: 0+155986 + +ex2-c : lines=743 seat=714 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+75 + +========================================= +================= day06 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +cc -w ex2-c.c -o ex2-c +ex1.bash : groups=484 count=6585 + time: 0:01.44 real, 1.25 user, 0.22 sys + context-switch: 132+1392, page-faults: 0+58723 + +ex1-bis.bash : groups=484 count=6585 + time: 0:01.26 real, 1.10 user, 0.17 sys + context-switch: 110+1330, page-faults: 0+59995 + +ex1-c : groups=484 count=6585 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + ++++++++++++++++++ ex2 +ex2.bash : groups=484 count=3276 + time: 0:01.47 real, 1.27 user, 0.21 sys + context-switch: 139+1399, page-faults: 0+60071 + +ex2-bis.bash : groups=484 count=3276 + time: 0:01.39 real, 1.16 user, 0.21 sys + context-switch: 139+1354, page-faults: 0+58513 + +ex2-c : groups=484 count=3276 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + +========================================= +================= day07 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : target=shinygold nkeys=594 res=287 + time: 0:07.19 real, 7.09 user, 0.01 sys + context-switch: 1291+3, page-faults: 0+408 + +ex1-bis.bash : target=shinygold res=287 + time: 0:00.12 real, 0.11 user, 0.00 sys + context-switch: 36+2, page-faults: 0+212 + +ex1-c : target=shinygold nkeys=594 res=287 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+118 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : target=shinygold res=48160 + time: 0:00.34 real, 0.28 user, 0.06 sys + context-switch: 69+251, page-faults: 0+12038 + +ex2-c : target=shinygold nkeys=594 res=48160 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+118 + +========================================= +================= day08 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : res=1594 + time: 0:00.06 real, 0.02 user, 0.00 sys + context-switch: 3+6, page-faults: 0+301 + +ex1-c : res=1594 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+79 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : res:758 + time: 0:02.27 real, 2.02 user, 0.26 sys + context-switch: 276+1097, page-faults: 0+43723 + +ex2-c : res=758 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+81 + +========================================= +================= day09 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : res=167829540 + time: 0:00.41 real, 0.38 user, 0.00 sys + context-switch: 41+3, page-faults: 0+176 + +ex1-c : res=167829540 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+76 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : res=167829540 sum=28045630 + time: 0:02.95 real, 2.70 user, 0.25 sys + context-switch: 227+937, page-faults: 0+49654 + +ex2-c : res=167829540 sum=28045630 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+76 + +========================================= +================= day10 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w ex1-c.c -o ex1-c +ex1.bash : diff1=71 diff2=27 res=1917 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 0+6, page-faults: 0+331 + +ex1-c : diff1=71 diff2=27 res=1917 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+74 + ++++++++++++++++++ ex2 +cc -w ex2-c.c -o ex2-c +ex2.bash : size=99 res=113387824750592 + time: 0:00.03 real, 0.01 user, 0.00 sys + context-switch: 5+5, page-faults: 0+334 + +ex2-c : size=99 res=113387824750592 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+75 + +========================================= +================= day11 ================= +========================================= + ++++++++++++++++++ ex1 +ex1.bash : res=2386 + time: 0:41.70 real, 41.39 user, 0.07 sys + context-switch: 6321+15, page-faults: 0+1315 + +ex1-c : res=2386 + time: 0:00.05 real, 0.00 user, 0.00 sys + context-switch: 1+2, page-faults: 0+92 + ++++++++++++++++++ ex2 +cc -w -O3 ex2-c.c -o ex2-c +ex2.bash : res=2091 + time: 1:19.94 real, 79.78 user, 0.05 sys + context-switch: 8678+11, page-faults: 0+1715 + +ex2-c : res=2091 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 3+1, page-faults: 0+91 + +========================================= +================= day12 ================= +========================================= + ++++++++++++++++++ ex1 +cc -w -O3 ex1-c.c -o ex1-c +ex1.bash : res=1457 + time: 0:00.07 real, 0.01 user, 0.01 sys + context-switch: 3+3, page-faults: 0+153 + +ex1-c : res=1457 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + ++++++++++++++++++ ex2 +cc -w -O3 ex2-c.c -o ex2-c +ex2.bash : res=106860 + time: 0:00.10 real, 0.09 user, 0.00 sys + context-switch: 11+2, page-faults: 0+154 + +ex2-c : res=106860 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+74 + +========================================= +================= day13 ================= +========================================= + ++++++++++++++++++ ex1 +ex1.bash : res=410 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 1+3, page-faults: 0+153 + +ex1-c : res=0 + time: 0:00.02 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+73 + ++++++++++++++++++ ex2 +i=45 - b=23 +i=56 - b=19 +i=54 - b=17 +i=0 - b=37 +./ex2.bash: line 43: (N+k) % i: division by 0 (error token is "i") +ex2.bash : N=14145 INV=-6716 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 0+2, page-faults: 0+159 + +ex2-c : res=0 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 1+2, page-faults: 0+75 + diff --git a/README b/README new file mode 100644 index 0000000..935cea8 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +advent of code 2020: +https://adventofcode.com/2020 + +Here are solutions to the 2 daily exercises in both C and bash. diff --git a/day01/INPUT.txt b/day01/INPUT.txt new file mode 100644 index 0000000..5bc2635 --- /dev/null +++ b/day01/INPUT.txt @@ -0,0 +1,200 @@ +1899 +1358 +1824 +1541 +1801 +1950 +1441 +1848 +1259 +1715 +1222 +1097 +1127 +1531 +1330 +1841 +1662 +1075 +1631 +1844 +1209 +1940 +2006 +1085 +1615 +1132 +1345 +1736 +1885 +1919 +1995 +1893 +1464 +1701 +1373 +1390 +1717 +1238 +1707 +1088 +1566 +1971 +1804 +1630 +1920 +1445 +1948 +1123 +1917 +1944 +1448 +1965 +1118 +1986 +1498 +1847 +1730 +1418 +1771 +1352 +1207 +1276 +1716 +1502 +1922 +1473 +1528 +1038 +1228 +1983 +1746 +1695 +1698 +1910 +1283 +1145 +1967 +1304 +1651 +1898 +1450 +1042 +1051 +1619 +1505 +1643 +1136 +1517 +1907 +1354 +1960 +1733 +1769 +1942 +43 +1743 +1981 +1711 +1141 +1169 +1239 +1032 +1148 +1045 +1768 +1173 +1389 +2007 +1614 +1028 +1729 +1083 +1165 +1461 +1850 +1913 +1958 +1121 +1613 +1341 +1055 +1510 +1054 +1064 +1875 +1429 +1799 +1570 +1293 +1702 +1313 +1576 +1050 +1340 +1117 +1342 +1914 +1773 +1281 +1176 +1371 +1520 +1131 +1438 +1741 +1583 +1840 +1412 +1792 +1656 +1628 +1089 +1124 +1291 +1476 +1367 +1467 +1529 +1925 +1555 +1518 +1496 +1745 +1533 +1557 +1861 +1091 +1364 +1894 +1760 +1622 +1149 +1776 +1547 +1329 +1988 +1697 +989 +1604 +1296 +319 +1459 +1860 +1916 +1838 +527 +1370 +1881 +1213 +2003 +1223 +1095 +1560 +1784 +1157 +1573 +1023 +1758 +1857 +1723 diff --git a/day01/Makefile b/day01/Makefile new file mode 100644 index 0000000..89b1a9f --- /dev/null +++ b/day01/Makefile @@ -0,0 +1,40 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +COBFLAGS=-fmfcomment -free -x +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 deploy ex1 ex2 output + +all: ex1 ex2 + +compile: ex1-c ex2-c ex2-pure-sort-cob ex2-sort-cob + +ex1: ex1-c ex1-cob + @$(TIME) ex1.bash < $(INPUT) + @echo -n "ex1-cob : res=" + @$(TIME) ex1-cob < $(INPUT) + @$(TIME) ex1-c < $(INPUT) + +ex2: ex2-c ex2-sort-cob ex2-pure-sort-cob + @$(TIME) ex2.bash < $(INPUT) + @$(TIME) ex2-sort.bash < $(INPUT) + @$(TIME) ex2-pure-sort.bash < $(INPUT) + @echo -n "ex2-sort-cob : res=" + @$(TIME) ex2-sort-cob < $(INPUT) + @echo -n "ex2-pure-sort-cob : res=" + @$(TIME) ex2-pure-sort-cob < $(INPUT) + @$(TIME) ex2-c < $(INPUT) + +clean: + @rm -f ex1-c ex2-c core ex2-galoisgirl-cob ex2-cob + +output: + $(MAKE) --no-print-directory all >OUTPUT 2>&1 + +deploy: + $(MAKE) -C .. deploy + +%: %.cob + cobc $(COBFLAGS) $^ diff --git a/day01/OUTPUT b/day01/OUTPUT new file mode 100644 index 0000000..cd6bae1 --- /dev/null +++ b/day01/OUTPUT @@ -0,0 +1,36 @@ +ex1.bash : 33:1701 180:319 sum=2020 mul=542619 + time: 0:00.06 real, 0.06 user, 0.00 sys + context-switch: 20+4, page-faults: 0+305 + +ex1-cob : res= 542619 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+172 + +ex1-c : 33:1701 180:319 sum=2020 mul=542619 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + +ex2.bash : 80:1450 94:43 185:527 sum=2020 mul=32858450 + time: 0:10.00 real, 10.00 user, 0.00 sys + context-switch: 1005+3, page-faults: 0+302 + +ex2-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 2+7, page-faults: 0+479 + +ex2-pure-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450 + time: 0:00.50 real, 0.50 user, 0.00 sys + context-switch: 62+4, page-faults: 0+311 + +ex2-sort-cob : res= 32858450 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 1+1, page-faults: 0+170 + +ex2-pure-sort-cob : res= 32858450 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 1+1, page-faults: 0+173 + +ex2-c : 80:1450 94:43 185:527 sum=2020 mul=32858450 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+75 + diff --git a/day01/README b/day01/README new file mode 100644 index 0000000..c4c9225 --- /dev/null +++ b/day01/README @@ -0,0 +1,45 @@ +--- Day 1: Report Repair --- + +After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you. + +The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room. + +To save your vacation, you need to get all fifty stars by December 25th. + +Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! + +Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up. + +Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. + +For example, suppose your expense report contained the following: + +1721 +979 +366 +299 +675 +1456 + +In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579. + +Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together? + +Your puzzle answer was 542619. +--- Part Two --- + +The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria. + +Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950. + +In your expense report, what is the product of the three entries that sum to 2020? + +Your puzzle answer was 32858450. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day01/TEST.txt b/day01/TEST.txt new file mode 100644 index 0000000..e3fb011 --- /dev/null +++ b/day01/TEST.txt @@ -0,0 +1,6 @@ +1721 +979 +366 +299 +675 +1456 diff --git a/day01/ex1-22.cob b/day01/ex1-22.cob new file mode 100644 index 0000000..ff7d5f1 --- /dev/null +++ b/day01/ex1-22.cob @@ -0,0 +1,99 @@ + IDENTIFICATION DIVISION. + PROGRAM-ID. AoC-2020-day-1-part-2. +*> Inspired by https://github.com/GaloisGirl/Coding +*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob +*> Utilisation: ./ex2-cob < INPUT.txt + AUTHOR. Bruno Raoult. + + ENVIRONMENT DIVISION. + INPUT-OUTPUT SECTION. + FILE-CONTROL. + SELECT INPUTFILE ASSIGN TO KEYBOARD + ORGANIZATION IS LINE SEQUENTIAL. + + DATA DIVISION. + FILE SECTION. + FD INPUTFILE. + 01 INPUTRECORD PIC 9(4). + + WORKING-STORAGE SECTION. + 01 FILE-STATUS PIC 9 VALUE 0. + 01 LEN PIC 9(3) VALUE 0. + 01 ARRAY. + 02 ARR OCCURS 0 TO 1024 DEPENDING ON LEN. + 05 VAL PIC 9(5). + 01 S PIC 9(4). + 01 P PIC 9(12). + 01 FMT PIC Z(12)9. + + LOCAL-STORAGE SECTION. + 01 I USAGE UNSIGNED-INT VALUE 1. + 01 J USAGE UNSIGNED-INT VALUE 1. + 01 K USAGE UNSIGNED-INT VALUE 1. + 01 TMP USAGE UNSIGNED-INT VALUE 1. + + PROCEDURE DIVISION. + 01-MAIN. + OPEN INPUT INPUTFILE. + PERFORM 02-READ UNTIL FILE-STATUS = 1. + CLOSE INPUTFILE. +*> PERFORM 06-PRINT. + MOVE 1 TO K +*> PERFORM 05-BUBBLE + SORT ARR ASCENDING KEY VAL +*> DISPLAY "=========================". +*> PERFORM 06-PRINT. + PERFORM 04-LOOP + STOP RUN. + 02-READ. + READ INPUTFILE + AT END MOVE 1 TO FILE-STATUS + NOT AT END PERFORM 03-WRITE-TO-TABLE + END-READ. + + 03-WRITE-TO-TABLE. + ADD 1 TO LEN. + UNSTRING INPUTRECORD INTO VAL(LEN). +*> Wrong: "234" becomes 2340 instead of 0234 +*> COMPUTE VAL(LEN) = INPUTRECORD. +*> DISPLAY VAL(LEN) " " LEN. + 04-LOOP. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + ADD I 1 GIVING TMP + PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN + ADD J 1 GIVING TMP + PERFORM VARYING K FROM TMP BY 1 UNTIL K > LEN + ADD VAL(I) VAL(J) VAL(K) GIVING S +*> DISPLAY I J K + IF S = 2020 THEN + MULTIPLY VAL(I) BY VAL(J) GIVING P + MULTIPLY P BY VAL(K) GIVING P + MOVE P TO FMT + DISPLAY FMT + EXIT PERFORM + ELSE + IF S > 2020 THEN + EXIT PERFORM + END-IF + END-IF + END-PERFORM + END-PERFORM + END-PERFORM. + 05-BUBBLE. + MOVE 1 TO K + PERFORM UNTIL K = 0 + MOVE 0 TO K + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN - 1 + ADD 1 I GIVING J + IF VAL(I) > VAL(J) + MOVE VAL(I) TO TMP + MOVE VAL(J) TO VAL(I) + MOVE TMP TO VAL(J) + MOVE 1 TO K + END-IF + END-PERFORM + END-PERFORM. + 06-PRINT. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + DISPLAY VAL(I) + END-PERFORM. diff --git a/day01/ex1-23.cob b/day01/ex1-23.cob new file mode 100644 index 0000000..dee700f --- /dev/null +++ b/day01/ex1-23.cob @@ -0,0 +1,96 @@ + IDENTIFICATION DIVISION. + PROGRAM-ID. AoC-2020-day-1-part-2. +*> Inspired by https://github.com/GaloisGirl/Coding +*> Compilation: obc -F -fmfcomment -std=rm -x ex2-cob.cob + AUTHOR. Bruno Raoult. + + ENVIRONMENT DIVISION. + INPUT-OUTPUT SECTION. + FILE-CONTROL. + SELECT INPUTFILE ASSIGN TO KEYBOARD + ORGANIZATION IS LINE SEQUENTIAL. + + DATA DIVISION. + FILE SECTION. + FD INPUTFILE. + 01 INPUTRECORD PIC 9(4). + + WORKING-STORAGE SECTION. + 01 FILE-STATUS PIC 9 VALUE 0. + 01 LEN PIC 9(3) VALUE 0. + 01 ARR. + 05 VAL PIC 9(5) OCCURS 1024. + 01 S PIC 9(4). + 01 P PIC 9(12). + 01 FMT PIC Z(12)9. + + LOCAL-STORAGE SECTION. + 01 I USAGE UNSIGNED-INT VALUE 1. + 01 J USAGE UNSIGNED-INT VALUE 1. + 01 K USAGE UNSIGNED-INT VALUE 1. + 01 TMP USAGE UNSIGNED-INT VALUE 1. + + PROCEDURE DIVISION. + 01-MAIN. + OPEN INPUT INPUTFILE. + PERFORM 02-READ UNTIL FILE-STATUS = 1. + CLOSE INPUTFILE. +*> PERFORM 06-PRINT. + MOVE 1 TO K + PERFORM 05-BUBBLE +*> DISPLAY "=========================". +*> PERFORM 06-PRINT. + PERFORM 04-LOOP. + STOP RUN. + 02-READ. + READ INPUTFILE + AT END MOVE 1 TO FILE-STATUS + NOT AT END PERFORM 03-WRITE-TO-TABLE + END-READ. + + 03-WRITE-TO-TABLE. + ADD 1 TO LEN. + MOVE INPUTRECORD TO FMT + MOVE FMT TO VAL(LEN) +*> UNSTRING INPUTRECORD INTO VAL(LEN). + DISPLAY VAL(LEN) " " LEN. + 04-LOOP. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + ADD I 1 GIVING TMP + PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN + ADD J 1 GIVING TMP + PERFORM VARYING K FROM TMP BY 1 UNTIL K > LEN + ADD VAL(I) VAL(J) VAL(K) GIVING S +*> DISPLAY I J K + IF S = 2020 THEN + MULTIPLY VAL(I) BY VAL(J) GIVING P + MULTIPLY P BY VAL(K) GIVING P + MOVE P TO FMT + DISPLAY FMT + EXIT PERFORM + ELSE + IF S > 2020 THEN + EXIT PERFORM + END-IF + END-IF + END-PERFORM + END-PERFORM + END-PERFORM. + 05-BUBBLE. + MOVE 1 TO K + PERFORM UNTIL K = 0 + MOVE 0 TO K + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN - 1 + ADD 1 I GIVING J + IF VAL(I) > VAL(J) + MOVE VAL(I) TO TMP + MOVE VAL(J) TO VAL(I) + MOVE TMP TO VAL(J) + MOVE 1 TO K + END-IF + END-PERFORM + END-PERFORM. + 06-PRINT. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + DISPLAY VAL(I) + END-PERFORM. diff --git a/day01/ex1-c.c b/day01/ex1-c.c new file mode 100644 index 0000000..75887a6 --- /dev/null +++ b/day01/ex1-c.c @@ -0,0 +1,29 @@ +/* ex1-c:c: Advent2020 game, day 1/game 1 + */ + +#include +#include + +int main(ac, av) +char **av; +{ + int numbers[1000]; /* should do differently : + * either read twice, either create list + */ + int n=0, i, j, n1, n2; + + while (scanf("%d", numbers+n) != EOF) + n++; + + for (i=0; i Inspired by https://github.com/GaloisGirl/Coding +*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob +*> Utilisation: ./ex2-cob < INPUT.txt + AUTHOR. Bruno Raoult. + + ENVIRONMENT DIVISION. + INPUT-OUTPUT SECTION. + FILE-CONTROL. + SELECT INPUTFILE ASSIGN TO KEYBOARD + ORGANIZATION IS LINE SEQUENTIAL. + + DATA DIVISION. + FILE SECTION. + FD INPUTFILE. + 01 INPUTRECORD PIC 9(4). + + WORKING-STORAGE SECTION. + 01 FILE-STATUS PIC 9 VALUE 0. + 01 LEN PIC 9(3) VALUE 0. + 01 ARRAY. + 02 ARR OCCURS 0 TO 1024 DEPENDING ON LEN. + 05 VAL PIC 9(5). + 01 S PIC 9(4). + 01 P PIC 9(12). + 01 FMT PIC Z(12)9. + + LOCAL-STORAGE SECTION. + 01 I USAGE UNSIGNED-INT VALUE 1. + 01 J USAGE UNSIGNED-INT VALUE 1. + 01 K USAGE UNSIGNED-INT VALUE 1. + 01 TMP USAGE UNSIGNED-INT VALUE 1. + + PROCEDURE DIVISION. + 01-MAIN. + OPEN INPUT INPUTFILE. + PERFORM 02-READ UNTIL FILE-STATUS = 1. + CLOSE INPUTFILE. +*> PERFORM 06-PRINT. + SORT ARR ASCENDING KEY VAL +*> DISPLAY "=========================". +*> PERFORM 06-PRINT. + PERFORM 04-LOOP + STOP RUN. + 02-READ. + READ INPUTFILE + AT END MOVE 1 TO FILE-STATUS + NOT AT END PERFORM 03-WRITE-TO-TABLE + END-READ. + + 03-WRITE-TO-TABLE. + ADD 1 TO LEN. + UNSTRING INPUTRECORD INTO VAL(LEN). +*> Wrong: "234" becomes 2340 instead of 0234 +*> COMPUTE VAL(LEN) = INPUTRECORD. +*> DISPLAY VAL(LEN) " " LEN. + 04-LOOP. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + ADD I 1 GIVING TMP + PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN + ADD VAL(I) VAL(J) GIVING S +*> DISPLAY I J K + IF S = 2020 THEN + MULTIPLY VAL(I) BY VAL(J) GIVING P + MOVE P TO FMT + DISPLAY FMT + EXIT PERFORM + ELSE + IF S > 2020 THEN + EXIT PERFORM + END-IF + END-IF + END-PERFORM + END-PERFORM. + 06-PRINT. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + DISPLAY VAL(I) + END-PERFORM. diff --git a/day01/ex1.bash b/day01/ex1.bash new file mode 100755 index 0000000..0594dc5 --- /dev/null +++ b/day01/ex1.bash @@ -0,0 +1,23 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 1/game 1 + +CMD=$(basename "$0") + +readarray -t numbers + +declare -i i j a b + +for ((i=0; i<${#numbers[@]}; ++i)); do + a=$((numbers[i])) + for ((j=i+1; j<${#numbers[@]}; ++j)); do + b=$((numbers[j])) + if ((a+b == 2020)); then + printf "${CMD} : %d:%d %d:%d sum=%d mul=%d\n" \ + $i $a $j $b \ + $((a+b)) $((a*b)) + break 2 + fi + done +done +exit 0 diff --git a/day01/ex2-c.c b/day01/ex2-c.c new file mode 100644 index 0000000..b039b09 --- /dev/null +++ b/day01/ex2-c.c @@ -0,0 +1,31 @@ +/* ex2-c:c: Advent2020 game, day 1/game 2 + */ + +#include +#include + +int main(ac, av) +char **av; +{ + int numbers[1000]; /* should do differently : + * either read twice, either create list + */ + int n=0, i, j, k, n1, n2, n3; + + while (scanf("%d", numbers+n) != EOF) + n++; + for (i=0; i Inspired by https://github.com/GaloisGirl/Coding +*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob +*> Utilisation: ./ex2-cob < INPUT.txt + AUTHOR. Bruno Raoult. + + ENVIRONMENT DIVISION. + INPUT-OUTPUT SECTION. + FILE-CONTROL. + SELECT INPUTFILE ASSIGN TO KEYBOARD + ORGANIZATION IS LINE SEQUENTIAL. + + DATA DIVISION. + FILE SECTION. + FD INPUTFILE. + 01 INPUTRECORD PIC 9(4). + + WORKING-STORAGE SECTION. + 01 FILE-STATUS PIC 9 VALUE 0. + 01 LEN PIC 9(3) VALUE 0. + 01 ARR. + 05 VAL PIC 9(5) OCCURS 1024. + 01 S PIC 9(4). + 01 P PIC 9(12). + 01 FMT PIC Z(12)9. + + LOCAL-STORAGE SECTION. + 01 I USAGE UNSIGNED-INT VALUE 1. + 01 J USAGE UNSIGNED-INT VALUE 1. + 01 K USAGE UNSIGNED-INT VALUE 1. + 01 TMP USAGE UNSIGNED-INT VALUE 1. + + PROCEDURE DIVISION. + 01-MAIN. + OPEN INPUT INPUTFILE. + PERFORM 02-READ UNTIL FILE-STATUS = 1. + CLOSE INPUTFILE. +*> PERFORM 06-PRINT. + PERFORM 05-BUBBLE +*> DISPLAY "=========================". +*> PERFORM 06-PRINT. + PERFORM 04-LOOP + STOP RUN. + 02-READ. + READ INPUTFILE + AT END MOVE 1 TO FILE-STATUS + NOT AT END PERFORM 03-WRITE-TO-TABLE + END-READ. + + 03-WRITE-TO-TABLE. + ADD 1 TO LEN. + UNSTRING INPUTRECORD INTO VAL(LEN). +*> Wrong: "234" becomes 2340 instead of 0234 +*> COMPUTE VAL(LEN) = INPUTRECORD. +*> DISPLAY VAL(LEN) " " LEN. + 04-LOOP. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + ADD I 1 GIVING TMP + PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN + ADD J 1 GIVING TMP + PERFORM VARYING K FROM TMP BY 1 UNTIL K > LEN + ADD VAL(I) VAL(J) VAL(K) GIVING S +*> DISPLAY I J K + IF S = 2020 THEN + MULTIPLY VAL(I) BY VAL(J) GIVING P + MULTIPLY P BY VAL(K) GIVING P + MOVE P TO FMT + DISPLAY FMT + EXIT PERFORM + ELSE + IF S > 2020 THEN + EXIT PERFORM + END-IF + END-IF + END-PERFORM + END-PERFORM + END-PERFORM. + 05-BUBBLE. + MOVE 1 TO K + PERFORM UNTIL K = 0 + MOVE 0 TO K + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN - 1 + ADD 1 I GIVING J + IF VAL(I) > VAL(J) + MOVE VAL(I) TO TMP + MOVE VAL(J) TO VAL(I) + MOVE TMP TO VAL(J) + MOVE 1 TO K + END-IF + END-PERFORM + END-PERFORM. + 06-PRINT. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + DISPLAY VAL(I) + END-PERFORM. diff --git a/day01/ex2-pure-sort.bash b/day01/ex2-pure-sort.bash new file mode 100755 index 0000000..05f2ec8 --- /dev/null +++ b/day01/ex2-pure-sort.bash @@ -0,0 +1,55 @@ +#!/bin/bash +# +# ex2-pure-sort.bash: Advent2020 game, day 1/game 2, bash bubble sort. + +CMD=${0##*/} + +readarray -t numbers + +# simple bubble sort for numeric array (ascending) +# args: 1: the array +# todo: add a parameter for asc/desc order. +sort_n() { + local -a array=( "$@" ) + local -i max=$(( ${#array[@]} - 1 )) + + for (( max= $(( ${#array[@]} - 1 )); max > 0; max-- )); do + local -i i + for (( i=0; i val2 )); then + local tmp=$val1 + array[$i]=$val2 + array[$((i + 1))]=$tmp + fi + done + done + echo "${array[@]}" +} + +sorted=( $(sort_n "${numbers[@]}") ) + +declare -i i j k a b c + +for ((i=0; i<${#sorted[@]}; ++i)); do + a=$((sorted[i])) + for ((j=i+1; j<${#sorted[@]}; ++j)); do + b=$((sorted[j])) + ((a+b > 2020)) && break + for ((k=j+1; k<${#sorted[@]}; ++k)); do + c=$((sorted[k])) + ((a+b+c > 2020)) && break + if ((a+b+c == 2020)); then + printf "${CMD} : %d:%d %d:%d %d:%d sum=%d mul=%d\n" \ + $i $a $j $b $k $c \ + $((a+b+c)) \ + $((a*b*c)) + break 3 + fi + done + done +done +exit 0 diff --git a/day01/ex2-sort-cob.cob b/day01/ex2-sort-cob.cob new file mode 100644 index 0000000..ff7d5f1 --- /dev/null +++ b/day01/ex2-sort-cob.cob @@ -0,0 +1,99 @@ + IDENTIFICATION DIVISION. + PROGRAM-ID. AoC-2020-day-1-part-2. +*> Inspired by https://github.com/GaloisGirl/Coding +*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob +*> Utilisation: ./ex2-cob < INPUT.txt + AUTHOR. Bruno Raoult. + + ENVIRONMENT DIVISION. + INPUT-OUTPUT SECTION. + FILE-CONTROL. + SELECT INPUTFILE ASSIGN TO KEYBOARD + ORGANIZATION IS LINE SEQUENTIAL. + + DATA DIVISION. + FILE SECTION. + FD INPUTFILE. + 01 INPUTRECORD PIC 9(4). + + WORKING-STORAGE SECTION. + 01 FILE-STATUS PIC 9 VALUE 0. + 01 LEN PIC 9(3) VALUE 0. + 01 ARRAY. + 02 ARR OCCURS 0 TO 1024 DEPENDING ON LEN. + 05 VAL PIC 9(5). + 01 S PIC 9(4). + 01 P PIC 9(12). + 01 FMT PIC Z(12)9. + + LOCAL-STORAGE SECTION. + 01 I USAGE UNSIGNED-INT VALUE 1. + 01 J USAGE UNSIGNED-INT VALUE 1. + 01 K USAGE UNSIGNED-INT VALUE 1. + 01 TMP USAGE UNSIGNED-INT VALUE 1. + + PROCEDURE DIVISION. + 01-MAIN. + OPEN INPUT INPUTFILE. + PERFORM 02-READ UNTIL FILE-STATUS = 1. + CLOSE INPUTFILE. +*> PERFORM 06-PRINT. + MOVE 1 TO K +*> PERFORM 05-BUBBLE + SORT ARR ASCENDING KEY VAL +*> DISPLAY "=========================". +*> PERFORM 06-PRINT. + PERFORM 04-LOOP + STOP RUN. + 02-READ. + READ INPUTFILE + AT END MOVE 1 TO FILE-STATUS + NOT AT END PERFORM 03-WRITE-TO-TABLE + END-READ. + + 03-WRITE-TO-TABLE. + ADD 1 TO LEN. + UNSTRING INPUTRECORD INTO VAL(LEN). +*> Wrong: "234" becomes 2340 instead of 0234 +*> COMPUTE VAL(LEN) = INPUTRECORD. +*> DISPLAY VAL(LEN) " " LEN. + 04-LOOP. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + ADD I 1 GIVING TMP + PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN + ADD J 1 GIVING TMP + PERFORM VARYING K FROM TMP BY 1 UNTIL K > LEN + ADD VAL(I) VAL(J) VAL(K) GIVING S +*> DISPLAY I J K + IF S = 2020 THEN + MULTIPLY VAL(I) BY VAL(J) GIVING P + MULTIPLY P BY VAL(K) GIVING P + MOVE P TO FMT + DISPLAY FMT + EXIT PERFORM + ELSE + IF S > 2020 THEN + EXIT PERFORM + END-IF + END-IF + END-PERFORM + END-PERFORM + END-PERFORM. + 05-BUBBLE. + MOVE 1 TO K + PERFORM UNTIL K = 0 + MOVE 0 TO K + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN - 1 + ADD 1 I GIVING J + IF VAL(I) > VAL(J) + MOVE VAL(I) TO TMP + MOVE VAL(J) TO VAL(I) + MOVE TMP TO VAL(J) + MOVE 1 TO K + END-IF + END-PERFORM + END-PERFORM. + 06-PRINT. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN + DISPLAY VAL(I) + END-PERFORM. diff --git a/day01/ex2-sort.bash b/day01/ex2-sort.bash new file mode 100755 index 0000000..122deef --- /dev/null +++ b/day01/ex2-sort.bash @@ -0,0 +1,28 @@ +#!/bin/bash +# +# ex2-sort.bash: Advent2020 game, day 1/game 2, with sort. + +CMD=$(basename "$0") + +readarray -t numbers <<< "$(sort -n)" + +declare -i i j k a b c m1 m2 +for ((i=0; i<${#numbers[@]}; ++i)); do + a=$((numbers[i])) + for ((j=i+1; j<${#numbers[@]}; ++j)); do + b=$((numbers[j])) + ((a+b > 2020)) && break + for ((k=j+1; k<${#numbers[@]}; ++k)); do + c=$((numbers[k])) + ((a+b+c > 2020)) && break + if ((a+b+c == 2020)); then + printf "${CMD} : %d:%d %d:%d %d:%d sum=%d mul=%d\n" \ + $i $a $j $b $k $c \ + $((a+b+c)) \ + $((a*b*c)) + break 3 + fi + done + done +done +exit 0 diff --git a/day01/ex2.bash b/day01/ex2.bash new file mode 100755 index 0000000..c528141 --- /dev/null +++ b/day01/ex2.bash @@ -0,0 +1,24 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 1/game 2 + +CMD=$(basename "$0") + +readarray -t numbers + +declare -i i j k + +for ((i=0; i<${#numbers[@]}; ++i)); do + for ((j=i+1; j<${#numbers[@]}; ++j)); do + for ((k=j+1; k<${#numbers[@]}; ++k)); do + if ((numbers[i] + numbers[j] + numbers[k] == 2020)); then + printf "${CMD} : %d:%d %d:%d %d:%d sum=%d mul=%d\n" \ + $i $((numbers[i])) $j $((numbers[j])) $k $((numbers[k])) \ + $((numbers[i]+numbers[j]+numbers[k])) \ + $((numbers[i]*numbers[j]*numbers[k])) + break 3 + fi + done + done +done +exit 0 diff --git a/day02/INPUT.txt b/day02/INPUT.txt new file mode 100644 index 0000000..6b4b96c --- /dev/null +++ b/day02/INPUT.txt @@ -0,0 +1,1000 @@ +1-4 m: mrfmmbjxr +5-16 b: bbbbhbbbbpbxbbbcb +7-8 x: qxrxmxccxxx +9-11 k: kkkkkkktmkhk +8-12 g: sgwvdxzhkvndv +6-9 v: zvmvvmvvvd +8-19 f: ffffsplmfflffhtfrfj +5-16 p: pppppppppspppjpcp +2-3 w: wwmw +7-19 j: jjjjjjjjjjjjjjjjjjvj +5-9 q: wqzqqqqqq +14-15 g: gggggggggglggfgg +4-6 p: tppzkppdt +11-14 p: vppgpktpppppptpppqp +5-9 f: bfflffrfgf +7-9 p: ppppptbzn +1-3 l: lllvn +2-4 g: qvcdg +1-3 m: wsmdv +1-5 v: vvvvvvvv +10-14 l: lckqlgjllltlwbl +3-4 t: bsttftltjhbqbgtm +15-17 j: jjzjjjjjhjjjjjjpzjj +2-3 t: thtt +6-17 f: ffwkwzjtjktvsfmfhvsf +3-5 b: rqxbb +4-7 m: nbcmcwmmxrxqvtjfmm +1-2 v: gzvvvv +1-3 w: hkwhv +7-8 p: pppppppp +3-4 h: hhnwh +2-4 t: ttrtjtththkr +3-4 w: wwww +4-6 s: xsntgrftmpx +4-7 s: ssskssmsbs +10-15 m: bmmrbmmmmlmmmmmm +12-13 w: wqpwdmwllnjwx +5-14 n: nnnnlnnnnnnnnmnn +5-6 k: kktkfczk +5-7 r: nrdrtrvr +4-6 c: ccqchcc +2-9 l: fnldtfnbxjlvnlsnjhml +1-13 d: dlchvkccnwrcc +5-7 j: jjjjjjj +3-5 z: zzzzz +6-12 f: ffffflvmfhfx +8-10 w: wwwwwwwwww +3-4 r: rrbtr +3-11 b: bbbrbbvphbxbqk +16-17 n: nqhknnnnsnnnnnnnnnb +18-20 k: kkkkkkkkkkkkkkkkkhtj +3-15 r: ktvzqbmbrvczprfcw +9-11 q: qnqqdqqqgrrqsqq +3-5 p: pvppp +7-11 m: mfcdmmxmmmp +7-8 t: ttttktnt +5-6 b: bbpbbbv +14-16 z: zzzzzzzzzrzzblzw +11-12 j: jkjjjkjjjjjjgj +7-9 q: qqqqqqqqqq +10-11 f: cfffffffffff +4-6 c: nccccc +7-8 r: gmdlqfpwmrr +6-8 v: nvvpdnjx +8-12 x: xxlxxbbxxxxx +8-10 s: ssxssssssss +5-12 z: zhzdfzgdzzhzlz +11-12 k: qbkhvqjpqzfq +2-11 w: wwmlttwjflwdjcpclww +2-16 w: twkkmcrxmvjtwxlwsksf +6-8 s: sssfxbskvs +5-6 s: ssnsxsbs +10-19 l: ndzmdxqlnllxsbbwvsl +2-4 g: gggg +5-10 x: zxxxsxxxxxxxsxx +7-15 f: ffbrbdtzvdffktxfm +7-8 m: xmkmmmmmtmm +9-13 s: hksrdhzlsdmps +15-17 b: bbbbbbbrbbbbbbbrbb +3-5 x: xxxxxxx +2-6 t: wtltztnct +3-15 v: vvwvvvvvhvvvbwwvmdvr +12-14 p: ppphsppppppbphp +8-9 t: ttttvttttttt +2-3 c: cchvj +8-9 z: vzzzzzzgl +11-12 q: qqmqqqqqqqsnqqqqqqq +12-13 d: dddddddddddmd +8-9 t: wttttttqt +7-8 p: ppppvvcw +4-5 g: gncgj +12-13 s: sssssssssssmw +7-9 f: pswpnjftf +11-16 p: ppppnpkpkzppcpzbppp +1-12 q: ksqrqpqnqmqxqb +2-10 l: lllllllllll +17-18 z: zzzzzzzzzzzxzzzzzz +11-12 g: qggbjgggggssgggrk +3-4 s: bpss +6-8 v: vwvvvrvvv +3-4 t: tglktt +4-12 l: kvrnzqslwrdkfll +12-16 b: fbbbbbnbbvbbbbbcbbb +5-6 t: ttttzz +5-8 w: sqwhwwxw +8-9 z: zzzvztzrzzzz +3-6 l: llvdlt +7-8 r: mrcvnrrr +10-11 k: kkkkkkkkkmc +1-3 n: nnnmn +4-16 c: dqlcbclcrxkkszvcv +9-10 g: fxssmlmgbh +5-6 p: nwpfpp +3-7 w: cwhgrfshdwhwwll +14-16 l: llllllllllllllpll +3-4 f: ffff +4-9 d: dcdddxzvmrd +4-11 w: jwwnwwwwwvx +3-4 t: ttvc +4-17 r: rrsrrrrrrrrrbtrrrr +8-9 x: wqxxxcfdx +6-8 d: qgddwddtdddlc +12-13 v: vvjvvvvvzvvtvv +3-4 g: sghxg +3-4 b: ckbbmprfbbmzgqtkbw +5-6 x: xxxxxv +10-13 f: zpfffbfchxfffffjff +5-6 f: ffffwf +12-19 l: mlllllplllldllnsllql +4-5 v: vvvvp +5-8 w: vjwvghggwww +1-3 n: lnnn +16-18 p: ppppppvpppppwppppppp +15-18 z: zzzzzzzzzzzzzzjzzg +4-6 b: bbmbbb +5-7 z: wzlwzmzzzzzzzzzzzwz +8-12 p: gppmpppvppkzpcnpb +16-19 h: hhbhhhhhhhhhhhlnhhw +2-3 n: nnnn +3-10 m: mmntfkdjftmtmmbm +3-5 j: jjjjw +4-6 z: zjnzzzdzz +1-7 s: zsvsssf +6-7 v: vtvvvcg +3-12 f: qsrtmnxkvlcmt +5-11 q: mqgqqqqqrpch +5-9 g: msxkggnggg +3-8 v: vvvvhvjvdvxtr +4-6 k: jcklxdhkwhsqhq +6-7 x: vppxbhcjzxdqx +2-3 n: ntrg +7-8 g: gggwgjdhg +1-3 d: dddd +14-18 l: llllllllwlllxllllllz +8-19 g: ggggghggkhgrtzcgrrk +7-14 q: phzxvmbxxfsfwr +8-14 r: hbjmdrhnpxnwgz +15-17 d: dddddpddddldddtdgddd +6-7 k: kkkkkgkkq +2-12 w: wwzwwwwwwwwww +5-8 q: qqqqqqqzqqwqqzqcq +5-6 z: czzzrz +14-15 b: bbbrzhbbbbnxbgbb +12-13 d: dgddddddddtvddddvbz +3-5 v: vvvvv +13-16 q: qqqqqvzwqqqqqqqq +2-3 n: jpznnwfpchs +7-10 w: jwwpwkwwwpw +6-7 b: bbcnbcjxbbb +14-17 w: wqtwwwwwnwcwwbsww +3-14 w: gwwwwwwwhwwnxwwwww +4-9 w: mwllcfjfwwwjp +9-12 n: nnnnnnnhnwjnn +2-4 j: jjjjj +3-6 t: mrmttccttqt +16-17 p: ppmjpgptzgbppphfbp +12-17 v: vvvvvvvvvvvvvfvvvv +13-15 x: xxxxxxxxxxxxxxgx +2-4 d: dddddddd +2-5 t: ttmvt +13-14 j: jjjjjjjjjjjjtj +3-5 r: ggmrdf +7-8 f: ffdfflszfsfffqff +10-11 t: ttpttttttrgtt +8-12 j: jjjjqdhzjhjdj +19-20 z: zbzzzzzzzzzzzzzzzzrq +7-11 b: bbbbbhbbbxbbb +4-9 p: gzmkkbtpkzpgthklpq +2-5 b: bbbbxgb +4-13 k: kkkkkkzktckkkwkjkk +10-12 q: vrxznfqqnqgq +15-16 m: mmmmkmmmmmmmmmmmm +15-18 h: hlhhvhchthhhhtphhh +2-3 m: mftzmc +6-11 s: ksssssssssszs +5-6 q: qqqpqkq +7-8 n: nnfnnnnpn +6-8 c: chfcllrvxcnnjhtc +6-8 k: kkkkkxlk +2-4 s: tsspb +9-12 q: qqqqqqqqqqqqqq +17-20 h: hhhhhhhhhhhhhhhhhhhh +6-13 r: rrrrrgrrrrrrn +4-8 f: fffrfffz +13-14 n: nbnnnngnnnnnnpz +8-11 f: fvffsfqftcffff +3-5 w: vwhxz +8-10 x: nmxxknlptx +3-4 p: pppt +17-18 v: vvvvvvhkvvvvvnvvjlvv +10-15 z: twnsdkmgpvzfmzg +3-5 n: njnjnkghp +10-12 q: qvqqqqqqqqqq +6-7 r: rrrrrrrr +3-4 t: qltt +2-16 f: gffflcvtpfkfcjfrjvfs +3-5 s: swssp +11-13 l: lrpllllllvtslwllllld +1-3 n: nnnn +3-5 g: jjggnvg +4-5 b: bvbbb +13-15 l: llvllllllllllnll +5-6 j: hjjjjfsj +3-4 b: bkfbnb +5-8 z: zdzzzzzzzrzzz +3-4 r: rrrx +2-12 c: mczhvchkmjdrjh +12-16 x: sjxbcgdqtpfxflsxx +2-4 c: tcxf +4-5 w: kwjww +1-2 r: rrrwzc +4-7 d: ddddddkdd +5-6 q: qqqqqp +13-16 l: flljlllvnzlllllclldl +3-6 j: jjjpjj +3-4 g: gxgg +10-11 n: nnnnnnnnnnn +2-5 j: ztchj +4-6 j: pjzzrmjvhcxn +4-11 c: jchcccgccckc +11-12 d: dddddddddddd +6-7 n: nznnnnnntnnnnzpjfjnn +8-9 k: kbkkkkkkk +13-18 x: xbxxmxqxxxmxkxsxxxx +7-16 r: rrrrrrzrrrrrrrjmrrr +3-5 h: hhqhd +1-2 b: bbmb +2-3 n: rlnntn +2-8 q: qnfqnqhx +2-5 b: bjbbh +5-11 t: twtjnpttqtvtttptt +16-17 w: wtmwwhxtgwrrswwfblll +8-16 g: ggggghshggdggggcpwg +1-3 l: lsllzl +11-13 n: nnnvncnnmvnqnnnnn +6-7 s: ssqssss +10-18 t: tttnmjxttjttttttdzt +4-10 n: nkvncgtdpz +16-18 p: cphtrgffcpphfspxppgp +2-6 k: kkhgkskkm +6-20 r: fhrwtrzwrddfrndnrlgr +2-8 w: lkwnccgw +6-10 x: lxxxxxxgxwxxxrxqxx +4-9 q: tqtpgqjzdmqfq +9-12 k: kkkkkqbkkckkkjkql +5-9 r: rrrrrrrhcr +17-20 h: hhshshhhhhhhshhhbhhq +7-9 s: dngddfsss +12-13 q: qqqqqqqqqqqcq +2-6 v: vqtvvvv +8-10 c: cccfcncccccc +14-15 m: mmmmmmmmmmmmmmdm +7-14 s: ssslssfdssflvvsj +2-5 z: wcllj +2-11 n: cvnrlftcjct +8-16 k: krzkfbkkqkhnsjkjgkk +2-7 q: mvhvqnzdjw +5-8 l: lgbnlnclkllll +4-12 l: llllmllrlgllrklnlrbt +7-10 j: jjjjjjnjjcjsj +1-2 k: dgwmgsn +2-5 z: zjmxc +6-7 c: ccccccccc +14-17 k: bkkkkkkkkkkkndkkkk +2-4 x: xxjxtxbq +7-9 w: drjcfwzwwwfwwfzxww +4-5 p: pppwc +4-5 r: rrrvm +1-8 r: qrrrmrrrrr +16-18 l: llllllllldllltlklk +1-11 p: pwcpppbppppppp +5-19 w: wwwwwwwwwwwwwwwcwww +17-18 k: kkkkkwkkkkkkkkkkkxk +12-15 f: fsffffffflfqgfx +6-13 d: zddpdvddrvrdxq +1-10 f: ffffffrfkb +13-14 x: xxxxxxxxxxxxxxx +16-20 j: jjjjjjjdwjjfjjjjjjdj +8-11 r: rrrrrrrrrrrr +15-16 x: dxjxxxdxxxxxxxxxwxx +3-9 m: mjhqdgkmzmsmtdmhfn +2-7 k: kkkhwgtxlkmkqkk +11-15 m: mmgmmmmnsjmmlmmm +1-2 v: wvvk +8-14 s: zssssssssssssts +3-4 z: ztbzzr +4-10 x: xxkmqxxxxx +3-5 p: pkppppppppzpp +3-15 q: qpqmkqfqqlqqdfqtkqq +16-17 d: ddddddzbxcdddcddq +4-5 s: nqssf +4-5 s: sssbks +4-5 s: tsjlhsbsmt +1-5 z: zzzbztzf +1-4 l: pvsgtvt +3-6 b: bbbjbb +2-4 d: wkdvd +16-18 w: wwwwwwwrwwwwwwwzwkw +15-17 t: tqttjttttttttvtttt +5-9 q: qcqqqqdtqq +8-11 s: sssssssfssssssssssss +5-6 x: xxxvcpbxr +13-15 d: ddwdtctnjdcdpch +5-6 z: zzzwzzzq +3-7 v: bsphcnvwvtvphdp +3-4 q: vhcprqqgdmlfpwqqw +11-18 b: bfbbbfbbbbvhwbbbzlb +12-15 c: cccccccccccccqfcc +4-10 j: jjjjjjjjjjjw +8-14 s: ssssssspmssssssssms +6-15 b: jxnbdvxbbbcbrsbxrs +10-12 s: ssssjsssssfs +8-12 f: ffffdfkqflfpf +8-9 w: wwwwwwwwz +7-10 z: fzzlzzbtmthzzzz +2-3 k: kqzzb +11-12 s: ssssssssssls +16-17 r: rrrrhrrrrrrrrrrnr +3-11 b: czpbpbzswgcddm +4-8 z: cnzztzgzqz +18-19 m: mjmmmmmmmmmmmmmmmpk +10-13 q: gqqqqqqsxqqqqdtqkq +10-11 x: nxmbxxxrgpmxxxfnxxxz +2-5 x: xbxzrxd +9-14 l: llllpljlllfllwv +1-2 m: mmmmmm +1-3 b: fblbbfbbbbbtbbgbbb +3-4 g: gnnccg +8-9 f: fffffffkffff +5-6 r: rrrrhf +7-8 l: lwlwllllllctl +5-7 j: jrjjwgjvkkncnjbqc +3-4 b: bbsbb +4-13 c: jmcczvkbxccdf +4-5 g: wgrgg +7-8 d: dgddddzh +3-8 h: hhbhhhhhhhh +15-16 l: llldllllllldlllllll +11-15 r: rrgrrrrrrrnrrgxr +10-11 l: lllllllllll +4-5 x: xfdbjsmbbcxdphvlfkxr +9-12 m: rqmnmrmhcmmms +3-8 b: tzsnnndnbwgbskbb +4-10 v: vvkvvdrvwvc +9-11 j: jjjdjjjjjsj +6-11 b: hbbmbbbbbbjbtrbbbz +13-16 v: vvpvvvbvcvvvvvvvv +11-14 j: jjjjjjljjjtjjmj +7-11 v: cvdglnvjxkvvgptxvp +1-6 t: ttjqtttzt +7-10 f: ffwpzfxjfgffzf +4-7 d: dddgddp +4-19 n: mjbdzqxhtfbnbfxrpgnh +6-13 d: ddddddddddddd +2-4 t: rhkd +5-9 c: ccrcxzjdzccx +3-4 j: fwjj +15-19 q: qqqtxqqqqqqqqqqqqqqq +1-11 c: gcccccmccctcc +3-5 d: dxhhdr +8-12 k: pkvrkkkvkkmbkcxjwktk +9-11 p: ppnpppppxppp +11-12 p: dppcsppppppqppp +7-8 b: bbbbbbmbqp +7-8 c: cbccccccc +8-9 b: bbbbbbbcrbb +2-6 z: gzlnpzpkhjwwqtswcrz +5-6 d: dddsdkd +1-5 w: dxwkgwwwwwwmwwwww +4-7 q: qqqnwqlqrqdcqpq +6-9 m: lmmmdmmvmmmm +7-8 h: hhhghhxh +2-3 j: jjjj +1-4 r: rrrw +4-7 m: mmjmmrm +4-5 j: vljjrj +19-20 j: jjjjjtjjjjjjjjjjjjgx +2-6 h: xhltfh +5-11 s: ssssjsssssss +3-4 b: bbbb +1-5 g: chgmtgnn +6-7 j: jjjjjjz +3-8 c: cccccrccccc +3-17 s: sslstssssssssspsgs +17-18 x: bxxxxxxxqxxxxxxxkwxx +9-10 q: qfqqzqqqsddqqqqqqq +6-10 k: kmkkkxvqkrk +5-9 f: kffffffffcdfffffplf +5-6 r: rrrrrz +2-5 d: hdgzt +7-10 k: zkrllwkkkjrkqfkkk +9-11 b: bbrkbbbbcbqqb +8-12 g: gfgkgggggggggggggg +12-14 s: sssjsspstlvlsrsssss +4-8 g: pqgzcgvgflgntlp +13-14 j: jjjjjjjjjjjjjx +4-6 k: kkkmqpk +3-5 j: sjjbjlvjjvjr +2-9 c: ckccjxzcrcctbfn +10-12 n: hvnnnnxnntnlnn +11-15 m: mmrmmmdmmmlmmmjqm +6-8 p: qpzrrpcpbxg +5-8 w: xwdzcgclxwsvfwtwbxnw +3-8 q: qzqzhlzc +2-3 v: hvvvvltft +5-12 s: tgvsswttkwfssnsqjsxk +5-17 b: bbbbbbbbbbbrbcbbbhb +3-6 p: ppjpzw +3-5 k: kkxvkswk +4-8 v: vbvrvxvgrvvwwvvm +3-12 v: njvvgvdcjvvtvvcnvg +4-5 n: bnnnrn +8-9 l: jlvxdlpll +12-14 w: fkwwwwwwcwwjwmwc +1-4 q: wmqrzpqhj +4-7 t: tctsttlt +13-14 h: hhhhhhhhhhhhhf +2-3 j: jmjjjjj +5-6 v: vvvvlv +5-13 v: fnxvvvvvnvqvvvvvws +16-17 l: gllllllllllgwqlll +1-2 s: pnpfsqw +6-13 g: ntzqggvbnwxrgskg +5-12 k: krxmbxqbkhxlnvdxdkkq +14-18 d: ddddddddjdddbdddddd +4-7 z: zzzzxzzs +15-18 d: dddddddddbddddpddh +8-11 x: slxxxxxjkxxxrsdx +4-6 n: nnnfzzn +1-4 h: gslcmnhhfhvz +1-4 d: tddqds +2-3 c: fccp +2-5 k: mkqrknj +9-16 g: gpggdggwgwgwglggg +2-5 h: hhjds +4-5 b: zbnvld +6-14 m: mfqmnmqtdmmzmm +7-8 j: thvcsgjn +1-5 x: xwjdxdqjtc +4-17 r: fqbrqrnpslndrmjdhpjp +4-5 c: ccccw +5-6 v: vvvvdm +5-12 n: nnqzjntfnnnd +3-4 c: nccqlccq +1-10 p: nrvvzpppqpn +9-16 v: jvvrpvvvvvzvhhvvgz +12-13 s: sjsslsssfxxkrssstkss +1-3 t: vtwbh +5-16 q: sxxfrqhqtvzbzqwg +5-9 z: dnzlhzzzsdzz +15-16 d: pdddddjddpkdddtdddd +3-4 p: pplqppp +7-9 s: scsslshsqssw +2-3 v: vxvxv +8-9 w: wqwwwwwgshww +8-9 k: kkkkkkkkc +9-10 v: vhfvvnvvtvvb +1-12 z: zzzbzzzzzzzdzzzz +6-7 b: bbbbbwhb +12-13 z: zzzsvtlzzzzzz +1-3 s: pssssssxw +8-9 w: xwwwwwwdzw +1-4 x: xxkxxx +10-12 f: fffffffpffvffff +18-19 w: wwspwwzwwqcrwwhwwww +6-8 s: ssssstsks +5-6 j: jjcjgm +2-5 p: wprwpxbdkrfpmppqpd +8-18 n: nnnnnnnnnnnnnnnnnn +4-11 c: txncpqclrlc +2-10 f: ffcffftfffxrxf +1-4 t: ttttttt +4-9 f: zdpjffffbfbl +6-7 f: kfsffffffm +1-4 b: bbdn +6-12 k: kkhkkkkdqkbkjkkkl +3-4 n: kmnn +4-11 l: kllcllldllclll +7-19 w: pfwdwdnkblwzgkfnfmh +3-9 z: zzphzdnhqwlzzwzz +15-16 h: hhhhhhhnhhhhhhhd +1-4 l: vllll +2-13 l: hdhvgdrlltlmjptzq +1-3 p: njvpltppbkxpfpppp +1-2 j: kkjv +10-11 c: ccccccmcccjcc +5-8 v: tqvmvtwvzfczvvvvw +6-9 x: xxmxxxfxxxxx +1-10 z: zgzzztmdtkzzpxztbgpp +10-12 g: ggwzjgdsgbnggl +5-6 j: jvxjvjj +4-7 x: xnxxmgxxtjxxkj +13-14 d: ddddmddddddddd +12-15 c: cccccccccccnccc +16-17 n: nnnnnnnntnnnnngnmn +1-12 m: bzckgvmmbdcxtgtmb +4-7 l: ljllllljl +5-11 w: wzvzwwrkmtwh +11-13 l: lmtpwxlllhlgllwvqnp +6-10 f: fvgkffqvcfffdbfff +3-5 j: kbfjjj +1-2 h: hhztdpbttnc +8-10 b: bbbbjbbqkbbbd +1-13 c: hccvcxtcclpckzd +6-10 w: wwwwwtwwzrwwf +2-3 j: fjnnj +2-4 j: njtjjjxrjv +4-5 w: fwwzw +7-9 k: qbnkghdbqlz +2-9 s: dsdftlzsszlf +4-5 v: vvvvd +1-2 w: fbfwwb +4-7 t: tttttmt +3-10 h: hhhhnhhhskhh +3-8 w: qwswwswfl +1-3 p: pmpgpp +2-7 n: nmmgnssmtn +2-3 j: djvjgjp +6-13 x: jxxbxxgnxvbxx +6-10 v: vrvvvvvvdz +2-3 b: bbbbjrkwnc +1-2 h: fshnf +1-5 n: htsknrzqnntknfnjx +5-9 d: kkgtwrdjmxkzc +12-13 x: xxxxxxxxxxxxx +2-10 m: wmmmmmdpmmmmh +2-12 n: xttqcmfkvnlkzskjhmzn +6-9 m: mmmmmxmkp +10-16 m: gmmdqmjmflmmmmmcmmm +13-14 p: pprprppppspxfgnptppp +1-16 b: gcnbbnbbmsjxnbppcb +10-11 d: dkdddddfjmpvdddd +1-9 f: zlfwstnzp +14-15 n: nnnnnndnnnnnrnnvcndn +2-6 l: lvvldlzdzgdf +6-10 z: zzzzztzzzhzz +13-14 n: nnnnnnnnnnnngln +8-9 z: zjqztzztqzzbxzz +2-3 v: dwcv +4-6 m: kmmmkmm +5-9 g: wtgfgdmxkx +3-5 b: vbzbf +10-11 w: wwwwwwwvwwz +2-8 s: sztstsnssq +2-6 l: lldzcslxdwghmn +1-2 n: vmznndnnnbrhknjwzkzx +3-4 k: kknp +1-5 h: hhhhhhh +12-13 z: zzzzzlszzzzzxz +1-12 m: mtbspfpdgpznrsmvgq +11-13 q: qqqqkqnqhqrqrq +14-18 d: dtdrdddddddddxfddddd +1-6 q: bqqqqqgqqqq +1-7 c: dlmvcsztzpx +4-6 g: gmgggg +5-6 s: ssswsssgdghv +8-9 k: kkkkkkkgn +1-17 s: ssssssssssksssssss +4-5 b: qqtlsh +5-6 g: gggggg +2-11 r: drkpvrrlrtrvrjhpd +3-7 k: fkkhmddh +6-7 h: hhhwhnfph +3-4 p: ptppmwpnps +3-6 w: hwcshlrm +7-8 d: dtddqddzn +1-6 s: sslsbssbsg +12-15 n: tbnnjknnkwnnnnsnnnz +8-9 z: zzzzzzzzz +2-5 w: kjnwn +4-7 m: mmmtdjmmmmtl +4-7 h: dqfhzrqhfhntzhkhhdvb +3-5 g: zvflgg +5-12 s: lvsvqnvssgcx +11-19 p: pcppvppplpwppppjlps +5-19 p: gkpmfxlmppczdnhbqcw +5-17 b: mdzljsdvxdmbbbbddvrw +1-5 s: shpdss +2-5 t: btggtltvw +1-6 b: sbbbbbb +3-6 w: wwpsfkwnrrr +13-18 f: ffffwfffsfffwffffn +4-5 m: mpmmmxmmggvnb +5-6 z: pwsqhcztlf +8-11 d: ddddddddddlf +1-2 t: xttt +6-8 m: mmmpmvmmm +16-17 h: hhhhhhhhhhhhhhhrw +1-5 g: gpgpgg +3-7 p: gplrpzp +11-18 t: ftqnxttzttxtgttntrtt +10-11 z: zzzzzzdzzlgzzz +3-10 z: lzfzzzzszbzzztj +2-10 l: khdbddnxltnk +4-8 b: tbbcjsnbrbhfb +3-6 x: xgsxmn +9-13 t: ttttttttmtttt +7-11 l: fllllllzllmdshrll +18-19 g: hffqfwssgqpcnmddkcw +3-4 m: mmhvmc +1-3 w: vwnw +5-6 r: rrrrrb +2-4 j: rjqj +5-7 b: kgblcbbdrb +3-5 g: jgggggg +1-11 v: vvrvvhxvrvvnrvvv +3-14 w: ncwphcwvjwhdpwqkg +6-7 x: xxxfqmkvxx +13-14 v: vvvvvvvvvvvvkt +2-12 x: xzcvvxhhwwxxc +6-14 m: mdtnmjmhmnmmmmm +15-16 z: zzzzzzzzzznzzzzzzz +4-6 s: mcssdssjshscvcl +11-12 b: bnbbbmbjsbxbbbbbj +7-13 c: cccfhccczccwcccsc +3-4 m: cgtmmm +5-10 l: hxhbggrllmtgn +6-8 d: sdfdddlv +4-9 x: xxxxxvxxcx +11-12 c: rcmqkzjccccrdccmc +1-3 g: jgng +4-5 l: lvlll +9-16 n: rnnnqjnvqnnnlnnwdnnn +4-10 x: crdxgxrfjhr +1-5 f: tgdffffffqf +15-16 q: qqqqqqqqvqbfqknqqqqq +9-11 s: sssnffssksq +3-4 v: vqvvv +5-19 z: jthjzpgmwjbftzvmnpzk +3-10 w: mpxhrrnqdvncwssqwlxz +14-15 b: bzbbbbbsbbbbbbbbbbb +3-5 j: jpjvjjrjtmjj +3-6 w: wwmwwzw +1-6 c: ccccvzcccm +10-11 m: mmvwgmjmmmrqjmmmglm +2-3 f: fffw +6-12 m: bmbzmmrshmmz +5-6 n: nnnnnn +4-5 k: kmsgkwvkk +6-8 t: tttttbttt +4-5 j: vtmvsqjl +5-6 q: qshzdqqk +1-3 w: dwgw +8-9 q: qbqqqqqwq +5-10 m: lmmmkgmmwb +6-9 n: nnnlnnnnn +4-5 t: tntct +6-8 g: hgngghgw +6-9 t: gttwtgvpgtlt +6-7 l: lllllll +4-5 j: jhjkjjm +13-15 h: hhhhhhhhhhhhxnh +1-4 m: mmmmm +4-6 q: cqrtqsdqqzrknf +9-13 f: fffffffffzfvff +2-16 p: prxdxjpkppgpxsjwpppp +3-4 r: rrzh +1-2 g: cglblsnkg +1-3 f: xqnfwjmmwqffd +13-14 q: qqqqqqqqqqqqvjqq +18-19 g: dggggggggggggggggvgg +5-6 s: dsssssss +4-6 z: szdzzb +9-12 t: xtltpgftttmtt +10-16 m: mqmmmmmmmcmmmmqx +3-7 c: cpccngcvccm +2-16 s: jsdfsjsjtswhkvmsskj +2-3 c: kccc +4-16 k: qhndnqmrvjcczfkpds +4-6 n: nnnrkxl +9-10 s: ssssssssds +2-3 z: hfzcz +2-3 s: sssz +8-10 b: qbbbjbbbbw +7-16 k: gcvskkjkkkkwkzkz +7-9 n: nnnnnnhnnnn +1-10 q: qjxqmqxcgg +9-11 l: jdhjbbcnlzll +7-14 l: llrlxlllllltlnl +3-6 f: wfdqfbrf +3-8 t: ttzttmtvttzpl +10-12 v: vfvvvvvvvsvv +5-6 v: vvvvvm +11-13 t: ttttxtttttptttctttt +3-4 k: kkdm +4-6 n: nnnlnt +13-15 m: mmmmmbmmmbmmmmmm +5-6 l: klllnl +2-3 f: flzff +3-4 r: frrs +1-5 d: cqwkvsdqdvb +9-15 l: jvtfqczlnlwdpclxwp +12-14 t: ttttttlqtpttnt +11-12 m: mmmmmmmmmmmm +4-5 j: jjjkbjjjjj +9-12 w: wwwwwwwwkwxkwcb +6-12 q: tcqrqqqqxjqqqqmqhq +5-6 m: mmmsmmmm +2-5 t: mttbttprttddtv +10-13 t: tttttttttttztt +10-11 f: kmqfxttfkfd +5-7 v: vrlzpvjtvv +6-7 l: nlllllj +1-5 q: szdbqqkqqtkmssq +3-4 q: kcmlwqzczwms +13-16 b: bsbbbbbbbbrbsbbpzbb +9-11 d: dddsdbddkdkdd +2-15 j: xkjnntffvvxfnntcv +4-6 v: hvvvcvqvrwv +9-12 j: qjjjjdjgqhrjjfjrdj +8-10 v: vvvvvvvnvnvv +4-9 t: ttttthttt +8-16 g: ggggklggcggggxgg +1-3 z: wpqz +4-5 g: wgdnmxccgj +8-11 r: rrrrrrrxrrmd +1-9 f: ffffsfgdbsqfffzf +7-8 q: qzqqqqrrq +12-13 n: nnnnnnnnnnnnnn +8-12 j: jpjjjmjjjjjzj +1-4 j: nvjw +7-13 z: zzzzsnzxzznzrzgzzzz +14-16 z: zzzzzzzzzzzzzzzm +1-4 x: xxxxnxh +8-15 k: gnrkktkpcmklkksnkk +2-4 h: klph +11-16 c: ccckzdccgckpcccsc +15-16 l: dlllllllllllllll +2-3 s: dsxss +1-4 v: jvvv +2-8 t: ktkdtxkt +1-3 t: ftftttttt +4-5 k: kkkbkk +4-6 g: llqcggg +4-14 r: fmdvrrwlstlbjr +2-16 p: rpdfhpbqfwxlxhhc +6-8 j: jjjjjjjjj +1-7 f: fnnffblbqffkrff +1-3 k: rfqwlnnkzdq +15-16 s: ssssswssssssskssss +11-13 z: zzzzngfzzzzzz +5-7 h: hhbhhcnhfghhhv +2-7 j: zjrmjgmjdkp +5-8 z: bzzzpzzrzzzz +12-16 j: jtjnjjjjgjjjjjjsj +5-9 s: ssssssssms +2-3 s: ssss +13-14 d: ddddddddddddkt +12-13 r: wfdtrknrhvrrc +6-8 p: vpxphxngzhnkppppfp +2-3 j: jtjx +3-6 k: tkkvjkb +2-6 t: tvftftvbfx +5-7 z: zzzqzzz +14-15 h: whdhxhhhhhhhfxzhhh +3-4 g: ngjgg +9-10 z: zzzzztzztq +2-7 f: fffffqfszchff +4-6 f: lfqjnzccffjslsdf +5-6 z: lpzzzzsz +11-13 t: tttttwtttttttttttt +2-6 z: zzzzzz +9-10 m: mmmmmmmmjm +5-6 w: xwggfcwvwlx +1-4 j: jjjjjljjjjjjj +8-10 t: ttttttwttt +5-12 x: zxxxxxxpxxxn +3-4 v: vvlrv +3-6 h: hhhwhlxhlrhl +2-5 p: cptsjktp +4-11 j: bmjjjjjjnwwdk +15-17 p: ppppppppppfpppvpp +7-11 m: wnnmmwmtmmxmm +9-19 k: kkkkkjkkhwkcvkkkmknw +11-15 s: sspssbshssscssssss +1-8 s: sssssmssssssssssss +4-8 x: xxxxdxxx +9-10 n: nnnrnlnnnnnnnnnnnnnn +10-11 d: ddddddddddn +5-9 z: tzzzqzbmzzzqzjkzlr +9-20 f: fffffnfffffffsfnffff +5-6 g: gvggggg +13-14 x: xxxxxxxxxxxxxxx +6-7 v: vvvzvvvv +5-10 l: kbcvlfvlszndtlldjlh +4-9 s: vkcsdvszthkwmmmxs +3-4 b: jbmb +2-4 w: nmwm +5-10 z: tztzzfzgdzzszq +2-8 k: kzkkkkzwf +16-17 d: vxvzdgzwssqdcgbdb +4-5 b: bbbbb +7-9 w: wwfwwwjwln +5-12 k: ljkkkqfvqtkkxsd +5-7 q: qqqqhqqq +8-9 n: vnnnnnnpstnn +5-10 m: mmgzmmmmmm +3-14 q: qgbvqjxqnqqqqqqq +2-4 c: cccc +3-7 b: szbbkbdbmbbzbqs +1-5 m: mhmmc +3-6 n: nnlvnnvnq +2-3 c: mccc +11-12 m: ddmmmmxmmmmwfmmm +8-10 n: nnnnnrnnnn +1-12 g: sgbggglnggddgggsngrx +7-9 p: phppppppp +4-5 q: qxqqqq +3-7 v: vwsvvrvxvvvvvvwvvdlv +8-18 j: jmjcjjltjmjwjzrllgcj +2-14 r: rdrrrrrrrrrrrp +2-4 r: cxcbkmr +6-9 r: vtlnnvbcndqhrxkkjp +2-3 s: hxssnsswzc +10-16 r: csrsrxrrrrrrfrfdrr +7-9 w: wwbwmvrwdxww +12-14 p: pppbpppppppgpt +2-7 n: nnnnnnnn +3-12 f: mhjxfxgbbvffpclfffg +10-14 l: lllskllljllllll +11-16 l: lwlklllglzllllllll +13-14 h: hhhhhhhhhhhhhdhhhh +1-2 t: tttb +1-6 z: zzzrrzzbf +1-6 v: jrvzvvrs +3-6 g: jggbnl +17-18 x: xxxxxxxxxxxxxxxxrx +8-17 f: xnmffffbwfdcfrdfw +3-9 k: jmkdvkdnk +7-9 m: mmmmmmdml +9-11 c: cdmshccckqmcccccckp +16-17 g: gggggngggggggggggdg +7-8 n: nnknnnnnnn +3-13 n: fjhgrspsnkmnf +14-17 z: zzzzzzzzzzzzzqzzzzzt +13-14 r: rrrtrrfsvrzngw +7-8 k: kvkvkpvc +1-2 b: bbpbb +1-7 n: tchnrtbtldnmnnvvnn +3-4 s: sssssjv +1-9 l: cllllztmlllrzfl +9-10 x: qjxxxxxxdhxxxxxh +6-7 p: ppppfppv +3-8 q: mkqzqqbqzjrqbq +6-8 x: xxxxxxxx +1-7 f: ffffffff +5-16 b: jbbbbbqcbbbbbbfb +9-11 c: ccccccccccq +1-2 x: fqkx +4-12 z: znzzzzzzzzzzzzz +3-4 l: lglll +9-11 m: mmmmmmmmmms +14-17 x: xrxpxgkxxzdrxxxxckxv +3-5 t: gxjbbfcpmkbkxbtwbt +5-6 j: jmjjjj +14-15 g: ggggggggggggggx +6-7 l: lllllbl +2-14 q: qqtsqkqvqqqpxzqqcqq +2-4 w: fwhw +4-5 m: mzmtkm +2-4 g: ghghgp +5-6 r: rrrrnl +1-4 h: chghhw +7-12 r: rrrzglvrrrrsrr +1-7 x: xxcxdwxjmx +5-12 r: rrgrrffwtrrnrrqrrjnr +2-5 r: srdqrlxrkrrdkr +3-4 t: ttdg +11-12 s: bfjrkqqgdtlwrskmfrp +3-5 n: nnrnfzsnm +10-15 z: zzzzhzzqqczxwzqztv +2-7 f: ffgdfgff +7-10 d: ddtbdddddjtdqfdddfq +2-9 k: gkrkkkkkkkkhkqkx +13-14 n: nngnlnnnhnnnnnnnn +6-9 r: qrxrxzrff +7-9 m: mmmmmtmqqmml +11-12 p: pppppppppppp +10-13 j: jjjjjjjjjznvbjj +1-2 c: ccvc +7-8 m: mmmmtdmckkpmcbkjmm +5-8 j: sjptjjbjjjjj +6-8 d: jchdwrhd +5-6 z: lqzzzzkrzzvzwbbzktp +6-8 l: ztbltlll +2-3 z: zkrxzp +7-18 g: phgpgggqgzgnmwlpkwd +8-9 f: hffffffqf +17-19 p: pppppphpppppppppzpp +2-3 z: zzzfg +1-2 v: bfvj +4-8 b: bcbbbbbb +15-16 s: ppsnsssssssnssss +2-3 m: mnsm +2-4 h: hwfshh +9-15 h: hhwhhhhxhhhhhhh +17-18 n: nnnnnnnnnnnnnnnnnn +7-11 n: nnnnnqpnlbgqnqnshn +15-16 j: cdjmbcwdppvvjqvv +6-11 p: ppplppppxpt +4-5 r: rrrrrr +10-11 w: wwwwwwwwwww +3-7 p: pzppzspppmkxbldwpnwf +2-12 w: zwdljlzwgxfwvtdm +2-10 f: mzfjqfspgfrfhst +14-15 m: mzmmmvhwmdmmdpz +1-12 r: vrxrrtrxgvrd +4-5 n: nnnnnnpn +6-12 x: xkbwfsxxxxxkxxxxk +4-6 n: mncnxn +9-10 f: ffffpfqftbf +3-5 b: bzwbk +4-7 c: ncdcccc +11-12 t: dgdkrjgsgtlf +8-11 z: rqzdjqznrpkzmblbt +1-3 z: zfzx +2-4 z: kzgzkp +6-10 v: jvvvvvvvvs +2-9 v: vrvvvvvvvc +5-6 q: qqqqqqc +6-10 p: ppdknlgpqkp +10-13 z: zzzzzzzzzzzzvz +19-20 g: dtckblrmggknmxwnrjgg +4-18 p: ppcjpqfpcxtphlppmhcx +6-7 n: nnsnnknthn +10-11 f: fffffffffff +2-4 f: ffccltsfgk +6-11 d: dsqddtddddjcnssrcd +7-13 x: xxxxxqlxxxxfw +12-13 r: rrrdrrrrrmrrrr +15-17 p: pqppppppppppvppppppp +7-8 j: jjjjjrdp +4-6 v: vvvvvt +1-4 t: tltftjtjhz +9-15 j: jjjjjjhjjjjjjjkjj +6-7 c: ccccccccz +8-9 x: xxjxxxxxsx +4-5 h: hhrhh +2-3 g: cgnm +8-10 l: llqlgllzlvrllg +12-13 c: cvnbccpzzxcccfh +3-8 z: zbzrwzzzwrzbqnr +6-7 n: nnnnnnn +8-11 j: tjqjrjggjxxjggjj +7-12 l: bsvxdhljlcsj +3-6 j: jjfjjjjb +2-3 z: dzztwhmzqdx +9-12 v: vvvvvvmvzvvcv +13-14 v: vvvzvvvvvvvvvk +6-7 g: fgggghgng +13-14 h: wcghlwdbjhpdphkcv +1-2 t: nncsg +6-7 w: kwjwwxlwz +4-5 z: zzztvz +3-4 n: nvbvngnw +15-17 z: zzzzzzzzzzzzzzzzzzp +8-10 r: rrrstrgxrhrr +8-10 g: cggggggvgcg +1-4 m: mmvbz +3-14 j: bdbhbjnjnrldhwlbrkrj +1-4 r: rrkrnnd +2-3 f: fvwc +4-13 c: ccccvcgwbhwrcqf +3-9 c: jcghltcfkjchxmccccbs +3-5 h: hhhshm +5-9 h: hhhsjhhhhgthfgldw +4-12 h: mcwvwwphwwbc +6-11 g: gqgggvggggh +9-15 x: xxxxxxxxxxxxxxsx +16-18 t: rmqqtbtvttsdtjvbttl +9-20 f: cllnvlfkfrwzpqxwqgnn +9-18 v: vvvvvvvvzvvvvvvzvxvv +4-5 f: fzffbfvfff +1-5 p: pppppp +1-7 z: zjvchwzqjrtxzgz +4-9 v: vvvvvvvvvv +5-8 w: cwwwzwwb +7-8 r: rrrrxrrr +8-9 f: sgdcqfhfcfsflb +3-7 g: gdgtnfggq diff --git a/day02/Makefile b/day02/Makefile new file mode 100644 index 0000000..855b615 --- /dev/null +++ b/day02/Makefile @@ -0,0 +1,26 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) + @$(TIME) ex1-c < $(INPUT) + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) + @#$(TIME) ex2-sort.bash < $(INPUT) + @$(TIME) ex2-c < $(INPUT) + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day02/OUTPUT b/day02/OUTPUT new file mode 100644 index 0000000..5fc14bf --- /dev/null +++ b/day02/OUTPUT @@ -0,0 +1,12 @@ +ex1.bash : lines: 1000 matched:607 + time: 0:00.03 real, 0.03 user, 0.00 sys + +ex1-c : lines: 1000 matched:607 + time: 0:00.00 real, 0.00 user, 0.00 sys + +ex2.bash : lines: 1000 matched:321 + time: 0:00.03 real, 0.03 user, 0.00 sys + +ex2-c : lines: 1000 matched:321 + time: 0:00.00 real, 0.00 user, 0.00 sys + diff --git a/day02/README b/day02/README new file mode 100644 index 0000000..40e6089 --- /dev/null +++ b/day02/README @@ -0,0 +1,48 @@ +--- Day 2: Password Philosophy --- + +Your flight departs in a few days from the coastal airport; the easiest way down to the coast from here is via toboggan. + +The shopkeeper at the North Pole Toboggan Rental Shop is having a bad day. "Something's wrong with our computers; we can't log in!" You ask if you can take a look. + +Their password database seems to be a little corrupted: some of the passwords wouldn't have been allowed by the Official Toboggan Corporate Policy that was in effect when they were chosen. + +To try to debug the problem, they have created a list (your puzzle input) of passwords (according to the corrupted database) and the corporate policy when that password was set. + +For example, suppose you have the following list: + +1-3 a: abcde +1-3 b: cdefg +2-9 c: ccccccccc + +Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times. + +In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies. + +How many passwords are valid according to their policies? + +Your puzzle answer was 607. +--- Part Two --- + +While it appears you validated the passwords correctly, they don't seem to be what the Official Toboggan Corporate Authentication System is expecting. + +The shopkeeper suddenly realizes that he just accidentally explained the password policy rules from his old job at the sled rental place down the street! The Official Toboggan Corporate Policy actually works a little differently. + +Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. (Be careful; Toboggan Corporate Policies have no concept of "index zero"!) Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement. + +Given the same example list from above: + + 1-3 a: abcde is valid: position 1 contains a and position 3 does not. + 1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b. + 2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c. + +How many passwords are valid according to the new interpretation of the policies? + +Your puzzle answer was 321. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day02/ex1-c.c b/day02/ex1-c.c new file mode 100644 index 0000000..51a5535 --- /dev/null +++ b/day02/ex1-c.c @@ -0,0 +1,31 @@ +/* ex1-c: Advent2020 game, day 2/game 1 + */ + +#include +#include + +int ccount(char *str, char c) +{ + int count=0; + + for (int i=0; str[i]; i++) + count += (str[i] == c); + return count; +} + +int main(ac, av) +char **av; +{ + int beg, end, nc, found=0, nlines=0; + char c, str[80]; + + while (scanf("%d-%d %c: %s", &beg, &end, &c, str) != EOF) { + nc=ccount(str, c); + if (nc >= beg && nc <= end) + found++; + + nlines++; + } + printf ("%s : lines: %d matched:%d\n", *av, nlines, found); + exit (0); +} diff --git a/day02/ex1.bash b/day02/ex1.bash new file mode 100755 index 0000000..c7813b7 --- /dev/null +++ b/day02/ex1.bash @@ -0,0 +1,18 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 2/game 1 + +CMD=${0##*/} + +declare -i beg end num found=0 nlines=0 + +IFS=$':- \t' +while read -r beg end char str; do + stripped=${str//[^$char]} # keep only specified char + ((num = ${#stripped})) # occurences of this char + ((num >= beg && num <= end && found++)) # match ? + ((nlines++)) +done +printf "${CMD} : lines: %d matched:%d\n" "$nlines" "$found" + +exit 0 diff --git a/day02/ex2-c.c b/day02/ex2-c.c new file mode 100644 index 0000000..b60ac4e --- /dev/null +++ b/day02/ex2-c.c @@ -0,0 +1,24 @@ +/* ex2-c: Advent2020 game, day 2/game 2 + */ + +#include +#include + +int ccount(char *str, char c, int pos1, int pos2) +{ + return ((str[pos1-1] == c) + (str[pos2-1] == c)) == 1; +} + +int main(ac, av) +char **av; +{ + int beg, end, nc, found=0, nlines=0; + char c, str[80]; + + while (scanf("%d-%d %c: %s", &beg, &end, &c, str) != EOF) { + found += ccount(str, c, beg, end); + nlines++; + } + printf ("%s : lines: %d matched:%d\n", *av, nlines, found); + exit (0); +} diff --git a/day02/ex2.bash b/day02/ex2.bash new file mode 100755 index 0000000..d0aa6a2 --- /dev/null +++ b/day02/ex2.bash @@ -0,0 +1,20 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 2/game 2. + +CMD=${0##*/} + +declare -i beg end num found=0 nlines=0 + +IFS=$':- \t' +while read -r beg end char str; do + num=0 + [[ ${str:beg-1:1} == "$char" ]] && ((num++)) + [[ ${str:end-1:1} == "$char" ]] && ((num++)) + #((num == 1 && found++)) + ((found += num == 1)) + ((nlines++)) +done +printf "${CMD} : lines: %d matched:%d\n" "$nlines" "$found" + +exit 0 diff --git a/day03/INPUT.txt b/day03/INPUT.txt new file mode 100644 index 0000000..9e00c06 --- /dev/null +++ b/day03/INPUT.txt @@ -0,0 +1,323 @@ +....#............#.###...#.#.#. +.#.#....##.........#.....##.#.. +.#..#.#...####.##..#......#..## +......#...#...#.......#........ +........#...###..#.#....#....#. +..##.....#.....#.#.........#.#. +.##.......#.#.#...#..#...##...# +...##.....#....##....#...###.#. +..#...#......##.#.##.....#.#..# +.#....#.###.........#.......... +.#.#..##.....###.....###....#.# +....###....#......#...#......## +......##...#.##.........#.#..#. +##.#....##...#..##....#.#..#.## +.#...#..#.....#.#.......#...#.. +..........#..###.###......##..# +..#.##.#..#......#.......###.#. +...#...#.#.#..#...#.#.......... +#........#..#..#.#....#.##..### +#...#.....#..####.........####. +.....###..........#.#...##...#. +.....#...#..#.......#....##.#.. +...........#..##.....#...#..#.. +......##..#........#........... +#.#..#.#.#..#.....#....#.....#. +..#....##....##...#.....#...... +.#.#....#..#.#......#..###...#. +.......#...#.#....##..#..#..#.. +.#.#.#.......#....#.#.#.......# +.#..........#.##.#...#..#.#.##. +..#.#..........#.#....##.#.##.. +###..#..#.#...##.#.#..#........ +##....#...#....#....#...#.#.... +#...#.#....#.##..##...#.#...... +......#...#.###......##....#... +.................#.###......#.. +##..#....#....##...###.#.#..### +..#..........#..####..##..#...# +.#......#..#.#...........##.#.. +.#..#......#...#.#.#..#.#.#.#.# +.#......###.....#.#.#......##.. +#..........#.##...#...........# +..#....#.##....#.........#..... +.#..##....#...##.........#..#.. +....##..#.###..#.#...#..###..#. +..#......#........#...#.#...... +........#..#..#..#...#.##...... +.##.#.#......#...#.........#... +#..###.#...#....###.##..###.... +........##.............#....#.. +...#...............#....#.#.... +#..........#..#..#.#.....#...#. +.#.............#...#.......#..# +.#..#..#...#........##......... +.....#.#..#.#..#..##.........#. +..#..##...#....#.#...#.###..#.. +#...........##.....#...#.##.... +#.#.#.#........##......#...#.#. +......#..#.###.#...#.##.##....# +.#....#...#....#........#....#. +..#.#..........#..##.......#..# +.....#...##..#................# +.#...............##...#.##...## +#.####....##.....#.......#.##.. +......#.##.#...##..###..#.#.... +.#.##.#...##..#.......#.#..#... +#...#.##..........##..........# +#.###...#...#..#.....#.#.##..## +.##.....#....#...##.....##..... +...#........#..###.###...#..... +##..#....#.....#...#.#....#.#.. +#....#....#.#..........#...#..# +...##..#......#..#..#..#..#.... +.....##...#..####..##.........# +.....#..#.#...#..#....##..##... +..#.......##.#..#.##...#.#....# +.#..#.#...##..##....#..#......# +..##.##..##...###..#....#...#.. +........##.......##...##.....## +.#....###...#..#..#..#.......#. +#.###............#....##.....#. +..........#...#...##..#...#.... +..#......#.##.......#....##..## +..#..###.....#...#.......#..... +#.#...##.....#...#....#.......# +....##.##.#....#.....#.#....#.. +...#....#.###............#..### +#..##..#.........##.....#.#...# +....#.......##......#....#...#. +....#..##.#..........#......... +....#...#.###.......#...#.#.... +#..#..#...#.......##...#..#.##. +#.......#...##.##......#....... +##..##...##...#......#...#...## +..#...#.#.####.#...##.....##... +#...#..#..#...##......#.#..#..# +..##..##.#.#..#...####.....###. +.#........#..##.###...#.##.#... +........#..#...##......#.#....# +..#...###.......##..##..#....#. +.##...#.#..#.##.......##.###... +#....#.#.#........#....#..#.##. +....#.##.#.##..#.#####.....###. +#.#..#..#...#.#..#.......#.#... +....#...#....###............... +.###.#.....#.#.......###......# +##...#.#.###....##..#...##..... +...#.#..#.###.#.......#...#.#.. +.#...#....#...#..####....###... +..#....##.....##.#.#.##....#... +#....#..##.......#...##.##....# +.##..#.......#..#....###....... +#.##.....##.#.........#......## +.####.#...#.....#..#...#.##..#. +....#...........#.....#........ +.#............##...#.......#.#. +#....#.##........#....#.#..#..# +#....#.##....#...##...#..#..#.. +...#..#.####.#....#............ +....#......#.........#......... +#....##....###.....#......#.#.. +...#..#....#........###..#...#. +..#.#........#.#.#.###..#.#.#.. +.....###.....##.#....###.#..... +##.#....#....##...##.###.#.##.. +.###.#..#.......##...#...##.... +.#...###........#.......##.##.. +#......####...#...##.#.######.. +....##.............#..##.##...# +...........#..##.#...#.#.#...#. +###.......#.##..#....#...#....# +.........#.....#.#.#..##.#..... +#...##..#....#..#.............# +...#.......#.##.............#.# +.....#..#...##......####..#.... +.#.#.#.....#...####..#...##...# +#...#.#..#..#.#..#.##.......... +.....#.##..#.#.##..#.#.#....#.# +...##..#...#...#..#....#....... +........#.#..#...#...#.#...#... +##..#........#..#.....#......## +.........#..#...#......#......# +..#.#.#........##...#.##.....## +.###....##....#...#....#..#.... +.#.............###...#..##..### +.##.##.##.......###.........#.# +..#..###...#...#....#..#.#..#.# +......#..#.#..#.....#.#........ +......#...####...#.#.....#..... +.#...##.......#..#......#...#.. +#..#...#.......###..#..#.#.#.#. +.....#.....###.##....#.#.##.#.# +#........#....##...#..#.##..#.. +...#.#........##....#.#..###.#. +#...#...##.........#........### +##...#.##..##...#.....#.###.#.. +#.###.#.#..#...........##..#... +........#.......#..#..#.###.... +#........#....#......###....... +..#.###.######...#.###..#...... +...#...######..#.....#....#.#.. +..#.......#..#..#.........#...# +.#...#..##.##.........##....... +.........#.#.##.#..#....#.#...# +#.......#....#......#.....###.# +##..............#.###........#. +..#.##..#.##.....#...#.#.#..### +..#.#......#..#..##.#........#. +..#.....#...#.#...#...###..#.#. +.......#...........#..#..#.#.## +.......#...##..#.###........... +.#........#.###.#..#..#..#..#.. +##.#.##....#..###..#.##.##...#. +.....#....##.#........#.#.#.... +....##....#..#..#....##....#.#. +#.....##....#.....#.###.#....#. +.#.##.##..#..#...#...........## +...#..###..#.....##....#....... +...#..##..###.#..#..#.#........ +......##..#.......#..##.....### +.#...##.#.#.#......#...#.#.#.## +....#.#....#...#........#...#.. +....#.#......#.#.###.#.#.##.#.. +#..#........###..#..#..#.....#. +...#....#...##...#........##.## +.....#..#..#.....#....#.#...#.. +..#.###....#.#..##......#.##.#. +..####......#..#.#.#..#.#####.. +.......##..#..###.#............ +..###.#........#..........##.## +#.#.........#.##.#......#..#... +...#.....#.....##..#..##.##..#. +#.#.##....#.......###....##.... +...##.#..#...##.#..#......#..#. +..##.........#.##.#####...#.#.. +.#....#...#....#.#.....##...### +##.....#..####............###.# +......#...........#....#....... +.#......#.....##...........###. +#......##.......#.#.#..##.....# +...###.#.....##.#...#.#....#.#. +...###.......#...#............. +..#..#.#....#.#.###.#.#.##..##. +..##...#..#.#..##.#.##....##... +..#...........#..#....#....#... +#.##...........#..#.#..##.#.#.. +...##...##................#..#. +.#...#.##......#.#......#.####. +#.##....#....#.........#....### +.....###........#.#.#.##...#.## +.....#....#.#....#.........#..# +..#...#.#.#.#...#...#...##.#..# +###.......#.....#.............# +#.####.#.......#.#.#.#..#.#.... +#..#..#####......#....#..##.... +...............#.....#.#....### +.###.....#...#.##..#.#..#.##### +#.##.....#......##.......##.... +..........###.......#...#.#.... +..#.#..#...##.....#........#.#. +........##.##....#####.#.#..##. +..##.#.#...#####..........#.#.# +#.........#......##...#.....#.. +.##.#........#...#..##...#...#. +.......##..#...#.....#.##...... +....#.#...##..##..#....##...... +#........#..........##..####.#. +...###...#.#.###.#...#....#.#.# +.....##.#.....#........#.#....# +#.......#....#...##..#......#.. +...#..........#.#.#...#.#.###.# +....##.....#.##..#.#.#......... +#.##..##..#....#.........#...#. +.###..........#...##.#..#...... +.....####.............##...###. +.#..#....#..#...#..#........... +#..#..##..#...#.##..#.###.#...# +......#.#..###...#..#.....#..#. +##.##......#...#.#...#......... +....##.#.......#.#..##....#.#.# +#..##..#...###.#....##......... +.............#.#....#...##..#.. +..#....#...#.....#.##.#..##..## +##.#..##.#..##.#.#.##.#...#.#.. +.##.#..#.#........##.#...##.... +#.........##....##..#......#... +.#.#.......##...#..#......###.# +........#.#.#.#......#....#..#. +...##..#...#...#.##..#....#.#.. +...#.#.#.#.......#.......###..# +...#..##..#####.#.....##.#..#.. +.......#.#.....#.....#...#...## +#...#...#......##.#....##...... +#.....#.#.#.....#....#......#.. +..#..#.##.#......##..#.#..#..## +####...#.....#....#.#.......... +....#.....###...#...##......#.. +.....#....#...#............#... +...#...#..##.........#...#...## +#.#..#.#...##.#.......#..#.#... +.#.....#...##.............#...# +.....#..##..#....#......#.##..# +....#...###.................#.. +...###...#....#...#...#........ +....#.##.#.......#..#.......... +...#..#......#.#...###...#.#... +..#.#..#...#.......#.......#.#. +.#.#...#.#.##........#......... +...#..#...#....#.#.#.#.#..###.. +.#..##......#.#.##..#.##....#.. +#....#.......##.....#.#........ +..###..#.#.#.......##....#..... +........#.#.#....##...##..#.... +#....##.#....#...##..##...#.... +...#..##.#.....#...#.....##.... +.#.#..#..#...#....#..##.#....#. +##.#.##....#.....#....#....#.#. +.##......#............##...#... +#..##.#.####.#.#....#..#..#.#.# +#...##...#......##....###.....# +..#.##.....#....#....#......#.. +.##.#...#.....#.#.#.#........## +.#..#....#.#...........#...#... +#.....#..#.....#.#.##.#.....#.. +....#.....#..#.#....###........ +.....###...........#.#..##.#.#. +....###....#.......###..#...#.# +.###.....#...##.#...##........# +..#..#.#..#...#.#...#.#..#...#. +#.####.......#....##.#..#.#..#. +....#.#.##.#..###.........##.#. +..#..#.#....#....#.##.......... +..##.###..#.#..#.#......#....#. +.#..#.....##...#.#......##.#..# +#.#....#..#.#.#........#.###... +...#....##....##..###.###.#.#.. +..#....#.....#....##.#......... +#.......#....#.........##..#... +.#..#...#.#..#.#....#.#........ +...#..###...#.....#......##.... +..#...........#.....#.......... +....###.#...#......#...#..#.... +.....#.##..#..#....#.......#..# +....##..#.#.#..............#.#. +.#.#..#..#.#......#...#.#...... +....#.......#.##....##.#.#.#..# +............#.#.#.....##....... +........#...##.#..#......#...## +.........#...#...#....#...#.##. +..#.....#......#......#.....#.. +#....#...##..#.#....#.#...#.### +.......#..#..#..#.#...#.....#.# +...#.#...#......##.....#..#.... +...#.#.####..##.#..#........... +..##..##....#.....####...#....# +###.......#...##.#...#...#...#. +.##..#.....#..####......#....#. +#.....#..#..##..##...#..#..#... +.#....#.....#...####..####..... +..#....#...#......#........#.#. +##.#.......#..#.....#..##..##.. +.#..#..#.#.#...#....##...#.##.# +##...#..#....#.........##...... diff --git a/day03/Makefile b/day03/Makefile new file mode 100644 index 0000000..855b615 --- /dev/null +++ b/day03/Makefile @@ -0,0 +1,26 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) + @$(TIME) ex1-c < $(INPUT) + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) + @#$(TIME) ex2-sort.bash < $(INPUT) + @$(TIME) ex2-c < $(INPUT) + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day03/OUTPUT b/day03/OUTPUT new file mode 100644 index 0000000..4000d74 --- /dev/null +++ b/day03/OUTPUT @@ -0,0 +1,12 @@ +ex1.bash : lines:322 pos=966 found:169 + time: 0:00.01 real, 0.01 user, 0.00 sys + +ex1-c : lines:322 pos:966 found:169 + time: 0:00.00 real, 0.00 user, 0.00 sys + +ex2.bash : lines=323 res=6847128288 + time: 0:00.04 real, 0.03 user, 0.00 sys + +ex2-c : lines=323 res=6847128288 + time: 0:00.00 real, 0.00 user, 0.00 sys + diff --git a/day03/README b/day03/README new file mode 100644 index 0000000..5e3a7f9 --- /dev/null +++ b/day03/README @@ -0,0 +1,82 @@ +--- Day 3: Toboggan Trajectory --- + +With the toboggan login problems resolved, you set off toward the airport. While travel by toboggan might be easy, it's certainly not safe: there's very minimal steering and the area is covered in trees. You'll need to see which angles will take you near the fewest trees. + +Due to the local geology, trees in this area only grow on exact integer coordinates in a grid. You make a map (your puzzle input) of the open squares (.) and trees (#) you can see. For example: + +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# + +These aren't the only trees, though; due to something you read about once involving arboreal genetics and biome stability, the same pattern repeats to the right many times: + +..##.........##.........##.........##.........##.........##....... ---> +#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +..#.##.......#.##.......#.##.......#.##.......#.##.......#.##..... ---> +.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +.#........#.#........#.#........#.#........#.#........#.#........# +#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#... +#...##....##...##....##...##....##...##....##...##....##...##....# +.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.# ---> + +You start on the open square (.) in the top-left corner and need to reach the bottom (below the bottom-most row on your map). + +The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers rational numbers); start by counting all the trees you would encounter for the slope right 3, down 1: + +From your starting position at the top-left, check the position that is right 3 and down 1. Then, check the position that is right 3 and down 1 from there, and so on until you go past the bottom of the map. + +The locations you'd check in the above example are marked here with O where there was an open square and X where there was a tree: + +..##.........##.........##.........##.........##.........##....... ---> +#..O#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +.#....X..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +..#.#...#O#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +.#...##..#..X...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +..#.##.......#.X#.......#.##.......#.##.......#.##.......#.##..... ---> +.#.#.#....#.#.#.#.O..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +.#........#.#........X.#........#.#........#.#........#.#........# +#.##...#...#.##...#...#.X#...#...#.##...#...#.##...#...#.##...#... +#...##....##...##....##...#X....##...##....##...##....##...##....# +.#..#...#.#.#..#...#.#.#..#...X.#.#..#...#.#.#..#...#.#.#..#...#.# ---> + +In this example, traversing the map using this slope would cause you to encounter 7 trees. + +Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter? + +Your puzzle answer was 169. +--- Part Two --- + +Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal stop, after all. + +Determine the number of trees you would encounter if, for each of the following slopes, you start at the top-left corner and traverse the map all the way to the bottom: + + Right 1, down 1. + Right 3, down 1. (This is the slope you already checked.) + Right 5, down 1. + Right 7, down 1. + Right 1, down 2. + +In the above example, these slopes would find 2, 7, 3, 4, and 2 tree(s) respectively; multiplied together, these produce the answer 336. + +What do you get if you multiply together the number of trees encountered on each of the listed slopes? + +Your puzzle answer was 7560370818. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day03/TEST.txt b/day03/TEST.txt new file mode 100644 index 0000000..d2aafe4 --- /dev/null +++ b/day03/TEST.txt @@ -0,0 +1,11 @@ +..##.........##.........##.........##.........##.........##....... +#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#.. +.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#. +..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.# +.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#. +..#.##.......#.##.......#.##.......#.##.......#.##.......#.##..... +.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....# +.#........#.#........#.#........#.#........#.#........#.#........# +#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#... +#...##....##...##....##...##....##...##....##...##....##...##....# +.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.# diff --git a/day03/ex1-c.c b/day03/ex1-c.c new file mode 100644 index 0000000..563943f --- /dev/null +++ b/day03/ex1-c.c @@ -0,0 +1,38 @@ +/* ex1-c: Advent2020 game, day 3/game 1 + */ + +#include +#include + +#define XMOVE 3 + +int my_strlen(str) + char *str; +{ + int i; + for (i=0; *str; ++i, ++str) + ; + return i; +} + + +int main(ac, av) + char **av; +{ + + int col=0, line=0, linelen=0, mod=0, count=0; + char str[80]; + + scanf("%s", str); /* ignore 1st line */ + + while (scanf("%s", str) != EOF) { + line++; + col+=XMOVE; + linelen=my_strlen(str); + mod=col%linelen; + if (*(str+mod) == '#') + count++; + } + printf ("%s : lines:%d pos:%d found:%d\n", *av, line, col, count); + exit (0); +} diff --git a/day03/ex1.bash b/day03/ex1.bash new file mode 100755 index 0000000..afa4f5a --- /dev/null +++ b/day03/ex1.bash @@ -0,0 +1,21 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 3/game 1. + +CMD=${0##*/} + +XMOVE=3 # 3 right + +declare -i xpos=0 ypos=0 count=0 linelen=0 modpos=0 + +read -r line # ignore 1st line + +while read -r line; do + (( ypos++ )) + (( xpos += XMOVE )) # move right + (( linelen = ${#line} )) + (( modpos = (xpos % linelen) )) + [[ ${line:modpos:1} = \# ]] && ((count++)) +done +printf "%s : lines:%d pos=%d found:%d\n" "$CMD" $ypos $xpos $count +exit 0 diff --git a/day03/ex2-c.c b/day03/ex2-c.c new file mode 100644 index 0000000..e9a4658 --- /dev/null +++ b/day03/ex2-c.c @@ -0,0 +1,56 @@ +/* ex1-c: Advent2020 game, day 3/game 2 + */ + +#include +#include + +struct { + int dx; + int dy; + int pos; + int count; +} set[] = { + { 1, 1, 0, 0}, + { 3, 1, 0, 0}, + { 5, 1, 0, 0}, + { 7, 1, 0, 0}, + { 1, 2, 0, 0}, + {-1, -1, 0, 0} /* end marker - not necessary */ +}; + +int my_strlen(str) + char *str; +{ + int i; + for (i=0; *str; ++i, ++str) + ; + return i; +} + +int main(ac, av) + int ac; + char **av; +{ + int line=1, linelen=0, mod=0, i; + unsigned long res=1; + char str[80]; + + scanf("%s", str); /* ignore 1st line */ + + while (scanf("%s", str) != EOF) { + line++; + linelen=my_strlen(str); + for (i=0; set[i].dx >= 0; ++i) { + if (! (line % set[i].dy )) { /* line ok for this set */ + set[i].pos += set[i].dx; /* increment set column */ + mod = set[i].pos % linelen; + if ( str[mod] == '#') + set[i].count++; + } + } + } + for (i=0; set[i].dx != -1; ++i) + res*=set[i].count; + printf ("%s : lines=%d res=%lu\n", *av, line, res); + exit (0); +} diff --git a/day03/ex2.bash b/day03/ex2.bash new file mode 100755 index 0000000..3fd1432 --- /dev/null +++ b/day03/ex2.bash @@ -0,0 +1,31 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 3/game 2. + +CMD=${0##*/} + +XMOVES=(1 3 5 7 1) # moves right +YMOVES=(1 1 1 1 2) # moves down +declare -i linelen=0 modpos=0 res=1 ypos=1 +declare -a xpos xcount + +read -r line # ignore 1st line + +while read -r line; do + (( ypos++ )) + (( linelen = ${#line} )) + + for ((i=0; i<${#XMOVES[@]}; ++i)); do + if ((ypos % YMOVES[i] == 0)); then + (( xpos[i] += XMOVES[i] )) # move right + (( modpos = (xpos[i] % linelen) )) + [[ ${line:modpos:1} = \# ]] && ((xcount[i]++)) + fi + done +done + +for ((i=0; i<${#XMOVES[@]}; ++i)); do + (( res *= xcount[i] )) +done +printf "%s : lines=%d res=%d\n " "$CMD" $ypos $res +exit 0 diff --git a/day04/INPUT.txt b/day04/INPUT.txt new file mode 100644 index 0000000..2ce5fa7 --- /dev/null +++ b/day04/INPUT.txt @@ -0,0 +1,957 @@ +byr:1971 +iyr:2017 hgt:160cm +eyr:2020 ecl:hzl +pid:157096267 + +hgt:183cm +pid:368895060 +ecl:oth eyr:2020 +iyr:2013 +byr:1966 + +ecl:lzr cid:279 pid:192cm +hcl:1f7352 iyr:2014 hgt:70cm eyr:1983 +byr:2004 + +hcl:#602927 iyr:2018 byr:1938 ecl:blu +eyr:2024 hgt:172cm +pid:839621424 + +ecl:#12f268 +hcl:#6b5442 +iyr:2012 byr:2011 +eyr:1933 pid:189cm hgt:155in + +byr:1954 +ecl:gry pid:664227667 eyr:2028 +hgt:151cm +iyr:2019 + +ecl:gry +byr:1931 iyr:2017 +pid:459927933 eyr:2028 +hgt:67in hcl:#fffffd + +cid:322 hgt:163cm +byr:1969 hcl:#a97842 pid:472877556 +iyr:2019 +ecl:amb eyr:2030 + +hcl:#733820 ecl:brn byr:2000 eyr:2022 iyr:2014 cid:320 pid:751634349 +hgt:180cm + +ecl:blu eyr:2028 +hcl:#866857 byr:2029 hgt:191cm iyr:2010 +pid:170cm cid:123 + +pid:258660154 byr:1921 hgt:161cm +eyr:2030 +cid:217 iyr:2012 +hcl:#4dd6d4 ecl:grn + +hgt:170cm byr:1978 eyr:2022 pid:399347273 +iyr:2010 cid:109 ecl:blu hcl:#602927 + +pid:172106685 +hgt:183cm +ecl:gry iyr:2020 eyr:2025 hcl:#18171d byr:1980 cid:289 + +cid:77 ecl:#254ad9 +byr:2017 pid:169290741 iyr:2003 hgt:85 hcl:z + +hgt:155cm byr:1987 ecl:oth hcl:#fffffd +iyr:2010 + +ecl:brn iyr:2014 cid:74 +hcl:#623a2f +hgt:187cm byr:1955 pid:008305281 eyr:2025 + +pid:428624233 ecl:grn +eyr:2027 hgt:167cm hcl:#623a2f byr:1960 iyr:2016 + +eyr:2027 pid:358876826 hgt:171cm ecl:oth byr:1957 iyr:2018 +hcl:#ceb3a1 +cid:314 + +ecl:grn eyr:2030 +hgt:73in iyr:2011 hcl:#602927 + +hgt:76in byr:2029 +pid:2703176 iyr:2020 +eyr:2037 ecl:#95d926 +hcl:9574d2 + +eyr:2020 hgt:164cm +byr:1949 hcl:#fffffd pid:591281293 iyr:2014 cid:136 + +cid:268 hgt:73in hcl:#6b5442 eyr:2025 ecl:brn byr:1988 pid:899417027 iyr:2015 + +iyr:2020 hcl:#b6652a hgt:177cm +eyr:2028 ecl:hzl +byr:1995 pid:594197202 + +hcl:#a97842 hgt:179cm byr:1930 +ecl:brn pid:010268954 eyr:2020 iyr:2010 + +iyr:2022 pid:93390086 +cid:321 eyr:2034 hcl:#a97842 hgt:168in byr:2006 ecl:#a8f84c + +eyr:2028 ecl:blu byr:1935 +hcl:#6b5442 pid:187679418 +hgt:174cm iyr:2016 + +iyr:2019 hgt:164cm pid:704379775 +ecl:oth hcl:#888785 byr:1930 +eyr:2025 + +hcl:#6b5442 cid:168 +hgt:171cm eyr:1944 iyr:2018 pid:675364934 +byr:1962 +ecl:hzl + +hcl:z +eyr:2039 +ecl:zzz pid:26281402 cid:144 iyr:1928 +hgt:166cm + +ecl:hzl hcl:#7d3b0c +eyr:2022 pid:011589584 +hgt:64in byr:1945 iyr:2014 + +byr:1950 hcl:#18171d pid:685748669 eyr:2028 iyr:2010 hgt:176cm ecl:grn + +byr:1989 +hgt:163cm hcl:#18171d ecl:grn iyr:2020 pid:721397788 cid:308 eyr:2020 + +pid:443496560 iyr:1999 +eyr:2027 hcl:z +hgt:69in ecl:zzz byr:2019 +cid:108 + +pid:#c9d804 eyr:2011 +ecl:#574df9 iyr:2027 hcl:z byr:2018 +hgt:64 + +hgt:69cm +iyr:1926 hcl:fdcce6 +ecl:#28b358 +eyr:2026 +byr:1994 +pid:76404593 + +eyr:2020 +ecl:hzl pid:978839539 hcl:#efcc98 +byr:1935 cid:121 +hgt:165cm + +ecl:amb +byr:1951 hgt:186cm pid:812513486 iyr:2012 eyr:2029 hcl:#fffffd + +hcl:fcdd61 hgt:168in ecl:grt pid:8474140699 byr:1924 iyr:2027 eyr:2023 + +ecl:oth hcl:#866857 +byr:1965 pid:533941934 hgt:166cm iyr:2019 eyr:2040 + +eyr:2032 pid:0795438812 iyr:2009 hcl:z +byr:2028 hgt:131 ecl:gmt + +cid:102 byr:1923 eyr:2025 +pid:222102208 iyr:2019 hcl:#341e13 +hgt:167cm +ecl:amb + +hgt:180cm byr:1956 iyr:2014 eyr:2022 +ecl:oth cid:175 hcl:#888785 + +cid:216 eyr:2022 +ecl:brn pid:002875069 iyr:2019 hcl:#cfa07d byr:1991 hgt:164cm + +iyr:2014 byr:1933 pid:537809907 +hgt:185cm eyr:2029 hcl:#341e13 ecl:blu + +cid:286 hgt:166cm byr:1977 iyr:2012 pid:541909675 ecl:oth eyr:2020 +hcl:#59eb12 + +hcl:#18171d cid:329 byr:1921 eyr:2027 iyr:2019 +pid:440820443 hgt:75in ecl:blu + +hcl:#733820 hgt:177cm +pid:085529831 eyr:2029 iyr:2010 ecl:amb byr:1972 + +pid:704125918 hcl:#b6652a byr:1981 +ecl:#698ae8 cid:141 iyr:2018 eyr:2026 hgt:66in + +iyr:2020 eyr:2022 +hgt:191cm hcl:#7d3b0c +ecl:blu byr:1943 pid:969407635 + +pid:10899196 +hgt:161cm +ecl:lzr iyr:2023 hcl:#ceb3a1 byr:1986 eyr:2012 + +hcl:#7d3b0c ecl:utc eyr:2020 +byr:2028 pid:#f8c441 iyr:2030 +hgt:164cm + +byr:2003 hcl:z iyr:2012 hgt:187in +ecl:gry eyr:2030 pid:150cm + +pid:427618420 hgt:155cm iyr:2012 +ecl:brn +byr:1948 eyr:2029 hcl:#6b5442 + +ecl:oth hgt:81 +byr:2025 cid:66 pid:174cm hcl:z +eyr:2021 + +byr:2027 ecl:lzr hcl:#888785 eyr:1923 hgt:110 cid:54 iyr:1939 + +hcl:#341e13 byr:1961 eyr:2022 hgt:163cm cid:137 ecl:amb +iyr:2019 + +hcl:#866857 +iyr:2020 +byr:2005 +hgt:139 ecl:amb cid:181 +eyr:2016 +pid:181cm + +byr:2030 +iyr:2014 +hcl:#733820 cid:74 eyr:2021 hgt:179cm +pid:7938817872 +ecl:amb + +hcl:91a6dd +iyr:2019 byr:2024 +hgt:72cm +ecl:gmt eyr:2023 +pid:8440093771 + +ecl:grn byr:1963 cid:60 iyr:2030 +hgt:74 eyr:2022 +pid:193189388 +hcl:#b6652a + +pid:403849590 byr:2012 +eyr:1951 cid:90 iyr:2023 +hgt:69cm hcl:z ecl:gmt + +iyr:2010 hcl:#341e13 +pid:011326174 hgt:185cm byr:1976 cid:207 eyr:2027 ecl:amb + +hgt:64in +pid:499837104 hcl:#3be285 +byr:1944 +eyr:2024 iyr:2017 +ecl:gry + +eyr:2032 pid:#850d4e hcl:deddda ecl:brn hgt:172 byr:2004 +cid:244 iyr:2022 + +hcl:a3346d ecl:amb +pid:#505713 hgt:74cm eyr:2010 iyr:2020 + +byr:1987 +ecl:oth iyr:2012 eyr:2023 +pid:131199420 cid:112 hcl:#a97842 + +cid:256 hcl:#a97842 byr:2000 iyr:2018 ecl:oth +eyr:2022 pid:637777693 hgt:160cm + +hgt:152cm +cid:164 hcl:#866857 ecl:grn eyr:2025 +pid:495224989 iyr:2020 byr:1949 + +iyr:2010 +cid:288 byr:1986 ecl:blu +pid:304077824 +eyr:2020 + +hgt:182cm +ecl:blu +hcl:#18171d pid:047931925 byr:1964 +iyr:2012 eyr:2030 cid:167 + +byr:1958 +hcl:#866857 iyr:2019 hgt:165cm pid:553631683 +cid:109 ecl:gry +eyr:2023 + +cid:156 +iyr:2014 pid:811368482 eyr:2026 hcl:#b6652a byr:1994 +hgt:184cm ecl:brn + +hcl:#733820 +hgt:183cm ecl:grn +pid:265625165 byr:1943 cid:344 +iyr:2011 + +iyr:2017 hcl:#c0946f pid:716422629 cid:104 byr:1974 +hgt:160cm eyr:2021 ecl:brn + +byr:2002 hgt:180cm hcl:#602927 +eyr:2025 ecl:grn iyr:2011 pid:887584172 + +hcl:#888785 ecl:brn eyr:2026 +pid:14483306 byr:1947 +hgt:177cm iyr:2015 + +hcl:#b6652a +ecl:#64783e eyr:2020 hgt:163 pid:651615946 +iyr:2012 byr:1999 + +iyr:2014 ecl:gry hgt:188cm eyr:2028 pid:503058612 hcl:#a31066 + +hgt:178cm hcl:z +ecl:amb +pid:17656631 +eyr:2031 byr:2023 + +hgt:166cm pid:783568747 hcl:#341e13 +byr:1955 ecl:grn eyr:2023 + +iyr:2016 hgt:161cm byr:1989 +eyr:2023 +ecl:amb pid:133770783 +hcl:#fffffd + +cid:75 byr:1986 eyr:2020 pid:099478576 ecl:blu +hcl:#733820 +iyr:2011 hgt:158cm + +pid:911200183 hcl:#602927 eyr:2029 iyr:2018 ecl:brn hgt:181cm +byr:1937 + +iyr:1928 byr:2020 hcl:579202 +hgt:60 ecl:utc eyr:1963 pid:157cm +cid:253 + +eyr:2028 iyr:1949 +pid:284455762 hcl:#a97842 ecl:oth byr:1947 hgt:163cm + +hcl:#18171d eyr:2025 cid:222 +byr:1924 ecl:oth +pid:898594506 hgt:182cm +iyr:2017 + +byr:1935 iyr:2027 +hgt:160in pid:#c090c3 +hcl:#623a2f cid:162 eyr:1942 ecl:amb + +iyr:2014 hgt:160cm eyr:2028 hcl:#623a2f byr:2010 +pid:684765216 ecl:blu + +byr:1958 +hgt:154cm hcl:#a97842 +ecl:oth iyr:2015 eyr:2020 cid:334 + +pid:636691339 iyr:2018 +byr:1930 +hcl:#b6652a cid:86 +hgt:184cm ecl:oth +eyr:2029 + +iyr:2025 +hgt:76cm ecl:#043004 hcl:z +byr:2009 eyr:1999 + +eyr:2020 pid:56419390 iyr:2015 hcl:#ceb3a1 ecl:utc +hgt:98 + +iyr:2014 byr:1927 hcl:#fffffd ecl:amb eyr:2022 +hgt:188cm pid:602778565 + +hcl:#cfa07d eyr:2029 byr:1937 pid:7912057436 +ecl:hzl +cid:192 hgt:68in iyr:2012 + +hgt:155cm +iyr:2015 byr:1954 pid:559203670 +ecl:blu hcl:#fffffd eyr:2025 + +hcl:#341e13 byr:1998 iyr:2019 +cid:312 +ecl:oth +pid:230874778 hgt:161cm + +iyr:2011 ecl:amb +eyr:2026 +hgt:163cm byr:1932 hcl:#733820 pid:850176278 + +eyr:2030 +hgt:170cm +iyr:2017 byr:1972 +pid:014731313 +hcl:#341e13 ecl:brn + +pid:133005637 +hgt:167cm +cid:317 +eyr:2025 hcl:#341e13 iyr:2012 ecl:gry byr:1950 + +iyr:2029 pid:745014772 hgt:68in +eyr:2034 ecl:hzl +hcl:ec07ce + +hgt:165cm +ecl:gry +hcl:#a97842 byr:1921 cid:263 pid:609363367 + +pid:192cm hcl:18f308 +ecl:oth +eyr:2037 cid:239 iyr:2026 byr:2010 + +hcl:d0e525 eyr:2037 iyr:2019 +cid:197 +pid:469740743 +hgt:186in ecl:brn byr:1977 + +ecl:hzl cid:254 hgt:165cm +eyr:2024 byr:1996 +iyr:2021 pid:797277746 hcl:e286e8 + +hcl:#b6652a cid:142 ecl:oth hgt:190cm byr:1962 pid:997137384 iyr:2020 +eyr:2029 + +ecl:brn byr:1962 hcl:#866857 iyr:2020 hgt:152cm pid:701556397 cid:121 eyr:2029 + +eyr:2024 cid:186 hcl:z +byr:1962 hgt:155cm pid:448098321 iyr:2017 ecl:grn + +iyr:2016 +hgt:168cm byr:1999 +cid:286 +hcl:#18171d pid:223995430 eyr:2022 ecl:blu + +pid:227780276 ecl:blu iyr:2017 byr:1985 hcl:#6b5442 hgt:183cm eyr:2028 + +hgt:190cm +ecl:oth eyr:2030 cid:223 hcl:#888785 iyr:2010 +pid:115829664 byr:1967 + +eyr:1992 pid:0688674980 hcl:z +byr:2028 +hgt:186in ecl:#849f7b +iyr:2029 +cid:64 + +eyr:2025 +iyr:2013 byr:1958 ecl:grn +hcl:#ceb3a1 +hgt:153cm pid:815357118 + +pid:038013822 hgt:180cm iyr:2013 +hcl:#623a2f +ecl:grn eyr:2029 byr:1949 + +byr:1923 +cid:299 hgt:184cm iyr:2020 +hcl:#fffffd eyr:2027 +ecl:hzl + +byr:1930 +iyr:2012 +ecl:grn hcl:#87f2c8 pid:787371085 + +iyr:2019 +eyr:2028 pid:107626362 hgt:183cm +ecl:grt hcl:#623a2f byr:1985 + +byr:2011 +hgt:68in iyr:2002 ecl:#5dfa18 hcl:#341e13 pid:205853974 + +iyr:2014 +pid:179cm +hcl:13b9e3 eyr:2022 ecl:#b1759b hgt:184in +byr:1954 + +hgt:183cm hcl:#efcc98 +pid:428260080 cid:231 eyr:2025 ecl:grn +iyr:2010 +byr:1957 + +iyr:2016 +ecl:gry +pid:192cm eyr:2026 +byr:1956 +hgt:174cm hcl:#623a2f + +eyr:2021 ecl:blu cid:230 +byr:1923 +pid:438732879 hgt:167cm +hcl:#602927 + +byr:1948 ecl:xry +pid:154cm hgt:179cm eyr:2029 iyr:2017 +hcl:#dd59ab + +hcl:#ceb3a1 +iyr:2014 byr:1981 hgt:167cm ecl:grn +eyr:2021 +pid:926925947 + +iyr:1985 +pid:652196636 hcl:#18171d ecl:#ff3e10 hgt:162cm byr:2012 eyr:2023 cid:171 + +eyr:2029 +hgt:166cm +pid:499909488 byr:1929 hcl:#866857 ecl:brn iyr:2013 + +pid:440245122 +byr:1992 hgt:179cm iyr:2010 cid:181 ecl:brn hcl:#888785 eyr:2020 + +eyr:2029 hcl:#888785 pid:274994154 ecl:hzl +iyr:2014 byr:1995 + +pid:3195072620 +hcl:z ecl:hzl cid:130 iyr:2030 eyr:2034 hgt:157 + +hcl:#1b0a51 +pid:129985083 eyr:2029 +hgt:192cm cid:236 byr:1996 ecl:blu iyr:2016 + +ecl:lzr pid:899902347 iyr:1982 +hcl:#cfa07d eyr:2028 byr:1927 hgt:155in + +cid:187 eyr:2029 hcl:#efcc98 byr:1986 pid:760318090 +hgt:169cm iyr:2018 ecl:amb + +hcl:#fffffd eyr:2021 pid:532530085 iyr:2019 byr:1995 hgt:169cm + +iyr:1980 +hcl:z eyr:2019 +hgt:72cm pid:6532875244 ecl:#2f2221 byr:2006 + +hgt:174cm byr:1920 +ecl:gry pid:#14fae7 eyr:2026 hcl:#1814d1 +iyr:2013 + +hcl:#ceb3a1 ecl:grn +iyr:2018 +byr:1978 +hgt:183cm pid:566862236 +eyr:2028 + +iyr:2020 ecl:amb +pid:618246345 byr:1940 +hgt:60cm eyr:2027 cid:242 hcl:#b6652a + +ecl:grn +hcl:#18171d byr:1957 pid:325895714 iyr:2018 +eyr:2023 hgt:162cm + +ecl:#a3ed7b +byr:2024 +hcl:z eyr:2022 iyr:2016 cid:350 hgt:119 pid:185cm + +iyr:2010 +byr:2004 eyr:2032 cid:326 hcl:6019c5 +ecl:gmt hgt:137 + +pid:477848102 eyr:2025 hgt:178cm hcl:#e31a3d ecl:brn +byr:1943 + +pid:#65fca1 eyr:2026 hgt:192cm cid:293 ecl:blu byr:2026 iyr:2024 hcl:#a97842 + +eyr:2025 cid:181 hgt:186cm byr:1968 +ecl:brn pid:318405093 hcl:#341e13 iyr:2015 + +hcl:#c12f4b eyr:2025 cid:311 pid:652667870 +ecl:oth +hgt:166cm +byr:1981 iyr:2016 + +ecl:hzl +byr:2025 iyr:2014 +hcl:138d5c eyr:2037 hgt:160in cid:206 +pid:#d9119b + +pid:51419740 cid:141 +iyr:2012 +hgt:90 ecl:#9438f4 hcl:#7d3b0c byr:2021 eyr:2020 + +pid:#0bc613 +hcl:z byr:2017 +hgt:91 cid:284 eyr:1966 iyr:2008 +ecl:#974ceb + +cid:344 iyr:1953 eyr:2020 ecl:hzl byr:2019 hcl:z pid:2969979 + +ecl:gry +byr:1925 cid:113 +hcl:#a97842 pid:744660539 hgt:153cm iyr:2020 + +hgt:177 pid:856186682 eyr:1968 ecl:blu +cid:167 byr:1986 hcl:#866857 iyr:2015 + +byr:1937 eyr:2021 iyr:2017 +cid:91 hgt:183cm hcl:#a97842 ecl:blu pid:149192621 + +hgt:154cm hcl:#602927 ecl:oth +byr:1939 iyr:2018 pid:670669747 eyr:2029 cid:301 + +eyr:2025 pid:249412970 ecl:oth +iyr:2016 +byr:1921 hcl:#a97842 hgt:176cm + +byr:1969 +iyr:2019 hcl:9de0cb +pid:644476999 hgt:75in +ecl:oth eyr:2022 + +hgt:164cm iyr:2016 +byr:1988 ecl:gry +eyr:2030 +hcl:#efcc98 pid:393258887 + +hgt:183cm pid:6930456 eyr:2023 cid:210 ecl:#766482 byr:2023 iyr:2017 hcl:z + +iyr:2011 hgt:165cm eyr:2020 byr:1966 +hcl:#efcc98 pid:691169980 ecl:blu + +iyr:2011 hcl:#602927 eyr:2029 +byr:1966 +ecl:oth hgt:165cm pid:945383793 + +pid:567096741 iyr:2025 +ecl:gry eyr:1944 hgt:187in byr:2026 hcl:8ac39a + +byr:2025 +eyr:2025 iyr:2015 +hgt:191 pid:1659927272 ecl:grn + +iyr:2027 hgt:63in byr:1963 pid:874200881 +ecl:oth hcl:#c0946f eyr:2029 + +hcl:#b37a48 +byr:1957 ecl:hzl +eyr:2030 +iyr:2013 + +pid:#38e0fd eyr:2019 cid:103 +hgt:153in +ecl:#956d7c +iyr:2029 byr:2029 hcl:z + +eyr:2021 pid:956654136 +hcl:#854d9d hgt:186cm byr:1960 iyr:2015 + +eyr:2020 +byr:1995 +hcl:#b6652a ecl:amb pid:746523744 iyr:2015 +hgt:178cm + +eyr:2020 hgt:173cm cid:322 byr:1956 iyr:2020 ecl:blu +pid:833595649 + +ecl:gry iyr:2017 eyr:2020 pid:537816651 hgt:183cm cid:160 byr:1996 hcl:#733820 + +iyr:1920 +byr:2013 +hcl:z eyr:1932 pid:169cm + +eyr:2030 cid:258 iyr:2020 ecl:grn byr:1947 pid:571610070 +hgt:162cm hcl:#888785 + +byr:2025 hgt:155cm iyr:2030 ecl:amb eyr:2002 + +iyr:2020 ecl:hzl +pid:090561426 hcl:#a97842 +byr:1923 + +ecl:hzl +iyr:2019 +hcl:#c0946f eyr:2025 +byr:1999 hgt:178cm pid:026042669 + +hgt:74in +eyr:2027 iyr:2015 ecl:gry +byr:2005 pid:#28b09d + +eyr:1953 byr:2014 ecl:lzr cid:202 hcl:1af88d +iyr:2028 + +cid:99 +pid:706477697 iyr:2018 hgt:171cm eyr:2027 +ecl:oth +byr:1978 hcl:#930aef + +iyr:2017 +byr:1935 +eyr:2029 +ecl:amb pid:321873254 hgt:179cm hcl:#1b9aea cid:160 + +iyr:2013 ecl:hzl eyr:2023 cid:233 byr:1996 pid:605962483 hgt:175cm hcl:#ceb3a1 + +pid:754905579 +ecl:brn eyr:2021 hcl:#ceb3a1 +byr:1943 hgt:59in + +cid:110 byr:1935 eyr:2021 hgt:172cm iyr:2020 +pid:643443673 hcl:#888785 ecl:brn + +ecl:gmt hcl:#cfa07d +hgt:148 iyr:2024 pid:635827422 +eyr:1935 +byr:1964 + +iyr:2012 byr:2016 hcl:z +hgt:178cm pid:213073693 eyr:2005 + +ecl:#b3cc58 byr:2027 pid:172cm hcl:#888785 hgt:177cm eyr:1988 +iyr:2027 + +eyr:2029 +byr:1923 +hcl:#d9855b cid:134 pid:068598146 hgt:152cm ecl:blu + +cid:309 +iyr:2010 ecl:oth hgt:188cm hcl:#18171d eyr:2028 pid:174227992 byr:1931 + +iyr:2010 hgt:72in cid:266 ecl:brn pid:0090854908 +hcl:#623a2f eyr:2032 +byr:1967 + +pid:192554211 eyr:2020 hgt:192cm ecl:gry cid:158 iyr:2015 byr:1940 +hcl:#efcc98 + +cid:248 hgt:75in eyr:2025 byr:1957 hcl:#c0946f +iyr:2019 +ecl:brn + +pid:96533216 hcl:z ecl:blu eyr:2027 hgt:193cm cid:224 +byr:1928 iyr:2014 + +iyr:2010 +eyr:2022 cid:276 hcl:#a97842 byr:1968 ecl:gry pid:808830560 hgt:188cm + +hgt:158in +pid:097590485 iyr:2030 eyr:1940 hcl:z cid:274 +ecl:#2ea9ec +byr:2024 + +pid:616947922 byr:1982 iyr:2014 hgt:186cm ecl:oth hcl:#888785 + +byr:1941 pid:039744699 hcl:#efcc98 hgt:190cm iyr:2011 +eyr:2020 ecl:blu + +byr:1971 +ecl:hzl hgt:65in +pid:076133019 iyr:2019 eyr:2030 + +ecl:blu iyr:2011 byr:1928 hcl:#c0946f hgt:172cm eyr:2026 pid:171544458 + +byr:1929 pid:145819079 ecl:hzl +hgt:192cm iyr:2015 eyr:2020 hcl:#b6652a + +byr:1981 ecl:amb pid:123467924 +eyr:2024 hcl:#18171d +hgt:184cm iyr:2017 + +byr:1957 +ecl:oth pid:881258191 hgt:65in iyr:2010 +hcl:#a97842 + +ecl:amb eyr:2020 hgt:152cm +iyr:2021 pid:9448811025 hcl:#c0946f cid:204 byr:2030 + +eyr:2022 pid:208725350 +byr:1944 ecl:blu hcl:#18171d cid:164 +hgt:170cm iyr:2014 + +hcl:#18171d eyr:1952 iyr:1939 pid:788651896 hgt:157in byr:2007 + +byr:1944 cid:87 pid:463367304 +iyr:2020 hgt:188cm ecl:gry +eyr:2027 hcl:#cfa07d + +ecl:hzl +iyr:2018 hgt:164cm byr:1972 cid:272 pid:990204374 +hcl:#6b5442 + +hgt:155cm pid:791416860 iyr:2015 +cid:278 hcl:#18171d byr:1994 ecl:brn +eyr:2023 + +iyr:2017 cid:245 eyr:2026 byr:1932 ecl:blu +hgt:159cm pid:904760812 hcl:#18171d + +ecl:blu hcl:#6b5442 +iyr:2015 eyr:2023 pid:535891497 hgt:175cm cid:168 byr:1920 + +byr:2000 hcl:#6b5442 hgt:156cm +pid:765444727 iyr:2012 +ecl:brn +eyr:2028 + +eyr:2005 pid:9092484649 +byr:2028 +ecl:#5fc7fc hgt:81 +iyr:1988 hcl:8280e1 + +cid:275 +byr:1928 iyr:2010 hcl:#888785 pid:596954301 ecl:brn eyr:2020 hgt:166cm + +cid:163 +byr:1984 eyr:2027 iyr:2020 +ecl:gry hgt:166cm pid:650001846 +hcl:#602927 + +iyr:1925 eyr:2030 +byr:1985 hcl:#cfa07d ecl:#f16a95 hgt:150cm pid:67853501 + +ecl:gry +eyr:1949 cid:218 hgt:73cm byr:2004 pid:055108092 +iyr:1961 + +eyr:2024 iyr:2016 pid:133523002 +hgt:62in hcl:#d99c14 +byr:1996 ecl:hzl + +eyr:2026 iyr:2019 hgt:189cm ecl:brn hcl:#623a2f +byr:1979 pid:172111665 + +iyr:2017 +eyr:1937 ecl:#bfd0ee +byr:1964 hcl:#733820 +hgt:169cm pid:33181449 + +eyr:2024 hcl:#6b5442 +iyr:2014 +hgt:68in pid:577055593 ecl:grn byr:1996 + +hcl:z cid:150 eyr:2039 byr:2015 pid:2453663020 ecl:brn +hgt:154cm + +hcl:#efcc98 eyr:2022 +ecl:grn hgt:167cm byr:1978 iyr:2010 pid:180446111 + +ecl:gry +iyr:2020 hgt:152cm pid:#cce9cf eyr:2028 +byr:1942 +hcl:z + +hcl:#341e13 ecl:brn iyr:2019 +pid:589837530 cid:157 byr:1925 hgt:183cm eyr:2020 + +byr:2009 +pid:179cm hgt:164cm +iyr:1927 hcl:#cfa07d eyr:2034 + +ecl:oth iyr:2012 +eyr:2028 hcl:#866857 pid:716964854 +byr:1940 cid:113 hgt:193cm + +byr:1985 iyr:2011 hcl:#866857 pid:454558712 eyr:2025 cid:301 +hgt:62in ecl:blu + +hcl:#733820 eyr:2025 ecl:amb +pid:855788635 iyr:2016 +byr:1965 +cid:140 hgt:183cm + +hcl:#efcc98 cid:326 eyr:1961 +pid:001357810 iyr:1947 ecl:#8abfc8 hgt:75 byr:2012 + +hgt:60cm pid:#e28da4 byr:2014 iyr:2019 eyr:2040 ecl:utc + +hcl:#733820 eyr:2022 pid:708208638 hgt:162cm cid:326 iyr:2018 ecl:oth byr:1997 + +iyr:1967 byr:2013 pid:8595504787 hgt:73cm ecl:dne + +pid:808787977 hcl:#18171d +cid:205 hgt:181cm +byr:1986 +ecl:gry iyr:2013 + +ecl:dne iyr:2009 +byr:2027 +hgt:188in hcl:#c0946f +pid:585147305 eyr:2024 + +hcl:#733820 iyr:2019 +eyr:2020 +hgt:190cm +pid:042907748 ecl:grn byr:1920 + +ecl:#603ad1 +eyr:2026 +hcl:33f9f8 +pid:862887360 hgt:156in byr:1993 +iyr:2013 + +ecl:oth eyr:2030 byr:1960 +hcl:#a97842 cid:285 +hgt:60in pid:655974048 iyr:2016 + +iyr:2030 +hgt:143 +pid:65806846 byr:1948 hcl:#72a0d3 eyr:1934 ecl:#7cd402 + +hcl:z pid:#0f7c0a iyr:2012 hgt:161cm +byr:2022 eyr:1937 + +hcl:#fffffd ecl:hzl +hgt:191cm byr:1935 iyr:2015 cid:240 eyr:2030 pid:778049989 + +ecl:amb iyr:2011 hcl:#e196f6 pid:231470794 eyr:2026 hgt:179in byr:1979 + +ecl:oth hcl:#6b5442 pid:181cm hgt:72cm +eyr:2040 iyr:2010 + +iyr:2016 eyr:2026 pid:113617276 +cid:117 hgt:176cm ecl:grn +hcl:#c5b999 + +iyr:2016 byr:1941 +pid:846760253 hgt:60cm +hcl:#7d3b0c ecl:zzz +eyr:1972 + +eyr:2023 hcl:#623a2f +cid:103 pid:476193829 hgt:181cm ecl:oth byr:1997 +iyr:2014 + +ecl:#b64a07 hcl:7bb40c byr:2028 eyr:2039 pid:#e2ba33 hgt:189 iyr:1940 + +pid:#3ecfd8 hcl:#7d3b0c iyr:2014 ecl:#30a5e7 hgt:73cm byr:1954 + +ecl:dne +byr:2011 pid:512088455 +hcl:#18171d eyr:2023 +iyr:2024 + +byr:1996 eyr:2026 pid:268556486 ecl:brn +hgt:150cm +iyr:2013 hcl:#7d3b0c + +iyr:2014 +ecl:grn pid:222910621 hcl:#602927 +eyr:2030 hgt:155cm + +pid:530689228 byr:1938 +iyr:2015 +hgt:185cm ecl:hzl eyr:2022 hcl:#866857 + +hcl:#b6652a byr:2028 iyr:2018 cid:150 ecl:lzr pid:706073193 hgt:169cm + +hgt:171cm ecl:gry hcl:#6b5442 byr:1953 +iyr:2011 pid:622763802 eyr:2026 + +eyr:2032 hgt:137 +pid:5033763648 +byr:1925 ecl:hzl hcl:#623a2f iyr:2024 + +byr:1930 pid:6999766453 ecl:#3e3e07 +hcl:#602927 iyr:2010 eyr:2039 +hgt:160cm + +hgt:122 ecl:amb pid:105302121 iyr:2017 +hcl:#733820 +eyr:2027 byr:1955 + +hcl:#95f96b +hgt:193cm iyr:2020 pid:719337690 +byr:1971 +ecl:brn eyr:2024 diff --git a/day04/Makefile b/day04/Makefile new file mode 100644 index 0000000..dbf2996 --- /dev/null +++ b/day04/Makefile @@ -0,0 +1,25 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day04/OUTPUT b/day04/OUTPUT new file mode 100644 index 0000000..3b3057a --- /dev/null +++ b/day04/OUTPUT @@ -0,0 +1,12 @@ +ex1.bash : valid=182/251 + time: 0:00.06 real, 0.06 user, 0.00 sys + +ex1-c : valid=182/251 + time: 0:00.00 real, 0.00 user, 0.00 sys + +ex2.bash : valid=109/251 + time: 0:00.16 real, 0.16 user, 0.00 sys + +ex2-c : valid=109/251 + time: 0:00.00 real, 0.00 user, 0.00 sys + diff --git a/day04/README b/day04/README new file mode 100644 index 0000000..11378e7 --- /dev/null +++ b/day04/README @@ -0,0 +1,127 @@ +--- Day 4: Passport Processing --- + +You arrive at the airport only to realize that you grabbed your North Pole Credentials instead of your passport. While these documents are extremely similar, North Pole Credentials aren't issued by a country and therefore aren't actually valid documentation for travel in most of the world. + +It seems like you're not the only one having problems, though; a very long line has formed for the automatic passport scanners, and the delay could upset your travel itinerary. + +Due to some questionable network security, you realize you might be able to solve both of these problems at the same time. + +The automatic passport scanners are slow because they're having trouble detecting which passports have all required fields. The expected fields are as follows: + + byr (Birth Year) + iyr (Issue Year) + eyr (Expiration Year) + hgt (Height) + hcl (Hair Color) + ecl (Eye Color) + pid (Passport ID) + cid (Country ID) + +Passport data is validated in batch files (your puzzle input). Each passport is represented as a sequence of key:value pairs separated by spaces or newlines. Passports are separated by blank lines. + +Here is an example batch file containing four passports: + +ecl:gry pid:860033327 eyr:2020 hcl:#fffffd +byr:1937 iyr:2017 cid:147 hgt:183cm + +iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 +hcl:#cfa07d byr:1929 + +hcl:#ae17e1 iyr:2013 +eyr:2024 +ecl:brn pid:760753108 byr:1931 +hgt:179cm + +hcl:#cfa07d eyr:2025 pid:166559648 +iyr:2011 ecl:brn hgt:59in + +The first passport is valid - all eight fields are present. The second passport is invalid - it is missing hgt (the Height field). + +The third passport is interesting; the only missing field is cid, so it looks like data from North Pole Credentials, not a passport at all! Surely, nobody would mind if you made the system temporarily ignore missing cid fields. Treat this "passport" as valid. + +The fourth passport is missing two fields, cid and byr. Missing cid is fine, but missing any other field is not, so this passport is invalid. + +According to the above rules, your improved system would report 2 valid passports. + +Count the number of valid passports - those that have all required fields. Treat cid as optional. In your batch file, how many passports are valid? + +Your puzzle answer was 182. +--- Part Two --- + +The line is moving more quickly now, but you overhear airport security talking about how passports with invalid data are getting through. Better add some data validation, quick! + +You can continue to ignore the cid field, but each other field has strict rules about what values are valid for automatic validation: + + byr (Birth Year) - four digits; at least 1920 and at most 2002. + iyr (Issue Year) - four digits; at least 2010 and at most 2020. + eyr (Expiration Year) - four digits; at least 2020 and at most 2030. + hgt (Height) - a number followed by either cm or in: + If cm, the number must be at least 150 and at most 193. + If in, the number must be at least 59 and at most 76. + hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f. + ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth. + pid (Passport ID) - a nine-digit number, including leading zeroes. + cid (Country ID) - ignored, missing or not. + +Your job is to count the passports where all required fields are both present and valid according to the above rules. Here are some example values: + +byr valid: 2002 +byr invalid: 2003 + +hgt valid: 60in +hgt valid: 190cm +hgt invalid: 190in +hgt invalid: 190 + +hcl valid: #123abc +hcl invalid: #123abz +hcl invalid: 123abc + +ecl valid: brn +ecl invalid: wat + +pid valid: 000000001 +pid invalid: 0123456789 + +Here are some invalid passports: + +eyr:1972 cid:100 +hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926 + +iyr:2019 +hcl:#602927 eyr:1967 hgt:170cm +ecl:grn pid:012533040 byr:1946 + +hcl:dab227 iyr:2012 +ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277 + +hgt:59cm ecl:zzz +eyr:2038 hcl:74454a iyr:2023 +pid:3556412378 byr:2007 + +Here are some valid passports: + +pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980 +hcl:#623a2f + +eyr:2029 ecl:blu cid:129 byr:1989 +iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm + +hcl:#888785 +hgt:164cm byr:2001 iyr:2015 cid:88 +pid:545766238 ecl:hzl +eyr:2022 + +iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719 + +Count the number of valid passports - those that have all required fields and valid values. Continue to treat cid as optional. In your batch file, how many passports are valid? + +Your puzzle answer was 109. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day04/TEST.txt b/day04/TEST.txt new file mode 100644 index 0000000..0ff208e --- /dev/null +++ b/day04/TEST.txt @@ -0,0 +1,13 @@ +ecl:gry pid:860033327 eyr:2020 hcl:#fffffd +byr:1937 iyr:2017 cid:147 hgt:183cm + +iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 +hcl:#cfa07d byr:1929 + +hcl:#ae17e1 iyr:2013 +eyr:2024 +ecl:brn pid:760753108 byr:1931 +hgt:179cm + +hcl:#cfa07d eyr:2025 pid:166559648 +iyr:2011 ecl:brn hgt:59in diff --git a/day04/ex1-c.c b/day04/ex1-c.c new file mode 100644 index 0000000..e2b82dd --- /dev/null +++ b/day04/ex1-c.c @@ -0,0 +1,146 @@ +/* ex1-c: Advent2020 game, day 4/game 1 + */ + +#include +#include + +struct key { + int mandatory; + char *key; + char val[80]; +} keys[] = { + { 1, "byr", "" }, + { 1, "iyr", "" }, + { 1, "eyr", "" }, + { 1, "hgt", "" }, + { 1, "hcl", "" }, + { 1, "ecl", "" }, + { 1, "pid", "" }, + { 0, "cid", "" }, + { -1, NULL, "" } +}; + +int my_strlen(str) + char *str; +{ + int i; + for (i=0; *str; ++i, ++str) + ; + return i; +} + +int my_strcmp(str1, str2) + char *str1, *str2; +{ + for(; *str1 && (*str1==*str2); str1++, str2++) + ; + return *str1 - *str2; +} + +char *my_strcpy(dst, src) + char *dst, *src; +{ + char *p=dst; + while (*dst++ = *src++) + ; + return p; +} + +struct key *get_key(key) + char *key; +{ + int i=0, found=0; + + for (i=0; keys[i].mandatory != -1; ++i) { + if (!my_strcmp(keys[i].key, key)) { + found=1; + break; + } + } + return found? &keys[i]: NULL; +} + +struct key *set_key(key, val) + char *key, *val; +{ + struct key *thekey; + + if (thekey=get_key(key)) + my_strcpy(thekey->val, val); + return thekey; +} + +void print_keys() +{ + int i=0; + + printf("keys: \n"); + for (i=0; keys[i].mandatory != -1; ++i) + printf("\t[%s]=[%s]\n", keys[i].key, keys[i].val); + return; +} + +void reset_keys() +{ + int i=0; + for (i=0; keys[i].mandatory != -1; ++i) + *keys[i].val='\0'; + return; +} + +int valid_passport() +{ + int valid=1; + struct key *key=&keys[0]; + + while (key->mandatory != -1) { + if (key->mandatory && !*key->val) { + valid=0; + break; + } + key++; + } + return valid; +} + +char *my_getkeyZOBI(str) /* search string before ':' */ + char *str; +{ + char *p=str; + + while (*str++ == ' ') /* skip spaces */ + ; + p=str; + while (*p && *p++ != ':') /* skip spaces */ + ; + *p='\0'; + return my_strlen(str) ? str: NULL; +} + +int main(ac, av) + char **av; +{ + int consumed, nkeys, nvalid=0, count=0; + char line[160], key[80], val[80]; + char *p; + + while (fgets(line, sizeof line, stdin)) { + nkeys=0; + p=line; + consumed=1; + while (sscanf(p, "%[^:]: %s %n", key, val, &consumed)==2) { + set_key(key, val); + p+=consumed; + nkeys++; + } + if (!nkeys) { /* empty line: reset */ + nvalid += valid_passport(); + count++; + reset_keys(); + } + } + nvalid += valid_passport(); + count++; + printf("%s : valid=%d/%d\n", *av, nvalid, count); + exit (0); +} diff --git a/day04/ex1.bash b/day04/ex1.bash new file mode 100755 index 0000000..1b6fae2 --- /dev/null +++ b/day04/ex1.bash @@ -0,0 +1,51 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 4/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -A KEYS +KEYS=( ["byr"]="" ["iyr"]="" ["eyr"]="" ["hgt"]="" ["hcl"]="" ["ecl"]="" + ["pid"]="" ["cid"]="" ) + +function reset_array() { # reset array values + local -n array=${1} + local key + + #echo before reset: ${KEYS[@]} + for key in "${!array[@]}"; do + array[$key]="" + done + #echo after reset: ${KEYS[@]} +} + +function passport_valid() { + local -i ret=0 + + for key in "${!KEYS[@]}"; do + [[ $key != "cid" ]] && [[ -z ${KEYS[$key]} ]] && ret=1 && break + done + return $ret +} + +declare -i current=0 nvalids=0 + +while read -r line; do + if [[ -z "$line" ]]; then + ((current++)) + passport_valid && ((nvalids ++)) + reset_array KEYS + continue + fi + for pair in $line; do + key=${pair%%:*} + val=${pair##*:} + [[ -v KEYS[$key] ]] && KEYS[$key]="$val" # valid key + done +done +((current++)) +passport_valid && ((nvalids ++)) +printf "%s : valid=%d/%d\n" "$CMD" "$nvalids" "$current" + +exit 0 diff --git a/day04/ex2-c.c b/day04/ex2-c.c new file mode 100644 index 0000000..9259e78 --- /dev/null +++ b/day04/ex2-c.c @@ -0,0 +1,247 @@ +/* ex1-c: Advent2020 game, day 4/game 1 + */ + +#include +#include + +#define BYR 0 +#define IYR 1 +#define EYR 2 +#define HGT 3 +#define HCL 4 +#define ECL 5 +#define PID 6 +#define CID 7 + +struct key { + int mandatory; + char *key; + char val[80]; +} keys[] = { + { 1, "byr", "" }, + { 1, "iyr", "" }, + { 1, "eyr", "" }, + { 1, "hgt", "" }, + { 1, "hcl", "" }, + { 1, "ecl", "" }, + { 1, "pid", "" }, + { 0, "cid", "" }, + { -1, NULL, "" } +}; + +char *eyes[] = { + "amb", "blu", "brn", "gry", "grn", "hzl", "oth", NULL +}; + +int my_strlen(str) + char *str; +{ + int i; + for (i=0; *str; ++i, ++str) + ; + return i; +} + +int my_strcmp(str1, str2) + char *str1, *str2; +{ + for(; *str1 && (*str1==*str2); str1++, str2++) + ; + return *str1 - *str2; +} + +char *my_strcpy(dst, src) + char *dst, *src; +{ + char *p=dst; + while (*dst++ = *src++) + ; + return p; +} +int my_digitstr(str) + char *str; +{ + for (; *str >= '0' && *str <= '9'; ++str) + ; + return *str? 0: 1; +} + +struct key *get_key(key) + char *key; +{ + int i=0, found=0; + + for (i=0; keys[i].mandatory != -1; ++i) { + if (!my_strcmp(keys[i].key, key)) { + found=1; + break; + } + } + return found? &keys[i]: NULL; +} +int get_key_num(key) + char *key; +{ + struct key *k=get_key(key); + + return k? k-&keys[0]: -1; +} + +struct key *set_key(key, val) + char *key, *val; +{ + struct key *thekey; + + if (thekey=get_key(key)) + my_strcpy(thekey->val, val); + return thekey; +} + +void print_keys() +{ + int i=0; + + printf("keys: \n"); + for (i=0; keys[i].mandatory != -1; ++i) + printf("\t[%s]=[%s]\n", keys[i].key, keys[i].val); + return; +} + +void reset_keys() +{ + int i=0; + for (i=0; keys[i].mandatory != -1; ++i) + *keys[i].val='\0'; + return; +} +int check_eyes(val) + char *val; +{ + int ret=0; + char **col=eyes; + + while (*col) { + if (!my_strcmp(val, *col)) { + ret=1; + break; + } + col++; + } + return ret; +} +int valid_key(keypos) + int keypos; +{ + int good=0, i, len; + char *val=keys[keypos].val, buf[80]; + + len=my_strlen(val); + switch (keypos) { + case BYR: + if (len == 4 && my_digitstr(val)) { + i=atoi(val); + if (i >= 1920 && i <= 2002) + good=1; + } + break; + case IYR: + if (len == 4 && my_digitstr(val)) { + i=atoi(val); + if (i >= 2010 && i <= 2020) + good=1; + } + break; + case EYR: + if (len == 4 && my_digitstr(val)) { + i=atoi(val); + if (i >= 2020 && i <= 2030) + good=1; + } + break; + case HGT: + if (sscanf(val, "%d%s", &i, buf) == 2) { + if ((!my_strcmp(buf, "cm") && i >= 150 && i <= 193 ) || + (!my_strcmp(buf, "in") && i >= 59 && i <= 76 )) + good=1; + } + break; + case HCL: + if (len == 7 && *val == '#') { + i = sscanf(val+1, "%[a-f0-9]", buf); + good = 1; + } + break; + case ECL: + good=check_eyes(val); + break; + case PID: + if (my_strlen(val) == 9 && + my_digitstr(val)) { + good=1; + } + break; + case CID: + good=1; + break; + default: + break; + } + return good; +} + +int valid_passport() +{ + int valid=1; + struct key *key=&keys[0]; + + while (key->mandatory != -1) { + if (!valid_key(key-&keys[0])) { + valid=0; + break; + } + key++; + } + return valid; +} + +char *my_getkeyZOBI(str) /* search string before ':' */ + char *str; +{ + char *p=str; + + while (*str++ == ' ') /* skip spaces */ + ; + p=str; + while (*p && *p++ != ':') /* skip spaces */ + ; + *p='\0'; + return my_strlen(str) ? str: NULL; +} + +int main(ac, av) + char **av; +{ + int consumed, nkeys, nvalid=0, count=0; + char line[160], key[80], val[80]; + char *p; + + while (fgets(line, sizeof line, stdin)) { + nkeys=0; + p=line; + consumed=1; + while (sscanf(p, "%[^:]: %s %n", key, val, &consumed)==2) { + set_key(key, val); + p+=consumed; + nkeys++; + } + if (!nkeys) { /* empty line: reset */ + nvalid+=valid_passport(); + count++; + reset_keys(); + } + } + nvalid += valid_passport(); + count++; + printf("%s : valid=%d/%d\n", *av, nvalid, count); + exit (0); +} diff --git a/day04/ex2.bash b/day04/ex2.bash new file mode 100755 index 0000000..2ffc419 --- /dev/null +++ b/day04/ex2.bash @@ -0,0 +1,125 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 4/game 2. + +CMD=${0##*/} +shopt -s extglob + +declare -A KEYS=( ["byr"]="" ["iyr"]="" ["eyr"]="" ["hgt"]="" ["hcl"]="" ["ecl"]="" + ["pid"]="" ["cid"]="" ) +NKEYS=${#KEYS[@]} + +function reset_array() { # reset array values + local -n array=${1} + local key + + #echo before reset: ${KEYS[@]} + for key in "${!array[@]}"; do + array[$key]="" + done + #echo after reset: ${KEYS[@]} +} + +# returns 1 if $1 length is $2 +function check_length() { + return $((${#1} != $2)) +} +function check_digits() { + [[ "$1" =~ ^[[:digit:]]+$ ]] && return 0 || return 1 +} +# returns 1 if $1 is between $2 and $3 +function check_range() { + (($1 >= $2 && $1 <= $3)) && return 0 || return 1 +} +declare -A COLORS=( ["amb"]="" ["blu"]="" ["brn"]="" ["gry"]="" + ["grn"]="" ["hzl"]="" ["oth"]="" ) +function check_eye() { + [[ -v COLORS["$1"] ]] && return 0 || return 1 +} +function check_height() { + local -i ret=1 + local val="$1" unit=${val: -2} + local num=${val%%$unit} + + #printf "unit=%s hgt=%s\n" "$unit" "$num" + case "$unit" in + "cm") check_digits "$num" && check_range "$num" 150 193 && ret=0 + ;; + "in") check_digits "$num" && check_range "$num" 59 76 && ret=0 + ;; + esac + return $ret +} +function check_hair() { + [[ "$1" =~ ^#[a-f0-9]{6}$ ]] && return 0 || return 1 +} + +function check_key() { + local -i valid=0 + local key="$1" val="${KEYS[$key]}" + + #printf "KEY/VAL = %s=[%s]\n" "$key" "$val" + case "$key" in + cid) valid=1 + ;; + byr) check_digits "$val" && + check_length "$val" 4 && + check_range "$val" 1920 2002 && valid=1 + ;; + iyr) check_digits "$val" && + check_length "$val" 4 && + check_range "$val" 2010 2020 && valid=1 + ;; + eyr) check_digits "$val" && + check_length "$val" 4 && + check_range "$val" 2020 2030 && valid=1 + ;; + pid) check_digits "$val" && + check_length "$val" 9 && valid=1 + ;; + ecl) check_eye "$val" && valid=1 + ;; + hcl) check_hair "$val" && valid=1 + ;; + hgt) check_height "$val" && valid=1 + ;; + # TODO + *) valid=1 + ;; + esac + #printf "%s=[%s] : valid=%d\n" "$key" "$val" $valid + #[[ ! $key == "cid" ]] && [[ -z ${KEYS[$key]} ]] && valid=0 && break + return $valid +} + +function passport_valid() { + local -i ret=0 + + for key in "${!KEYS[@]}"; do + #printf "current key: %s\n" "$key" + [[ $key != "cid" ]] && #[[ -z ${KEYS[$key]} ]] && + check_key "$key" && ret=1 && break + done + return $ret +} + +declare -i current=0 nvalids=0 + +while read -r line; do + if [[ -z "$line" ]]; then + ((current++)) + passport_valid && ((nvalids ++)) + reset_array KEYS + continue + fi + for pair in $line; do + key=${pair%%:*} + val=${pair##*:} + [[ -v KEYS[$key] ]] && KEYS[$key]="$val" # valid key + done +done +((current++)) +passport_valid && ((nvalids ++)) +printf "%s : valid=%d/%d\n" "$CMD" "$nvalids" "$current" + +exit 0 diff --git a/day05/INPUT.txt b/day05/INPUT.txt new file mode 100644 index 0000000..946fd8a --- /dev/null +++ b/day05/INPUT.txt @@ -0,0 +1,743 @@ +FBBFBFFRRL +BBFFBFBRRR +BFBBFFFLRR +FFBFFFBLLR +BFBBBBBRLR +FBFFFBFLRL +BFBBBBFLRR +FFBBBFBRRL +FBFBBFBLRL +BBFFFFBRRR +FBBBBBBRRR +FFBBFFBLLL +BBFFFFFRRR +BFFBFBFRLL +BFBFFFBRLR +FBBBFFFLRL +BFBFBBFLRL +BFFFBBBRRR +FBFFBBFRRL +BBFBFFFLLR +FBBFBBBRLL +BBFFBFBRLR +BFFFFFBRLL +BBFFFBBRRR +FBFFBBFRLL +FBBBBBFRRL +BBFBFFFLRL +BFBFBBBRLR +FBBFBBBRRR +BFBBBFFLLL +BFFFFFFLLL +BFBFFFBLRL +FBBFBBBLLR +BFFFBFBRLL +FFBFFBFLRL +FBFBFFBRLR +BBFFFBBLLR +BFFBFFFRRR +FBFFBBBLLL +BFBFFFFRLL +FBFBBFFRRL +BFBFBBBLLL +BFBFBFFRLR +FBBFBFFLRL +BFFFFBBLLR +FBBBFBBRRR +FBBBFFFLRR +FFBBBBFLRR +BFFFFFFRRL +BBFFFFFLRL +BFFBFBFRLR +FFBBBBBLLR +FBFFBFBRLL +BFBBFFBLRR +BFFFFBFLLR +FBBBBFFRLL +FBBBFFBLLR +BFFBBBFLLL +FFBFBBBLLL +BBFBFFFLRR +FBFBFFFLRL +BFFBBFFRRL +FBFBFBBLRR +FFBBFBBRLL +FFBBFBFRLL +BFFFBFBRRL +BFBBBBFRLL +BFFFBFBLLL +BFFFBBBLLL +FBBBBFBRRL +BFBBFFBRLL +BFBBFFFRLR +FFBBBBFLLL +FFBBFFBRLL +FFBFBFFLRR +FBBBFBBRRL +FFBFBBBLRR +BFBFFFFRRR +BFFBBFBRRR +BFFFFBBRLL +BFFFBBFRRR +BFFFBFBRLR +FFBBBFFRRR +BFFBFBFRRL +FFBBBBBRLR +FBBFBFBRRL +FFBBBBBLRL +FFBBFBBRRR +BFFBBBFLLR +FFBFBFFRRR +BFBBFFBRRL +BFBBFBBLLR +FBBBFFBRLR +BFFBBFFLLR +FBBFFBFRRR +FFBFFFBLRR +FFFBBBBRRL +FBBFBFBLLR +FBBBBFFRRR +FFFBBBBLRL +FBBBFFBRRL +BFBBFBFLRL +FBBFBBFRRL +FBBFBBFRLR +BBFFBBFRLL +BBFFFFBLLR +FFBFFBFLLL +FFBBFFBRRL +FBBFFFBLLL +FFFBBBBLLR +FBBBFFBLLL +FFBFBBFLLR +FBBFBBFLRL +BBFFBBFRLR +FBFBBBBRLR +BFBFBFFRRL +BFBBFBBLLL +FBFBBBBRRL +FFBFBFBRLL +BFFBBBFRRL +BFFBFFBRLR +FBFBBFBLRR +FFBBFFFLRL +BBFFFBBRLR +FFBBBFFLLR +FBFFFBBRRR +BFFBBFBRRL +BFBBFFFRLL +FBFBBFBRLR +FBBBBFFLLL +FFBBFBFLLR +BFFBBBFRLR +BFFFBBBRLL +BFBFBFBLRL +BFFFFFFRRR +FBFBFBBRRL +BFBFFBFRRL +FBFFFFFLRR +FFBBBBFRRL +FFBBBBBRRR +BFBFFBBRRL +BBFFFBFLRL +FBBBBFBRLR +BFBFFBFRRR +FFBBBFBRLL +FFBFFBBLLL +BFBBBFBLRL +BFFBBBBLLR +FFBFBBFRLL +BFFFBBBLRR +BBFBFFFRRL +BBFFBFFRLL +BFBBFBFRRR +FFBFBFBLRL +BFBBFFBLLR +FBBFBFBRLR +FFBBBFBRLR +FBFBBBFLRL +FBFBBFFLRR +FBBBFBBRLR +FBFBBBFRRL +BFFBBFBLRR +FFFBBFBLLL +BFBFFBFLRL +FBBBBFFLLR +FBFFFFFRRR +FBFBFBFLLL +BFBBFFFRRR +BFBBBFBLRR +BFBBFBFRLR +FFBFFBFRLL +BFBFBBFRRR +BFBFFBFRLL +BBFFBBBLLR +FBFFBFBRLR +FBBBFBFLRL +BBFFBBFLLR +BFBBBFFLRR +FBBBFFFLLL +FFBBFBFRRL +FBFFBBFLLR +FFBFFBBLRL +FFBFBFBLLR +FFFBBFFLLL +BFBBFBBRRL +BFBFBFBRLL +BFFBFFFRLR +BFFFBBFLLR +FBFFFFFLLR +FBFFFFBLLR +FFFBBBFLRL +FFBBFBBLRL +FFBFBBBRRL +FBBFBFBRRR +BFFFFFBLLL +BFBFBBBLRL +BFFFFFBRRR +FFBFBFFRLL +BFFBFFBLLR +BFFBFFFLLL +BFBBBFFRLR +BFBBBBBLLR +FBFBFBBRLR +BFFFBBBRLR +FFFBBBFRLL +BFBBBBBLLL +FBBFFBBLLR +BFBFFBBLLL +FBFBBBBRRR +FFBBFFFLLR +BFFBFFBLRR +BFFFFBFLRR +FFBFFFBLLL +FFBFBBFRRL +FBBFFFFLRR +FBFBFBBRLL +BFFBFBFLRR +BFFBBFBRLR +BFFBFBBLRR +BFFFBBBLLR +FBFBFFBRRL +BFBBBFFRRR +BFBFFBBRLR +FBFFBBFLRR +FFFBBFFRRR +BBFFBFFLRL +FBFBBFBRRR +FBFFFFBRLR +FFBFFFFRLL +FBBBFFBLRL +FBBBBBBLLR +BFFFBFBRRR +FBBBBBFLLR +BFFBFBBLLL +FBBBFBFRRR +FBBBFBFLLR +FFBFBFFLRL +BFFFBFBLRR +FFFBFBBRRR +BFBFBBFRLL +FFBFBBBRLR +BFBFBFFLRL +FFBBFFBRRR +BFBBFBBRLL +FBFBBBFLLL +BFBFFBFRLR +FBBFBFFRLR +BFFFFFFRLL +BFFFFFBLRL +FBBFBBFRRR +FBBFBBFRLL +FBFBFFFRLR +FFBFFBFLLR +FBBBFFFLLR +BFBBBBBLRR +FBFBBFFLLL +FFFBBBBLRR +FBBBBBBLRL +FFBFFBBLRR +FBBFFFFRRR +BFBFBBFRLR +FFBFBBFLRL +BFBBFFFLLL +BFBBBBFRRL +FBBBFBFRRL +FBFFBBBRLL +FFBBFFFRLL +FBFBBBBLRR +FFBBBBFRLR +FBFBBFFLRL +FFBBFFBRLR +FBBBFBFLRR +FBFFFBFRLR +FFBBBFFLLL +FBFFBBFLRL +FFBFFBFLRR +FBFFBFFLLR +FFFBBFFLRR +FBBFFBBRRL +BFBBBFBRRR +BBFFBBBRRR +BFFFBBFLRL +FBFBFFBLRL +FBBFFBFLLL +BFFBBBFRRR +BFFFBFFRRL +FBFFFBFRRR +FFBBFFBLRL +BBFFFFBRLR +FBFBBFBLLR +FFBFFBFRLR +BFBBFBFRLL +FFBBFBFRRR +BBFFBFBLLL +FBBFBBBLLL +FBBFFBFLRL +FBFFFBFRRL +FBBBFBBLRR +FBBFFFFRRL +FBBBBBFLRR +FFBBFFFLLL +BFBBBFBRLL +FBFFFFBRLL +BFBFBBFLLR +FFBBBFFLRL +FBBFBBBRLR +BFBFBFFLLR +BFFBBFBRLL +FFFBBFFRRL +FFBFFFFLRR +BFFFFBFRLR +FBFFFBBLLL +FFBFBFFRRL +FBFFFBBLRR +FBFFBFFRLR +FBBFBBBRRL +FBFFBFBRRL +FBFFFBFLLL +BFFBFFBRLL +BFFBBFBLLL +BFFFFBBRRR +FBFFFFBLLL +FBBFBFFLRR +FFFBBBFRRR +BBFFFFFLLL +BFFBFBBLRL +BBFFFBBLRR +FBFFBFBLLL +BFBFFFBLLL +BBFFBBFRRR +FFBBFBBRRL +FBBBBBBRRL +FBFFFBFLLR +BBFBFFFLLL +BFFBBBBRRR +FBBFFBBRRR +FBBBBFFRLR +FBBFBBFLRR +FBBFFFFRLR +BFBBBBFRLR +FBFBFFBRLL +BFFBFFFLLR +FBFFBFBLLR +FFFBBBFRLR +FBBBFFBRRR +BFFBBBBLRL +BFFFFBBLLL +BFFBBFFLRR +BFFBFFFLRL +FBBFBBBLRR +FFFBBFBLRR +FFBBBFBLRR +FBFFFBBLRL +FBFFBFFRRL +FFBFBFFLLL +FFBBBBBLRR +FFBBFBFLLL +FBFBFBFRLL +BFFBBBFLRR +FFBFBFBRRR +FBBFFFBLRL +FBBBFFBLRR +BFBBBBFLRL +BFBBFBFRRL +FFBBBFFRLL +BBFFFFFRLL +BBFFFBBRRL +FBBBFBBLLR +FBBBBBBLRR +FFBBFBFRLR +FFBFFBFRRL +FBBBBBFLRL +BFBFBFBRRR +BBFFBBFLLL +BFFFBBFRLL +FBFBBBBLRL +BFFFBFFLRL +FBBBFFFRRR +BFFFFBFLLL +BFFBBBFRLL +FBFFFBBRLL +FBFBFFBLLL +FBBBBFBLRL +FBBFBFFRRR +FFFBBBBLLL +FFBFBFBRLR +BFBFBFBRRL +BFBFBBBLRR +FBFFFFFRLL +BFBBBBBRRL +FBBFFFFLLR +FBBBBFBLLR +FBBFFFFLRL +FBBFBFBLLL +BFFFFBBLRR +FBBBBFFLRR +BBFFFBFRRR +FBFBBBFRLL +BFFFFFBRRL +BFFFFFFLRR +FFBFFFBRLL +FBFFBFFLLL +BFBBFFBRRR +BFBBFBFLRR +BFBBFFBLLL +FBBFFBFRLL +BFBFBBFLRR +FFBFFFFRLR +FBFBBFBRLL +FBFFBBBLRR +FBBFBFBLRL +FBFFBFBLRL +FFBFFFFRRR +BFFBBBBLLL +BFFBFFFRRL +BFBFBBBRRL +BBFFFBFLLL +FBBBFFBRLL +BFFFFFFRLR +FFFBBFBLRL +BFBFBFBLLR +FBFBFFFRRR +FBFBFFBRRR +BBFFFFBLRR +BFBBBBFLLR +FBFBFBFLRR +FBBBBBFRLR +BFFFBBFLRR +BBFFBFBLRL +BBFFFFFLRR +FBFBFBFLLR +FFFBBBBRRR +BFFBBFFLLL +FBBFBBBLRL +BFFFBFFLRR +BFBBBFBRLR +FFBBFBBLLL +BBFFBBBLLL +BFFFBFBLLR +FFBBBFBRRR +BBFFFFFRLR +FFBBBFFRRL +FFBBFFBLLR +FBBFBFFLLL +FBBFFBBLLL +BFFBFBBRLR +FFBBFFFLRR +BFFBFBBLLR +FFBFFBBRRL +FFBFBBBLLR +BFFFBBFLLL +FBBFFBFRLR +BFFFBFFRRR +FFFBBFFLLR +FBFBBBFRRR +BFFFFFBLRR +FFBBFFFRRL +FFBFBBBLRL +FBBFFBFLRR +BFFFFBBRLR +FFBBBFBLLR +FFFBBFBRLR +BBFBFFFRLL +FBFFBFFRLL +FBBBBFBRLL +BBFFFBBLRL +BFFBFFFRLL +FBFFFFFRRL +BBFFBBBRLR +BBFFFFFRRL +BBFFBBBRLL +BFFBFFBRRR +BFBBFFFLLR +BFFFBBFRLR +FBBBFBFLLL +BFBBFBBRLR +FBBBFBBLLL +BFBFBBBRLL +BBFFFBFLLR +BFBBFFFLRL +FFBFBBFRRR +FFBBBBBRRL +BBFFBBFLRR +FBFBBBFLRR +FFBFFBBRRR +FBBFFFBRLR +BFBBBFFRRL +FBFBFBFRLR +FBBBBFBRRR +FBBFFBBLRL +BBFFFFBLRL +FFFBBBBRLR +FBFBBFFRLR +BFBFFBBLLR +BFFFFBBRRL +BFBFFFBRLL +FBFFBFBRRR +FFBBFBBLLR +BFFBBFFRRR +FBFFFBBLLR +FBFBBFBRRL +FFBFFFFLLL +BFFBBBBRLR +BFFBBFBLRL +BFFBFBBRRL +FFBFFBBLLR +FBBFFFBRRL +BFBFFBBRLL +FBFFFFBRRR +BFFFFFFLLR +FBFFBBFLLL +FFFBBFBRLL +BFBFFBFLLL +BFBBFBBLRL +BFFFBFFLLR +FBFFFBBRRL +BBFFFFBRLL +FFBBBBBRLL +FBFBFBFRRR +BBFFBBFLRL +FBBBBFBLLL +BFBFBFFRRR +FFBFBFFRLR +BBFFBFBLRR +BFBFFFFLRL +BFFFBBFRRL +FFBFBBBRRR +FBFFFBFRLL +BFFFFBFRRR +BFBFBFFRLL +FBBBFBFRLR +BFFFFFBLLR +BFBBBFBRRL +BFFBBFFRLR +FBBBFBBLRL +BFFFBFFRLR +BBFFFBFRLR +BFFFBFFRLL +FBBBBFFRRL +BBFFBBFRRL +BBFFBFFRRR +BBFFBFFRRL +FBBBBFBLRR +BBFFFBBRLL +FBBFBFBLRR +BFFBBFFLRL +BFBBBFBLLL +BBFFBFBRLL +FFBFFFBRLR +BBFFBBBLRL +FFBBFBBLRR +FFBBBFBLLL +BFFBFFFLRR +FFBFBFFLLR +FFBFFBBRLL +BFBBBBBLRL +BBFFFBFLRR +BFBFBFFLRR +BFFBFBFLLL +FFBFFFFRRL +FFBBBBFRLL +FBFBFBBLRL +BBFFFBFRRL +FFBFFBFRRR +FBBFFBBRLR +FBBFBFBRLL +BFBFBFBRLR +BFFBFFBLRL +BFBBFFBRLR +FBFBFFFLLR +BFFFFFFLRL +FBBFBBFLLL +FFBBBFBLRL +BFBFBBFLLL +BBFFFBFRLL +FBFBFFBLRR +FBBBFFFRLR +BFBBBFFLRL +FFBBFFBLRR +BBFFBFFLLR +FBFFBFFRRR +FBFFBBBRLR +BFBFBFBLRR +BFFBBBBRLL +FBFFFFBLRL +FBBFBFFLLR +FBFBFFFLRR +FBBBBBBRLL +FBFFBBBRRR +FBBFFBBLRR +BFFFBFBLRL +FBBFFFFRLL +BFBFFBBRRR +BBFFBBBLRR +BFBFFFBLLR +FBFBFBBRRR +FFFBBBFLLR +BFBFFFFLLL +FBFFBBBLRL +FBFBFBBLLL +FFBFFFFLRL +FBFBBBBLLR +FBFBFBFLRL +FBBBFBFRLL +FBFBFFFRRL +BBFFBBBRRL +BBFFFFBLLL +FBFFBBBRRL +BFBFFFBRRL +BBFFFFBRRL +BFBFFBBLRR +BFFBFFBRRL +FFBFBFBRRL +FFFBBFBRRR +BBFFFBBLLL +FBFBBFFRLL +BFFFBFFLLL +BFFBFBFLRL +FBBBBBFRLL +FBFBBBFLLR +FFBBFFFRRR +FBBBBBBLLL +BFBFBBFRRL +FBFBFBFRRL +BFBFFFFRLR +FFFBBFFRLR +FFBBBFFRLR +FFFBBFBRRL +FBFFFFFRLR +FBFFBBFRLR +FFBFFFBLRL +BFBFFFFLRR +FFBFFFFLLR +FFBBFFFRLR +BFBFFFBLRR +FBBBBBFRRR +BFFBBBBRRL +FFFBBFFLRL +FBFFBFBLRR +FBBFFBFRRL +FBBFBFFRLL +FFBFBBBRLL +BFFFBBBLRL +BFFBFBBRRR +FBBBBBBRLR +BFBFBBBRRR +BBFFBFFRLR +FBBBFFFRRL +FFBBBBFRRR +BFBBBFBLLR +FFFBBBFRRL +FFFBBBBRLL +FBBBFBBRLL +BFFFFFBRLR +BFBFBBBLLR +BFBFFBBLRL +FBBFFFBRRR +BFBBFBBLRR +FBBFFBFLLR +FBFBBBBRLL +BFFBBFFRLL +FFBBFBBRLR +BFBFFBFLLR +BFBBBFFLLR +FBBFBBFLLR +BFBBFBFLLR +BFFBFBFRRR +BFBFBFBLLL +FBFFFFFLLL +FFBFFFBRRL +FBFFBBFRRR +BBFFBFBRRL +FBFFBFFLRR +FBFBBFFRRR +BFFFBBBRRL +BFBFBFFLLL +BFBFFFFLLR +FBBBBBFLLL +FFBFFBBRLR +FFBBBBBLLL +BFFBBBFLRL +FFBBFBFLRL +FFFBBBFLLL +BFFFFBFLRL +FBBFFFBLLR +FBFBBBFRLR +BBFFBFBLLR +BFBBBBFRRR +FBFBFFFRLL +FBFBBFBLLL +FBFBFFFLLL +FFBFBBFLLL +BFBFFFFRRL +FFFBBFBLLR +BFFBBFBLLR +FFBFBFBLLL +FBFBBFFLLR +FFBFBFBLRR +BFFBFBFLLR +BFBBBBBRRR +FBBFFFBLRR +FBBFFFFLLL +FFBBFBFLRR +BBFFFFFLLR +BFBBFBFLLL +BFFFFBBLRL +FBFFFFBLRR +FFBBBFFLRR +FFFBBFFRLL +BFFBBBBLRR +FBFFBBBLLR +FFBFFFBRRR +FBFFFFFLRL +BFFFFBFRRL +BFFFFBFRLL +BFBFFBFLRR +BFBBBFFRLL +BFBBFBBRRR +BBFBFFFRLR +BBFFBFFLRR +FFBFBBFRLR +BFBBBBFLLL +FBFFFBBRLR +BFBBBBBRLL +BBFFBFFLLL +FBFFBFFLRL +FBFBFBBLLR +BFFBFFBLLL +FFBBBBFLRL +FBFBFFBLLR +FFFBBBFLRR +FBBBFFFRLL +BFFBFBBRLL +FFBFBBFLRR +BFBBFFFRRL +FFBBBBFLLR +FBBFFFBRLL +FBFBBBBLLL +FBFFFBFLRR +FBBBBFFLRL +BFBFFFBRRR +FBBFFBBRLL +FBFFFFBRRL diff --git a/day05/Makefile b/day05/Makefile new file mode 100644 index 0000000..dbf2996 --- /dev/null +++ b/day05/Makefile @@ -0,0 +1,25 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day05/OUTPUT b/day05/OUTPUT new file mode 100644 index 0000000..99b8f6a --- /dev/null +++ b/day05/OUTPUT @@ -0,0 +1,12 @@ +ex1.bash : lines=743 max=838 + time: 0:00.94 real, 0.62 user, 0.41 sys + +ex1-c : lines=743 max=838 + time: 0:00.00 real, 0.00 user, 0.00 sys + +ex2.bash : lines=743 seat=714 + time: 0:00.99 real, 0.65 user, 0.43 sys + +ex2-c : lines=743 seat=714 + time: 0:00.00 real, 0.00 user, 0.00 sys + diff --git a/day05/README b/day05/README new file mode 100644 index 0000000..9fafdba --- /dev/null +++ b/day05/README @@ -0,0 +1,62 @@ +--- Day 5: Binary Boarding --- + +You board your plane only to discover a new problem: you dropped your boarding pass! You aren't sure which seat is yours, and all of the flight attendants are busy with the flood of people that suddenly made it through passport control. + +You write a quick program to use your phone's camera to scan all of the nearby boarding passes (your puzzle input); perhaps you can find your seat through process of elimination. + +Instead of zones or groups, this airline uses binary space partitioning to seat people. A seat might be specified like FBFBBFFRLR, where F means "front", B means "back", L means "left", and R means "right". + +The first 7 characters will either be F or B; these specify exactly one of the 128 rows on the plane (numbered 0 through 127). Each letter tells you which half of a region the given seat is in. Start with the whole list of rows; the first letter indicates whether the seat is in the front (0 through 63) or the back (64 through 127). The next letter indicates which half of that region the seat is in, and so on until you're left with exactly one row. + +For example, consider just the first seven characters of FBFBBFFRLR: + + Start by considering the whole range, rows 0 through 127. + F means to take the lower half, keeping rows 0 through 63. + B means to take the upper half, keeping rows 32 through 63. + F means to take the lower half, keeping rows 32 through 47. + B means to take the upper half, keeping rows 40 through 47. + B keeps rows 44 through 47. + F keeps rows 44 through 45. + The final F keeps the lower of the two, row 44. + +The last three characters will be either L or R; these specify exactly one of the 8 columns of seats on the plane (numbered 0 through 7). The same process as above proceeds again, this time with only three steps. L means to keep the lower half, while R means to keep the upper half. + +For example, consider just the last 3 characters of FBFBBFFRLR: + + Start by considering the whole range, columns 0 through 7. + R means to take the upper half, keeping columns 4 through 7. + L means to take the lower half, keeping columns 4 through 5. + The final R keeps the upper of the two, column 5. + +So, decoding FBFBBFFRLR reveals that it is the seat at row 44, column 5. + +Every seat also has a unique seat ID: multiply the row by 8, then add the column. In this example, the seat has ID 44 * 8 + 5 = 357. + +Here are some other boarding passes: + + BFFFBBFRRR: row 70, column 7, seat ID 567. + FFFBBBFRRR: row 14, column 7, seat ID 119. + BBFFBBFRLL: row 102, column 4, seat ID 820. + +As a sanity check, look through your list of boarding passes. What is the highest seat ID on a boarding pass? + +Your puzzle answer was 838. +--- Part Two --- + +Ding! The "fasten seat belt" signs have turned on. Time to find your seat. + +It's a completely full flight, so your seat should be the only missing boarding pass in your list. However, there's a catch: some of the seats at the very front and back of the plane don't exist on this aircraft, so they'll be missing from your list as well. + +Your seat wasn't at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list. + +What is the ID of your seat? + +Your puzzle answer was 714. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day05/TEST.txt b/day05/TEST.txt new file mode 100644 index 0000000..afec010 --- /dev/null +++ b/day05/TEST.txt @@ -0,0 +1,5 @@ +FBFBBFFRLR +BFFFBBFRRR +FFFBBBFRRR +BBFFBBFRLL +BBBBBBBRRR diff --git a/day05/ex1-c.c b/day05/ex1-c.c new file mode 100644 index 0000000..4563852 --- /dev/null +++ b/day05/ex1-c.c @@ -0,0 +1,35 @@ +/* ex1-c: Advent2020 game, day 4/game 1 + */ + +#include +#include + +int str2int(str) + char *str; +{ + int res=0; + while (*str) { + res <<= 1; + if (*str == 'B' || *str == 'R') + res ^= 1; + str++; + } + return res; +} + +int main(ac, av) + char **av; +{ + int max=0, cur, count=0; + char rowstr[80], colstr[80], line[80]; + + while (fgets(line, sizeof line, stdin)) { + count++; + sscanf(line, "%[FB]%[RL]", rowstr, colstr); + cur=(str2int(rowstr)<<3) ^ str2int(colstr); + if (cur > max) + max=cur; + } + printf("%s : lines=%d max=%d\n", *av, count, max); + exit (0); +} diff --git a/day05/ex1.bash b/day05/ex1.bash new file mode 100755 index 0000000..017294f --- /dev/null +++ b/day05/ex1.bash @@ -0,0 +1,33 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 5/game 1. + +CMD=${0##*/} +shopt -s extglob + +function rownum() { + local val=${1%%???} + val=${val//F/0} + val=${val//B/1} + echo $((2#$val)) +} +function colnum() { + local val=${1##???????} + val=${val//L/0} + val=${val//R/1} + echo $((2#$val)) +} + +declare -i lines=0 max=0 row col res + +while read -r line; do + ((lines++)) + row=$(rownum "$line") + col=$(colnum "$line") + res=$((row * 8 + col)) + ((max < res)) && max=$res + #echo "row=$row col=$col res=$res" +done +printf "%s : lines=%d max=%d\n" "$CMD" "$lines" "$max" + +exit 0 diff --git a/day05/ex2-c.c b/day05/ex2-c.c new file mode 100644 index 0000000..2c32de3 --- /dev/null +++ b/day05/ex2-c.c @@ -0,0 +1,53 @@ +/* ex1-c: Advent2020 game, day 4/game 1 + */ + +#include +#include + +static int seats[128][8]; + +int str2int(str) + char *str; +{ + int res=0; + while (*str) { + res <<= 1; + if (*str == 'B' || *str == 'R') + res ^= 1; + str++; + } + return res; +} + +void print_seats() +{ + int i, j; + printf("seats:\n"); + for (i=0; i<128; i++) { + printf("%03d : ", i); + for (j=0; j<8; ++j) + printf("%d ", seats[i][j]); + printf("\n"); + } +} + +int main(ac, av) + char **av; +{ + int cur, count=0; + char rowstr[80], colstr[80], line[80]; + int *pseats=(int *)seats; + + while (fgets(line, sizeof line, stdin)) { + count++; + sscanf(line, "%[FB]%[RL]", rowstr, colstr); + cur=(str2int(rowstr)<<3) ^ str2int(colstr); + *(pseats+cur)=1; + } + for (; *pseats == 0; ++pseats) + ; + for (; *pseats == 1; ++pseats) + ; + printf("%s : lines=%d seat=%ld\n", *av, count, pseats-(int *)seats); + exit (0); +} diff --git a/day05/ex2.bash b/day05/ex2.bash new file mode 100755 index 0000000..21951dd --- /dev/null +++ b/day05/ex2.bash @@ -0,0 +1,40 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 5/game 2. + +CMD=${0##*/} +shopt -s extglob +declare -a seats + +function rownum() { + local val=${1%%???} + val=${val//F/0} + val=${val//B/1} + echo $((2#$val)) +} +function colnum() { + local val=${1##???????} + val=${val//L/0} + val=${val//R/1} + echo $((2#$val)) +} + +declare -i lines=0 max=0 row col res next + +while read -r line; do + ((lines++)) + row=$(rownum "$line") + col=$(colnum "$line") + res=$((row * 8 + col)) + seats[$res]=1 + #((max < res)) && max=$res + #echo "row=$row col=$col res=$res" +done + +for seat in ${!seats[@]}; do + ((next=seat+1)) + [[ -v seats[$next] ]] || break +done +printf "%s : lines=%d seat=%d\n" "$CMD" "$lines" "$next" + +exit 0 diff --git a/day06/INPUT.txt b/day06/INPUT.txt new file mode 100644 index 0000000..7c53d58 --- /dev/null +++ b/day06/INPUT.txt @@ -0,0 +1,2199 @@ +wzaopvscxknyjtiul +belforsgdhuvmwc + +kiocpzjhsetqnwbl +maciywlbpodgevujnsktrz +kpebjfchtlsonwiz +pcezwjtsinxlobk + +lrcp +cdlbepr +rlqcyutp +lcpr +lrtcpym + +gxcqbowaelfjhpknz +gflhackzwqxojeb +lfwocexqbkjhgaz + +fpivaozqnkextmbrjhycdwug +motgcrzaisqlpdwbeyhukf +hbiqcdmkpawueyztgfor +zrcehkqyoblmtgfiaupdw + +umjz +jzu +vzowkpuj +jmzau + +unzlx +lnzxu +mxluzn +nzuxli + +szkiauhyntdrbcljomqwepv +amowcpzbyukihsjtqelnd +lzctuqjabmsynphkewxido +sikblhptcoqemdanywzju +jdeiylutspabcwngfzqkhom + +zgyhwtnxjdqucbp +ptnzhudjbcxgkqf +zgqhjbdnutcmpx +gjbzhpdxanqcut + +wjkcsqlu +ucsjq + +vkzsqtunmr +ljcexadhg + +fhtrusba +svtk +pgzwytn +astb + +hvzjiwlsmnatbxgqcuke +zeaukbgtnjxslmvwiqc +tqsjxvznemblwkgcuai +atwjqckzmgvnlbuxise +mwkteqbcjasvxuizlgn + +gypubxsntl +untylgpsb +uynbgltps +nylgutpsb +unsltybpg + +qwygnalv +rxdohlnatwy + +fcytwkqjaorm +rqfcjokwtmay +cjyftkaqmrwo +tqomfrwycakj +cjktaroqmfwy + +ygnjepucrfkxq +ypnqjfgcuerxk +fyghkncqpjxuer +runfcyxgjqpek +qxjrupnygkcfe + +rneuh +rhneu +rhune + +dyct +tycd +cdty +ytdc +cdty + +hoaxpnic +zrocenxaphui +pigyovantxch +vanbocxdpihw + +nmadf +cdbykfwtim +fdms + +xrlweqhyomniupgasjb +wxlaijmhqbopnugsery +xmkuswchylpbeqirngojfa + +u +yuq + +d +d +d +dqf + +kfnbcvxtogwhsz +nctfsoezgwhkvb +kncbtvgwsozhf +kcfsobzntgrwhv +obnsfczkrvwhtg + +ektcsq +tcpqesdk +ightwqxosekj + +y +y +y + +oaqwmublkjz +qhkduvibzmfx +bomkquz +bzpuqwmko + +uybzfemjhvnp +amxynpve +kyvmenxp + +vpyznetlsow +njewpszg + +a +a +a +x +a + +ytixaokbv +vybaoxtudi +kaivoytbx +oavbxity +vytbaixo + +okdmepqj +kqodjpe +kpdeoqj +yqdpkeoj + +nqtkbvrjizuyhlwmxopscga +ojzqryxwvpkclighnmeuabts +socwtinpyakuqljzmrxvhbg +sruhgbvjmpwnaklioqyzctx +pmojxvkiwztgaslbuyhqrnc + +sruqibofpetmjxacnyw +meoxusytawnfrhqicpb + +udzjvpxcmg +vzmpdjgxuc +vdgcxujzpim +gpujzcmdvx +jcvpzxmdug + +lwke +jqa +jdp + +ebcnymdxhsgjoptvkluw +mhewbjgldvutnkxocysp +odkvwhlcupyejbsmxntg +ylhetjbxumpwdocgvkn +yvugktojibqwncmlpehdx + +wc +cw +wc +wc + +fnatmwk +vbqnoptm +zldysrjuiegcmx +fm +vmh + +e +e +e + +nd +nd +dn +tdn +nd + +bswceuimx +vfugqjk +phrbaulzsdy + +fcmpeuxkjiwtszhgnoravb +tjhbsovuzipmracnxf +zfxuvbitrcnamosjph +bafxzhonrsipvcjumt + +uopkgzec +uogckpz +czgkopu +gtuopckz + +k +k +k +k +k + +bucx +zxfu +oluinsxk +cupfxb +pxuz + +gw +wg + +nif +qby + +kazpqyrt +uocensfmvw + +vjy +srdfuy +tyz +yt +jyow + +wcn +d +m +r + +afo +pruao +afo + +jiqp +jhp +jp +pj + +rvxucn +xmurvtc +uzvmrxc +xeykobgvjpclharfu + +bc +cb +bc +bc + +hxnparv +vtqhxapn +apnvhx +dvwanihpxys + +jdszlimbca +drtmxcqlw +cmdzloub + +podyqvae +qyapveod +pvqedayo + +scmbwfuldnvkxqehjgatpry +zgltckiwjnuebahmpfqvdrsx + +hu +fkixsu + +a +a +a +a +a + +fkmupthae +vumpwh +ugxwmph + +kspnwvyr + +yxaw +xw +hzrmxbekw +wicx + +l +lz +l +l +l + +rwn +wnr + +tpankiydfghwlzv +hwnztpvbcsykgl + +hcn +cuhq +qc +uq +xsdz + +izsctumjfvyo +iuymczbtjo +ytprwciogjkmzx +iyztahnjemcdo + +kmepowc +cwmkoep +bmopkect +cjepnkmqo + +gqlxpwt +qgwtplv +lpgmwcit +twglpx +gwtphl + +uiyeqacdlwxpgt +deyxtgqaulicwp +pgawxtyiucdelq +iaepwxugtdlqyc +aewygpuilxctqd + +ksextofz +zxhesk + +zxnlhrd +hlnxdbczr +qnxfzdlhr + +foryns +rnofsy +fsyonr +rosnyf +orsnyf + +o +o +t + +raktoz +atkzo +vtkzoaf + +yianzfpwbqsvltcuhjg +tpjzhvwfibacusylnq +wdctlhzbjeqavnfiuyps + +mhexryd +yerhdmx +ehxymdr +bgmrehxyd +xreyhdm + +pebtkqchsoydz +dklqohsupwcyztxe +ckvmsqjpdhaetzofy +wsuohqckerltypdzn +lkxsyowcdzteqhp + +nqbmshlg +ebshnqla +sqngvb +nbwqsi + +sauebdjhpwtcgnlzorfkxqyvim +adpvbqlnofyxmiuzjsctwkeghr + +gy +gy + +jf +wpy + +guztycblrhfiq +bertlfcmguhv +lbytrcfk +fscbltnpxwor +lqfbjtcrdv + +egbvoxlmpczqnafyu +enxqbazvpmolyfcug +kpuezgxbqnaymvflocs +vipegmfbnocqxuzlay +mnofzbaylcqxegvpu + +wrclt +lfrcw +rcwl + +zbwvmuifpjxqnlgo +jfpwmuznqxvbgio + +baheoslg +fygoa +aygo +aog +fmoga + +ltpdcyn +ycdlnt +lchgtydn + +fvdgksnmphxzcboql +zboknxmfgdvclpq +qdlkpgcbzvmoxfnt +ciozqlvjgkwbfxdumpne + +gmlyqkjehrp +ojeizhlgpm +uhgpmflje + +atzscwnb +snahqtc + +zyjml +jryml +lymtxvod +mryil + +iudskmzex +udqkzxesmi + +qdywgafzmn +ufobhixk + +r +q + +alndhupswj +npwlua +nlueapw +npalwu +lawnpu + +obsxpclziwauf +fcsjxiblwuotzp +spcvbiwfzdxulo +ulfwpiczosbxd + +hknftpgaey +typoqgehn + +tyhzpoejxdl +xoejpdztlhyk +hreinoyjvtbdzmfqlp +otjyedpzlh +zdoywjeltshp + +n +b +b +b +b + +kvupfreobljsmw +sukjromplvfwe +rdwhpkjvqcemlosfi +pvmblwsfkexroj +eokvlrmsujwpgtf + +wiqs +l +j +j +h + +jx +jxc +jxr + +tzv +tzv + +aqe +qan +avqs +asqcm +glpayuq + +hncexlkpjbdmoftag +pfbxckmdantlogh + +ocsdtg +sdgtm +sdgt +tdsg + +jfioeacnqv +oqafnicjev +veijfnocaq + +xfmnhbpljcg +cmlfhbjxengs +xnmbjhlgfc + +vcn +kvc +cv +cv +cvm + +wodgjfls +owdgfq +niogfwtd +vcdgfwo +mgwyfod + +ozhwa +kotuedjcql + +kw +kw +kw +kw + +sufmi +rmsuf +jumfpeogs +tmfsbu + +siadk +mijcgosk +ski +isk + +kplwzh +hzpkwl +lwzkph +wzhkpl + +yot +y +sy + +lz +fl +lf + +kexljydcihonwtgumfsrz +iuywlcxomsedjnhztgrf +wtorjgfuxsmlyecdnhbzi +tczliugorhdjsfyenamxw +zklniaxyujdcwtrhgsmfoe + +juzqtibn +qijvuzgtnf +nqxziutj +qitonjzw +zqtnjgbis + +tuqygv +gutvqy +tvuqyg + +plzqmthysawexgvnr +ptvxasryleqmgznwh +htlgnqexsmwpvyiraz + +hizrfynjmqceg +bplsuoydv + +bptjc +jcbtp + +qedcultavjwizxnkob +bokndqcuivwtxlaezfj +qazlvsjexikwbcudont + +vgihxmquktcrywpzj +kpujwxfrgqmzlyihct +domjarxitczhwnqugkyp + +wilctjzq +ouwylicgkxvsha +npbwlric +liwqc +qclpidbnw + +gbvpdkxclfejtaoq +elabncgfkjdvxop + +vfi +vpif +ivf +vif + +hvjzxebtqsfwpia +gvaxyiwfkcru + +hzrg +lz +fqz +az +qabzpyi + +redfpogvxc +zeyh +ainye +tlyzeu +alsemu + +gwph +gpw +ywgp +gwp + +fjkhnmuwtxialyo +tjuolfnkamwxy + +umrdifo +mrfodtuix +jkruohmfdi +ufikzrmdo + +blioreudh +brfiaheulndgy + +ywstjalubez +wbsautezj +usebjtawz + +gatfpyjoznwh +oztwypanjghf +nzgojwfphyta +yjhpgfotwzna + +metqoknri +kqmtnwerofi +qtkohjnmeir +qmkrefiton + +xdgrbua +bwdukoghcrxln +euvxgrdbi +vusgbrxd + +rovayxgkqshzi +olgxhutwzsikbecqdajyp +ksrahngzyiqxo +vosgzynxqaifkh +soqzigyxakh + +fopjqgdmhtbv +qbvl +lbvqy +buwvq +bcuqv + +feyvazrnows +waynzeorfs +sorafznewy + +jpohmywkvqextsd +ahtvoxpumsedq +qbgtpoecsnlmxfirvh + +uic +ij +ixgp +alzehimdvybf +tkgi + +fpjubodeznsqyxkwami +anuymqskowpedbifzjx +oubjpwznfdxemiyqska + +uhzd +uzhd +dhuz +uhdgz +zdhu + +bsyituhq +rbjahg + +xszkrj +znhjrsx +jzrptkxs +mlzrjbxsyoq +unjxrzsk + +rmyilhnkdqjpfceogzu +oegpdtmkwuvfal +budoxkfpglem + +xekshpotfzlcmuiwgj +sauekziobrthjgxfcpwl +eifjhkwgpcltsxozvu +okenjhgscxqfuzwtpl +uhcjxzowitsdlegpkf + +kzwdconulqtrvihsgp +qwmhutnpyrsxbvgzokdeicl + +x +x +i + +umvbcenh +egun +tnue +ktuen +wgneu + +wysximljeua +uiwxleymsja +iemlyxjsawu + +r +wt + +hcvos +xv + +enzmuaxc +jcdnvzam +nltvobeaym +mfnahipgswr + +hsnmlpkujoigaftc +fslotjmpghacnkiu +cbfjlkohumapsngt +lsutoajknfpgchmi + +xisn +insxw +nixs + +eunflkcda +gcnxefzlud +lspcndufea +onwjutcefdlm + +hrivmxpatkgd +hdmxigtprakv +dvpmgcrthkiax +gkdraxithpmv +rvmutdbixpklehga + +elmdxqwn +mqlwedn +mwnleqd +qldwnem +qemlwdn + +wpjfrlqbs +vwqjlsfbr +srqwbjflk +fpjlqwsrb + +u +j +l +w +l + +kr +rk +kr +rkd +rk + +irpxjsmqgf +bhqpendrjfc +pmwryfjql +mpfqjryux + +kjgheiqcox +iklufxgcjeh +gynvizspjhmebakxt + +udwlimhqsorbpeynctva +qzxobjpsckyfmtr + +xqyjwueda +juehakzxygq +nexjlqumsyra + +mvsyaec +xalsmwvyr +msyuav +awsmvy + +tyfrcibenxdvgum +diptcfwuroegnlvx + +c +i +g +arelmvpoz +n + +q +pq +q +asq + +oxrchtjsekpw +twoesdckuhnpxml +xkovyetpwschai +tsjwecqzxhgfopk +fspcxrtdoewhk + +urjh +njd + +ygo +syog +gfoyr + +lrvkw +dwarkyg +cphkiqfjsut +xk +vwk + +chlog +wughvb +cngh +hlgc + +hlu +luh + +tgfsbj +btgjf + +yflqdgp +dqpflg +dlgpfq +qgdlfp +lfgdpumq + +dybxtrizvhcksalqpungfwmeoj +lpotsjiaydfbqwzruxhemcnvgk +qletwyopaivjkgcbszrudmxfnh + +rowgvahemlyjkbupxct +jrewbpmktlvxaouhcy + +soerhzknbijd +oekgznidsrhj +oihrkaduezsnjf + +axthin +ojhtxa +tnahxv + +t +f +cv + +yjkatqum +eksumriab +wksmhvuaeb + +wz +w +wfb + +ruvglckjpomqthy +afjbuxevlqiwog + +zpyfolacrnqgvhksmjdeuw +ebctoamjkusdrnpyxwih + +fxs +sfirlxm +swfx + +imftqohzgjcpdlsrebxuv +agptlnyxujfzwkoqmdh + +mkbqsgaxojivpyrzwtn +svhgocwbazkyqtxrfnlmu +nozrjkdabxwitqmvegys + +tuzmyvqgnsekbwo +vmqgbozknywetus +ymsnwuqoezktgvb +euzowmbqtskgynv +uzesoybkngwmqtv + +khgx +xhz +aclrne + +kq +qk +qk + +i +gmf +dh +owqjb + +pstycjadezlbnqwxrig +rbhdpqysicgnlwa +ryhibaqwdclpngs +npdcyqsbrhawiglko +cbsdiwpnqgylra + +quvsagtxw +uatvcx +vutax + +ndbrlpzxasmto +sdbxjyrofatqlpmnz +bolmatxdszpnr +wplobctzsmxdnra + +boxmgljp +xfap + +cndeqvy +mzneys +eyn +sieyn +eynm + +kbuwp +bqpkh + +ljngohfvtpdmw +iwctogfnmvuzlhd + +napvy +gaczoidwybm +ysaet + +wrnixgyv +wnrxgvyi +vxirywgn +iwryxvng +gvwinxyr + +ygtbiqfuornlx +ieuwpgzxhbo +vubi + +ohxbkqgsj +qalpsfmiwtzyrdc + +fyakzqidcpswn +qwkizfdpyacn + +lxf +ifxl +njuxfl + +yxqzebvsgpwl +mnjcftohyi +adycurkmo + +nxdfptvebg +swjzqh +kwyasj +oljz + +lrzunheqgjvcs +kiqshjmgdvuz + +feyljcgrqmsdbz +qsgnumzhiayfbcle +pcwfstmkvgeqloxy + +qnwcftx +hyewofcl +cwhfi +clfehw + +kjuebdvpgqs +ahpubjyske +zksejxbuwp +juboekswp + +u +bcytf + +rpvshwmn +djnshvmwr +wrshnvm + +itpduvwechnrk +ntirkpvdelcuhws +vrcktepindwuqhf + +q +q +q + +mzhrygf +zhfygmr +msofhrygz +rqfymhzg +ygfmhqrz + +nwbpshv +twspladobu +hzinpwbqs + +c +c +xecuy +c +c + +y +y +o +y + +qauijgxfhbztlpvecrk +ljhcamkegxbqyptnzrfoviu +ejibuzlprhtcxaqfgvk +biczvdhgqlxrktjafuep +hpsxgwujqakbrltzevcfi + +cmfryo +froyc +dcofgye + +dcrbjnza +jdirzacb +bzrdacj +cbadrzj + +fnjryzvtxlsdioepm +ldxfzcjtovyisepbmrn +mtoxdilwfrvjespyzn + +omqxjypgin +bcrdezl +vflwkrzt + +dkabj +ntwpy +jb + +krjawyexhnlmcspizoufgt +ryebdifmlhwxgasvkoqjztcn + +lrctmfu +clzrmtfu + +ivagzecmwyl +xgkrojtfc + +bsalmjthvwgxirpqcnyue +ymtqgcsjrxnvaepfhwiolb + +tgwkeapzjo +oewgpzjkati +hejoalxzmgprkwb +nojwgazkpet +asoedjkzgpw + +zpjncustlhb +nswvlbtzphcjf +pzjlnebcmtfhs +cljbzfinthkps +aoplbczxrstjhyd + +ulvwcxg +bsvqwelud +dmxwioulv +nuwqvlb +ulwvpjft + +fmlvprndhxa +hmapdxvrnfl +npavmrgfxhdl +mhlanxrdpvf + +vfgwzy +yjgfzqxovu +wzbyfgvj +rvfkpdslzthngmyice +qfyzxavg + +hzfjbs +sjhzb +nhsjmzgw + +gmtvalyn +igtmvlyn +ymgtvn +gueftonmvxcy + +j +jge +jm +qwbj +j + +oftpvnykgdxrels +nrvksolftepdxy +xopknvbwltdeysrf +dkexlnhprtysfov + +gwcvu +wuec +cuw +wcvu +cuw + +cndywt +ntdwcy +cqtyngd + +noayvkcdsu +yaedfcjozs + +rzfhlitjvwdnp +hzxntilvwfprjd +ljinfmrdtpvwzh + +zylgudcfbxprwaqokmtjnsi +bosdnqlyjxgwkmrutcifpaz +kqwuipcfsdnzagojrxymbtl +wbixmynrjkgoaztslupqfcd +bnearosgumfildkjtcyxpqzw + +fherd +dfreh +hredf +dfrhe + +aysber +se +sze +seo + +cjofniwx +zxieaocf +uipxrgodcf +bsjtveoizwfxc +jfcoxinzt + +wzoegvhldfibmcj +ohurkdjcvifbazew +sxqzjwioefhbdvc + +ifrstw +btkqyfscx +lgvedonzjphu + +buw +wubhop +zrfwsbu +ucwhb +wbcu + +bdpn +wdpnb +dpbn +bdpn +padnb + +ukgziahpfyq +etvsrnjilx + +w +w +w + +kdjrc +kjcd +cqpjidh +tjdmc + +fihjqetkprmbgzl +hgjrtqiefompbzkld +amqipgtlebkjrfzh +elgqwbmpkjvzfihrt +mfjcpszthlirkbeqg + +vpfmytuob +bftvyoup +zobynvutpf +uypbftov +ubotpfyv + +t +k +k +t +po + +he +eh +he +he +he + +jwkorfnpqhlxvbziedcasm +vzsfxlkqarmhnjpidcboe +lpzkacexvthbfrojsinqmd +mcnzqiekusafdpvoylxbrhj + +doqcnfpshrgwxkjubyval +bdjuhgnlvpkcrfawqsyx +pnasqwryjfvduhbtcklxg + +pyba +bacs +autxkrgm +bwfa +eiola + +bhfrtklucjpinqoexmdgz +orchqmilekjbupzfgdnt +klhbtnzuomxegdivcjqfp +itfgdqualjokhnbspzemc + +kjz +jkoz +kzj +zkj + +w +v + +qfytlkjconpsw +ltfpyjcskqown +wqsfkcoyjpltn +nckhwtpofjyslq + +bvsqmnyjkur +qrukybsjvmn +bnkyvusrmqj +ykqumnrsvjb +bjvyursmnkq + +ykmabugfjtqdwpevrzx +maezkwtbvxdqupojfyg +mqyvxasgkezudpwtjfb +fumzpyxtdwqjgabveks + +wcntqs +sntbcwq + +tywbgnf +gbuynfcrt +sygnrufbt +gabfiznmhtpy +ltbfngy + +ngkfqmv +gnqavfm +qgmnavf +qgmvfn +gvmqnf + +w +a +id +uj + +zvqypkadneblrf +vaeqpldzkfrbny +friaqcvdsexyhbnlp +ezflrdqkbnypva + +tlhucpwyzrex +lcpwrhqe +pkehrwlc +ewcpfhrl +chlpwerm + +wrtljgy +ygqjlw + +jvygmx +qxmyg +myxgqr +gmyx +xmgqy + +nvmufjzsb +amnzthf + +ujgebphztf +hezupfjbt +zjxfubelsphdt +pthbzefju +bzfuhpjte + +xhfj +fhgdx +uifoxqwh +hnexaylbfk +fphux + +jiqhofdlwkgas +wftrvupnze +bwfmu +muenywfct + +mzrf +gkxsqcwo + +uaewm +ewmua +muaew +umwea +awmue + +njduohc +hndcjou +chndoju + +sqrdgapl +dqgpasrl +gdqplsra + +aunzfm +munah + +paqvw +qp +fqs +vpq +qwp + +gfmbcvriax +dlcmbwvfeyp +bixcrmvaf + +uczi +bzi +izwrb +wiz +zi + +s +j + +khqea +qehxka + +mj +dj +j + +uibwnrakt +torn +rtevnlh + +ghwzt +tghz +gzht +zvgcuqh +hzg + +t +t +t +mt +t + +cszjtfdulmprbawqkexv +fbwnhigouy + +eq +feq +eqo + +tyvjnf +tfnsvj +bftngjkv + +bpxwhjcmogyq +tnfezpk + +as +as +as +as + +ybqntwcfld +qytlfbdcwn +wtcqdbfnyl +ltywfcbdqn + +fzshuktvwp +ukrbmxvyelindgq + +fjoweph +tbvfmscxe +aofzek + +axjyiwok +mviqawyxutnjbd +xawiyjc +jhwziyxa + +xgwq +mohv +w +ertbzsjk +udf + +crozvbt +btzvroc + +tvnqzmiouaeslxyw +taymuelowxqsviz + +jv +xjwv + +npohlyejtkbuvca +ytlokvaebhucjn +kutvyngeclaohbj + +nbvwheisoxpa +evlntxsmowighb +kwnaviojeshxb +owvphiexbfns +jwbnheyqvsxio + +fkvcteqsgd +bzcftexmvwohp + +srwf +frnwsj +swafr +rswf +fwrs + +iovjntkewabzumpxryqdfcgh +gskjxpotluzqycrifwbdne + +luokigf +nuherolcyiqxam +ugiobls +lwtduoi +ligwou + +gbfiqhdmpwjo +fwdqmbpjhig +bwjdrqhmfpaig + +ulzyiqamhxvpgbj +ftdrcnewhgszp + +oakzwdjupviqn +rgdelufhymik + +clxpqaryjkt +qycujtlpxa +xulpctqjya +ucjlatypqx + +zckf +fvek + +zncuxhaeli +nalhizkcuex +xaceinzulh +acuixzelhn +aczqnxhulie + +wrzuljei +lnremi +hrikyacgf + +odnqfwpugy +ptlwnydoqhmf +fyovwdpqn +oznwbfpykdqecs + +gchqvjlypom +kcwtvxdoiu +cvwzfkoe + +xw +wmzx + +mfihkroznydqp +kfpzqdinhrovym +hnrzqpyfdmiok +rtqdphyfonizmk +fimnyphzdrkqo + +nsbiyukormjhdclvaptw +bcrsitpxdklunmajvhwyo +dktbwvmfyrlhisjnacuop +ztcdupvhgjaboqinsylrmkw +clovapfbyhsukwmdtrjni + +bgq +gq +egqk +qxg +wfjulqgzo + +foc +ocf +fco +vofc +fco + +oitbdfympxjw +jfbowrptimh +ftoimpbwj +ktufiojpwbm +yiwbjpmfout + +adznvugpirxyfwkeml +uyefgikmdxrznvwap +gwxdmkyiavrpfnzeu + +mi +mi +mi +im +mi + +redwqtcgofk +apuhyrsdkbf +kfsrnuvmhbxd +rijdfk + +ekxfaqmhbptj +efayqhb +wqfuhabecy +qbfheac +eafbuhoq + +yxzarums +yzqforucaxs + +cwlqfxdrmp +dxipzcambwoveglk +twhmqspdcxl +cjwxpdsumnrfly + +zxdgqawjntukphsfbievrloc +twslhaqxoenmzrfybckdijv + +abzlckqrnsgfijdtvy +cgdteqlkbzinyrvasjf +dikaynsgrflzjcqbvt + +kptwqavin +wkviqxptn +swpvmgnqbiktl +qpvdtkuiwn +vrjikpytnqewo + +evjqxlktmroh +tjmelkoxrq +jlekomtqxr +molerxtkjq +njxkltqermo + +wkacmijsrponf +rwvpcsu +wzyplbstrc +cdhwylserpx + +jocqfpgmy +qjfpgmc +cfpqjmg +zcqjhfpgme +gjcmfqp + +evchkgpdqourmtlzbs +plvdteqmjzurk + +fgwbxpihnrz +cdktfvmljbeiwr + +mhdcqiaswfpeyogjkbtzvxn +xdyifpqzevknmcoathjsgwb +ndvlyzretgkqbswmpxcjiafho +mstqzycowgjhbnaxepvkidf +aygkezvcxowbjfqithmnspd + +qdmetvs +dmslt + +vaxsreikfulbohwc +avxeiolbkhcswrfu + +wiukdyzvenogj +nouyvkdrwagij +yewvhtjkgndqoiu +dnvfswojigkuey + +gnuatzydv +amzvnutgy +cvuengaywtz +zmdgvpyntaohu +oayngzstxfhvu + +jed +jefn +mjkch + +uojxteazifrsch +kaurlehco +ceuaohr +chkauwero +hacuekrno + +ygopsthm +fzduxalqjk + +hkdu +khdu +dhpuk +duhk +hudk + +mtoqrcgzxuavek +mntwviorack +frtmkyaocv + +pstolmvrqkwxz +rsipltjxqnmagvwuk +kspmrxtlyqebwv +twqmlexkprvsf + +lacfoveut +ocafve +acvjofbresx + +ohgclnjdz +ejsyzdolg +riqbwtkjfdgl +zdgjyvxhl +jldaug + +weqdtuvmfnygaskb +lsugyqfvnkebmj + +zdcpefjvaly +azcjfvdelpy +tayfqbdzlcepjvk +jedazcvfply +zdplceafyjv + +jw +j +chj + +cda +ja +yaxn +ja + +cfrqx +vlju +v +untv +uy + +cowiekfanus +wjgdupicnsfr + +sh +foywprquj + +b +u + +dhezbkx +nwyvlisjpr +jqcotumgfa + +eoh +oe +oe +xoe + +cjdhmxuyg +gjmxhdyuc +huedcxymgj +dugjxyhcm + +srctb +crstb +bvlezstprc +trbcs +rctsbj + +fbdvkeltq +lotm +nojtl +tl +rtl + +nryhkvetf +xkhrtnfycv +rhtvfynk +rfnvhtky + +qabfkzotdyjieghumplxs +sqtuhaxjgdkfliymeozbpv +lqgpjmeofdkaxuhtsyizb +usbyhegwtzlpkxidjqfmoa +pyjdkigtbafqohmsxuelz + +zolurkepnsm +ymplgvtrond + +skgedcfiqz +gfqzsckdei +ckgsdeqzif + +uazvbnlgtiscdmfjreq +iszgbjrnqfdtveauclm +nsilqzuamtgjcbrdfev +zmquiblfsgrvjetdcan + +faghxbimzvkcqsy +ikzagfmxbqvycsh +fbsxakqcghymviz + +ztvufgy +vzftgey + +g +g +g +kg +g + +cdihy +tsb + +ibl +l + +cpdatberqxhugms +rgaxceqptsmhdbu +emcrsdbhtxqapgu +drechgxtmapbqus +xqdpcmshbgeatru + +f +poj +f +z +u + +ezagw +usolp + +jagq +qgajs +gjasqk +jqagm + +josgetlqimcdxruyazvf +riclqagoskmwuyxdetpfjz +surlmyegxdnactoqzifj +lcydfrxusmvqjagzbeiot +lcrsoyavzubgimfeqxtdj + +qevptljankmhs +qmjukehpsatvo +vzmsaqgwetjphkr + +e +s +m +iaxgyd + +isn +nis +uisn +nis +sni + +bvjza +bv +bdv +dvb + +betqjy +nqbetj +tjvqe +hqkraxcp + +dbhzkpt +pzdhtk +thdkzp +pzthkd + +gyrfsaox +xafgkysh +sgxfapy +gmjuitsbcafyxq +xsgafy + +mvenpfxkwc +wpstxfev + +rftiyogqvue +gucfjnskpxzqoba + +htoyrpesdvnbg +rohysvgpetn + +yxntbv +vbxynt +vbyuxqn +ynbfxtav +myxnbdv + +v +v +vs +fv + +exodvrya +vomacygxduis +wajloydxvp +fyxokvadj + +qxipfbwly +wpxtlfcibyq +pylqfibwx +lxpyqbifw + +bydioqrcftehkaznlvgsx +ypklfesavizqxgohmtnbcrd +dagnriqsexyvflcbtkohz + +uhsjxyfniqbvw +auzbviynqgsjxwf +vfjsbqxurwadinlyt +yevixlqfnsbojwu + +y +y +y +zh +d + +etyjmxdwkfosbqzhigrpacuvn +uktqciyfbnrosaedlzgpwhjvmx + +w +wim +w +w + +zlkgwbseno +zqngksb +zgnbsk +gobztsnqkp +kcainfghdxzyubmvsjr + +qywhjzxlarbutdnvimc +vrbyjhtqlxmcnzui +tljuhyrnqbpzvxcm +tezubhyjxnqlfrvmc +quhxlzrvbmnctjy + +mwqlo +xqonftwmacj +gwrodqum +imwoqre + +q +q +zq + +tgnvlhc +nhcvtgl +clvhgtn +chvlntg +cnhvdgmbftlq + +pmfreuoyzdlhsgvcwb +exahjnytqcgk + +ayglbtwcxiopqh +ifxvqhtpbyowa +uqyhwaimtfkbxvpjdo + +mcwt +tdrckp +ct + +hskjp +fihpjksl + +rlvixasmotuqcye +tcyvolauqxsmeri +tpsvmceoailuyxrq + +ckvego +ecgokv + +bu +b +mjb +by +b + +odsbf +odbfs +fsdbo +bsdfo +obsfd + +tofgjlhuvmyzcnd +uyljsxtdpbewoqnvg + +bcfgaorvwqnmdup +rcvxaunwmdpszjfqgyob +fcrmabgqdwonpvue + +z +h + +cgbpk +uyzbop + +ptzqufrekvilxwmgn +qigueptylvckrnxzfhmd +nrxlezgmqoivkfupt +bpexuzmltqifgvrkns +aeglzqrbotpkvximfjnu + +cradozehb +dhslfoqrezca + +mpzkgcnbot +cemogtbpzn +lozwngjtbpcm +znlwpbgtmco + +lodrywvcxjgp +jxgyclpwrvdo +xrcpljogyvdw +pygvwrodcjxl + +bfrmhpiuwxznslcdqke +wikulxhrfdnbsmeqzc +dnhurewbcxsqzifkml +mibcendxwshkulrzqf +frnkcszlqmwbxiudeh + +pny +ypd +pyg + +msgwzuoydtaxkpqhbj +aknbtivsjgupozme + +uizgp +gpizau +gpzxu +aupzgi + +zkohbpscea +losacbgm +bogytalcs +owtysabnc + +swjkz +kszjw +zksqwj + +m +mdtqyw +bsnlkfjprv +demi +itdao + +gkwqdnaosmvyrbz +brpomnqgk +qotikclbrnemg +rkgqomnpb + +pyoclwjxrmibhgaqu +laymqhioubrpgcwxj +qbhuypxogwrjlamci +muicbghjwoyplqarx + +xfut +utfx +vfztuhxd +tuyxf +uxtf + +foabejk +wugejvkoca +qjazoxli +ahojt + +pqzk +zrkp +adihs + +xqcigryhkevpfmutwanlds +ecdqztaxonbiprugfmk +rqnpatukmefgoxdic + +ewtfcyagsizj +wyfgtviej +stejyfigrw +ctgajeyfwki +jdbeoyqhitufglpnxw + +krmsglcbi +kgbmilc +blkgicm +inxkcqglohwmb +kiblcmg + +tubipv +ejwrgfqci +nishoxzp + +hoqxjktugmrclzywedi +utgiceynvphszkmodrl +cdbihmzolufgea + +ichtmwx +cwmuvixtb + +qihgfsxnydtwcrkoeamupzvb +goizemfwhaxcdtknubypqsrv +ovshacfrekugzbwipymtxqnd +rqgntzkwxihovbedpsfumacy +oryitqesazgnckxhdvfpwmub + +qafihpdwy +saikmrbxecjgnvoy + +devizumtxonjhrkpq +zidmnptujoqhkvxre +gqjmkuohnrtxziedvp +pqoixumtdvrzjkhnbe + +fmtbrhsynpkxu +npsmubhyktfrx +sbhyxkumnprft +nhpusmfkbrxty + +je +x + +dagmxqbfvz +sxkur +xkptl +jixu + +e +e +hits +q + +tko +qtkhoy +topk +otk + +r +erh +r +r +r + +agrkjyfcw +ktryaw +whyirak + +pgzhswrvxumqlai +skgvdxwp +pgvjxdnws +wpnkgjxvs + +risotwzapm +eytfvlx + +ocdw +dowlc + +mlspy +ylpsmn + +rxf +xfr + +jaecdxiptnzrlomgwuqyhvf +mexsutcqaviywjzhgnropdl +imeufcjtvwyonxpzardlgqh + +dxqtwsvaernygbu +unceigjraxzbqwkvd + +skogcitreyx +xewqysmchvikduzj +ycxebsrlki + +gqdyaw +skxclpvq + +uja +kaju +jau + +xnjhqlyvgod +odlvnyxf +yndolkxv +vnldxyo + +ksmuqpily +oanrztfwjxvu + +gulobnahfmjdpkrqvecsz +lnymgsxrcpheqobjdavkf + +mgtdqzrebiswvcfnlopaukh +nbomqafeulrtvigdcpwszk +ginlmsraopdukxftbewqzcv +iamtqzvowldfnuscpkrgeb + +ipkonymxsvhjcu +iypevgxdhcnl +xhcinvply + +d +qbwhp +r +r + +hrdmq +qxld +hcqd +dqfc + +pqrgvxtzcbmws +xomdqytbiwzr + +eo +eo +oe +oe +oe + +dwgmk +mrwjdgk +gwmdk + +nspfzvdhocirwyebma +bfdcnzoswyermahivp +minwdycaephzsbvrfo + +iydcwfzaxlnret +qlusgcyk +lgcyjs + +tmjsecngzxfiq +vhzbyoedfu + +o +s +s + +ubfrgyp +vybfpkrg +erfybg +nbgyrf +zfwrgby + +gjnbfartdoiem +ajbtgermdqnfio +mfijcthadgnobuew diff --git a/day06/Makefile b/day06/Makefile new file mode 100644 index 0000000..7245945 --- /dev/null +++ b/day06/Makefile @@ -0,0 +1,41 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +compile: ex1-c ex2-c + +ex1: compile + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-bis.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: compile + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-bis.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +bash: + @$(TIME) $(MAKE) --no-print-directory bash-run + +c: compile + @$(TIME) $(MAKE) --no-print-directory c-run + +bash-run: + @ex1.bash < $(INPUT) 2>&1 + @ex2.bash < $(INPUT) 2>&1 + +c-run: + @ex1-c < $(INPUT) 2>&1 + @ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day06/OUTPUT b/day06/OUTPUT new file mode 100644 index 0000000..d0eb3dd --- /dev/null +++ b/day06/OUTPUT @@ -0,0 +1,24 @@ +ex1.bash : groups=484 count=6585 + time: 0:01.42 real, 1.26 user, 0.20 sys + context-switch: 135+1393, page-faults: 0+59578 + +ex1-bis.bash : groups=484 count=6585 + time: 0:01.22 real, 1.09 user, 0.16 sys + context-switch: 145+1311, page-faults: 0+60076 + +ex1-c : groups=484 count=6585 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+71 + +ex2.bash : groups=484 count=3276 + time: 0:01.35 real, 1.18 user, 0.20 sys + context-switch: 139+1360, page-faults: 0+59397 + +ex2-bis.bash : groups=484 count=3276 + time: 0:01.30 real, 1.11 user, 0.22 sys + context-switch: 112+1356, page-faults: 0+57986 + +ex2-c : groups=484 count=3276 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+72 + diff --git a/day06/README b/day06/README new file mode 100644 index 0000000..d1330b6 --- /dev/null +++ b/day06/README @@ -0,0 +1,90 @@ +--- Day 6: Custom Customs --- + +As your flight approaches the regional airport where you'll switch to a much larger plane, customs declaration forms are distributed to the passengers. + +The form asks a series of 26 yes-or-no questions marked a through z. All you need to do is identify the questions for which anyone in your group answers "yes". Since your group is just you, this doesn't take very long. + +However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer "yes", one per line. For example: + +abcx +abcy +abcz + +In this group, there are 6 questions to which anyone answered "yes": a, b, c, x, y, and z. (Duplicate answers to the same question don't count extra; each question counts at most once.) + +Another group asks for your help, then another, and eventually you've collected answers from every group on the plane (your puzzle input). Each group's answers are separated by a blank line, and within each group, each person's answers are on a single line. For example: + +abc + +a +b +c + +ab +ac + +a +a +a +a + +b + +This list represents answers from five groups: + + The first group contains one person who answered "yes" to 3 questions: a, b, and c. + The second group contains three people; combined, they answered "yes" to 3 questions: a, b, and c. + The third group contains two people; combined, they answered "yes" to 3 questions: a, b, and c. + The fourth group contains four people; combined, they answered "yes" to only 1 question, a. + The last group contains one person who answered "yes" to only 1 question, b. + +In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11. + +For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts? + +Your puzzle answer was 6585. +--- Part Two --- + +As you finish the last group's customs declaration, you notice that you misread one word in the instructions: + +You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions to which everyone answered "yes"! + +Using the same example as above: + +abc + +a +b +c + +ab +ac + +a +a +a +a + +b + +This list represents answers from five groups: + + In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c. + In the second group, there is no question to which everyone answered "yes". + In the third group, everyone answered yes to only 1 question, a. Since some people did not answer "yes" to b or c, they don't count. + In the fourth group, everyone answered yes to only 1 question, a. + In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b. + +In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6. + +For each group, count the number of questions to which everyone answered "yes". What is the sum of those counts? + +Your puzzle answer was 3276. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. \ No newline at end of file diff --git a/day06/TEST.txt b/day06/TEST.txt new file mode 100644 index 0000000..0f5b3bc --- /dev/null +++ b/day06/TEST.txt @@ -0,0 +1,15 @@ +abc + +a +b +c + +ab +ac + +a +a +a +a + +b diff --git a/day06/bash.sh b/day06/bash.sh new file mode 100755 index 0000000..4cdc037 --- /dev/null +++ b/day06/bash.sh @@ -0,0 +1,4 @@ +#! /bin/bash +PATH=.:$PATH +IN="$1" +time { ex1.bash < $IN; ex2.bash < $IN; } 2>&1 diff --git a/day06/c.sh b/day06/c.sh new file mode 100755 index 0000000..0b5bd2d --- /dev/null +++ b/day06/c.sh @@ -0,0 +1,5 @@ +#! /bin/bash +PATH=.:$PATH +IN="$1" +echo IN=$IN +time for i in ex1-c ex2-c; do "$i" < "$IN"; done 2>&1 diff --git a/day06/ex1-bis.bash b/day06/ex1-bis.bash new file mode 100755 index 0000000..8924ee1 --- /dev/null +++ b/day06/ex1-bis.bash @@ -0,0 +1,73 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 6/game 1. + +CMD=${0##*/} +shopt -s extglob +declare -a Q=() + +printf -v AC "%d" "'a" +printf -v ZC "%d" "'z" +(( NC = ZC-AC+1 )) + +function reset_array() { + Q=() + #for (( elt=AC; elt<=ZC; ++elt )); do + # Q[$elt]=0 + #done +} +function count_array() { + #local -i count + echo ${#Q[@]} + #for ${!Q[@]}; do + # ((Q[elt] > 0)) && ((count++)) + # #printf "%d=%d (%d)" "$elt" "${Q[$elt]}" "$count" + #done + #echo $count +} + +function set_array() { + local str="$1" + local c asc + + for ((i=0; i<${#str}; ++i)); do + c=${str:$i:1} + if [[ "$c" =~ [a-z] ]]; then + printf -v asc "%d" "'$c" + Q[$asc]=1 + fi + done +} + +declare -i people=0 group=0 count=0 +declare line grouped +function calcgroup() { + set_array "$grouped" + ((group++)) + i=$(count_array) + #printf "c=%d" $i + ((count += i)) + ((people=0)) + grouped="" + reset_array +} + +while read -r line; do + if [[ -z "$line" ]]; then + #echo "G=$grouped" + #set_array "$grouped" + calcgroup + #for (( elt=AC; elt<=ZC; ++elt )); do + #printf "%s=%s " "$elt" "${Q[$elt]}" + #done + #echo + continue + fi + ((people++)) + grouped+="$line" +done +calcgroup + +printf "%s : groups=%d count=%d\n" "$CMD" $group $count + +exit 0 diff --git a/day06/ex1-c.c b/day06/ex1-c.c new file mode 100644 index 0000000..aab77b6 --- /dev/null +++ b/day06/ex1-c.c @@ -0,0 +1,78 @@ +/* ex1-c: Advent2020 game, day 4/game 1 + */ + +#include +#include + +#define QSIZE ('z'-'a'+1) +static int Q[QSIZE]; + +void reset_array(){ + int i; + for (i=0; i= 'a' && *str <= 'z') + Q[*str-'a']++; + } +} + +int count_array () +{ + int count=0, i; + for (i=0; i 0) + count++; + } + return count; +} + +void calcgroup(group, count, people) + int *group, *count, *people; +{ + (*group)++; + *count += count_array(); + *people=0; + reset_array(); +} + +int main(ac, av) + char **av; +{ + int people=0, group=0, count=0; + char line[80]; + + while (fgets(line, sizeof line, stdin)) { + if (*line == '\n') { + calcgroup(&group, &count, &people); + continue; + } + people++; + set_array(line); + //sscanf(line, "%[FB]%[RL]", rowstr, colstr); + //cur=(str2int(rowstr)<<3) ^ str2int(colstr); + // *(pseats+cur)=1; + } + //for (; *pseats == 0; ++pseats) + // ; + //for (; *pseats == 1; ++pseats) + // ; + //printf("%s : lines=%d seat=%ld\n", *av, count, pseats-(int *)seats); + calcgroup(&group, &count, &people); + printf("%s : groups=%d count=%d\n", *av, group, count); + exit (0); +} diff --git a/day06/ex1.bash b/day06/ex1.bash new file mode 100755 index 0000000..0ec950e --- /dev/null +++ b/day06/ex1.bash @@ -0,0 +1,55 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 6/game 1. + +CMD=${0##*/} +shopt -s extglob +declare -A Q=() + +function reset_array() { + for elt in {a..z}; do + Q[$elt]=0 + done +} +function count_array() { + local -i count + for elt in "${!Q[@]}"; do + (( Q[$elt] > 0 )) && ((count++)) + done + echo $count +} + +function set_array() { + local str="$1" + local c + + for ((i=0; i<${#str}; ++i)); do + c=${str:$i:1} + if [[ "$c" =~ [a-z] ]]; then + Q[$c]=1 + fi + done +} + +declare -i people=0 group=0 count=0 + +function calcgroup() { + ((group++)) + ((count += $(count_array))) + ((people=0)) + reset_array +} + +while read -r line; do + if [[ -z "$line" ]]; then + calcgroup + continue + fi + ((people++)) + set_array "$line" +done +calcgroup + +printf "%s : groups=%d count=%d\n" "$CMD" $group $count + +exit 0 diff --git a/day06/ex2-bis.bash b/day06/ex2-bis.bash new file mode 100755 index 0000000..f64c1f3 --- /dev/null +++ b/day06/ex2-bis.bash @@ -0,0 +1,59 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 6/game 1. + +CMD=${0##*/} +shopt -s extglob +declare -A Q=() + +function reset_array() { + Q=() + #for elt in {a..z}; do + # Q[$elt]=0 + #done +} +function count_array() { + local -i count=0 people="$1" + for elt in "${!Q[@]}"; do + if (( Q[$elt] == people )); then + ((count++)) + fi + done + echo $count +} +function set_array() { + local str="$1" + local c + + for ((i=0; i<${#str}; ++i)); do + c=${str:$i:1} + if [[ "$c" =~ [a-z] ]]; then + (( Q[$c]++ )) + fi + done +} + +declare -i people=0 group=0 count=0 + +function calcgroup() { + local -i i + i=$(count_array $people) + ((group++)) + (( count += i )) + ((people=0)) + reset_array +} + +while read -r line; do + if [[ -z "$line" ]]; then + calcgroup + continue + fi + ((people++)) + set_array "$line" +done +calcgroup + +printf "%s : groups=%d count=%d\n" "$CMD" $group $count + +exit 0 diff --git a/day06/ex2-c.c b/day06/ex2-c.c new file mode 100644 index 0000000..89e949e --- /dev/null +++ b/day06/ex2-c.c @@ -0,0 +1,79 @@ +/* ex1-c: Advent2020 game, day 4/game 1 + */ + +#include +#include + +#define QSIZE ('z'-'a'+1) +static int Q[QSIZE]; + +void reset_array(){ + int i; + for (i=0; i= 'a' && *str <= 'z') + Q[*str-'a']++; + } +} + +int count_array (people) + int people; +{ + int count=0, i; + for (i=0; i&1 + @$(TIME) ex1-bis.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c shinygold < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c shinygold < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day07/OUTPUT b/day07/OUTPUT new file mode 100644 index 0000000..aaf146f --- /dev/null +++ b/day07/OUTPUT @@ -0,0 +1,20 @@ +ex1.bash : target=shinygold nkeys=594 res=287 + time: 0:06.99 real, 6.99 user, 0.00 sys + context-switch: 720+1, page-faults: 0+403 + +ex1-bis.bash : target=shinygold res=287 + time: 0:00.12 real, 0.12 user, 0.00 sys + context-switch: 14+1, page-faults: 0+209 + +ex1-c : target=shinygold nkeys=594 res=287 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+117 + +ex2.bash : target=shinygold res=48160 + time: 0:00.34 real, 0.28 user, 0.06 sys + context-switch: 37+253, page-faults: 0+12039 + +ex2-c : target=shinygold nkeys=594 res=48160 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+118 + diff --git a/day07/README b/day07/README new file mode 100644 index 0000000..b6d0acf --- /dev/null +++ b/day07/README @@ -0,0 +1,72 @@ +--- Day 7: Handy Haversacks --- + +You land at the regional airport in time for your next flight. In fact, it looks like you'll even have time to grab some food: all flights are currently delayed due to issues in luggage processing. + +Due to recent aviation regulations, many rules (your puzzle input) are being enforced about bags and their contents; bags must be color-coded and must contain specific quantities of other color-coded bags. Apparently, nobody responsible for these regulations considered how long they would take to enforce! + +For example, consider the following rules: + +light red bags contain 1 bright white bag, 2 muted yellow bags. +dark orange bags contain 3 bright white bags, 4 muted yellow bags. +bright white bags contain 1 shiny gold bag. +muted yellow bags contain 2 shiny gold bags, 9 faded blue bags. +shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags. +dark olive bags contain 3 faded blue bags, 4 dotted black bags. +vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. +faded blue bags contain no other bags. +dotted black bags contain no other bags. + +These rules specify the required contents for 9 bag types. In this example, every faded blue bag is empty, every vibrant plum bag contains 11 bags (5 faded blue and 6 dotted black), and so on. + +You have a shiny gold bag. If you wanted to carry it in at least one other bag, how many different bag colors would be valid for the outermost bag? (In other words: how many colors can, eventually, contain at least one shiny gold bag?) + +In the above rules, the following options would be available to you: + + A bright white bag, which can hold your shiny gold bag directly. + A muted yellow bag, which can hold your shiny gold bag directly, plus some other bags. + A dark orange bag, which can hold bright white and muted yellow bags, either of which could then hold your shiny gold bag. + A light red bag, which can hold bright white and muted yellow bags, either of which could then hold your shiny gold bag. + +So, in this example, the number of bag colors that can eventually contain at least one shiny gold bag is 4. + +How many bag colors can eventually contain at least one shiny gold bag? (The list of rules is quite long; make sure you get all of it.) + +Your puzzle answer was 287. +--- Part Two --- + +It's getting pretty expensive to fly these days - not because of ticket prices, but because of the ridiculous number of bags you need to buy! + +Consider again your shiny gold bag and the rules from the above example: + + faded blue bags contain 0 other bags. + dotted black bags contain 0 other bags. + vibrant plum bags contain 11 other bags: 5 faded blue bags and 6 dotted black bags. + dark olive bags contain 7 other bags: 3 faded blue bags and 4 dotted black bags. + +So, a single shiny gold bag must contain 1 dark olive bag (and the 7 bags within it) plus 2 vibrant plum bags (and the 11 bags within each of those): 1 + 1*7 + 2 + 2*11 = 32 bags! + +Of course, the actual rules have a small chance of going several levels deeper than this example; be sure to count all of the bags, even if the nesting becomes topologically impractical! + +Here's another example: + +shiny gold bags contain 2 dark red bags. +dark red bags contain 2 dark orange bags. +dark orange bags contain 2 dark yellow bags. +dark yellow bags contain 2 dark green bags. +dark green bags contain 2 dark blue bags. +dark blue bags contain 2 dark violet bags. +dark violet bags contain no other bags. + +In this example, a single shiny gold bag must contain 126 other bags. + +How many individual bags are required inside your single shiny gold bag? + +Your puzzle answer was 48160. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day07/TEST.txt b/day07/TEST.txt new file mode 100644 index 0000000..1cec74f --- /dev/null +++ b/day07/TEST.txt @@ -0,0 +1,9 @@ +light red bags contain 1 bright white bag, 2 muted yellow bags. +dark orange bags contain 3 bright white bags, 4 muted yellow bags. +bright white bags contain 1 shiny gold bag. +muted yellow bags contain 2 shiny gold bags, 9 faded blue bags. +shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags. +dark olive bags contain 3 faded blue bags, 4 dotted black bags. +vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. +faded blue bags contain no other bags. +dotted black bags contain no other bags. diff --git a/day07/TEST2.txt b/day07/TEST2.txt new file mode 100644 index 0000000..2723ca0 --- /dev/null +++ b/day07/TEST2.txt @@ -0,0 +1,7 @@ +shiny gold bags contain 2 dark red bags. +dark red bags contain 2 dark orange bags. +dark orange bags contain 2 dark yellow bags. +dark yellow bags contain 2 dark green bags. +dark green bags contain 2 dark blue bags. +dark blue bags contain 2 dark violet bags. +dark violet bags contain no other bags. diff --git a/day07/ex1-bis.bash b/day07/ex1-bis.bash new file mode 100755 index 0000000..3a6f461 --- /dev/null +++ b/day07/ex1-bis.bash @@ -0,0 +1,76 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 7/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -A CONT +declare -A match + +function search() { + local -i ret=1 + local val="$1" key="$2" + local cont="${CONT[$key]}" + if [[ $cont =~ $val ]]; then + match[$key]=1 + ret=0 + fi + return $ret +} + +function contains() { + local -i + local -a container + local key="$1" + + set ${CONT[$key]} + while (( $# > 0 )); do + [[ -v CONT[$1] ]] && contains "$1" + match[$1]=1 + shift + done +} + +function parse() { + set $@ + local container="" in="" + local -i i=0 + + while [[ ! "$1" =~ bag[s]* ]]; do + container+="$1" + shift + done + shift + shift + while (( $# > 0 )); do + case "$1" in + contain) + echo contain + ;; + [[:digit:]]*) + ;; + bag*) + CONT[$in]+="$container " + in="" + ;; + *) + in+="$1" + ;; + esac + shift + done +} + +declare -a array +while read -r line; do + parse "$line" +done + +target="shinygold" +match=() +contains $target +printf "%s : target=%s res=%d\n" "$CMD" "$target" "${#match[@]}" +#printf "%s : lines=%d max=%d\n" "$CMD" "$lines" "$max" + +exit 0 diff --git a/day07/ex1-c.c b/day07/ex1-c.c new file mode 100644 index 0000000..6fa5b05 --- /dev/null +++ b/day07/ex1-c.c @@ -0,0 +1,233 @@ +/* ex1-c: Advent2020 game, day 7/game 1 + */ + +#include +#include +#include + +struct node { + struct chain *chain; + struct node *next; + int num; + //int taken; +}; + +struct chain { + char key[80]; /* arbitrary, to avoid multiple mallocs. + char* would be better */ + struct chain *next; /* if collision */ + struct node *contains; /* bags contained */ + struct node *isin; /* is part of */ + int taken; +}; + +#define HASH_SIZE 1024 +static struct chain *thehash[HASH_SIZE]; +static nkeys=0; + +unsigned long get_hash(str) + char *str; +{ + register unsigned long hash = 5381; + register int c; + + while (c = *str++) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + return hash; +} + +struct chain *find_key(key) + char *key; +{ + unsigned long hash; + struct chain *entry; + + hash=get_hash(key)%HASH_SIZE; + entry=thehash[hash]; + while (entry) { + if (!strcmp(key, entry->key)) + break; + entry=entry->next; + } + return entry; +} + +struct chain *insert_key(key) + char *key; +{ + unsigned long hash; + struct chain *entry; + + if (!(entry=find_key(key))) { + hash=get_hash(key)%HASH_SIZE; + entry=malloc(sizeof (struct chain)); + + strcpy(entry->key, key); + entry->next=thehash[hash]; + thehash[hash]=entry; + + entry->contains=NULL; + entry->isin=NULL; + entry->taken=0; + nkeys++; + } + return entry; +} + +void insert_node(container, contained, num) + struct chain *container; + struct chain *contained; + int num; +{ + struct node *node; + + node=malloc(sizeof node); + node->chain=contained; + node->num=num; + //node->taken=0; + node->next=container->contains; + container->contains=node; + + node=malloc(sizeof node); + node->chain=container; + node->num=0; + //node->taken=0; + node->next=contained->isin; + contained->isin=node; +} + +void print_node_list(node) + struct node*node; +{ + while (node) { + printf("\t\t%d %s", node->num, node->chain->key); + node=node->next; + } + printf("\n"); + +} + +int count_containers(chain) + struct chain *chain; +{ + struct chain *p; + struct node *node; + int i=0; + + node = chain->isin; + while (node) { + p=node->chain; + if (p->taken == 0) { + i++; + p->taken=1; + i+=count_containers(p); + } + node=node->next; + } + chain->taken=1; + return i; +} +int count_contained(chain) + struct chain *chain; +{ + struct chain *p; + struct node *node; + int i=1, num; + + node = chain->contains; + while (node) { + p=node->chain; + num=node->num; + i+=num*count_contained(p); + node=node->next; + } + return i; +} + +void print_keys() +{ + int i, j; + struct chain *chain; + struct node *node; + + for (i=0; ikey); + node=chain->contains; + if (node) { + printf("\tCONTAINS:"); + print_node_list(node); + } + + node=chain->isin; + if (node) { + printf("\tIS IN:"); + print_node_list(node); + } + + chain=chain->next; + j++; + } + } +} + +char*sep=" \t\n"; /* separator */ + +char *getcolor(line) + char *line; +{ + static char buf[80], *tok, *ret=NULL; + + if (tok=strtok(line, sep)) { + strcpy(buf, tok); + if (tok=strtok(NULL, sep)) { + ret=strcat(buf, tok); + } + } + return ret; +} + +int main(ac, av) + int ac; + char **av; +{ + int num=0, count=0; + char line[512], *color, *numstr; + struct chain *entry, *isin; + + if (ac != 2) { + fprintf(stderr, "usage: %s key\n", *av); + exit (1); + } + while (fgets(line, sizeof line, stdin)) { + count++; + if (!(color=getcolor(line))) { + continue; + } + entry=insert_key(color); + + // skip "bag" and "contain" + strtok(NULL, sep); + strtok(NULL, sep); + + while (numstr=strtok(NULL, sep)) { + if (num=atoi(numstr)) { + color=getcolor(NULL); + isin=insert_key(color); + insert_node(entry, isin, num); + strtok(NULL, sep); + } + } + + continue; + } + //print_keys(); + entry=find_key(*(av+1)); + //num=count_contained(entry)-1; /* ex 2 */ + num=count_containers(entry); /* ex 1 */ + + printf("%s : target=%s nkeys=%d res=%d\n", *av, *(av+1), nkeys, num); + exit (0); +} diff --git a/day07/ex1.bash b/day07/ex1.bash new file mode 100755 index 0000000..bc3d468 --- /dev/null +++ b/day07/ex1.bash @@ -0,0 +1,78 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 7/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -A CONT +declare -A match + +function search() { + local -i ret=1 + local val="$1" key="$2" + local cont="${CONT[$key]}" + if [[ $cont =~ $val ]]; then + match[$key]=1 + ret=0 + fi + #printf "search val=%s key=%s cont=[%s] ret=%d\n" "$val" "$key" "$cont" "$ret" + return $ret +} + +function contains() { + local -i + local value="$1" + + for container in "${!CONT[@]}"; do + if [[ ${match[$container]} = "" ]]; then + if search "$value" "$container" ; then + contains "$container" + fi + fi + done +} + +function parse() { + local -a val=("$@") + local container="" in="" + local -i i=0 num=0 + # get container + while [[ ! "${val[$i]}" =~ bag[s]* ]]; do + container+="${val[$i]}" + ((i++)) + done + CONT[$container]="" + ((i++)) + while [[ -n "${val[$i]}" ]]; do + case "${val[$i]}" in + contain) + #echo contain + ;; + [[:digit:]]*) + num=${val[$i]} + ;; + bag*) + CONT[$container]+="$num " + CONT[$container]+="$in " + in="" + num=0 + ;; + *) + in+="${val[$i]}" + ;; + esac + ((i++)) + done +} + +declare -a array +while read -r -a array; do + parse "${array[@]}" +done +target="shinygold" +match=() +contains $target +printf "%s : target=%s nkeys=%d res=%d\n" "$CMD" "$target" "${#CONT[@]}" "${#match[@]}" + +exit 0 diff --git a/day07/ex2-c.c b/day07/ex2-c.c new file mode 100644 index 0000000..32b38ed --- /dev/null +++ b/day07/ex2-c.c @@ -0,0 +1,233 @@ +/* ex2-c: Advent2020 game, day 7/game 2 + */ + +#include +#include +#include + +struct node { + struct chain *chain; + struct node *next; + int num; + //int taken; +}; + +struct chain { + char key[80]; /* arbitrary, to avoid multiple mallocs. + char* would be better */ + struct chain *next; /* if collision */ + struct node *contains; /* bags contained */ + struct node *isin; /* is part of */ + int taken; +}; + +#define HASH_SIZE 1024 +static struct chain *thehash[HASH_SIZE]; +static nkeys=0; + +unsigned long get_hash(str) + char *str; +{ + register unsigned long hash = 5381; + register int c; + + while (c = *str++) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + return hash; +} + +struct chain *find_key(key) + char *key; +{ + unsigned long hash; + struct chain *entry; + + hash=get_hash(key)%HASH_SIZE; + entry=thehash[hash]; + while (entry) { + if (!strcmp(key, entry->key)) + break; + entry=entry->next; + } + return entry; +} + +struct chain *insert_key(key) + char *key; +{ + unsigned long hash; + struct chain *entry; + + if (!(entry=find_key(key))) { + hash=get_hash(key)%HASH_SIZE; + entry=malloc(sizeof (struct chain)); + + strcpy(entry->key, key); + entry->next=thehash[hash]; + thehash[hash]=entry; + + entry->contains=NULL; + entry->isin=NULL; + entry->taken=0; + nkeys++; + } + return entry; +} + +void insert_node(container, contained, num) + struct chain *container; + struct chain *contained; + int num; +{ + struct node *node; + + node=malloc(sizeof node); + node->chain=contained; + node->num=num; + //node->taken=0; + node->next=container->contains; + container->contains=node; + + node=malloc(sizeof node); + node->chain=container; + node->num=0; + //node->taken=0; + node->next=contained->isin; + contained->isin=node; +} + +void print_node_list(node) + struct node*node; +{ + while (node) { + printf("\t\t%d %s", node->num, node->chain->key); + node=node->next; + } + printf("\n"); + +} + +int count_containers(chain) + struct chain *chain; +{ + struct chain *p; + struct node *node; + int i=0; + + node = chain->isin; + while (node) { + p=node->chain; + if (p->taken == 0) { + i++; + p->taken=1; + i+=count_containers(p); + } + node=node->next; + } + chain->taken=1; + return i; +} +int count_contained(chain) + struct chain *chain; +{ + struct chain *p; + struct node *node; + int i=1, num; + + node = chain->contains; + while (node) { + p=node->chain; + num=node->num; + i+=num*count_contained(p); + node=node->next; + } + return i; +} + +void print_keys() +{ + int i, j; + struct chain *chain; + struct node *node; + + for (i=0; ikey); + node=chain->contains; + if (node) { + printf("\tCONTAINS:"); + print_node_list(node); + } + + node=chain->isin; + if (node) { + printf("\tIS IN:"); + print_node_list(node); + } + + chain=chain->next; + j++; + } + } +} + +char*sep=" \t\n"; /* separator */ + +char *getcolor(line) + char *line; +{ + static char buf[80], *tok, *ret=NULL; + + if (tok=strtok(line, sep)) { + strcpy(buf, tok); + if (tok=strtok(NULL, sep)) { + ret=strcat(buf, tok); + } + } + return ret; +} + +int main(ac, av) + int ac; + char **av; +{ + int num=0, count=0; + char line[512], *color, *numstr; + struct chain *entry, *isin; + + if (ac != 2) { + fprintf(stderr, "usage: %s key\n", *av); + exit (1); + } + while (fgets(line, sizeof line, stdin)) { + count++; + if (!(color=getcolor(line))) { + continue; + } + entry=insert_key(color); + + // skip "bag" and "contain" + strtok(NULL, sep); + strtok(NULL, sep); + + while (numstr=strtok(NULL, sep)) { + if (num=atoi(numstr)) { + color=getcolor(NULL); + isin=insert_key(color); + insert_node(entry, isin, num); + strtok(NULL, sep); + } + } + + continue; + } + //print_keys(); + entry=find_key(*(av+1)); + num=count_contained(entry)-1; /* ex 2 */ + //num=count_containers(entry); /* ex 1 */ + + printf("%s : target=%s nkeys=%d res=%d\n", *av, *(av+1), nkeys, num); + exit (0); +} diff --git a/day07/ex2.bash b/day07/ex2.bash new file mode 100755 index 0000000..2add168 --- /dev/null +++ b/day07/ex2.bash @@ -0,0 +1,69 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 7/game 2. + +CMD=${0##*/} +shopt -s extglob + +declare -A CONT + +function contains() { + local -i i size count=0 count2 + local -i nbags + local key="$1" color + local -a contents + + read -ra contents <<< "${CONT[$key]}" + ((size = ${#contents[@]}/2)) + + for ((i=0; i&1 > OUTPUT + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c shinygold < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c shinygold < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day08/OUTPUT b/day08/OUTPUT new file mode 100644 index 0000000..53f92d3 --- /dev/null +++ b/day08/OUTPUT @@ -0,0 +1,16 @@ +ex1.bash : res=1594 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 0+4, page-faults: 0+303 + +ex1-c : res=1594 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+0, page-faults: 0+79 + +ex2.bash : res:758 + time: 0:02.26 real, 2.12 user, 0.16 sys + context-switch: 212+1084, page-faults: 0+43039 + +ex2-c : res=758 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+78 + diff --git a/day08/README b/day08/README new file mode 100644 index 0000000..6a02a86 --- /dev/null +++ b/day08/README @@ -0,0 +1,94 @@ +--- Day 8: Handheld Halting --- + +Your flight to the major airline hub reaches cruising altitude without incident. While you consider checking the in-flight menu for one of those drinks that come with a little umbrella, you are interrupted by the kid sitting next to you. + +Their handheld game console won't turn on! They ask if you can take a look. + +You narrow the problem down to a strange infinite loop in the boot code (your puzzle input) of the device. You should be able to fix it, but first you need to be able to run the code in isolation. + +The boot code is represented as a text file with one instruction per line of text. Each instruction consists of an operation (acc, jmp, or nop) and an argument (a signed number like +4 or -20). + + acc increases or decreases a single global value called the accumulator by the value given in the argument. For example, acc +7 would increase the accumulator by 7. The accumulator starts at 0. After an acc instruction, the instruction immediately below it is executed next. + jmp jumps to a new instruction relative to itself. The next instruction to execute is found using the argument as an offset from the jmp instruction; for example, jmp +2 would skip the next instruction, jmp +1 would continue to the instruction immediately below it, and jmp -20 would cause the instruction 20 lines above to be executed next. + nop stands for No OPeration - it does nothing. The instruction immediately below it is executed next. + +For example, consider the following program: + +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 + +These instructions are visited in this order: + +nop +0 | 1 +acc +1 | 2, 8(!) +jmp +4 | 3 +acc +3 | 6 +jmp -3 | 7 +acc -99 | +acc +1 | 4 +jmp -4 | 5 +acc +6 | + +First, the nop +0 does nothing. Then, the accumulator is increased from 0 to 1 (acc +1) and jmp +4 sets the next instruction to the other acc +1 near the bottom. After it increases the accumulator from 1 to 2, jmp -4 executes, setting the next instruction to the only acc +3. It sets the accumulator to 5, and jmp -3 causes the program to continue back at the first acc +1. + +This is an infinite loop: with this sequence of jumps, the program will run forever. The moment the program tries to run any instruction a second time, you know it will never terminate. + +Immediately before the program would run an instruction a second time, the value in the accumulator is 5. + +Run your copy of the boot code. Immediately before any instruction is executed a second time, what value is in the accumulator? + +Your puzzle answer was 1594. +--- Part Two --- + +After some careful analysis, you believe that exactly one instruction is corrupted. + +Somewhere in the program, either a jmp is supposed to be a nop, or a nop is supposed to be a jmp. (No acc instructions were harmed in the corruption of this boot code.) + +The program is supposed to terminate by attempting to execute an instruction immediately after the last instruction in the file. By changing exactly one jmp or nop, you can repair the boot code and make it terminate correctly. + +For example, consider the same program from above: + +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 + +If you change the first instruction from nop +0 to jmp +0, it would create a single-instruction infinite loop, never leaving that instruction. If you change almost any of the jmp instructions, the program will still eventually find another jmp instruction and loop forever. + +However, if you change the second-to-last instruction (from jmp -4 to nop -4), the program terminates! The instructions are visited in this order: + +nop +0 | 1 +acc +1 | 2 +jmp +4 | 3 +acc +3 | +jmp -3 | +acc -99 | +acc +1 | 4 +nop -4 | 5 +acc +6 | 6 + +After the last instruction (acc +6), the program terminates by attempting to run the instruction below the last instruction in the file. With this change, after the program terminates, the accumulator contains the value 8 (acc +1, acc +1, acc +6). + +Fix the program so that it terminates normally by changing exactly one jmp (to nop) or nop (to jmp). What is the value of the accumulator after the program terminates? + +Your puzzle answer was 758. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day08/TEST.txt b/day08/TEST.txt new file mode 100644 index 0000000..178df53 --- /dev/null +++ b/day08/TEST.txt @@ -0,0 +1,9 @@ +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 diff --git a/day08/ex1-c.c b/day08/ex1-c.c new file mode 100644 index 0000000..b38eb28 --- /dev/null +++ b/day08/ex1-c.c @@ -0,0 +1,143 @@ +/* ex1-c: Advent2020 game, day 8/game 1 + */ + +#include +#include +#include + +struct instr { + struct instr *next; + int instr; + int val; + int visited; +}; + +struct program { + unsigned length; + unsigned total; + struct instr *prg; +}; + +#define BLOCKSIZE 1024 /* number of elements for realloc() */ +#define NOP 0 +#define ACC 1 +#define JMP 2 + +void print_instr(prg, step, cur) + struct program *prg; + int step; + int cur; +{ + struct instr *instr=prg->prg+step; + char *op="unknown"; + + switch (instr->instr) { + case NOP: + op="nop"; + break; + case JMP: + op="jmp"; + break; + case ACC: + op="acc"; + break; + } + fprintf(stderr, "%03d: [%s][%4d] -> %4d\n", step, op, instr->val, cur); +} + +void print_prg(prg) + struct program *prg; +{ + unsigned i, psize=prg->length; + struct instr *pinstr=prg->prg; + + fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", prg, pinstr, psize); + for (i=0; iprg=malloc(sizeof(struct instr)*BLOCKSIZE); + + //fprintf(stderr, "alloc buf: prg=%p prog=%p\n", prg, prg->prg); + prg->length=0; + prg->total=BLOCKSIZE; + } + cur=prg->length; + total=prg->total; + + if (cur == total) { + total+=BLOCKSIZE; + prg->total=total; + prg->prg=realloc(prg->prg, sizeof(struct instr)*total); + //fprintf(stderr, "realloc buf: cur=%d total=%d prog=%p\n", cur, total, prg->prg); + } + pinstr=prg->prg+cur; + + sscanf(line, "%s %d", sinstr, &val); + pinstr->visited=0; + pinstr->val=val; + if (!strcmp(sinstr, "jmp")) + pinstr->instr=JMP; + else if (!strcmp(sinstr, "acc")) + pinstr->instr=ACC; + else pinstr->instr=NOP; + prg->length++; + + return prg; +} + +int run(prg) + struct program *prg; +{ + int cur=0, length=prg->length; + struct instr *instr=prg->prg; + int res=0; + + //fprintf(stderr, "RUN: address=%p prg=%p size=%d\n", prg, instr, length); + while (cur>=0 && curprg+cur; + //print_instr(prg, cur, res); + if (instr->visited) { + break; + } + instr->visited=1; + switch (instr->instr) { + case NOP: + cur++; + break; + case JMP: + cur+=instr->val; + break; + case ACC: + res+=instr->val; + cur++; + break; + } + } + return res; +} + +int main(ac, av) + char **av; +{ + char line[512]; + struct program *prg=NULL; + + while (fgets(line, sizeof line, stdin)) { + prg=add_line(prg, line); + } + //print_prg(prg); + printf("%s : res=%d\n", *av, run(prg)); + exit(0); +} diff --git a/day08/ex1.bash b/day08/ex1.bash new file mode 100755 index 0000000..091490d --- /dev/null +++ b/day08/ex1.bash @@ -0,0 +1,58 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 8/game 1. + +CMD=${0##*/} +shopt -s extglob + +#declare -i ACCUMULATOR=0 + +declare -a PRG=() +declare -i ARG=() +declare -i PRGSIZE=0 + +function run() { + local -i arg cur=0 res=0 + local -a steps=() + local instr + + while [[ ${steps[$cur]} != 1 ]]; do + instr=${PRG[$cur]} + arg=${ARG[$cur]} + steps[$cur]=1 + case "$instr" in + acc) + ((res += arg)) + ((cur++)) + ;; + nop) + ((cur++)) + ;; + jmp) + ((cur += arg)) + ;; + esac + done + echo "$res" +} + +function print_prog() +{ + local -i i + for ((i=0; i +#include +#include + +struct instr { + struct instr *next; + int instr; + int val; + int visited; +}; + +struct program { + unsigned length; + unsigned total; + struct instr *prg; +}; + +#define BLOCKSIZE 1024 /* number of elements for realloc() */ +#define NOP 0 +#define ACC 1 +#define JMP 2 + +void print_instr(prg, step, cur) + struct program *prg; + int step; + int cur; +{ + struct instr *instr=prg->prg+step; + char *op="unknown"; + + switch (instr->instr) { + case NOP: + op="nop"; + break; + case JMP: + op="jmp"; + break; + case ACC: + op="acc"; + break; + } + fprintf(stderr, "%03d: [%s][%4d] -> %4d\n", step, op, instr->val, cur); +} + +void print_prg(prg) + struct program *prg; +{ + unsigned i, psize=prg->length; + struct instr *pinstr=prg->prg; + + fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", prg, pinstr, psize); + for (i=0; ilength; + struct instr *pinstr=prg->prg; + + for (i=0; iprg=malloc(sizeof(struct instr)*BLOCKSIZE); + + //fprintf(stderr, "alloc buf: prg=%p prog=%p\n", prg, prg->prg); + prg->length=0; + prg->total=BLOCKSIZE; + } + cur=prg->length; + total=prg->total; + + if (cur == total) { + total+=BLOCKSIZE; + prg->total=total; + prg->prg=realloc(prg->prg, sizeof(struct instr)*total); + //fprintf(stderr, "realloc buf: cur=%d total=%d prog=%p\n", cur, total, prg->prg); + } + pinstr=prg->prg+cur; + + sscanf(line, "%s %d", sinstr, &val); + pinstr->visited=0; + pinstr->val=val; + if (!strcmp(sinstr, "jmp")) + pinstr->instr=JMP; + else if (!strcmp(sinstr, "acc")) + pinstr->instr=ACC; + else pinstr->instr=NOP; + prg->length++; + + return prg; +} + +int run2(prg, ret) + struct program *prg; + int *ret; +{ + int cur=0, length=prg->length; + struct instr *instr=prg->prg; + int res=0; + + reset_prg(prg); + //fprintf(stderr, "RUN: address=%p prg=%p size=%d\n", prg, instr, length); + //print_prg(prg); + while (cur>=0 && curprg+cur; + //print_instr(prg, cur, res); + if (instr->visited) { + return 1; + } + instr->visited=1; + switch (instr->instr) { + case NOP: + cur++; + break; + case JMP: + cur+=instr->val; + break; + case ACC: + res+=instr->val; + cur++; + break; + } + } + *ret=res; + return 0; +} + +int main(ac, av) + char **av; +{ + char line[512]; + struct program *prg=NULL; + struct instr *instr; + int res=0, cur=0, length=0; + + while (fgets(line, sizeof line, stdin)) { + prg=add_line(prg, line); + } + instr=prg->prg; + length=prg->length; + if (run2(prg, &res)) { + do { + while (cur=0 && cur= 0)); then + PRG[$lastchanged]="$savstep" + fi + ((lastchanged++)) + while ((lastchanged <= PRGSIZE)) && [[ ${PRG[$lastchanged]} == acc ]]; do + ((lastchanged++)) + done + + savstep=${PRG[$lastchanged]} + case $savstep in + jmp) + PRG[$lastchanged]="nop" + ;; + nop) + PRG[$lastchanged]="jmp" + ;; + esac +done + +printf "%s : res:%d\n" "${CMD}" "$res" + +exit 0 diff --git a/day09/INPUT.txt b/day09/INPUT.txt new file mode 100644 index 0000000..afb24d5 --- /dev/null +++ b/day09/INPUT.txt @@ -0,0 +1,1000 @@ +48 +34 +42 +10 +36 +30 +12 +31 +38 +1 +37 +9 +16 +26 +20 +23 +13 +5 +39 +14 +4 +32 +21 +17 +22 +6 +7 +19 +8 +62 +69 +41 +10 +18 +15 +11 +27 +53 +25 +9 +29 +83 +12 +13 +28 +14 +33 +16 +46 +30 +24 +38 +17 +39 +93 +19 +20 +21 +22 +26 +23 +35 +31 +36 +34 +25 +27 +29 +47 +37 +40 +41 +43 +42 +44 +46 +67 +38 +45 +39 +48 +71 +60 +49 +50 +52 +56 +54 +59 +62 +95 +64 +66 +93 +80 +79 +81 +88 +91 +83 +100 +77 +84 +87 +126 +114 +120 +99 +101 +102 +106 +136 +141 +121 +143 +236 +145 +379 +187 +157 +156 +199 +243 +160 +219 +161 +164 +232 +188 +205 +331 +282 +320 +203 +245 +227 +257 +407 +346 +664 +301 +302 +451 +313 +317 +435 +393 +348 +321 +325 +349 +352 +574 +391 +634 +797 +430 +700 +502 +575 +648 +582 +1130 +923 +603 +1316 +615 +630 +1623 +638 +743 +646 +755 +670 +967 +779 +1188 +821 +893 +932 +1564 +1060 +1666 +1213 +1157 +1185 +1931 +1261 +1370 +1401 +1245 +1253 +1276 +1284 +1308 +1687 +1316 +1425 +1449 +1600 +1672 +1714 +2583 +2138 +2193 +2872 +2217 +2342 +2438 +2402 +2430 +2498 +2959 +2521 +2529 +2537 +2569 +3810 +2592 +3272 +2988 +2741 +4121 +5120 +4038 +3889 +7558 +4331 +7462 +4832 +4559 +6152 +4744 +4840 +10127 +4928 +5019 +5050 +5058 +5278 +5129 +5161 +6798 +5333 +12582 +9303 +15020 +8452 +7927 +8220 +11210 +9163 +9350 +9391 +9688 +13292 +9584 +9672 +9890 +9947 +9978 +10069 +14862 +16379 +15108 +21188 +10494 +14636 +19653 +17090 +16147 +16672 +20078 +17277 +17383 +20898 +18934 +18741 +19868 +19272 +20441 +19256 +28824 +19837 +20016 +28810 +20563 +30147 +26873 +26641 +25130 +39884 +37653 +32819 +35944 +35403 +33949 +34660 +36018 +36124 +37675 +75328 +52075 +60447 +45897 +64842 +39272 +62765 +39853 +40579 +52003 +56788 +62585 +51771 +57949 +98100 +73221 +66768 +67479 +119373 +68609 +102685 +91448 +122791 +106621 +192594 +79125 +79851 +91275 +80432 +91043 +149224 +91624 +91856 +143451 +200239 +114356 +109720 +118539 +124717 +135377 +146604 +134247 +309959 +147734 +170749 +238062 +273941 +237879 +158976 +159557 +170894 +160283 +171475 +182667 +274291 +183480 +201344 +201576 +224076 +232895 +228259 +373256 +243256 +258964 +294338 +280851 +434239 +306710 +318483 +402813 +434574 +456958 +318533 +721296 +478040 +519877 +331758 +354142 +384824 +385056 +407556 +539605 +425652 +597398 +565674 +549966 +637016 +502220 +703307 +575189 +587561 +625193 +734571 +819630 +1050845 +951540 +650291 +1156893 +685900 +716582 +779794 +935022 +960013 +950730 +1402482 +1474627 +1023050 +927872 +1052186 +1067894 +1077409 +1420471 +1089781 +1726357 +1162750 +1212754 +1384862 +1430085 +2070762 +1465694 +1336191 +2027907 +2474643 +1620922 +2146667 +2488744 +2380484 +1878602 +1950922 +2129595 +1980058 +1995766 +2264940 +2482271 +2497880 +2167190 +3867133 +2302535 +2628444 +2498941 +3874368 +3883803 +3858660 +2801885 +4181137 +2957113 +3788112 +4376482 +3600980 +4432130 +3829524 +3930980 +4125361 +3946688 +5758998 +5259648 +5104420 +4567475 +4469725 +4666131 +4795634 +7369360 +4801476 +7977462 +7906305 +6402865 +13736460 +6558093 +6589997 +8127825 +6786637 +21642765 +8070705 +7430504 +7760504 +14717822 +7877668 +9900054 +8416413 +9037200 +15638172 +9135856 +9233606 +12376030 +9461765 +9597110 +11359569 +11204341 +14819278 +12960958 +19120073 +21088783 +13148090 +13376634 +14547141 +14857342 +15191008 +15948373 +15846917 +16176917 +30704259 +17474778 +24757396 +17453613 +18173056 +18369462 +18597621 +31330420 +19058875 +20666106 +31558579 +22563910 +24352431 +26337592 +26109048 +30048350 +42285965 +26524724 +35523448 +53696504 +34121429 +33630530 +31795290 +36513023 +33651695 +41622785 +34928391 +44697780 +35626669 +36542518 +36967083 +37656496 +72490531 +39724981 +43230016 +56685339 +77809413 +72593752 +52446640 +86733689 +56573074 +68762373 +63491807 +65425820 +70618778 +104740207 +65446985 +66723681 +72139692 +88989158 +101148303 +170187192 +94229570 +72169187 +198969777 +95676656 +123065418 +105171966 +82954997 +128854526 +109019714 +115938447 +137616172 +128917627 +128712766 +120064881 +137565512 +161158345 +130872805 +132170666 +138892868 +195234605 +138863373 +144308879 +155124184 +166398757 +221110413 +300051213 +177341153 +188126963 +251983045 +191974711 +203019878 +341883251 +167829540 +224958161 +267810495 +419812585 +248777647 +250937686 +252235547 +268438317 +263043471 +361633362 +271034039 +277756241 +508282008 +387509170 +299433063 +321522941 +334228297 +435640035 +345170693 +355956503 +359804251 +586211342 +370849418 +687053944 +644770746 +596108379 +602038792 +619627065 +499715333 +686942233 +528693927 +679398990 +534077510 +771325479 +548790280 +1085926675 +677479444 +620956004 +701127196 +1221131454 +655751238 +1046858195 +1702609433 +704974944 +888498178 +972888210 +1082867790 +1015620164 +1028409260 +1120671337 +1616945300 +1208092917 +1320754261 +1169746284 +1575440411 +1742170427 +1226269724 +1155033514 +1427076717 +1204541518 +1775989518 +1649365264 +1593473122 +2447401178 +1360726182 +1904118342 +1677863154 +2566361332 +1720595108 +2977671482 +1988508374 +2044029424 +2389135442 +2325212855 +2275704851 +2324779798 +2359575032 +2374287802 +2582110231 +2381303238 +2430811242 +2954199304 +3020549839 +3948147766 +3010091446 +4259973385 +4706516093 +3038589336 +3742029420 +3720301214 +3398458262 +3709103482 +3764624532 +4080170140 +6346734763 +4032537798 +4403604456 +4600484649 +4600917706 +4635279883 +6729653321 +4733862834 +4812114480 +4963413469 +7016583121 +6151112456 +6437047598 +7658431647 +6408549708 +6747692818 +6758890550 +8448545513 +7118759476 +7107561744 +7163082794 +9367017925 +7473728014 +10554716912 +11566687250 +8436142254 +9413032186 +9004089105 +11400461067 +9236197589 +9447394363 +9545977314 +11722304019 +15954527022 +11114525925 +13195938148 +16106977160 +13882277722 +13167440258 +13910775612 +13866452294 +13877650026 +19558806017 +14226321220 +14270644538 +14636810808 +18588253939 +15909870268 +18417121291 +17440231359 +18240286694 +28795003606 +22432135737 +25455847582 +18683591952 +18993371677 +22836829944 +24310464073 +24281966183 +24996803647 +30180514806 +27033892552 +28863132028 +34195616825 +27777227906 +27744102320 +28103971246 +40849257028 +31666552579 +33053932099 +30546681076 +39872367096 +33350101627 +35680518053 +36123823311 +45268965681 +46770599583 +59443780485 +64182831169 +60087824651 +53100774893 +47118796127 +92286802415 +49278769830 +52030696199 +54777994872 +79774798519 +55521330226 +55848073566 +95720440662 +58290783396 +87547751731 +62213233655 +63896782703 +143957629688 +95563335282 +69030619680 +95768342704 +107569553226 +81392788992 +147064797287 +93889395710 +96397565957 +146990170603 +101309466029 +99149492326 +101896790999 +104056764702 +107878769765 +106808691071 +117734563881 +111369403792 +119418112929 +114138856962 +156102629365 +120504017051 +163046275029 +131243853335 +132927402383 +150423408672 +162920015390 +164798962384 +212636642327 +269854966100 +276168366176 +197707031986 +193038888036 +195547058283 +200458958355 +201046283325 +213266194791 +297095652738 +210865455773 +214687460836 +218178094863 +225508260754 +250661966264 +233556969891 +270927425723 +316051075334 +391431442774 +264171255718 +283350811055 +366484372274 +421055319037 +327718977774 +357837850420 +688527095512 +388585946319 +471386384078 +390745920022 +393497846391 +396006016638 +401505241680 +411911739098 +424131650564 +451735064754 +425552916609 +432865555699 +489105520586 +735085875809 +561275947665 +504484395614 +535098681441 +547522066773 +780017389093 +665676497398 +611069788829 +685556828194 +723724994412 +807917755736 +746423796739 +779331866341 +895230315636 +784243766413 +940840585340 +789503863029 +827058158289 +825636892244 +836043389662 +849684567173 +858418472308 +914658437195 +1082620748214 +1720172451681 +1722576192931 +1039583077055 +1052006462387 +1685476630597 +1515361064571 +1276746286227 +1334794783241 +1296626617023 +1582143466720 +1786006873794 +2390061222456 +1525755663080 +1563575632754 +1573747629442 +1609880658657 +1615140755273 +1676742725462 +1954241514250 +1750701826857 +1685727956835 +1708103039481 +2386801245628 +2860550446321 +3408052823528 +5362294337778 +2091589539442 +2316329363282 +3089108694013 +2573372903250 +2611541069468 +2886626944884 +2631421400264 +2822382280103 +4386306109401 +3089331295834 +5981425726778 +4594729984365 +3137323262196 +3183628288099 +3225021413930 +5682932726424 +3842291366299 +3393830996316 +3436429783692 +3777317496277 +3799692578923 +4952139985763 +4407918902724 +4664962442692 +6067851183956 +5459999848134 +4889702266532 +5184913972718 +5453803680367 +5959705542299 +6937015841119 +5720752696098 +12918441567897 +6226654558030 +6272959583933 +6362344676126 +6320951550295 +8177161399693 +10634573460754 +9504280967648 +6830260780008 +9861722583091 +9617102428455 +22535543996352 +7577010075200 +8207611481647 +9072881345416 +15890062012388 +13928364177745 +10074616239250 +12390819521486 +11144619515017 +11457873556651 +11816148356493 +22211013562683 +19649116873843 +13103220363941 +12499614141963 +14450120983626 +12593911134228 +13192605456134 +13151212330303 +24067223412081 +14407270855208 +15903142125424 +29510839456934 +15784621556847 +16649891420616 +17280492827063 +17651626314450 +23274021913144 +19147497584666 +21219235754267 +21532489795901 +37259828868215 +22602493071668 +22960767871510 +23957487698614 +27044032117854 +42703444913068 +25093525276191 +25602834505904 +25650826472266 +31687763682271 +41451302973062 +40878146833038 +27558483185511 +42751725550168 +36427990411729 +32434512977463 +34301517735066 +33065114383910 +33930384247679 +34932119141513 +53077881832345 +40679987380567 +68231901982745 +46822070260171 +44134982867569 +102533419717811 +48563602377414 +53209309657777 +59023909523870 +60025644417704 +88273373233233 +50696359782095 +51253660978170 +71693466053080 +59246246867782 +100903791250742 +59992996162974 diff --git a/day09/Makefile b/day09/Makefile new file mode 100644 index 0000000..fc4813e --- /dev/null +++ b/day09/Makefile @@ -0,0 +1,28 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +output: + @$(MAKE) --no-print-directory all 2>&1 > OUTPUT + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash 25 < $(INPUT) 2>&1 + @$(TIME) ex1-c 25 < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash 25 < $(INPUT) 2>&1 + @$(TIME) ex2-c 25 < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day09/OUTPUT b/day09/OUTPUT new file mode 100644 index 0000000..73a5527 --- /dev/null +++ b/day09/OUTPUT @@ -0,0 +1,16 @@ +ex1.bash : res=167829540 + time: 0:00.42 real, 0.42 user, 0.00 sys + context-switch: 57+1, page-faults: 0+172 + +ex1-c : res=167829540 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+76 + +ex2.bash : res=167829540 sum=28045630 + time: 0:03.22 real, 3.01 user, 0.21 sys + context-switch: 359+921, page-faults: 0+49208 + +ex2-c : res=167829540 sum=28045630 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+75 + diff --git a/day09/README b/day09/README new file mode 100644 index 0000000..02138c9 --- /dev/null +++ b/day09/README @@ -0,0 +1,93 @@ +--- Day 9: Encoding Error --- + +With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you. + +Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input). + +The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness. + +XMAS starts by transmitting a preamble of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair. + +For example, suppose your preamble consists of the numbers 1 through 25 in a random order. To be valid, the next number must be the sum of two of those numbers: + + 26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24). + 49 would be a valid next number, as it is the sum of 24 and 25. + 100 would not be valid; no two of the previous 25 numbers sum to 100. + 50 would also not be valid; although 25 appears in the previous 25 numbers, the two numbers in the pair must be different. + +Suppose the 26th number is 45, and the first number (no longer an option, as it is more than 25 numbers ago) was 20. Now, for the next number to be valid, there needs to be some pair of numbers among 1-19, 21-25, or 45 that add up to it: + + 26 would still be a valid next number, as 1 and 25 are still within the previous 25 numbers. + 65 would not be valid, as no two of the available numbers sum to it. + 64 and 66 would both be valid, as they are the result of 19+45 and 21+45 respectively. + +Here is a larger example which only considers the previous 5 numbers (and has a preamble of length 5): + +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576 + +In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is 127. + +The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is not the sum of two of the 25 numbers before it. What is the first number that does not have this property? + +Your puzzle answer was 167829540. +--- Part Two --- + +The final step in breaking the XMAS encryption relies on the invalid number you just found: you must find a contiguous set of at least two numbers in your list which sum to the invalid number from step 1. + +Again consider the above example: + +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576 + +In this list, adding up all of the numbers from 15 through 40 produces the invalid number from step 1, 127. (Of course, the contiguous set of numbers in your actual list might be much longer.) + +To find the encryption weakness, add together the smallest and largest number in this contiguous range; in this example, these are 15 and 47, producing 62. + +What is the encryption weakness in your XMAS-encrypted list of numbers? + +Your puzzle answer was 28045630. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day09/TEST.txt b/day09/TEST.txt new file mode 100644 index 0000000..28d66e4 --- /dev/null +++ b/day09/TEST.txt @@ -0,0 +1,20 @@ +35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576 diff --git a/day09/ex1-c.c b/day09/ex1-c.c new file mode 100644 index 0000000..e2a9f54 --- /dev/null +++ b/day09/ex1-c.c @@ -0,0 +1,103 @@ +/* ex1-c: Advent2020 game, day 8/game 1 + */ + +#include +#include +#include + +struct list { + unsigned size; + unsigned last; + unsigned long long *list; +}; + +#define DEFNSUM 25 +#define BLOCKSIZE 1024 /* number of elements for realloc() */ + +void print_list(list) + struct list *list; +{ + unsigned i, psize=list->last; + unsigned long long *ptr=list->list; + + fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", list, ptr, psize); + for (i=0; ilist=malloc(sizeof(unsigned long long)*BLOCKSIZE); + list->size=BLOCKSIZE; + list->last=0; + } + cur=list->last; + size=list->size; + + if (cur == size) { + size+=BLOCKSIZE; + list->size=size; + list->list=realloc(list->list, sizeof(unsigned long long)*size); + fprintf(stderr, "realloc buf: cur=%d size=%d ptr=%p\n", cur, size, list->list); + } + ptr=list->list+cur; + + sscanf(line, "%d", &val); + *ptr=val; + list->last++; + + return list; +} + +unsigned long long calc(list, cur, nsum) + struct list *list; + int cur; + unsigned nsum; +{ + unsigned long long *ptr=list->list; + unsigned long long res=*(ptr+cur); + int start=cur-nsum, i, j; + + for (i=start; ilast; ++i) { + if (res=calc(list, i, nsum)) { + printf("%s : res=%llu\n", *av, res); + break; + } + } + exit (0); +} diff --git a/day09/ex1.bash b/day09/ex1.bash new file mode 100755 index 0000000..cff378b --- /dev/null +++ b/day09/ex1.bash @@ -0,0 +1,51 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 8/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -i N=5 SIZE +declare -a LIST=() + +function push25 () { + local i + for ((i=0; i<25; ++i)); do + read -r i + LIST+=("$i") + done +} + +function push() { + local i + while read -r i; do + LIST+=("$i") + done + SIZE=${#LIST[@]} +} + +function badnum() { + local -i cur="$1" i j + local -i res=${LIST[$cur]} + local -i start=$((cur-N-1)) + for ((i=start; i +#include +#include + +struct list { + unsigned size; + unsigned last; + unsigned long long *list; +}; + +#define DEFNSUM 25 +#define BLOCKSIZE 1024 /* number of elements for realloc() */ + +void print_list(list) + struct list *list; +{ + unsigned i, psize=list->last; + unsigned long long *ptr=list->list; + + fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", list, ptr, psize); + for (i=0; ilist=malloc(sizeof(unsigned long long)*BLOCKSIZE); + list->size=BLOCKSIZE; + list->last=0; + } + cur=list->last; + size=list->size; + + if (cur == size) { + size+=BLOCKSIZE; + list->size=size; + list->list=realloc(list->list, sizeof(unsigned long long)*size); + fprintf(stderr, "realloc buf: cur=%d size=%d ptr=%p\n", cur, size, list->list); + } + ptr=list->list+cur; + + sscanf(line, "%d", &val); + *ptr=val; + list->last++; + + return list; +} + +unsigned long long badnum(list, cur, nsum) + struct list *list; + int cur; + unsigned nsum; +{ + unsigned long long *ptr=list->list; + unsigned long long res=*(ptr+cur); + int start=cur-nsum, i, j; + + for (i=start; ilast; + unsigned long long *ptr=list->list, cur; + unsigned long long min=*(ptr+start), max=min; + + for (i=start; i max) + max=cur; + if (sum == target) { + return min+max; + } + } + return 0; +} + +int main(ac, av) + int ac; + char **av; +{ + unsigned nsum=DEFNSUM; + char line[80]; + struct list *list=NULL; + unsigned i; + unsigned long long res1, res2; + + if (ac==2) { + nsum=atoi(*(av+1)); + } + + while (fgets(line, sizeof line, stdin)) { + list=add_line(list, line); + } + //print_list(list); + for (i=nsum; ilast; ++i) { + if (res1=badnum(list, i, nsum)) { + //printf("%s : res=%llu\n", *av, res1); + break; + } + } + for (i=0; ilast; ++i) { + if (res2=findsum(list, res1, i)) { + printf("%s : res=%llu sum=%llu\n", *av, res1, res2); + break; + } + } + exit (0); +} diff --git a/day09/ex2.bash b/day09/ex2.bash new file mode 100755 index 0000000..cf9db19 --- /dev/null +++ b/day09/ex2.bash @@ -0,0 +1,75 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 8/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -i N=5 SIZE +declare -a LIST=() + +function push25 () { + local i + for ((i=0; i<25; ++i)); do + read -r i + LIST+=("$i") + done +} + +function push() { + local i + while read -r i; do + LIST+=("$i") + done + SIZE=${#LIST[@]} +} + +function badnum() { + local -i cur="$1" i j + local -i res=${LIST[$cur]} + local -i start=$((cur-N-1)) + for ((i=start; ihigh)) && high=$cur + + if ((sum==target)); then + echo $((small+high)) + return 0 + fi + done + return 1 +} + + +[[ $# = 1 ]] && N=$1 + +push +declare -i i target num +for ((i=N; i&1 > OUTPUT + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day10/OUTPUT b/day10/OUTPUT new file mode 100644 index 0000000..100a55d --- /dev/null +++ b/day10/OUTPUT @@ -0,0 +1,18 @@ +cc -w ex1-c.c -o ex1-c +ex1.bash : diff1=71 diff2=27 res=1917 + time: 0:00.01 real, 0.00 user, 0.00 sys + context-switch: 1+4, page-faults: 0+331 + +ex1-c : diff1=71 diff2=27 res=1917 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + +cc -w ex2-c.c -o ex2-c +ex2.bash : size=99 res=113387824750592 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 2+4, page-faults: 0+329 + +ex2-c : size=99 res=113387824750592 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+73 + diff --git a/day10/README b/day10/README new file mode 100644 index 0000000..e6e69a9 --- /dev/null +++ b/day10/README @@ -0,0 +1,150 @@ +--- Day 10: Adapter Array --- + +Patched into the aircraft's data port, you discover weather forecasts of a massive tropical storm. Before you can figure out whether it will impact your vacation plans, however, your device suddenly turns off! + +Its battery is dead. + +You'll need to plug it in. There's only one problem: the charging outlet near your seat produces the wrong number of jolts. Always prepared, you make a list of all of the joltage adapters in your bag. + +Each of your joltage adapters is rated for a specific output joltage (your puzzle input). Any given adapter can take an input 1, 2, or 3 jolts lower than its rating and still produce its rated output joltage. + +In addition, your device has a built-in joltage adapter rated for 3 jolts higher than the highest-rated adapter in your bag. (If your adapter list were 3, 9, and 6, your device's built-in adapter would be rated for 12 jolts.) + +Treat the charging outlet near your seat as having an effective joltage rating of 0. + +Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get to your resort and realize you can't even charge your device! + +If you use every adapter in your bag at once, what is the distribution of joltage differences between the charging outlet, the adapters, and your device? + +For example, suppose that in your bag, you have adapters with the following joltage ratings: + +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 + +With these adapters, your device's built-in joltage adapter would be rated for 19 + 3 = 22 jolts, 3 higher than the highest-rated adapter. + +Because adapters can only connect to a source 1-3 jolts lower than its rating, in order to use every adapter, you'd need to choose them like this: + + The charging outlet has an effective rating of 0 jolts, so the only adapters that could connect to it directly would need to have a joltage rating of 1, 2, or 3 jolts. Of these, only one you have is an adapter rated 1 jolt (difference of 1). + From your 1-jolt rated adapter, the only choice is your 4-jolt rated adapter (difference of 3). + From the 4-jolt rated adapter, the adapters rated 5, 6, or 7 are valid choices. However, in order to not skip any adapters, you have to pick the adapter rated 5 jolts (difference of 1). + Similarly, the next choices would need to be the adapter rated 6 and then the adapter rated 7 (with difference of 1 and 1). + The only adapter that works with the 7-jolt rated adapter is the one rated 10 jolts (difference of 3). + From 10, the choices are 11 or 12; choose 11 (difference of 1) and then 12 (difference of 1). + After 12, only valid adapter has a rating of 15 (difference of 3), then 16 (difference of 1), then 19 (difference of 3). + Finally, your device's built-in adapter is always 3 higher than the highest adapter, so its rating is 22 jolts (always a difference of 3). + +In this example, when using every adapter, there are 7 differences of 1 jolt and 5 differences of 3 jolts. + +Here is a larger example: + +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 + +In this larger example, in a chain that uses all of the adapters, there are 22 differences of 1 jolt and 10 differences of 3 jolts. + +Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt differences multiplied by the number of 3-jolt differences? + +Your puzzle answer was 1917. +--- Part Two --- + +To completely determine whether you have enough adapters, you'll need to figure out how many different ways they can be arranged. Every arrangement needs to connect the charging outlet to your device. The previous rules about when adapters can successfully connect still apply. + +The first example above (the one that starts with 16, 10, 15) supports the following arrangements: + +(0), 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, (22) +(0), 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 5, 7, 10, 12, 15, 16, 19, (22) +(0), 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 6, 7, 10, 12, 15, 16, 19, (22) +(0), 1, 4, 7, 10, 11, 12, 15, 16, 19, (22) +(0), 1, 4, 7, 10, 12, 15, 16, 19, (22) + +(The charging outlet and your device's built-in adapter are shown in parentheses.) Given the adapters from the first example, the total number of arrangements that connect the charging outlet to your device is 8. + +The second example above (the one that starts with 28, 33, 18) has many arrangements. Here are a few: + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 48, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 48, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 46, 49, (52) + +(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31, +32, 33, 34, 35, 38, 39, 42, 45, 47, 48, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +46, 48, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +46, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +47, 48, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +47, 49, (52) + +(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, +48, 49, (52) + +In total, this set of adapters can connect the charging outlet to your device in 19208 distinct arrangements. + +You glance back down at your bag and try to remember why you brought so many adapters; there must be more than a trillion valid ways to arrange them! Surely, there must be an efficient way to count the arrangements. + +What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device? + +Your puzzle answer was 113387824750592. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day10/TEST.txt b/day10/TEST.txt new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/day10/TEST.txt @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 diff --git a/day10/TEST2.txt b/day10/TEST2.txt new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/day10/TEST2.txt @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 diff --git a/day10/ex1-c.c b/day10/ex1-c.c new file mode 100644 index 0000000..54a00dd --- /dev/null +++ b/day10/ex1-c.c @@ -0,0 +1,132 @@ +/* ex1-c: Advent2020 game, day 10/game 1 + */ + +#include +#include +#include + +struct list { + unsigned size; + unsigned last; + unsigned long long *list; +}; + +#define DEFNSUM 25 +#define BLOCKSIZE 1024 /* number of elements for realloc() */ + +void merge(array, left, mid, right) + unsigned long long *array; + int left, mid, right; +{ + unsigned long long temp[right-left+1]; + int pos=0, lpos = left, rpos = mid + 1, iter; + while(lpos <= mid && rpos <= right) { + if(array[lpos] < array[rpos]) { + temp[pos++] = array[lpos++]; + } + else { + temp[pos++] = array[rpos++]; + } + } + while(lpos <= mid) temp[pos++] = array[lpos++]; + while(rpos <= right)temp[pos++] = array[rpos++]; + for(iter = 0;iter < pos; iter++) { + array[iter+left] = temp[iter]; + } + return; +} + +void mergesort(array, left, right) + unsigned long long *array; + int left, right; +{ + int mid = (left+right)/2; + if(leftlast; + unsigned long long *ptr=list->list; + + fprintf(stderr, "LIST: address=%p pinstr=%p size=%d\n", list, ptr, psize); + for (i=0; ilist=malloc(sizeof(unsigned long long)*BLOCKSIZE); + list->size=BLOCKSIZE; + list->last=0; + } + cur=list->last; + size=list->size; + + if (cur == size) { + size+=BLOCKSIZE; + list->size=size; + list->list=realloc(list->list, sizeof(unsigned long long)*size); + fprintf(stderr, "realloc buf: cur=%d size=%d ptr=%p\n", cur, size, list->list); + } + ptr=list->list+cur; + + //sscanf(line, "%d", &val); + *ptr=val; + list->last++; + + return list; +} + +unsigned *calc(list) + struct list *list; +{ + static unsigned res[4]; + unsigned long long *ptr=list->list; + unsigned last=list->last, i; + + for (i=0; i<4; ++i) + res[i]=0; + for (i=1; ilist, 0, list->last-1); + last=list->list[list->last-1]; + list=add_val(list, last+3); + //print_list(list); + result=calc(list); + printf("%s : diff1=%u diff2=%u res=%u\n", *av, result[1], result[3], + result[1]*result[3]); + exit (0); +} diff --git a/day10/ex1.bash b/day10/ex1.bash new file mode 100755 index 0000000..f1e139b --- /dev/null +++ b/day10/ex1.bash @@ -0,0 +1,31 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 10/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -a numbers +readarray -t numbers <<< "$(sort -n)" +numbers=(0 "${numbers[@]}") +size=${#numbers[@]} +((last=${numbers[size-1]}+3)) +numbers+=("$last") +((size++)) +#echo S="$size" $last "[${numbers[@]}]" + +# last +declare -a res=(0 0 0 0) + +for ((i=1; i +#include +#include + +struct list { + unsigned size; + unsigned last; + unsigned long long *list; +}; + +#define DEFNSUM 25 +#define BLOCKSIZE 1024 /* number of elements for realloc() */ + +void merge(array, left, mid, right) + unsigned long long *array; + int left, mid, right; +{ + unsigned long long temp[right-left+1]; + int pos=0, lpos = left, rpos = mid + 1, iter; + while(lpos <= mid && rpos <= right) { + if(array[lpos] < array[rpos]) { + temp[pos++] = array[lpos++]; + } + else { + temp[pos++] = array[rpos++]; + } + } + while(lpos <= mid) temp[pos++] = array[lpos++]; + while(rpos <= right)temp[pos++] = array[rpos++]; + for(iter = 0;iter < pos; iter++) { + array[iter+left] = temp[iter]; + } + return; +} + +void mergesort(array, left, right) + unsigned long long *array; + int left, right; +{ + int mid = (left+right)/2; + if(leftlast; + unsigned long long *ptr=list->list; + + fprintf(stderr, "LIST: address=%p pinstr=%p size=%d\n", list, ptr, psize); + for (i=0; ilist=malloc(sizeof(unsigned long long)*BLOCKSIZE); + list->size=BLOCKSIZE; + list->last=0; + } + cur=list->last; + size=list->size; + + if (cur == size) { + size+=BLOCKSIZE; + list->size=size; + list->list=realloc(list->list, sizeof(unsigned long long)*size); + fprintf(stderr, "realloc buf: cur=%d size=%d ptr=%p\n", cur, size, list->list); + } + ptr=list->list+cur; + + //sscanf(line, "%d", &val); + *ptr=val; + list->last++; + + return list; +} + +unsigned long long calc(list) + struct list *list; +{ + unsigned long long *res, result; + unsigned long long *ptr=list->list; + unsigned last=list->last, ival, jval; + unsigned diff, i, j; + + res=malloc(sizeof(unsigned long long)*list->last); + + for (i=1; i 0 && diff < 4) { + res[j]+=res[i]; + } + } + } + } + result=res[last-1]; + free(res); + return result; +} + +int main(ac, av) + int ac; + char **av; +{ + char line[80]; + struct list *list=NULL; + unsigned long long res, last; + + list=add_val(list, 0); + while (fgets(line, sizeof line, stdin)) { + sscanf(line, "%llu", &res); + list=add_val(list, res); + } + mergesort(list->list, 0, list->last-1); + last=list->list[list->last-1]; + list=add_val(list, last+3); + //print_list(list); + res=calc(list); + printf("%s : size=%u res=%llu\n", *av, list->last, res); + exit (0); +} diff --git a/day10/ex2.bash b/day10/ex2.bash new file mode 100755 index 0000000..726d3bc --- /dev/null +++ b/day10/ex2.bash @@ -0,0 +1,39 @@ +#!/bin/bash +# +# ex2.bash: Advent2020 game, day 10/game 2. + +CMD=${0##*/} +shopt -s extglob + +declare -a numbers +readarray -t numbers <<< "$(sort -nr)" +numbers+=(0) +((last=numbers[0]+3)) +numbers=("$last" "${numbers[@]}") +size=${#numbers[@]} + +declare -a res +for ((i=1; i 0 && diff <= 3)); then + ((res[next]+=r)) + fi + fi + done +done + +printf "%s : size=%d res=%d\n" "$CMD" "$size" "${res[size-1]}" + +exit 0 diff --git a/day11/INPUT.txt b/day11/INPUT.txt new file mode 100644 index 0000000..7b26308 --- /dev/null +++ b/day11/INPUT.txt @@ -0,0 +1,98 @@ +LLLLL.LLLL.LLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLL.LL.LLLLLLLLL.LLL +LLLLLLLL.L.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLL.LLL.LLL.LLLLL.LLLL.L..LLLLLL.LLLLLLLL +LLLLLLLLLLLLLLLLLLLLLLLLLLL.LL.LLLLLLLLLLLLLL..LLLLLLLL.L.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLL.LLL.LLLLL.LL +LLLLLLLLLL.LLLLL.LLL.LLLLLL.LLLLLLL..LLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLL..LLLLLL.LLLLLLLL +LLLLLLLLLL..LLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LL.LLLL.LLLLLLLLLLLL.LLLLLLL.LLLLLL.L +L.LL.L...LL......LLL........LL...L.L.L...L...L....L........LL......L.............LL..LL.L...L +LLLLLLLLL..LL.LLLL.L.LL.LLL.LLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL +LLLLLL.LLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLL.LL.LLLLLLLLL.L.LLLLL.LLLLL.L.LLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLL.L.LLLLLLLLL.LLL..LLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.L.LLLL.LLLLLLL.LLLLLLLL +LLLLLLLL.L.LLLLLLL.LLLL.LLLLLLLLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLL.L.LLLLLLLLLL.LLLLLLLLLLLLLL.L +...L..L..L..L...L..L...L............L......L.....L...LLL.........L..L.LL.LL.LL.LLLL.......... +LLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLL.LLL.LLL.LLLLLLLL +LL.LLLLLL..LLLLLLL.LLL.LLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLL.LLLLL.LLLLLLLL.LLLLLLLL +LLLLLLLLLLLLLLLLLL.LLLLLLLL.L.LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLL.LL.LL.LLLLLLLLLLLLLLLLLL.LLLL +LLLLLLLLLL.LLLLLLL.LLLLLLLL..LLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.L.LL.LLLLL +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLL.LL.LLLLLLLLL.LLLLLLL.LL.LL.LLL.LLLLLLLLLL.LLLLLLLL +LLLL.LLLLLLLLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLL.LL.LLLLL.LLLLLL.LLLLLLL.LLL.LLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LL.LLLLLLL.LL.LLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLL +LLLLLL.LLL..LL.LLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLL.LLLL.LLL.LLLLLLLL..LLLLLLLLLLLLLLLLLL.LLLLLLLLL +.LL...L......LLL.....L..LL..L.....L.LLLL.L......L.......L...L....L.L..L..LL.L.L.....L.L...... +LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LL.LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLL.LL.LLLLL +LLLLLLLLLL.LL.LLLL.LLLLLLLLLLLLL.LL.LLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLL..LLLLLLLL.LL.LL.LLLLLLLL +LLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLL.L..LLLLLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLL.LL.LLLL.LLLLLLLLLLLLLLLL.LLLLLL.LL.LLLLLLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLLLLLLLLLL.LLLLLLLL.LLL.LLL.LLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLL..LLLLLLL +LLLLLLLLLL.LLLLLLLLLLLL.LLL.LLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLL.L +LLLLLL.LLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLLLL.LLLLLLLLL.L.L.LLL.LLLLL..LLLLLLLLLLLLL.LLLLLLLL +LLLLLL.LLLLLLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLL..LLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLL.LLL.LLLLLLLL +LLLLL...L..L.L..L.L..LL..L..L...L.LL.LLLLL.L...........L....LLLLLL..L..L..L.LL.LL...LL.L..... +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.L.LLLLLLLLLLL.LLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL..LLLLL.LLLLLLLLLL.LLLLL.LLLLLL.LLLLLLL.LLLLLLLL +L.LLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL..LLLLL.LL.LLLLLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLLLL..LLLLLLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL..LLLLLLLL.LLLLLLL.LLLLL.LL.LLL.LLLL.LL.LL.LLLLL +.L....L.L.L...LL..L.L.LL..L.LL..LL..LLL.LL..L..L.LL..L.......L.....L...LL......LLLL...L.....L +LLLLLLLLLL.LLLLLLLLLLLL.L.L.LLL.LLL.LLLL..LLL.LLLLL.LLL.LLLLLLLLLLLL..LLLLLLLLLLLLLL..LLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LL.LLLL.LLLL.LLLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLL..LLLLLL.LLLLLLLLL +LLLLLLLLLL.LLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLL..LLLLLLL.LLLLLLLL.LLLLLLL.LLLL.LLLL.LLLLLLLLL.LLLLLLL.L.LLL.LLLLLLLLLLLLLLLLLLLLLLL +LLLLLLLLLLLLLL.LLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLL..LLLLLLLL +L......LLLL...LLL.L..L.LL...LL.L.....L.LL...L...LL..LL..LLL..L.L.LLL..L..L...L.LLL..L....L... +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLL.LLLLLLLLL.LL..LLLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLL +LLLLLLLLLL..LLLLLL..LLLLLLL..LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLL.L.LLLLLL +LLLLLLLLLLLLLLLLLL.LLLLLLLLLLL..LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLL.LLLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.L.LLL.LLLLLL.LLL.LLL.LL.LLLLL +..LL.LLLLL.L...L...LL.......L.........LL..L.LLL.....LL..L.L.LL..L..L.L..L.L..L...L.L.......LL +LLLLLLLLLL.LLL.L.L.LLLLLLLLLLL.LL.L.LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLL.LLLLLLLLLLLLLLLLL +LLLLLLLLLL.LLLL.LLLLLL..LLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLL.LLLLL.LLLLLLLLLLLL.LLLLLLL.L..LLLLL +LLLLLLLLLLLLLLLLLLL..LLLLLLLL.LL.LL.LL.LLLLLL.LLLL.LLLLLLLL..LL.LLLLLLLLLLLL.LLLLLLL.L.LLLLLL +LLLLLLLLL..LLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLL..LLLLLLLLLLLL.LLLLLL.LLLLL.LLL.LLLLLLL.LLLLL.LLLLL..LLLLLLL.LLLLLLLL +LLLLLLLL..LLLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLL.LL.LLLL.LLL.LLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLL.LLLLLL.LLLLLLL..LLLLL.L +LL...LL........L.L....L..L....LLL.L...LL.L......L.LL..L...L.L.L.L....L..L.L...L.L.L....L.L..L +LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.L.LLLLLLLL.LLLLLLLL.LLLLLL..LLLLLLL.LLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LL.LLLLLLLLLLLLL +LLLLLLLLL..LLLLLLL.LLLLL.LLLLLLL.LL.LLLLLLLLL.LLLLL.LLL.LLLLLLL..LLLL.LLL.LL.LLLLLLL.LLLLLLLL +LL.LLLLLLL.LLLLLLL.LLLLL.LL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL +L.LL.LLLLLLLLLLLLL.LLLLLLLL.LL.L.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLL..LLLLLLL.LLLLLLLL +LLLLL.LLLL.LLLLLLL.LLLLLLLL.L.L.LL.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLLLLLLLLLLLLL +LLLLLLLLLL.LLLLLLLLLLLLL.LL.LLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLL.L.LLLL.LL.LL.LLLLL +...L.....L............L...L.LL.L.L.L.L...LLL.L.LL.......LL.L.LLLL..LL......L...LLL..L....L... +LLLLLLLL.L.LLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLL.LLLLLLLLLLLLLLL..LLLLLLLLLLL.L.LLLLLLLLLLLLLLLL +LLLLLLL..LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLLLLL.LLLLLLLL +.LL.LLLL.L.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLL..LL.LL.L.LLLLL.LLLLLLLLLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLL.LLL.LLLLLLLLLLLLL.L.LLL.LLLLLL.L.LLLLL.LLLLLLL. +LLLLLLLLLLLL.L.LLL.LLLLLLLL.LL.LLLL.LLLLLLLLL.LLL.L.LLL.LLLLLLL.LLLLLLLLLLLL.LLLLLL..LLLLL..L +.L..LL.......L..L..L.L........LL...LL.LLLLL........L.LLL......L..L.....L.LL....LLL.....LLL.L. +.LLLLLLL.L.LLLLL.L.LLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL..LLLLLLLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LL.LLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLL.L.LLLLLLLLLLLL.LLLLLLLL +LLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLL.LLLLLLLLLLLL +LLLLLLLLLLLLLLLLLL.LL.LL.LL.LLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL +LLL.LLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLLLLL.LLLLLLL.LLLLLLLL +LLLLL.LLLLLLLLLLLL.LL.LLLLL.LLLLL.L.LLLLLL.L..LLLLLLLL..LLLL.LLL.LLLLLLLLLLL.LLLLLLLLLLLL.LLL +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLL +LL..L....L...L.L........L.L....L..L..L...L..L..L........L...LLL..L.LL..L........LL..........L +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLL.L.LLLLLLLLL.LLLL.LL.LLLLL.LLLLLLLLLLL.LL.LLLLLLL. +LLLLLLLLLL.LLLLLLL.LLLLLL.L.LL.L.LL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLLL.LLL.LLL.L.LLLLLLLLL.LLLLLL.LL.LLLLLL..LLLLL.LLLLLL.LLL.LLLLLLLLLLLL +LLLLLLL.LL.LLLL.LL.LLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLLL +.LL.LLLLLLL.LLLLLLLLLLL.LLL.LLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLL.LL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLL +LL.LLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLLLLLLLLLLL.LLLLLLLLL..LLLL.L.LLLLLLLLLLLLLL.LLLLL.LLLLLLLL +.....L.....LLLL......LL.L...LL...L..L........L...L..LL..LL.LL.LL..LL...L.......L..LLL..L...LL +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLL.L.LLL.LLLLLL.LLLLLL..LLLLLLLL +.LLLLLLL.L.LL.LL.L.LLLLL.L..LLLLLLLLLLLLLLLLLLLLLLLL.LL.LLLLLLLLLLLLL.LLLLLL.LLLL.LL.LLLLLLLL +LLLLLLLLLL..LLLLLL.LLLLLLLL.LLLLLLL..LLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLL.LL.LLL.LL.LLLL. +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLL.LLL.LL.LL.LLLLL.LLL.LLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLLL.LLLL.LL.LLL.L.LLL.LLLLLLL.LLL.L.LLLL.LLLLLLLLL.LLLLLLLL +..L...LLL..LL...L.L.LLLL..L.L...L..LLL...L...LLL.....L.....LL.L....L....L.L..L.L.L....L..L..L +LLLLLLLLLLLLLLLLLL.LLL.LLLL.LLLLLLL.LLLLLLLLL.LL.LLLL.LLLLL.L.L.LLLLL.LLLLLL.LLL.LLL.LLLLLL.L +LLLLLLLLLL.LLLLLLLLLLLLLLLL.LL.LL.L.LLLLLLLLL..LL.LLLLL.LLLLLLLLLLL.L.L.L.LL.LLLLLLL.LLLLLLLL +LLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLL.LL.LLLLLLLLLLLLLLLLLLL.LL.LL.L.LLLLL.LLLLLLLLL.LLLL.LLLLLLLL +LLLLLLLLLL..LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.L.LLLLLLL.LLLLLLLLLLLL.LLLLLLL..LLLLLLL +LL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLL.LLLLLL.LLL..LL.LLLLLLLL diff --git a/day11/Makefile b/day11/Makefile new file mode 100644 index 0000000..9ceff1b --- /dev/null +++ b/day11/Makefile @@ -0,0 +1,30 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +#CFLAGS := -w -pg +CFLAGS := -w -O3 +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 deploy ex1 ex2 + +all: ex1 ex2 + +output: + @$(MAKE) --no-print-directory all 2>&1 > OUTPUT + +compile: ex1-c ex2-c + +ex1: ex1-c + @#$(TIME) ex1-slow.bash < $(INPUT) 2>&1 + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day11/OUTPUT b/day11/OUTPUT new file mode 100644 index 0000000..ad78ce3 --- /dev/null +++ b/day11/OUTPUT @@ -0,0 +1,16 @@ +ex1.bash : res=2386 + time: 0:41.94 real, 41.86 user, 0.03 sys + context-switch: 4503+14, page-faults: 0+1329 + +ex1-c : res=2386 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 1+1, page-faults: 0+91 + +ex2.bash : res=2091 + time: 1:20.09 real, 80.00 user, 0.03 sys + context-switch: 8232+13, page-faults: 0+1721 + +ex2-c : res=2091 + time: 0:00.01 real, 0.01 user, 0.00 sys + context-switch: 2+1, page-faults: 0+91 + diff --git a/day11/README b/day11/README new file mode 100644 index 0000000..19b1c6b --- /dev/null +++ b/day11/README @@ -0,0 +1,219 @@ +--- Day 11: Seating System --- + +Your plane lands with plenty of time to spare. The final leg of your journey is a ferry that goes directly to the tropical island where you can finally start your vacation. As you reach the waiting area to board the ferry, you realize you're so early, nobody else has even arrived yet! + +By modeling the process people use to choose (or abandon) their seat in the waiting area, you're pretty sure you can predict the best place to sit. You make a quick map of the seat layout (your puzzle input). + +The seat layout fits neatly on a grid. Each position is either floor (.), an empty seat (L), or an occupied seat (#). For example, the initial seat layout might look like this: + +L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL + +Now, you just need to model the people who will be arriving shortly. Fortunately, people are entirely predictable and always follow a simple set of rules. All decisions are based on the number of occupied seats adjacent to a given seat (one of the eight positions immediately up, down, left, right, or diagonal from the seat). The following rules are applied to every seat simultaneously: + + If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied. + If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty. + Otherwise, the seat's state does not change. + +Floor (.) never changes; seats don't move, and nobody sits on the floor. + +After one round of these rules, every seat in the example layout becomes occupied: + +#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.## + +After a second round, the seats with four or more occupied adjacent seats become empty again: + +#.LL.L#.## +#LLLLLL.L# +L.L.L..L.. +#LLL.LL.L# +#.LL.LL.LL +#.LLLL#.## +..L.L..... +#LLLLLLLL# +#.LLLLLL.L +#.#LLLL.## + +This process continues for three more rounds: + +#.##.L#.## +#L###LL.L# +L.#.#..#.. +#L##.##.L# +#.##.LL.LL +#.###L#.## +..#.#..... +#L######L# +#.LL###L.L +#.#L###.## + +#.#L.L#.## +#LLL#LL.L# +L.L.L..#.. +#LLL.##.L# +#.LL.LL.LL +#.LL#L#.## +..L.L..... +#L#LLLL#L# +#.LLLLLL.L +#.#L#L#.## + +#.#L.L#.## +#LLL#LL.L# +L.#.L..#.. +#L##.##.L# +#.#L.LL.LL +#.#L#L#.## +..L.L..... +#L#L##L#L# +#.LLLLLL.L +#.#L#L#.## + +At this point, something interesting happens: the chaos stabilizes and further applications of these rules cause no seats to change state! Once people stop moving around, you count 37 occupied seats. + +Simulate your seating area by applying the seating rules repeatedly until no seats change state. How many seats end up occupied? + +Your puzzle answer was 2386. +--- Part Two --- + +As soon as people start to arrive, you realize your mistake. People don't just care about adjacent seats - they care about the first seat they can see in each of those eight directions! + +Now, instead of considering just the eight immediately adjacent seats, consider the first seat in each of those eight directions. For example, the empty seat below would see eight occupied seats: + +.......#. +...#..... +.#....... +......... +..#L....# +....#.... +......... +#........ +...#..... + +The leftmost empty seat below would only see one empty seat, but cannot see any of the occupied ones: + +............. +.L.L.#.#.#.#. +............. + +The empty seat below would see no occupied seats: + +.##.##. +#.#.#.# +##...## +...L... +##...## +#.#.#.# +.##.##. + +Also, people seem to be more tolerant than you expected: it now takes five or more visible occupied seats for an occupied seat to become empty (rather than four or more from the previous rules). The other rules still apply: empty seats that see no occupied seats become occupied, seats matching no rule don't change, and floor never changes. + +Given the same starting layout as above, these new rules cause the seating area to shift around as follows: + +L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL + +#.##.##.## +#######.## +#.#.#..#.. +####.##.## +#.##.##.## +#.#####.## +..#.#..... +########## +#.######.# +#.#####.## + +#.LL.LL.L# +#LLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLL# +#.LLLLLL.L +#.LLLLL.L# + +#.L#.##.L# +#L#####.LL +L.#.#..#.. +##L#.##.## +#.##.#L.## +#.#####.#L +..#.#..... +LLL####LL# +#.L#####.L +#.L####.L# + +#.L#.L#.L# +#LLLLLL.LL +L.L.L..#.. +##LL.LL.L# +L.LL.LL.L# +#.LLLLL.LL +..L.L..... +LLLLLLLLL# +#.LLLLL#.L +#.L#LL#.L# + +#.L#.L#.L# +#LLLLLL.LL +L.L.L..#.. +##L#.#L.L# +L.L#.#L.L# +#.L####.LL +..#.#..... +LLL###LLL# +#.LLLLL#.L +#.L#LL#.L# + +#.L#.L#.L# +#LLLLLL.LL +L.L.L..#.. +##L#.#L.L# +L.L#.LL.L# +#.LLLL#.LL +..#.L..... +LLL###LLL# +#.LLLLL#.L +#.L#LL#.L# + +Again, at this point, people stop shifting around and the seating area reaches equilibrium. Once this occurs, you count 26 occupied seats. + +Given the new visibility method and the rule change for occupied seats becoming empty, once equilibrium is reached, how many seats end up occupied? + +Your puzzle answer was 2091. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day11/TEST.txt b/day11/TEST.txt new file mode 100644 index 0000000..1beaede --- /dev/null +++ b/day11/TEST.txt @@ -0,0 +1,10 @@ +L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL diff --git a/day11/ex1-c.c b/day11/ex1-c.c new file mode 100644 index 0000000..dc58071 --- /dev/null +++ b/day11/ex1-c.c @@ -0,0 +1,187 @@ +/* ex1-c: Advent2020 game, day 10/game 1 + */ + +#include +#include +#include + +struct seats { + char status; + unsigned neighbours; +}; + +struct plane { + unsigned nrow; + unsigned ncol; + unsigned size; + unsigned last; + unsigned seated; + struct seats *seats; +}; + +#define BLOCKSIZE (10*1024) /* number of elements for realloc() */ + +void print_count(plane) + struct plane *plane; +{ + unsigned i, psize=plane->last; + unsigned ncols=plane->ncol; + struct seats *ptr=plane->seats; + + for (i=0; i0 && !(i%ncols)) { + putchar('\n'); + } + printf("%2d ", (ptr+i)->neighbours); + } + putchar('\n'); +} + +void print_seats(plane) + struct plane *plane; +{ + unsigned i, psize=plane->last; + unsigned ncols=plane->ncol, nrow=plane->nrow; + struct seats *ptr=plane->seats; + + fprintf(stderr, "PLANE: address=%p seat=%p rows=%d cols=%d size=%d\n", + plane, ptr, nrow, ncols, psize); + for (i=0; i0 && !(i%ncols)) { + putchar('\n'); + } + printf("%c ", (ptr+i)->status); + } + putchar('\n'); +} + +void reset_seats(plane) + struct plane *plane; +{ + unsigned i, last=plane->last; + struct seats *seat=plane->seats; + + for (i=0; ineighbours=0; +} + +struct plane *add_row(plane, c) + struct plane *plane; + char *c; +{ + unsigned size; + + if (!plane) { + plane=malloc(sizeof(struct plane)); + plane->seats=malloc(sizeof(struct seats)*BLOCKSIZE); + plane->size=BLOCKSIZE; + plane->ncol=strlen(c)-1; + plane->last=0; + } + size=plane->size; + + while (*c) { + if (*c != '\n') { + if (plane->last == size) { + size+=BLOCKSIZE; + plane->size=size; + plane->seats=realloc(plane->seats, sizeof(struct seats)*size); + } + plane->seats[plane->last].status=*c; + plane->last++; + plane->nrow=plane->last/plane->ncol; + } + c++; + } + //sscanf(line, "%d", &val); + + return plane; +} + +int sit(plane) + struct plane *plane; +{ + unsigned changed=0, cur, seated=0; + unsigned last=plane->last; + struct seats *seats=plane->seats; + + for (cur=0; cur= 4) { + seats[cur].status='L'; + changed++; + } else { + seated++; + } + break; + case 'L': + if (seats[cur].neighbours == 0) { + seats[cur].status='#'; + changed++; + seated++; + } + break; + } + } + plane->seated=seated; + return changed; +} + +int calc(plane) + struct plane *plane; +{ + unsigned row, col, cur; + unsigned last=plane->last; + unsigned cols=plane->ncol; + unsigned rows=plane->nrow; + struct seats *seats=plane->seats; + + for (cur=0; cur 0) { + seats[cur - cols].neighbours++; + if (col > 0) + seats[cur - cols - 1].neighbours++; + if (col < (cols-1)) + seats[cur - cols + 1].neighbours++; + } + if (col > 0) { + seats[cur - 1].neighbours++; + if (row < (rows-1)) + seats[cur + cols - 1].neighbours++; + } + if (col < (cols-1)) { + seats[cur + 1].neighbours++; + if (row < (rows-1)) + seats[cur + cols + 1].neighbours++; + } + if (row < (rows-1)) + seats[cur + cols].neighbours++; + } + } + return 1; +} + +int main(ac, av) + int ac; + char **av; +{ + char line[512]; + struct plane *plane=NULL; + + while (fgets(line, sizeof line, stdin)) { + plane=add_row(plane, line); + } + do { + reset_seats(plane); + calc(plane); + } while (sit(plane) > 0); + + printf("%s : res=%d\n", *av, plane->seated); + exit (0); +} diff --git a/day11/ex1-slow.bash b/day11/ex1-slow.bash new file mode 100755 index 0000000..81bc468 --- /dev/null +++ b/day11/ex1-slow.bash @@ -0,0 +1,175 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 10/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -a rowsstr rows +declare -i RLENGTH +declare -i NROWS +declare floor + +function adj() { + local -i r="$1" n="$2" c="$3" count=0 + local -a prow=(${rows[r-1]}) + local -a row=(${rows[r]}) + local -a nrow=(${rows[r+1]}) + #echo + #echo p="${prow[*]}" + #echo r="${row[*]}" + #echo p1=${prow[1]} + #echo r1=${row[1]} + #return + #echo r="$row" + #echo n="$nrow" + if (( row[n] == 0 )); then + echo "-1" + return + fi + for ((i=n-1; i 0)); do + changed=0 + seated=0 + for ((r=1; r<=NROWS; ++r)); do + row=(${rows[r]}) + newrow=(0) + for ((c=1; c<=RLENGTH; ++c)); do + newrow+=("${row[c]}") + #if ((r == 1 && c ==1 )); then + # printf "r1c1: %s %d\n" "${row[c]}" $(adj $r $c 2) + #fi + case ${row[c]} in + 0) continue + ;; + 1) if (( $(adj $r $c 2) == 0 )); then + ((++changed)) + newrow[c]=2 + fi + #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 + ((++changed)) + newrow[c]=1 + fi + ;; + esac + ((newrow[c] == 2)) && ((seated++)) + done + newrow+=(0) + new[$r]="${newrow[*]}" + #echo "${newrow[*]} ;" + done + #echo "$floor" + #echo "${new[*]}" + #echo changed=$changed + ((loop++)) + #echo loop="$loop" + for ((r=1; r<=NROWS; ++r)); do + rows[r]="${new[$r]}" + done + #printf "loop %d seated %d\n" "$loop" "$seated" >&2 + done + echo "$seated" +} + + + +readarray -t rowsstr + +for ((i=0; i<${#rowsstr[@]}; ++i)); do + rowsstr[i]=${rowsstr[i]//./0} + rowsstr[i]=${rowsstr[i]//L/1} + #echo "${rowsstr[i]}" +done + +RLENGTH=${#rowsstr[0]} +NROWS=${#rowsstr[@]} +#echo "L=$RLENGTH N=$NROWS" + +# add floor rows at beginning and end +printf -v floor '%0.s0 ' $(seq 1 $((RLENGTH+2))) +#echo floor="$floor" +#echo floor="$floor" +#echo +#for ((i=0; i<${#rowsstr[@]}; ++i)); do +# rowsstr[i]=0${rowsstr[i]}0 +# echo "${rowsstr[i]}" +#done + +# split arrays in integers +for ((r=0; r 0)); do + changed=0 + seated=0 + read -ra control <<< "$zeroed" + for ((r=0; r= 4 )); then + ((++changed)) + seats[cell]=1 + fi + esac + #printf "r=%d c=%d cell=%d val=%s\n" "$r" "$c" "$cell" "${seats[cell]}" + done + #echo + done + + for ((r=0; r +#include +#include + +struct seats { + char status; + unsigned neighbours; +}; + +struct plane { + unsigned nrow; + unsigned ncol; + unsigned size; + unsigned last; + unsigned seated; + struct seats *seats; +}; + +#define BLOCKSIZE (10*1024) /* number of elements for realloc() */ + +void print_count(plane) + struct plane *plane; +{ + unsigned i, psize=plane->last; + unsigned ncols=plane->ncol; + struct seats *ptr=plane->seats; + + for (i=0; i0 && !(i%ncols)) { + putchar('\n'); + } + printf("%2d ", (ptr+i)->neighbours); + } + putchar('\n'); +} + +void print_seats(plane) + struct plane *plane; +{ + unsigned i, psize=plane->last; + unsigned ncols=plane->ncol, nrow=plane->nrow; + struct seats *ptr=plane->seats; + + fprintf(stderr, "PLANE: address=%p seat=%p rows=%d cols=%d size=%d\n", + plane, ptr, nrow, ncols, psize); + for (i=0; i0 && !(i%ncols)) { + putchar('\n'); + } + printf("%c ", (ptr+i)->status); + } + putchar('\n'); +} + +void reset_seats(plane) + struct plane *plane; +{ + unsigned i, last=plane->last; + struct seats *seat=plane->seats; + + for (i=0; ineighbours=0; +} + +struct plane *add_row(plane, c) + struct plane *plane; + char *c; +{ + unsigned size; + + if (!plane) { + plane=malloc(sizeof(struct plane)); + plane->seats=malloc(sizeof(struct seats)*BLOCKSIZE); + plane->size=BLOCKSIZE; + plane->ncol=strlen(c)-1; + plane->last=0; + } + size=plane->size; + + while (*c) { + if (*c != '\n') { + if (plane->last == size) { + size+=BLOCKSIZE; + plane->size=size; + plane->seats=realloc(plane->seats, sizeof(struct seats)*size); + } + plane->seats[plane->last].status=*c; + plane->last++; + plane->nrow=plane->last/plane->ncol; + } + c++; + } + + return plane; +} + +int sit(plane) + struct plane *plane; +{ + unsigned changed=0, cur, seated=0; + unsigned last=plane->last; + struct seats *seats=plane->seats; + + for (cur=0; cur= 5) { + seats[cur].status='L'; + changed++; + } else { + seated++; + } + break; + case 'L': + if (seats[cur].neighbours == 0) { + seats[cur].status='#'; + changed++; + seated++; + } + break; + } + } + plane->seated=seated; + return changed; +} + +int calc(plane) + struct plane *plane; +{ + unsigned row, col, cur; + unsigned last=plane->last; + int cols=plane->ncol; + int rows=plane->nrow; + int c, r; + struct seats *seats=plane->seats; + reset_seats(plane); + for (cur=0; cur=0 && c>=0; --r, --c) { + seats[r*cols + c].neighbours++; + if (seats[r*cols + c].status != '.') + break; + } + // up right + for (r=row-1, c=col+1; r>=0 && c=0; ++r, --c) { + seats[r*cols + c].neighbours++; + if (seats[r*cols + c].status != '.') + break; + } + // down right + for (r=row+1, c=col+1; r=0; --r) { + seats[r*cols + c].neighbours++; + if (seats[r*cols + c].status != '.') + break; + } + // down + for (r=row+1, c=col; r=0; --c) { + seats[r*cols + c].neighbours++; + if (seats[r*cols + c].status != '.') + break; + } + // right + for (r=row, c=col+1; c 0); + + printf("%s : res=%d\n", *av, plane->seated); + exit (0); +} diff --git a/day11/ex2.bash b/day11/ex2.bash new file mode 100755 index 0000000..bb154eb --- /dev/null +++ b/day11/ex2.bash @@ -0,0 +1,184 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 11/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -a rowsstr rows seats control +declare -i NCOLS NROWS SIZE +declare zeroed + +function print_control() { + local -i r c + for ((r=0; r&2 + done + echo >&2 + done + echo >&2 +} +function print_seats() { + local -i r c + for ((r=0; r&2 + done + echo >&2 + done + echo >&2 +} +function run() { + local -i c r i cell changed=1 + #local -a control + local -i loop=0 seated=0 + + read -ra seats <<< "${rows[*]}" + #print_seats + while ((changed > 0)); do + changed=0 + seated=0 + read -ra control <<< "$zeroed" + for ((r=0; r=0 && c1>=0)); do + ((control[r1*NCOLS+c1]++)); + #echo up >&2 + ((seats[r1*NCOLS+c1] != 0)) && break + ((r1--)) + ((c1--)) + done + # diagonal up right + r1=$((r-1)) + c1=$((c+1)) + while ((r1>=0 && c1=0)); do + ((control[r1*NCOLS+c1]++)); + ((seats[r1*NCOLS+c1] != 0)) && break + ((r1++)) + ((c1--)) + done + # diagonal down right + r1=$((r+1)) + c1=$((c+1)) + while ((r1=0)); do + ((control[r*NCOLS+c1]++)); + ((seats[r*NCOLS+c1] != 0)) && break + ((c1--)) + done + # line right + c1=$((c+1)) + while ((c1=0)); do + ((control[r1*NCOLS+c]++)); + ((seats[r1*NCOLS+c] != 0)) && break + ((r1--)) + done + # column down + r1=$((r+1)) + while ((r1= 5 )); then + ((++changed)) + seats[cell]=1 + fi + esac + #printf "r=%d c=%d cell=%d val=%s\n" "$r" "$c" "$cell" "${seats[cell]}" >&2 + done + #echo >&2 + done + + for ((r=0; r&1 > OUTPUT + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/day12/OUTPUT b/day12/OUTPUT new file mode 100644 index 0000000..453f9f3 --- /dev/null +++ b/day12/OUTPUT @@ -0,0 +1,16 @@ +ex1.bash : res=1457 + time: 0:00.02 real, 0.02 user, 0.00 sys + context-switch: 5+1, page-faults: 0+152 + +ex1-c : res=1457 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+72 + +ex2.bash : res=106860 + time: 0:00.09 real, 0.08 user, 0.00 sys + context-switch: 10+1, page-faults: 0+153 + +ex2-c : res=106860 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 1+1, page-faults: 0+75 + diff --git a/day12/README b/day12/README new file mode 100644 index 0000000..a3d21f1 --- /dev/null +++ b/day12/README @@ -0,0 +1,76 @@ +--- Day 12: Rain Risk --- + +Your ferry made decent progress toward the island, but the storm came in faster than anyone expected. The ferry needs to take evasive actions! + +Unfortunately, the ship's navigation computer seems to be malfunctioning; rather than giving a route directly to safety, it produced extremely circuitous instructions. When the captain uses the PA system to ask if anyone can help, you quickly volunteer. + +The navigation instructions (your puzzle input) consists of a sequence of single-character actions paired with integer input values. After staring at them for a few minutes, you work out what they probably mean: + + Action N means to move north by the given value. + Action S means to move south by the given value. + Action E means to move east by the given value. + Action W means to move west by the given value. + Action L means to turn left the given number of degrees. + Action R means to turn right the given number of degrees. + Action F means to move forward by the given value in the direction the ship is currently facing. + +The ship starts by facing east. Only the L and R actions change the direction the ship is facing. (That is, if the ship is facing east and the next instruction is N10, the ship would move north 10 units, but would still move east if the following action were F.) + +For example: + +F10 +N3 +F7 +R90 +F11 + +These instructions would be handled as follows: + + F10 would move the ship 10 units east (because the ship starts by facing east) to east 10, north 0. + N3 would move the ship 3 units north to east 10, north 3. + F7 would move the ship another 7 units east (because the ship is still facing east) to east 17, north 3. + R90 would cause the ship to turn right by 90 degrees and face south; it remains at east 17, north 3. + F11 would move the ship 11 units south to east 17, south 8. + +At the end of these instructions, the ship's Manhattan distance (sum of the absolute values of its east/west position and its north/south position) from its starting position is 17 + 8 = 25. + +Figure out where the navigation instructions lead. What is the Manhattan distance between that location and the ship's starting position? + +Your puzzle answer was 1457. +--- Part Two --- + +Before you can give the destination to the captain, you realize that the actual action meanings were printed on the back of the instructions the whole time. + +Almost all of the actions indicate how to move a waypoint which is relative to the ship's position: + + Action N means to move the waypoint north by the given value. + Action S means to move the waypoint south by the given value. + Action E means to move the waypoint east by the given value. + Action W means to move the waypoint west by the given value. + Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees. + Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees. + Action F means to move forward to the waypoint a number of times equal to the given value. + +The waypoint starts 10 units east and 1 unit north relative to the ship. The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it. + +For example, using the same instructions as above: + + F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north), leaving the ship at east 100, north 10. The waypoint stays 10 units east and 1 unit north of the ship. + N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at east 100, north 10. + F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north), leaving the ship at east 170, north 38. The waypoint stays 10 units east and 4 units north of the ship. + R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and 10 units south of the ship. The ship remains at east 170, north 38. + F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south), leaving the ship at east 214, south 72. The waypoint stays 4 units east and 10 units south of the ship. + +After these operations, the ship's Manhattan distance from its starting position is 214 + 72 = 286. + +Figure out where the navigation instructions actually lead. What is the Manhattan distance between that location and the ship's starting position? + +Your puzzle answer was 106860. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day12/TEST.txt b/day12/TEST.txt new file mode 100644 index 0000000..d382291 --- /dev/null +++ b/day12/TEST.txt @@ -0,0 +1,5 @@ +F10 +N3 +F7 +R90 +F11 diff --git a/day12/ex1-c.c b/day12/ex1-c.c new file mode 100644 index 0000000..b2a5805 --- /dev/null +++ b/day12/ex1-c.c @@ -0,0 +1,61 @@ +/* ex1-c: Advent2020 game, day 12/game 1 + */ + +#include +#include +#include + +#define ABS(i) ((i)>=0?(i):(-i)) + +int main(ac, av) + int ac; + char **av; +{ + char line[512], command; + unsigned val, direction=90, real; + int x=0, y=0; + + while (fgets(line, sizeof line, stdin)) { + command=*line; + val=atoi(line+1); + switch (command) { + case 'N': + real=0; + break; + case 'E': + real=90; + break; + case 'S': + real=180; + break; + case 'W': + real=270; + break; + case 'F': + real=direction; + break; + case 'L': + val=360-val; + case 'R': + direction=(direction+val)%360; + } + if (command != 'R' && command != 'L') { + switch (real) { + case 0: + y+=val; + break; + case 90: + x+=val; + break; + case 180: + y-=val; + break; + case 270: + x-=val; + break; + } + } + } + printf("%s : res=%d\n", *av, ABS(x)+ABS(y)); + exit (0); +} diff --git a/day12/ex1.bash b/day12/ex1.bash new file mode 100755 index 0000000..53c88c9 --- /dev/null +++ b/day12/ex1.bash @@ -0,0 +1,53 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 12/game 1. + +CMD=${0##*/} +shopt -s extglob + +declare -i X=0 Y=0 +declare -i DIRECTION=90 REAL + +while read -r line; do + C=${line:0:1} + A=${line:1} + #printf "CMD=%s ARG=%d\n" "$C" "$A" + + case "$C" in + R) ((DIRECTION=(DIRECTION+A)%360)) + #printf "D=%d\n" "$DIRECTION" + ;; + L) ((DIRECTION=(DIRECTION+360-A)%360)) + #printf "D=%d\n" "$DIRECTION" + ;; + F) REAL=$DIRECTION + ;; + N) REAL=0 + ;; + S) REAL=180 + ;; + E) REAL=90 + ;; + W) REAL=270 + ;; + esac + if [[ "$C" != "R" && "$C" != "L" ]]; then + case "$REAL" in + 0) ((Y+=A)) + ;; + 90) ((X+=A)) + ;; + 180) ((Y-=A)) + ;; + 270) ((X-=A)) + ;; + esac + fi + #printf "D=%3d POS=(%d,%d)\n" "$DIRECTION" "$X" "$Y" +done + +((X<0)) && ((X=-X)) +((Y<0)) && ((Y=-Y)) +printf "%s : res=%d\n" "$CMD" $((X+Y)) + +exit 0 diff --git a/day12/ex2-c.c b/day12/ex2-c.c new file mode 100644 index 0000000..d5f2c95 --- /dev/null +++ b/day12/ex2-c.c @@ -0,0 +1,58 @@ +/* ex2-c: Advent2020 game, day 12/game 1 + */ + +#include +#include +#include + +#define ABS(i) ((i)>=0?(i):(-i)) + +int main(ac, av) + int ac; + char **av; +{ + char line[512], command; + unsigned val, direction, i; + int x=0, y=0, wx=10, wy=1, tmp; + + while (fgets(line, sizeof line, stdin)) { + command=*line; + val=atoi(line+1); + direction=0; + switch (command) { + case 'L': + direction=360-val; + break; + case 'R': + direction=val; + break; + case 'F': + for (i=0; i 0)); then + for ((i=90; i<=DIRECTION; i+=90)); do + ((tx=WY)) + ((WY=-WX)) + ((WX=tx)) + done + fi +done + +((X<0)) && ((X=-X)) +((Y<0)) && ((Y=-Y)) +printf "%s : res=%d\n" "$CMD" $((X+Y)) + +exit 0 diff --git a/templates/Makefile b/templates/Makefile new file mode 100644 index 0000000..855b615 --- /dev/null +++ b/templates/Makefile @@ -0,0 +1,26 @@ +INPUT := INPUT.txt +SHELL := /bin/bash +CFLAGS := -w +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 deploy ex1 ex2 + +all: ex1 ex2 + +compile: ex1-c ex2-c + +ex1: ex1-c + @$(TIME) ex1.bash < $(INPUT) + @$(TIME) ex1-c < $(INPUT) + +ex2: ex2-c + @$(TIME) ex2.bash < $(INPUT) + @#$(TIME) ex2-sort.bash < $(INPUT) + @$(TIME) ex2-c < $(INPUT) + +clean: + @rm -f ex1-c ex2-c core + +deploy: + @$(MAKE) -C .. deploy diff --git a/templates/ex1-c.c b/templates/ex1-c.c new file mode 100644 index 0000000..563943f --- /dev/null +++ b/templates/ex1-c.c @@ -0,0 +1,38 @@ +/* ex1-c: Advent2020 game, day 3/game 1 + */ + +#include +#include + +#define XMOVE 3 + +int my_strlen(str) + char *str; +{ + int i; + for (i=0; *str; ++i, ++str) + ; + return i; +} + + +int main(ac, av) + char **av; +{ + + int col=0, line=0, linelen=0, mod=0, count=0; + char str[80]; + + scanf("%s", str); /* ignore 1st line */ + + while (scanf("%s", str) != EOF) { + line++; + col+=XMOVE; + linelen=my_strlen(str); + mod=col%linelen; + if (*(str+mod) == '#') + count++; + } + printf ("%s : lines:%d pos:%d found:%d\n", *av, line, col, count); + exit (0); +} diff --git a/templates/ex1.bash b/templates/ex1.bash new file mode 100755 index 0000000..afa4f5a --- /dev/null +++ b/templates/ex1.bash @@ -0,0 +1,21 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 3/game 1. + +CMD=${0##*/} + +XMOVE=3 # 3 right + +declare -i xpos=0 ypos=0 count=0 linelen=0 modpos=0 + +read -r line # ignore 1st line + +while read -r line; do + (( ypos++ )) + (( xpos += XMOVE )) # move right + (( linelen = ${#line} )) + (( modpos = (xpos % linelen) )) + [[ ${line:modpos:1} = \# ]] && ((count++)) +done +printf "%s : lines:%d pos=%d found:%d\n" "$CMD" $ypos $xpos $count +exit 0 diff --git a/templates/ex2-c.c b/templates/ex2-c.c new file mode 100644 index 0000000..590c40c --- /dev/null +++ b/templates/ex2-c.c @@ -0,0 +1,60 @@ +/* ex1-c: Advent2020 game, day 3/game 2 + */ + +#include +#include + +struct { + int dx; + int dy; + int pos; + int count; +} set[] = { + { 1, 1, 0, 0}, + { 3, 1, 0, 0}, + { 5, 1, 0, 0}, + { 7, 1, 0, 0}, + { 1, 2, 0, 0}, + {-1, -1, 0, 0} /* end marker - not necessary */ +}; + +int my_strlen(str) + char *str; +{ + int i; + for (i=0; *str; ++i, ++str) + ; + return i; +} + +int main(ac, av) + int ac; + char **av; +{ + int line=0, linelen=0, mod=0, i; + unsigned long res=1; + char str[80]; + + scanf("%s", str); /* ignore 1st line */ + + while (scanf("%s", str) != EOF) { + line++; + linelen=my_strlen(str); + for (i=0; set[i].dx >= 0; ++i) { + if (! (line % set[i].dy )) { /* line ok for this set */ + set[i].pos += set[i].dx; /* increment set column */ + mod = set[i].pos % linelen; + if ( str[mod] == '#') + set[i].count++; + } + } + } + printf("%s : ", *av); + for (i=0; set[i].dx != -1; ++i) { + printf("[%d]=[%d, %d, %d, %d] ", i, + set[i].dx, set[i].dy, set[i].pos, set[i].count); + res*=set[i].count; + } + printf ("res:%lu\n", res); + exit (0); +} diff --git a/templates/ex2.bash b/templates/ex2.bash new file mode 100755 index 0000000..51fc9cb --- /dev/null +++ b/templates/ex2.bash @@ -0,0 +1,34 @@ +#!/bin/bash +# +# ex1.bash: Advent2020 game, day 3/game 2. + +CMD=${0##*/} + +XMOVES=(1 3 5 7 1) # moves right +YMOVES=(1 1 1 1 2) # moves down +declare -i linelen=0 modpos=0 res=1 +declare -a xpos xcount + +read -r line # ignore 1st line + +while read -r line; do + (( ypos++ )) + (( linelen = ${#line} )) + + for ((i=0; i<${#XMOVES[@]}; ++i)); do + if ((ypos % YMOVES[i] == 0)); then + (( xpos[i] += XMOVES[i] )) # move right + (( modpos = (xpos[i] % linelen) )) + [[ ${line:modpos:1} = \# ]] && ((xcount[i]++)) + fi + done +done +echo "xpos=${xpos[*]} xcount=${xcount[*]}" +printf "%s : " "$CMD" +for ((i=0; i<${#XMOVES[@]}; ++i)); do + printf "[%d]=[%d, %d, %d, %d] " $i "${XMOVES[$i]}" "${YMOVES[$i]}" \ + "${xpos[$i]}" "${xcount[$i]}" + (( res *= xcount[i] )) +done +printf "res:%d\n " $res +exit 0