Xah Talk Show 2023-12-26 Advent of Code Day 4, Live Coding, in WolframLang

Advent of Code 2023, Day 4, Problem Description

Part 1

Advent of Code 2023 Day 4 2dDJ
Advent of Code 2023 Day 4 2dDJ

xtodo description incomplete

input

Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1
Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

each card has two lists of numbers separated by a vertical bar (|): a list of winning numbers and then a list of numbers you have. You organize the information into a table (your puzzle input).

Part 2

xtodo description incomplete

get the total of multiplicity of each line. multiplicity is determined by ...

(* advent of code 2023, day 4, part 1.

*)

input = "Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1
Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11";

input = ReadString[ "c:/Users/xah/web/xahlee_info/talk_show/i/advent_of_code_2023_day_4_input.txt" ];

(2^(# - 1) &) /@
DeleteCases[0]@
(( Length@ Intersection[#[[1]], #[[2]]]&) /@
StringSplit /@ StringSplit[ StringSplit[ input, "\n" ], "|" ] ) // Total

(*
sample input answer is 13
user input answer is 23235
 *)

(*
code explanation
input2= StringSplit[ input, "\n" ]
input3= Map[ StringSplit[ #, "|" ]& , input2 ]
input4 = Map[ StringSplit , input3 ]

lineOne = input4[[1]]

Intersection[lineOne[[1]], lineOne[[2]]]

2^((Length@#) -1)& @Intersection[lineOne[[1]], lineOne[[2]]]

 *)
(* advent of code 2023, day 4, part 2.

solution incomplete

*)

input = "Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1
Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"

(* input = ReadString[ "c:/Users/xah/web/xahlee_info/talk_show/i/advent_of_code_2023_day_4_input.txt" ]; *)

xx=
(( Length@ Intersection[#[[1]], #[[2]]]&) /@
StringSplit /@ StringSplit[ StringSplit[ input, "\n" ], "|" ] )

MapIndexed[ Range[ #2, #2 + #1] &, xx]

Tally@
Flatten@
MapIndexed[
 If[#1 === 0, {#2[[1]]}, {Range@#1 + #2[[1]]}] &,
 xx]

multiplicityList = MapIndexed[ If[0 === #, 0, 1] &, xx];
(* {1, 1, 1, 1, 0, 0} *)

Map[
Function[{x},
multiplicityList[[x[[1]]]]= multiplicityList[[x[[1]]]] + x[[2]] ] ,

]