sln_2017_18
Copyright(c) Eric Mertens 2017
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

https://adventofcode.com/2017/day/18

Day 18 defines a simple programming language with arithmetic operations and asynchronous communication. Our task will be to analyze the behavior of the send and receive commands performed by these kinds of programs.

This implementation uses the following passes to transform the input program into a high-level interpretation of the effects of the program from which we can then easily answer the questions posed.

  1. Get input file with getInput
  2. Parse the input with parser
  3. Compute effects with interpreter
  4. Analyze the effects with part1 and part2
>>> :main
Just 2951
7366
Synopsis

Main drivers

main :: IO () Source #

Print the solution to both parts of the puzzle. Input file can be overridden via command-line argument.

>>> :main
Just 2951
7366

part1 Source #

Arguments

:: (Integer -> Effect)

program ID to effect

-> Maybe Integer

last non-zero snd before rcv

Compute the last send command that precedes a non-zero receive command.

>>> :{
let pgm = map (runP instruction) $ lines
          "set a 1\nadd a 2\nmul a a\nmod a 5\nsnd a\nset a 0\n\
          \rcv a\njgz a -1\nset a 1\njgz a -2\n"
  in part1 (interpreter pgm)
:}
Just 4

part2 Source #

Arguments

:: (Integer -> Effect)

program ID to effect

-> Int

sends from program 1

Run two programs concurrently and count how many sends the second program executes once both programs are blocked.

>>> :{
let pgm = map (runP instruction) $ lines
          "snd 1\nsnd 2\nsnd p\nrcv a\nrcv b\nrcv c\nrcv d\n"
  in part2 (interpreter pgm)
:}
3

feed :: Integer -> Effect -> Effect Source #

Provide the given Integer argument to the first Receive command in a given effect sequence.

Interpreter

The Interpreter transforms a program from the world of instructions, registers, and program counters into only the effects of interpreting those programs. We'll be able to process these effects in order to answer the questions posed in part 1 and part 2 of this task.

data Effect Source #

Observable program execution effects

Constructors

Halt

Execution complete

Send Integer Effect

Send integer, continue

Receive Integer (Integer -> Effect)

Receive with original register value and continuation taking new value

interpreter Source #

Arguments

:: [Instruction]

instructions

-> Integer

program ID

-> Effect

program effect

Compute the effect of executing a program starting at the first instruction using the given map as the initial set of registers.

Parser

The language defined by this problem is particularly simple and so is its parser. Each instruction can be found on its own line, and tokens in the language are separated by whitespace. Each instruction has one or two operands. Some of these operands need to be register names while others can be an expression composed of either an integer literal or a register name.

data Instruction Source #

Program instruction

Constructors

Snd Expression

snd e: send e

Rcv Register

rcv r: receive to r

Set Register Expression

set r e: r=e

Add Register Expression

add r e: r=r+e

Mul Register Expression

mul r e: r=r*e

Mod Register Expression

mod r e: r=r%e

Jgz Expression Expression

jgz t o: if t>0 then pc+=o

Instances

Instances details
Read Instruction Source # 
Instance details

Defined in Main

Show Instruction Source # 
Instance details

Defined in Main

data Expression Source #

Expressions are either integer literals or register values

Constructors

RegisterExpression Register

read from register

IntegerExpression Integer

constant integer

Instances

Instances details
Read Expression Source # 
Instance details

Defined in Main

Show Expression Source # 
Instance details

Defined in Main

newtype Register Source #

Register names: single letters

Constructors

Register Char 

Instances

Instances details
Read Register Source # 
Instance details

Defined in Main

Show Register Source # 
Instance details

Defined in Main

Eq Register Source # 
Instance details

Defined in Main

Ord Register Source # 
Instance details

Defined in Main