Skip to content

Instantly share code, notes, and snippets.

@tfwright

tfwright/1.exs Secret

Last active December 17, 2022 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfwright/5db72978a508db56d8713a13532f6f13 to your computer and use it in GitHub Desktop.
Save tfwright/5db72978a508db56d8713a13532f6f13 to your computer and use it in GitHub Desktop.
AOC 2022 11
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
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