Day 4

Author

Julia Romanowska

The input

Each line consists of symbols and digits. Digits form numbers, delimited by either . (full stop) or another symbol. A number is part of the engine if it is adjacent to any symbol other than . (even diagonally).

root = dirname(@__FILE__);
input_file = joinpath(root, "..", "..", "DATA", "2023", "input_day04.txt");
input_data = CSV.read(input_file, DataFrame; header = false, delim = "|")
218×2 DataFrame
193 rows omitted
Row Column1 Column2
String String
1 Card 1: 79 93 21 74 81 76 17 89 3 5 5 67 87 81 76 35 79 21 15 80 8 74 99 28 3 23 19 42 89 16 22 77 92 70 34
2 Card 2: 83 16 24 23 59 70 14 57 74 53 79 82 70 23 61 14 74 57 36 37 59 72 83 16 3 2 28 63 50 60 38 86 97 24 53
3 Card 3: 12 77 13 14 48 55 69 4 18 81 69 7 94 88 18 73 55 48 49 81 14 21 12 15 5 27 22 84 51 52 13 77 4 57 17
4 Card 4: 32 35 57 27 15 5 16 40 36 46 84 47 76 35 83 5 22 15 72 48 57 33 46 53 43 16 10 65 32 51 36 44 40 27 97
5 Card 5: 8 72 57 36 45 96 7 13 17 14 46 40 8 7 57 60 69 89 17 13 16 51 45 72 55 64 76 42 14 29 21 9 96 36 94
6 Card 6: 22 46 55 8 70 91 11 89 31 5 31 22 40 10 11 61 42 19 87 92 96 34 76 21 89 18 46 8 55 30 36 70 91 63 5
7 Card 7: 5 66 19 57 97 24 45 44 29 86 27 1 70 97 60 90 93 69 29 44 86 49 42 57 58 45 88 24 5 19 95 66 46 78 31
8 Card 8: 67 4 51 50 91 39 63 52 77 47 61 23 54 77 63 51 98 20 47 57 90 7 91 65 79 82 39 72 75 24 52 27 50 67 4
9 Card 9: 99 26 2 15 51 56 63 69 39 41 91 10 81 53 23 41 75 33 3 39 9 24 2 5 93 51 25 37 99 69 15 7 35 67 63
10 Card 10: 31 90 84 42 34 74 96 66 71 75 4 31 43 90 96 70 60 75 5 42 11 6 21 76 2 41 34 99 84 14 77 33 24 71 12
11 Card 11: 66 55 30 68 45 5 82 71 27 85 60 26 40 3 94 93 2 76 7 75 46 47 82 37 8 34 62 92 51 53 32 88 12 44 83
12 Card 12: 49 39 86 99 88 3 51 41 46 64 31 95 25 32 19 98 3 78 88 94 50 80 4 51 55 35 17 24 89 71 67 99 2 69 37
13 Card 13: 53 56 84 7 35 8 55 75 24 13 42 34 30 94 81 95 12 68 69 38 50 17 11 23 89 19 15 79 47 26 88 99 16 44 27
207 Card 207: 9 91 26 59 87 90 22 12 51 99 40 9 85 99 1 11 93 61 56 82 90 91 59 6 12 31 86 76 51 87 92 35 19 34 54
208 Card 208: 54 92 81 40 30 51 72 70 84 52 30 76 74 92 7 81 51 21 35 99 77 80 94 78 16 86 70 31 46 90 44 14 6 52 40
209 Card 209: 49 44 6 38 33 83 75 95 39 72 46 30 75 17 1 28 15 9 44 6 16 89 78 22 95 2 72 59 38 36 25 43 42 41 67
210 Card 210: 10 40 91 1 46 69 92 45 31 41 35 1 55 31 30 14 96 91 92 69 45 20 40 77 15 82 17 79 46 44 56 23 53 38 73
211 Card 211: 67 27 31 84 61 64 58 68 21 74 21 52 88 6 73 36 58 89 1 46 18 17 86 85 28 23 27 25 29 78 79 50 90 30 70
212 Card 212: 61 14 31 4 8 43 52 37 56 45 70 90 64 58 99 82 21 3 38 74 67 59 17 86 36 51 49 69 68 45 71 10 72 1 20
213 Card 213: 51 69 54 42 23 68 27 98 47 4 20 99 66 15 61 43 62 3 60 75 90 47 16 40 80 51 13 87 83 94 49 42 69 74 24
214 Card 214: 38 6 78 12 88 14 51 82 29 75 70 16 48 74 19 29 5 71 40 93 1 18 83 27 76 59 44 63 58 64 69 23 73 65 54
215 Card 215: 67 60 86 35 17 62 55 27 54 70 9 26 45 56 43 72 1 32 25 88 5 87 73 81 2 34 57 13 61 3 84 4 33 92 59
216 Card 216: 18 71 4 89 17 31 63 28 25 20 67 97 6 76 3 95 30 75 99 26 27 32 21 73 86 31 59 98 85 1 54 93 94 69 66
217 Card 217: 54 65 75 13 46 8 37 25 95 82 57 14 83 33 69 47 68 64 4 21 17 92 2 48 30 70 62 50 36 7 72 66 41 85 97
218 Card 218: 68 97 66 41 88 16 65 31 23 63 29 67 55 64 91 4 12 83 1 40 74 94 58 81 98 82 78 70 26 34 96 14 36 50 56

Part 1

How many of the numbers in column1 are in column2?

function create_numvec(in_string::String, sep::Char)
    return (split(in_string, sep; keepempty = false) |>
        x -> parse.(Int, x))
end
create_numvec (generic function with 1 method)
all_points = Int32[];
for row_no in 1:nrow(input_data)
    input_nums = last(split(input_data[row_no, :Column1], ":"));
    input_nums = create_numvec(String(input_nums), ' ');

    winning_nums = create_numvec(String(input_data[row_no, :Column2]), ' ');

    how_many_win = length(intersect(input_nums, winning_nums));
    how_many_points = how_many_win == 0 ? 0 : 2^(how_many_win - 1);
    push!(all_points, how_many_points);
end
all_points
218-element Vector{Int32}:
 128
 512
 512
 512
 512
 512
 512
 512
 128
 128
   1
   8
   0
   ⋮
 128
  64
  32
 128
   4
   1
   8
   1
   0
   1
   0
   0

The solution

The sum of these numbers is 26443.