# Xah Talk Show 2023-12-10 Advent of Code Day 2, in WolframLang, Shortest Solution on Earth

## Advent of Code 2023, Day 2, Problem Description

### Part 1

problem 1, given a text like this

```Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
```

each line describes a game. and each semicolon field shows a โdrawโ of colored balls.

and given info that there are total of 12 red balls, 13 green balls, and 14 blue balls, in code

```max = {red -> 12, green -> 13, blue -> 14 };
```

for each game, if any of the number is greater than the possible max, that game is invalid.

for the remaining games, sum up the game number.

in sample input, games 1, 2, and 5 are valid. Sum is 8.

### Part 2

Same input. For each line, take the max of each color, then take their product. Then, sum result for all lines.

Example: this line `3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green` , the max of each color is 6 blue, 4 red, 2 green. Product is 48.

in sample input, the product for each lines are 48, 12, 1560, 630, 36. Sum is 2286.

## Advent of Code 2023, Day 2, part 1, in WolframLang

```(* Advent of Code 2023, Day 2, part 1, in WolframLang *)

input = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";

Total@MapIndexed[If[#,0,#2]&,Or@@@StringCases[StringSplit[input,"\n"],d:NumberString~~" "~~c:"red"|"green"|"blue":>ToExpression@d><|"red"->12,"green"->13,"blue"->14|>@c]]

(*
{8}

{2512}
*)
```

## Advent of Code 2023, Day 2, part 2, in WolframLang

```(* Advent of Code 2023, Day 2, part 2, in WolframLang *)

input = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";