Xah Talk Show 2024-12-01 Ep597, Wolfram Language, Advent of Code 2024, Day 1

By Xah Lee. Date: .
grok draw me cat warrior 2024-12-01
grok draw me cat warrior 2024-12-01
xinput = "3   4
4   3
2   5
1   3
3   9
3   3";

xinput = Import[ "http://xahlee.info/comp/advent/advent_of_code_2024_1_input.txt" ];

(* answer for toy input is 11 *)
(* answer for personal input is 1580061 *)

Total @ Abs@ Apply[ Subtract, Map[Sort, Transpose @ Partition[ ToExpression @ StringSplit[ xinput ], 2 ] ]]

part 2

xinput =
"3   4
4   3
2   5
1   3
3   9
3   3";

xinput = Import[ "http://xahlee.info/comp/advent/advent_of_code_2024_1_input.txt" ];

xinput = Transpose @ Partition[  ToExpression @ StringSplit[ xinput ], 2 ];
(* {{3, 4, 2, 1, 3, 3}, {4, 3, 5, 3, 9, 3}} *)

Total@ With[ { col1 = xinput[[1]], col2 = xinput[[2]] }, Map[ Function[{x}, x * Count[ col2, x ]] , col1 ] ]

(* answer for toy input is 31 *)
(* answer for personal input is 23046913 *)