-
-
Save tfwright/5db72978a508db56d8713a13532f6f13 to your computer and use it in GitHub Desktop.
AOC 2022 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parser = ~r/Monkey (\d):\s.*items:([\d,\s]*)\s*Operation:\snew\s=\sold\s([*+])\s(.*)\s*Test:\sdivisible\sby\s(\d{1,2})\s*.*(\d)\s+.*(\d)/ | |
monkeys = | |
System.argv() | |
|> List.first() | |
|> File.read!() | |
|> String.split("\n\n", trim: true) | |
|> Enum.map(fn monkey -> | |
Regex.run(parser, monkey, capture: :all_but_first) | |
|> List.update_at(0, &String.to_integer/1) | |
|> List.update_at(1, & String.trim(&1) |> String.split(", ") |> Enum.map(fn i -> String.to_integer(i) end)) | |
|> List.update_at(4, &String.to_integer/1) | |
|> List.update_at(5, &String.to_integer/1) | |
|> List.update_at(6, &String.to_integer/1) | |
|> Enum.concat([0]) | |
end) | |
Enum.reduce(0..19, monkeys, fn _round, monkeys -> | |
monkeys | |
|> Enum.reduce(monkeys, fn [idx, _items, op, amt, test_denom, true_target, false_target, _], monkeys -> | |
monkeys | |
|> Enum.at(idx) | |
|> Enum.at(1) | |
|> Enum.reduce(monkeys, fn item_code, monkeys -> | |
int_amount = if amt == "old", do: item_code, else: String.to_integer(amt) | |
new_item_code = | |
Kernel | |
|> apply(String.to_existing_atom(op), [item_code, int_amount]) | |
|> Integer.floor_div(3) | |
test = Integer.mod(new_item_code, test_denom) == 0 | |
target_monkey = if test, do: true_target, else: false_target | |
monkeys | |
|> List.update_at(idx, fn monkey -> List.update_at(monkey, 7, & &1 + 1) end) | |
|> List.update_at(target_monkey, fn monkey -> List.update_at(monkey, 1, & &1 ++ [new_item_code]) end) | |
end) | |
|> List.update_at(idx, fn monkey -> List.replace_at(monkey, 1, []) end) | |
end) | |
end) | |
|> Enum.map(fn [_, _, _, _, _, _, _, handle_count] -> handle_count end) | |
|> Enum.sort() | |
|> Enum.take(-2) | |
|> Enum.reduce(fn count, total -> count * total end) | |
|> IO.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parser = ~r/Monkey (\d):\s.*items:([\d,\s]*)\s*Operation:\snew\s=\sold\s([*+])\s(.*)\s*Test:\sdivisible\sby\s(\d{1,2})\s*.*(\d)\s+.*(\d)/ | |
monkeys = | |
System.argv() | |
|> List.first() | |
|> File.read!() | |
|> String.split("\n\n", trim: true) | |
|> Enum.map(fn monkey -> | |
Regex.run(parser, monkey, capture: :all_but_first) | |
|> List.update_at(0, &String.to_integer/1) | |
|> List.update_at(1, & String.trim(&1) |> String.split(", ") |> Enum.map(fn i -> String.to_integer(i) end)) | |
|> List.update_at(4, &String.to_integer/1) | |
|> List.update_at(5, &String.to_integer/1) | |
|> List.update_at(6, &String.to_integer/1) | |
|> Enum.concat([0]) | |
end) | |
lcm = Enum.map(monkeys, & Enum.at(&1, 4)) |> Enum.product() | |
Enum.reduce(1..10000, monkeys, fn round, monkeys -> | |
monkeys | |
|> Enum.reduce(monkeys, fn [idx, _items, op, amt, test_denom, true_target, false_target, _], monkeys -> | |
monkeys | |
|> Enum.at(idx) | |
|> Enum.at(1) | |
|> Enum.reduce(monkeys, fn item_code, monkeys -> | |
int_amount = if amt == "old", do: item_code, else: String.to_integer(amt) | |
new_item_code = | |
Kernel | |
|> apply(String.to_existing_atom(op), [item_code, int_amount]) | |
|> rem(lcm) | |
test = Integer.mod(new_item_code, test_denom) == 0 | |
target_monkey = if test, do: true_target, else: false_target | |
monkeys | |
|> List.update_at(idx, fn monkey -> List.update_at(monkey, 7, & &1 + 1) end) | |
|> List.update_at(target_monkey, fn monkey -> List.update_at(monkey, 1, & &1 ++ [new_item_code]) end) | |
end) | |
|> List.update_at(idx, fn monkey -> List.replace_at(monkey, 1, []) end) | |
end) | |
end) | |
|> Enum.map(fn [_, _, _, _, _, _, _, handle_count] -> handle_count end) | |
|> Enum.sort() | |
|> Enum.take(-2) | |
|> Enum.reduce(fn count, total -> count * total end) | |
|> IO.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment