Xah Talk Show 2024-12-03 Ep599, Wolfram Language, Advent of Code 2024, Day 3

By Xah Lee. Date: .
xah talk show 2024-12-03 wolf girl
xah talk show 2024-12-03 wolf girl

part 1

xinput = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";

(* xinput = ReadString @ "c:/Users/xah/web/xahlee_info/comp/advent/advent_of_code_2024_3_input.txt"; *)

xinput = Import @ "http://xahlee.info/comp/advent/advent_of_code_2024_3_input.txt";

xinput = StringCases[ xinput, RegularExpression[ "mul\\(\\d+,\\d+\\)" ] ];

xinput = ToExpression @ StringCases[ xinput, RegularExpression[ "\\d+" ] ];

(* answer for toy input 161 *)
(* answer for personal input 189600467 *)

Total @ Map[ Function[{x}, Apply[ Times, x ]] , xinput]

part 2

xinput = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" ;

(* xinput = ReadString @ "c:/Users/xah/web/xahlee_info/comp/advent/advent_of_code_2024_3_input.txt"; *)

xinput = Import @ "http://xahlee.info/comp/advent/advent_of_code_2024_3_input.txt";

(* answer for toy input 48 *)
(* answer for personal input 107069718 *)

xinput = StringReplace[ xinput , RegularExpression[ "(?s)don't\\(\\).*?do\\(\\)" ] -> "" ];
xinput = StringReplace[ xinput , RegularExpression[ "(?s)don't\\(\\).+$" ] -> "" ];

xinput = StringCases[ xinput, RegularExpression[ "mul\\(\\d+,\\d+\\)" ] ];
xinput = ToExpression @ StringCases[ xinput, RegularExpression[ "\\d+" ] ];
Total @ Map[ Function[{x}, Apply[ Times, x ]] , xinput]