From 624c45b8ae8e6ac05f8eb443088aaa2dc01c35f5 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 4 Dec 2021 00:52:54 -0700 Subject: [PATCH] day 3 and 4 solutions --- data/day03-test.txt | 12 + data/day03.txt | 1000 +++++++++++++++++++++++++++++++++++++++++++ data/day04-test.txt | 19 + data/day04.txt | 601 ++++++++++++++++++++++++++ src/day03.rs | 114 +++++ src/day04.rs | 96 +++++ src/main.rs | 4 + 7 files changed, 1846 insertions(+) create mode 100644 data/day03-test.txt create mode 100644 data/day03.txt create mode 100644 data/day04-test.txt create mode 100644 data/day04.txt create mode 100644 src/day03.rs create mode 100644 src/day04.rs diff --git a/data/day03-test.txt b/data/day03-test.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/data/day03-test.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/data/day03.txt b/data/day03.txt new file mode 100644 index 0000000..52865a8 --- /dev/null +++ b/data/day03.txt @@ -0,0 +1,1000 @@ +000011001000 +001111100100 +110101011111 +010100001101 +110001001000 +010000110100 +010011010010 +110101000011 +100111001001 +001101110011 +001101011010 +000010111000 +111001101010 +001010011011 +000101101100 +010011011101 +111100101001 +110110100011 +001100000110 +111010110110 +110110011100 +110000011111 +010101111110 +011001111010 +110101100111 +101111010011 +001111000011 +001110011100 +000011001001 +100101101100 +010110000101 +000001110001 +000100011101 +011110001101 +000111000110 +000111011001 +110111110100 +101110010000 +111010000000 +000111001001 +010101010001 +111100100000 +100000000101 +100111010111 +000111110110 +000000101010 +010010001011 +000100100000 +111101011100 +001100110000 +001101010100 +001011100101 +010011000101 +000001000101 +010101110110 +101110101101 +011011110011 +110101111101 +110101011000 +001001110100 +001001110111 +110100100100 +110110110111 +111001001100 +010101101010 +010101110010 +110100011101 +011111100110 +001011010011 +010100010100 +010111101000 +111000000101 +110101110101 +111010111111 +101000110011 +011111000011 +001011010100 +000001101001 +011110010010 +000001100111 +111010110010 +000111100110 +010001000101 +101010100001 +110011000101 +010101010110 +111110111000 +000100110001 +100011100111 +000110011010 +001110100111 +110001111000 +101000010111 +110000010011 +111101111011 +010001011010 +011110000111 +010011101100 +011101010010 +100111110011 +100010100010 +010000011100 +001111010011 +111000111111 +000100111100 +001100101110 +001000011110 +001101101000 +100101000011 +011001010011 +111001100101 +111110011111 +011010111010 +001100101101 +111100110011 +111001000100 +100111111101 +101101101111 +110011100111 +010000111101 +001100111101 +011110101000 +001110010001 +011010001110 +110110110010 +001010111101 +110110011110 +011110111111 +001000011010 +001011000010 +110011111001 +010110110000 +101011111111 +110100111001 +100110000011 +101010011100 +011100011100 +101011001011 +001010100011 +001110100011 +000100000001 +111011101000 +000010011111 +010101101110 +001101010110 +110100101101 +101101010001 +100110010111 +100101010010 +011000101101 +010001100000 +110100110010 +001011101001 +110100111000 +111000111000 +100011001001 +011010100110 +001110011110 +101011110001 +000100101010 +001111101001 +111011101010 +001001010000 +111000011000 +001110100110 +001111011100 +000000111111 +000100110111 +010010111101 +100011000110 +001110011011 +001011011000 +000100101110 +100001010100 +110101010111 +110110101011 +111110011100 +100001111101 +001001001010 +100101111001 +101111010101 +100110110000 +101110010110 +101000000111 +100000110001 +001111110100 +011111010101 +010100110111 +010110101110 +100001010010 +101011100110 +100001001010 +001110101000 +100010110010 +100101100011 +111000000010 +101110110110 +011110001011 +101101111000 +001111001000 +010111000101 +111111111100 +000000011010 +001000010110 +111110000110 +110001110010 +101110001000 +010100011110 +111110001001 +010111111100 +111100100001 +001000010101 +010000001011 +011000101000 +001000010011 +111100000111 +100110101010 +011001011111 +101110100111 +111011100010 +010111110100 +010111110110 +111010101001 +100110100111 +100000001000 +111001110100 +000010101011 +101000001111 +001000011111 +000010010000 +110010000001 +111110111010 +111100101010 +101000100111 +110101011110 +000010001010 +100101111000 +011001000011 +101001000011 +010101001000 +111111101000 +101001001101 +010101000101 +010010011100 +011011011100 +100010001111 +100000000100 +101111101001 +011110001110 +010110011000 +111110110101 +010101000010 +001100110011 +111110100001 +110110111100 +110101011101 +100010111111 +101111000111 +001100011000 +011001001110 +101110111111 +010110111100 +111000001100 +000001111110 +101001011010 +000001101100 +110111111110 +101010000101 +010101001100 +011101111001 +110101100001 +111000010110 +100001101101 +000000011110 +011101001011 +011111111000 +011001001100 +010011001100 +001111010000 +111111000001 +010000010000 +010000101001 +111001110111 +100100000101 +100011010011 +110111100010 +111100110001 +000000111000 +000101101101 +101110001010 +011101111100 +110000100101 +010010111001 +011011010111 +011111101001 +010001100001 +110011100011 +111010110011 +011000101001 +000011101001 +001000110111 +011010010001 +101111111111 +110011110101 +000000100000 +101111110010 +011100010001 +000001100001 +000010000001 +110110000100 +011011001101 +101001011111 +000111100101 +110101111010 +100101100100 +100001110111 +101110001001 +110110010110 +111000000011 +001101110010 +010000001000 +101001010101 +000101111000 +110011101110 +010010001000 +110010010000 +011001110111 +000010110000 +000110100101 +000101101001 +000110101110 +011101100001 +100111110010 +010100111001 +110111111100 +110110000001 +001011000100 +101010101010 +011100000001 +100001001000 +010110110110 +011000100001 +100001001100 +010000110000 +111111101100 +000101101110 +111010101010 +101001111100 +010011100001 +001011001001 +100111110110 +010111001000 +101110000101 +110110001110 +010000000101 +110010010110 +011100100011 +001111101000 +111111011001 +010100100010 +001111000110 +001110000110 +110101010001 +001100011100 +010001101101 +011110010100 +110000101101 +011110011110 +110101001100 +101011101000 +101000001100 +101000000010 +011010100100 +110000000101 +000010001001 +111010010110 +000110110010 +101000111001 +101001011000 +010111011111 +000110111101 +000100011001 +010101110100 +101010010111 +001010100111 +111010101101 +101011011101 +010011101110 +000111111000 +111000111011 +011101110100 +100100111110 +011100000101 +100011111010 +010010010000 +001011101111 +110011001011 +100011111100 +000010011101 +110010000000 +001011010110 +010110111110 +101011010101 +011111111111 +001011001011 +010101011001 +110011110100 +111001001111 +111001110110 +001000000111 +111011111110 +100100100111 +110001110101 +011001111110 +001101000100 +101011000100 +110111101000 +100100001100 +011001011010 +001000010001 +011001111011 +111010011000 +111001001110 +111100010000 +111101100001 +001000111110 +011111001100 +101001111001 +010010000110 +001010000011 +100011100000 +111101110110 +100111010001 +111000011111 +100100010110 +010100000101 +101110001011 +101100100011 +010000101010 +110110101001 +010110100101 +100000110111 +010010000010 +101000010001 +110001111101 +011001011100 +011111100011 +010100100000 +001110011101 +100011010001 +000111010101 +001111000100 +001111101011 +001001011101 +110110101100 +011101111010 +011110001000 +000111001100 +010100111000 +100100110000 +110100001111 +010010010110 +110111101011 +111011011001 +011001101010 +011011001010 +111001101100 +000011110001 +111111001011 +110111100110 +110111110011 +101010111011 +111010110111 +000100000010 +110011011110 +101010111101 +000110111000 +111011011111 +010001110110 +000011000100 +100001010101 +000111001101 +111011000011 +001110110000 +110101111011 +011011010100 +001100100000 +100011101001 +010101100110 +001101000000 +011110100001 +000110110011 +000000000100 +110110111010 +000100001101 +110101111000 +010100101011 +000011011011 +011011100001 +011011001100 +000101111100 +101010011110 +011111100000 +100011000101 +010111101110 +100000110101 +001101111100 +010111100010 +011000000001 +101001101111 +001011000001 +111000110101 +000000110100 +100001110001 +001000101100 +001011101100 +110010101000 +010011001011 +001111011000 +110001100100 +110010100001 +100101010100 +010000101100 +111010101111 +000010000100 +000101000010 +110000101110 +010001000110 +110001011100 +101000111101 +111011111100 +011100101111 +010111100111 +110111011001 +010100000000 +010100101101 +101010111001 +001010100001 +011010010011 +111011100101 +010110110101 +110010101100 +000101010111 +100010001110 +100111101100 +001000100001 +110111110001 +101100011100 +100101101001 +110010101111 +000010001100 +011111110010 +011111011011 +110101010010 +001110101100 +101111101100 +101001101001 +011010101010 +111110100111 +000110001111 +100110110100 +111011100100 +100100101001 +000011101100 +100001000011 +101100001011 +000101011011 +110001010000 +111111011110 +000100110100 +011110110010 +101111100111 +010010110100 +001001000001 +111101101111 +000010010001 +001011001100 +110110010000 +101000100110 +111100001010 +011010101101 +011101101110 +010101000110 +100110011110 +011001000100 +101111111010 +011001101000 +001010100100 +000101111101 +111010110001 +001100111110 +100000100000 +101011101011 +011101011110 +110011111101 +001111000111 +101110111011 +001110111011 +001010100101 +111011110111 +011011000100 +111000001101 +111110011101 +101001001000 +111110001011 +100000000110 +011100010101 +000111101001 +110101001000 +011000000011 +100000001001 +111010000100 +111110010111 +110000010001 +100001000110 +000100000000 +101000100010 +011011010011 +001000111000 +001101101010 +000010010100 +100101111100 +101110110101 +101110100010 +010100000010 +011110001001 +111111010011 +111111101001 +001010000001 +100100111000 +000011001011 +010111000010 +000000001101 +010100011111 +101001010100 +001110111111 +111110111100 +010010101011 +100010000001 +101001111010 +111101000010 +100100011101 +011111010001 +111111011000 +110101010110 +110101011011 +010011110000 +010000011001 +011000111101 +101011011000 +110111101101 +101010000000 +101100100010 +111001010111 +100101010111 +011101101011 +011000011010 +111010011101 +000100011000 +110111100011 +001100010111 +000111100100 +011010000000 +101010000001 +010100011100 +110001111001 +011111010111 +101111110011 +101100001000 +111011101001 +010011001101 +100001111110 +000111011101 +011110100010 +010110010010 +001110101001 +001000100000 +000110111110 +111110011010 +111100100100 +010100110000 +001101100100 +100010111010 +010000101111 +100001011000 +010100101100 +101010011001 +100111111000 +001100001011 +111111010000 +100001111000 +110010000111 +000000110101 +000101000100 +100000101011 +101100010100 +100111101000 +100111100001 +100001100111 +110001110000 +001010100010 +110000011010 +111010111000 +111100111001 +011011001111 +101011110111 +001001010100 +111110010011 +111000110100 +100111110101 +001001001110 +111000101111 +011001110100 +011100000110 +000111010010 +000111000010 +111000111010 +111100001110 +000011111100 +000111100001 +110111001111 +011111000100 +101001000001 +010001101111 +001101001010 +001100001100 +000111001010 +011010011101 +000111111100 +001011111110 +101100101111 +100010110001 +101100001110 +111000011001 +010010000011 +110000000000 +111111011100 +100011101010 +101000000110 +111111101101 +001010101101 +100101110011 +001010111111 +101001101000 +100100000110 +011011000011 +000010110110 +110110011111 +100011100001 +010011111100 +011001101011 +111001110001 +111100111000 +101010011111 +010101111011 +000001000100 +110010111101 +110101010101 +100100100010 +011000010000 +111101011000 +111111110101 +111010100011 +000100100010 +001110110101 +100001100110 +001011000000 +001110010101 +010101100111 +111101100111 +000011110111 +101000010101 +000000101011 +100101000111 +110011001000 +111101101101 +111100001101 +010101010100 +011111001111 +011010011011 +001100011110 +001111110101 +010001010010 +011010100000 +100110100010 +110110110001 +100101000101 +011101101100 +111000100001 +101011011010 +101001100111 +100000110110 +101101011101 +011000101110 +000011011100 +001010010101 +010001010110 +010100000100 +001101100011 +011110110000 +101101100110 +101011110000 +111100111010 +001101111010 +011011011101 +011111000111 +001011000110 +001110010110 +110100101001 +111010010010 +011100101000 +110100110011 +111011111101 +011011001011 +011101101001 +000101110110 +111010101100 +100100011001 +111011101110 +010001101110 +010101011011 +111010000110 +111111111001 +011011001000 +110000110011 +100010101001 +010101100011 +010011111010 +101111011101 +100110000111 +110100100101 +001000001100 +001101100110 +110110101111 +111001011100 +110110001001 +010111110011 +010000001010 +000101100110 +001011101101 +001010110001 +111100011111 +110110100100 +001001111011 +010100010011 +000100010011 +010011010100 +100011000100 +001000101111 +000011010001 +111111001101 +101011010001 +011101100100 +101110110100 +100101100101 +111000110010 +010000010011 +011011000010 +011010011110 +001000110101 +011011101010 +001101010101 +001110110010 +111100111100 +011011100111 +011101001101 +010011011001 +110100100000 +110011010111 +010111001100 +101110101011 +111001001010 +010011101011 +100000001010 +000010101010 +001110101011 +000100111001 +010011111011 +110001001110 +111111001100 +011010100101 +101001100010 +100110000010 +010001001000 +111111010001 +000110011110 +001011011110 +000111101100 +000001110000 +111011011010 +110001001010 +011110011001 +001110100010 +001101110110 +100011011010 +101000111010 +010010101101 +011100111000 +011001100001 +011100110110 +111101011001 +100100010000 +101000001001 +000000101110 +001001000000 +111110011000 +101110100101 +110010001100 +110010001001 +101110100110 +100101000100 +001010110110 +000001011110 +001001011111 +011000001001 +101001100001 +100011011100 +010000111111 +001001010101 +110011010011 +010110111011 +000010101001 +110101101101 +110101000111 +111110010010 +001110111101 +100011110101 +111000101011 +110011101000 +111111001000 +010010111110 +111001111001 +100101111111 +010011011000 +100010010011 +111110111001 +100000011011 +000110000100 +101001001100 +110101110010 +010101101111 +101010011010 +110110110000 +101100110100 +110111010001 +011011101111 +000111110111 +011000101010 +001011110001 +001010010001 +110100110110 +000101010101 +101100101011 +001010000110 +111101010111 +011011111000 +011010010010 +110010001110 +001111100101 +101101001111 +011110110110 +011011011011 +101001100100 +111001000000 +010001101011 +010011110101 +101011000011 +100010100111 +100110111010 +000010110101 +111101010101 +100100000100 +101111111011 +010101000111 +000110001101 +001101101110 +011011011010 +010100100001 +101101110001 +010010100001 +011100100101 +001110000011 +010011011110 +000100100011 +110101100000 +111011010110 +110100111111 +000101110101 +110000000011 +010110101101 +100001011110 +110110101101 +001101000010 +101110101000 +010100101000 +110001000010 +111111000010 +001000111101 +001111111001 +010100010000 diff --git a/data/day04-test.txt b/data/day04-test.txt new file mode 100644 index 0000000..669a51d --- /dev/null +++ b/data/day04-test.txt @@ -0,0 +1,19 @@ +7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 + +22 13 17 11 0 + 8 2 23 4 24 +21 9 14 16 7 + 6 10 3 18 5 + 1 12 20 15 19 + + 3 15 0 2 22 + 9 18 13 17 5 +19 8 7 25 23 +20 11 10 24 4 +14 21 16 12 6 + +14 21 17 24 4 +10 16 15 9 19 +18 8 23 26 20 +22 11 13 6 5 + 2 0 12 3 7 diff --git a/data/day04.txt b/data/day04.txt new file mode 100644 index 0000000..1c64115 --- /dev/null +++ b/data/day04.txt @@ -0,0 +1,601 @@ +6,69,28,50,36,84,49,13,48,90,1,33,71,0,94,59,53,58,60,96,30,34,29,91,11,41,77,95,17,80,85,93,7,9,74,89,18,25,26,8,87,38,68,5,12,43,27,46,62,73,16,55,22,4,65,76,54,52,83,10,21,67,15,47,45,40,35,66,79,51,75,39,64,24,37,72,3,44,82,32,78,63,57,2,86,31,19,92,14,97,20,56,88,81,70,61,42,99,23,98 + +22 59 7 10 6 +33 36 96 55 23 +13 85 18 29 28 +75 46 83 73 58 +34 40 87 56 98 + +73 96 47 14 10 +28 11 79 84 20 +74 30 0 59 71 +80 93 42 22 17 +44 2 81 29 15 + +73 32 37 93 39 + 2 87 38 99 97 +15 12 42 49 33 + 9 23 25 94 19 +57 4 18 70 79 + +43 79 59 71 78 +51 97 37 28 26 +46 21 84 8 86 +96 30 17 34 49 +55 90 99 75 61 + +43 86 35 51 24 +16 25 29 21 3 + 2 13 4 36 54 +89 27 8 85 34 +96 59 65 14 56 + + 5 57 43 4 1 +86 80 67 30 20 +22 16 99 0 14 +40 25 59 91 54 +82 95 96 37 24 + +97 20 87 66 21 +25 40 9 78 89 +52 75 95 63 62 +32 43 13 47 69 +53 48 56 29 4 + +24 4 65 12 8 +76 3 81 99 49 +45 33 31 66 34 +17 94 75 35 88 +64 10 7 93 95 + +88 20 99 15 83 +81 40 5 6 98 +49 74 18 27 9 +43 69 28 37 71 +87 82 90 14 47 + +21 97 92 42 60 +11 65 98 95 29 + 2 49 75 20 74 +56 40 78 66 81 +68 4 46 77 61 + +26 14 39 76 11 +41 74 34 91 4 +51 9 97 29 75 +83 86 23 35 77 +90 2 49 30 89 + +34 97 0 22 15 +65 60 82 66 47 +30 9 49 95 48 +54 73 67 17 78 +33 96 44 1 32 + +90 29 10 49 63 + 8 26 95 61 0 +54 13 51 12 44 +32 48 16 47 84 +55 46 93 14 3 + +68 55 46 16 54 +97 59 49 6 21 +52 32 15 96 70 +57 56 61 51 0 +40 31 62 24 48 + +75 56 70 42 14 +77 9 23 62 25 +29 27 54 17 2 +55 72 43 15 96 +45 30 88 86 1 + +65 12 22 6 51 +77 81 15 91 19 +80 71 27 89 52 +75 92 41 83 57 +40 44 66 84 98 + +89 80 73 59 18 +74 55 67 71 10 +78 27 37 57 3 +92 26 17 5 72 +52 83 28 76 51 + +64 0 87 4 13 +14 43 42 62 31 +17 84 72 91 83 +53 80 59 61 74 +47 22 38 28 77 + +65 49 91 94 19 +25 81 64 57 9 +50 95 82 12 66 +53 54 85 36 11 +31 56 83 18 60 + + 6 12 76 37 33 +14 96 16 31 40 +17 82 27 94 83 + 1 50 49 9 54 +29 70 59 91 77 + +85 0 33 41 16 + 7 86 80 53 65 +94 19 28 35 32 + 4 55 93 97 63 +83 22 67 56 36 + +28 70 65 17 19 +88 51 15 73 12 +11 49 80 23 42 +26 54 90 75 29 +74 77 91 86 66 + +88 22 83 10 43 +57 34 15 23 8 +94 42 68 67 37 +84 3 6 71 62 +89 45 72 55 38 + +68 27 93 7 15 +85 96 1 56 6 +32 2 30 55 17 +99 90 37 80 50 +77 78 58 98 57 + + 5 95 22 13 97 +19 2 52 92 20 +75 49 86 31 41 +51 63 56 33 10 +82 27 79 94 57 + +15 44 91 26 4 +41 78 53 95 86 +90 92 46 56 33 +80 93 68 66 70 +36 10 38 0 6 + +11 47 52 64 17 +16 60 39 55 78 +46 18 33 83 35 +13 92 4 23 50 +40 5 29 6 90 + +20 60 2 23 74 +43 39 91 75 12 +58 67 50 29 79 +10 40 92 95 25 +13 24 53 72 36 + +69 64 15 53 86 +61 11 48 71 27 +26 93 38 70 73 +96 29 30 98 45 +13 7 31 59 84 + +13 55 50 54 57 +32 24 45 5 77 +82 86 0 4 99 +75 72 14 40 56 +76 94 89 17 30 + +66 71 86 41 0 +80 64 3 29 78 +23 21 42 99 87 +96 60 75 61 49 + 2 83 85 12 1 + +78 96 4 82 6 +73 36 57 99 8 +11 83 15 35 30 +85 87 25 22 66 +32 64 16 50 43 + + 6 39 55 2 4 + 1 61 99 71 80 +46 88 22 31 60 +62 66 37 97 76 +74 34 10 52 24 + +42 38 13 65 12 + 4 39 60 62 21 +81 99 1 69 40 +59 15 11 95 53 +91 78 72 30 73 + +43 68 82 72 2 +34 92 33 97 89 +63 70 9 0 45 +69 10 12 65 21 +40 20 55 57 49 + +41 22 8 16 49 +71 64 19 65 92 +17 10 26 36 29 +30 88 7 54 11 +77 55 70 72 47 + +15 94 90 6 39 +99 55 16 54 91 +18 7 82 44 51 +25 34 83 14 12 +30 2 77 27 66 + +44 50 74 97 58 +87 42 56 92 28 +49 82 52 17 78 +54 89 63 77 27 +83 14 24 16 84 + +47 32 8 55 22 +19 14 24 82 4 +76 73 9 10 64 +40 6 92 67 17 +68 44 43 3 54 + +26 10 9 92 81 +46 98 13 0 14 +68 99 35 18 72 +74 33 22 61 93 +80 38 71 6 75 + +17 23 54 55 22 +10 8 60 76 24 +71 28 16 62 82 +13 12 21 78 39 +26 66 89 64 79 + +72 12 91 79 99 +84 18 37 98 41 +10 71 88 23 24 +11 34 26 83 74 +58 27 77 5 8 + + 9 90 13 0 46 + 6 58 74 92 8 +75 77 56 76 80 +55 60 44 68 91 +67 28 96 66 18 + +14 26 54 21 35 +69 67 16 76 62 +78 45 87 44 94 +27 89 39 85 28 + 3 81 93 64 74 + +10 28 35 84 76 +40 11 95 59 57 +53 4 24 50 45 + 7 43 78 17 81 + 1 74 82 16 27 + + 6 26 72 53 52 +51 91 80 11 18 +20 63 74 25 33 +79 4 8 59 67 + 3 13 55 81 83 + +98 85 27 84 42 +90 15 17 61 34 +40 64 86 96 45 +59 47 53 5 35 +11 7 41 80 13 + +47 48 54 31 76 +99 32 98 20 15 +61 41 30 94 37 +34 59 86 55 45 + 9 83 92 53 3 + + 3 80 24 94 25 +17 23 64 76 71 +20 97 0 56 72 +95 73 28 59 21 +14 81 46 67 88 + + 2 95 5 38 90 +63 62 11 24 34 +19 31 57 84 80 +47 86 36 85 74 +13 39 73 94 42 + +28 6 60 34 15 +63 36 51 30 92 +43 10 7 88 49 +78 76 31 19 66 +22 20 35 45 79 + +19 42 49 57 73 +99 50 97 93 43 +67 52 40 16 33 + 2 55 0 71 46 +21 75 59 66 83 + +19 86 30 25 3 +32 39 65 54 29 +38 6 85 52 13 +43 95 18 44 15 +53 70 16 31 71 + +68 73 74 83 70 +56 15 12 78 4 +43 87 63 90 86 +41 16 23 17 77 +80 14 61 30 50 + +88 28 45 80 65 +64 11 68 33 27 +29 70 44 82 37 +42 66 9 32 87 +10 24 15 3 46 + +99 81 5 62 97 + 4 36 23 38 35 +42 16 37 98 54 +34 41 25 30 48 + 8 60 63 89 72 + +25 33 94 23 14 +45 10 79 30 3 +22 28 95 27 11 +74 13 39 84 83 +72 88 56 53 97 + +31 92 91 84 71 +54 90 89 80 0 +98 96 65 66 68 +35 39 70 11 82 +15 34 42 52 2 + +68 71 86 82 37 +28 48 12 34 54 +62 55 10 25 89 +60 4 50 21 22 + 3 2 18 40 84 + +18 96 95 47 45 +14 51 2 88 43 +94 56 19 15 8 +48 65 62 6 75 +35 28 25 72 30 + +43 85 69 92 26 +61 75 5 73 66 +16 87 4 99 48 +18 19 79 23 83 +37 88 31 38 40 + +23 10 89 84 76 +45 39 62 55 66 +25 73 79 43 60 +12 69 36 93 71 + 9 77 14 58 49 + + 9 24 26 53 79 +99 15 30 50 16 +14 95 12 25 33 +54 40 58 46 66 + 5 23 39 29 48 + +64 67 11 10 92 +59 99 80 97 66 +45 51 88 47 82 +35 27 72 85 16 +54 58 2 1 52 + +10 97 27 54 66 +89 85 39 99 98 + 8 68 95 51 19 + 4 17 79 87 3 +72 43 76 58 33 + +95 72 0 18 17 + 3 42 24 86 34 +52 79 46 58 98 +76 77 78 19 10 +81 61 88 85 54 + +74 70 15 80 72 +77 89 11 19 22 +34 59 56 65 91 +58 6 50 40 16 +93 30 95 26 85 + +47 62 57 6 25 +40 79 22 95 29 +42 11 70 10 92 +60 53 84 96 17 +75 86 74 89 18 + +30 42 4 19 92 +40 58 72 7 70 +17 98 45 76 50 +93 57 65 79 2 +56 94 73 84 62 + +51 0 14 7 53 +63 36 48 81 84 +50 22 11 88 6 +83 99 21 31 91 +86 17 72 42 94 + +83 0 20 26 7 +42 24 37 86 65 +73 82 63 21 27 +30 35 9 47 80 +79 53 3 14 84 + +50 99 83 86 42 +81 36 24 4 76 + 0 71 66 41 57 + 7 54 94 78 97 +12 8 82 45 31 + + 4 91 57 50 37 +22 46 86 24 26 +39 54 48 7 42 + 2 45 95 29 12 +38 25 52 0 72 + +51 94 46 44 62 +95 60 0 48 61 +38 13 85 32 8 +22 56 53 30 80 +43 65 58 68 88 + +49 76 41 43 51 +57 46 45 82 90 +48 33 36 5 23 +81 30 3 61 75 +56 70 29 91 59 + +58 74 50 47 84 + 2 1 62 36 60 +32 16 95 43 27 +79 12 39 56 11 + 9 33 4 25 61 + +23 84 16 51 39 +72 19 53 64 43 + 9 44 10 52 26 +45 68 29 56 74 +62 42 46 95 0 + +16 83 27 85 56 +13 41 49 79 53 +18 63 7 60 3 +45 15 48 69 29 +46 86 35 34 32 + +85 2 96 15 43 +33 30 29 53 98 +21 55 61 73 40 +31 4 66 75 59 +26 32 91 38 80 + +69 81 65 30 77 +82 22 83 0 38 + 2 3 29 47 94 +42 55 9 18 97 +53 45 90 31 44 + +23 86 0 35 84 +27 80 3 64 12 + 1 96 48 93 85 +69 24 61 15 22 +91 72 62 13 76 + +81 51 67 60 16 +65 48 86 39 97 +92 93 49 77 59 +15 94 88 52 19 +80 83 23 61 4 + +47 84 46 79 55 +92 38 65 42 76 + 9 58 26 95 86 +30 49 56 69 59 +94 4 25 89 44 + +73 10 29 0 48 +56 40 19 84 61 +52 31 25 86 21 +79 55 53 51 5 +81 9 35 72 15 + +41 95 30 58 73 +26 80 4 21 96 +61 92 76 93 74 + 2 69 60 8 20 +46 98 70 72 83 + + 1 99 31 4 86 +93 64 8 43 61 +33 36 75 90 50 +52 13 3 42 34 +22 65 60 18 76 + +17 63 6 66 92 +51 67 86 88 18 +82 83 32 74 30 + 5 33 9 28 61 +72 75 25 23 60 + +43 28 40 53 52 +54 12 77 10 83 +21 44 63 0 1 +15 22 33 49 2 +80 41 3 46 55 + + 1 97 75 37 4 +47 33 13 21 40 +27 62 15 90 30 +11 83 63 36 35 + 0 12 60 91 42 + + 0 45 17 88 18 +66 10 63 62 8 +36 5 47 39 67 +21 3 61 29 19 +82 58 33 6 59 + +37 92 69 56 52 +46 66 20 78 13 +83 99 16 31 0 +36 35 2 68 9 +70 82 94 96 29 + +62 65 85 37 3 +74 95 34 96 58 +15 33 49 21 93 +19 83 66 6 25 +81 84 23 0 76 + +95 84 71 92 52 +54 36 66 59 82 + 0 76 32 45 83 +69 27 25 88 38 +81 96 63 4 61 + +73 51 28 48 40 + 3 38 11 14 35 +66 91 86 20 81 +53 39 46 71 1 +97 60 21 93 23 + +92 70 4 60 95 +58 49 20 15 25 +55 68 21 84 80 +56 41 82 23 19 +30 74 65 27 29 + +81 97 68 46 75 +62 73 63 36 41 + 1 5 91 84 37 +45 92 20 49 7 +25 26 3 88 56 + +25 9 94 37 26 +44 58 84 91 38 +39 46 57 98 50 +96 42 73 24 70 +71 32 53 48 13 + +98 72 25 96 77 +80 64 88 53 23 +21 37 45 24 18 +41 86 59 68 5 +76 50 36 26 12 + +77 79 88 74 12 +21 9 85 26 68 +11 62 64 4 5 +47 33 76 63 87 +55 19 2 60 95 + +74 79 30 14 35 +90 52 17 29 63 +18 69 78 34 26 +92 42 85 71 56 +12 2 5 0 98 diff --git a/src/day03.rs b/src/day03.rs new file mode 100644 index 0000000..1a686b0 --- /dev/null +++ b/src/day03.rs @@ -0,0 +1,114 @@ +use crate::utils::read_file; +use anyhow::Result; + +fn strings_to_ints(s: &str) -> Vec { + s.lines() + .map(|line| usize::from_str_radix(line, 2).unwrap()) + .collect() +} + +fn diagnose_power(s: &str) -> usize { + // collect all positions with ones + let bits = s.lines().next().unwrap().len(); + let n_words = s.lines().count(); + let mut counts = vec![0; bits]; + s.lines().for_each(|line| { + line.chars().enumerate().for_each(|(i, c)| { + if c == '1' { + counts[i] += 1; + } + }) + }); + + // determine whether zero or one is greatter than half for each position + let ret: String = counts + .iter() + .map(|n| if n > &(n_words / 2) { '1' } else { '0' }) + .collect(); + + // put together gamma (all majority 1 slots) and epsilon (!gamma) + let gamma = usize::from_str_radix(&ret, 2).unwrap(); + let epsilon = !gamma & ((1 << bits) - 1); + gamma * epsilon +} + +fn find_oxygen_or_co2(s: &str, oxygen: bool) -> usize { + let bits = s.lines().next().unwrap().len(); + let mut kept = strings_to_ints(s); + usize::from_str_radix( + &(0..bits) + .rev() + .map(|bit| { + // split current set into ones/zeroes for current bit + let mut ones = vec![]; + let mut zeroes = vec![]; + kept.iter().for_each(|x| { + if x & (1 << bit) != 0 { + ones.push(*x); + } else { + zeroes.push(*x); + } + }); + + if kept.len() == 1 { + // only one left so we take its 0 or 1 regardless + if kept[0] & (1 << bit) != 0 { + '1' + } else { + '0' + } + } else if oxygen { + // keep whichever has the most, ties are 1 + if ones.len() >= zeroes.len() { + kept = ones; + '1' + } else { + kept = zeroes; + '0' + } + } else { + // keep whichever has the least, ties are 0 + if zeroes.len() <= ones.len() { + kept = zeroes; + '0' + } else { + kept = ones; + '1' + } + } + }) + .collect::(), + 2, + ) + .unwrap() +} +fn diagnose_lifesupport(s: &str) -> usize { + let oxygen_generator = find_oxygen_or_co2(s, true); + let co2_scrubber = find_oxygen_or_co2(s, false); + oxygen_generator * co2_scrubber +} + +pub(crate) fn run() -> Result<(usize, usize)> { + let input = read_file("data/day03.txt")?; + Ok((diagnose_power(&input), diagnose_lifesupport(&input))) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let input = read_file("data/day03-test.txt").unwrap(); + assert_eq!(diagnose_power(&input), 198); + assert_eq!(diagnose_lifesupport(&input), 230); + } + + #[test] + fn test_my_data() { + let input = read_file("data/day03.txt").unwrap(); + assert_eq!(diagnose_power(&input), 4191876); + // assert_eq!(follow_course(&input), 2070300); + // assert_eq!(follow_course_better(&input), 2078985210); + } +} diff --git a/src/day04.rs b/src/day04.rs new file mode 100644 index 0000000..f40dddd --- /dev/null +++ b/src/day04.rs @@ -0,0 +1,96 @@ +use crate::utils::read_file; +use anyhow::Result; + +#[derive(Debug)] +struct Bingo { + draws: Vec, + boards: Vec>, +} + +fn parse_bingo(s: &str) -> Bingo { + Bingo { + // take the first line and split on comma + draws: s + .lines() + .next() + .unwrap() + .split(',') + .map(|s| s.parse::().unwrap()) + .collect(), + // split on whitespace, collect all the remaining numbers and then chunk them out in 25s + boards: s + .lines() + .skip(2) + .map(|line| line.split_whitespace().map(|s| s.parse::().unwrap())) + .flatten() + .collect::>() + .chunks(25) + .map(|c| c.into()) + .collect(), + } +} + +fn test_for_win(marked: usize) -> bool { + let col_mask: usize = 0b1000010000100001000010000; + let row_mask: usize = 0b1111100000000000000000000; + (0..5).any(|i| { + marked & (col_mask >> i) == (col_mask >> i) + || marked & (row_mask >> (i * 5)) == (row_mask >> (i * 5)) + }) +} + +fn compute_score(board: &[usize], marked: usize, last_draw: usize) -> usize { + (0..25) + .map(|i| if marked & 1 << i == 0 { board[i] } else { 0 }) + .sum::() + * last_draw +} + +fn find_winning_score(s: &str, first: bool) -> usize { + let bingo = parse_bingo(s); + let mut markers = vec![0; bingo.boards.len()]; + let mut boards_won = vec![false; bingo.boards.len()]; + // for every drawn number, mark each board and check whether it wins + for draw in bingo.draws { + for i in 0..bingo.boards.len() { + for j in 0..bingo.boards[i].len() { + if bingo.boards[i][j] == draw { + markers[i] |= 1 << j; + } + if test_for_win(markers[i]) { + boards_won[i] = true; + if first || (!first && boards_won.iter().all(|w| *w)) { + return compute_score(&bingo.boards[i], markers[i], draw); + } + } + } + } + } + unreachable!() +} + +pub(crate) fn run() -> Result<(usize, usize)> { + let input = read_file("data/day04.txt")?; + Ok(( + find_winning_score(&input, true), + find_winning_score(&input, false), + )) +} +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let input = read_file("data/day04-test.txt").unwrap(); + assert_eq!(find_winning_score(&input, true), 4512); + assert_eq!(find_winning_score(&input, false), 1924); + } + + #[test] + fn test_my_data() { + let input = read_file("data/day04.txt").unwrap(); + assert_eq!(find_winning_score(&input, true), 71708); + assert_eq!(find_winning_score(&input, false), 34726); + } +} diff --git a/src/main.rs b/src/main.rs index 0f4ae56..daf1dd5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,14 @@ use anyhow::Result; mod day01; mod day02; +mod day03; +mod day04; mod utils; fn main() -> Result<()> { println!("day01: {:?}", day01::run()?); println!("day02: {:?}", day02::run()?); + println!("day03: {:?}", day03::run()?); + println!("day04: {:?}", day04::run()?); Ok(()) }