sln_2022_11
Copyright(c) Eric Mertens 2022
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

https://adventofcode.com/2022/day/11

This solution relies on the fact that there are no interactions between items, so each item can be simulated separately. When a monkey throws an item to a monkey with a larger ID that monkey will get to throw the item in the same round, but when it throws it to an earlier monkey, that item will not be thrown until the next round. Using this we can keep track of when an item would stop moving.

>>> :{
:main +
  "Monkey 0:\n\
  \  Starting items: 79, 98\n\
  \  Operation: new = old * 19\n\
  \  Test: divisible by 23\n\
  \    If true: throw to monkey 2\n\
  \    If false: throw to monkey 3\n\
  \\n\
  \Monkey 1:\n\
  \  Starting items: 54, 65, 75, 74\n\
  \  Operation: new = old + 6\n\
  \  Test: divisible by 19\n\
  \    If true: throw to monkey 2\n\
  \    If false: throw to monkey 0\n\
  \\n\
  \Monkey 2:\n\
  \  Starting items: 79, 60, 97\n\
  \  Operation: new = old * old\n\
  \  Test: divisible by 13\n\
  \    If true: throw to monkey 1\n\
  \    If false: throw to monkey 3\n\
  \\n\
  \Monkey 3:\n\
  \  Starting items: 74\n\
  \  Operation: new = old + 3\n\
  \  Test: divisible by 17\n\
  \    If true: throw to monkey 0\n\
  \    If false: throw to monkey 1\n"
:}
10605
2713310158
Synopsis

Documentation

type Input = [(Int, [Int], Char, Maybe Int, Int, Int, Int)] Source #

Input file contains a list of:

  • Monkey ID
  • Starting items
  • + or *
  • A literal or the old variable
  • A divisor
  • The monkey ID when the divisor divides the value
  • The monkey ID when the divisor does not divide the value

main :: IO () Source #

>>> :main
151312
51382025916

solve Source #

Arguments

:: (Int -> Int)

extra operation to run after each monkey updates worry

-> Int

number of rounds to run

-> Input

input file contents

-> Int

product of top 2 counts of times each monkey threw something

Run the given number of monkey throwing rounds and compute monkey business.

eval :: Char -> Maybe Int -> Int -> Int Source #

Evaluate a monkey's worry update function.

top :: Ord a => Int -> [a] -> [a] Source #

Returns the largest n numbers in a list