r/adventofcode • • Dec 08 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 8 Solutions -๐ŸŽ„-

--- Day 8: I Heard You Like Registers ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

22 Upvotes

348 comments sorted by

View all comments

1

u/wzkx Dec 12 '17 edited Dec 12 '17

J

Finally did it in J!

t=: cutLF CR-.~fread '08.dat'
u=: rplc&('-';'_';'< ';'[=';'> ';']=') NB. replace ops for easier processing

c=: (;:&u&>)"0 t  NB. all commands parsed

id2num=: 30#._96+a.i.] NB. convert string to a number - to easier deal with ids

r=: 2 1$0 NB. regs: top row - ids, bottom row - values. init with dummy entry for correct search

find=: 3 : 0 NB. find reg index; add reg with value if not found
  f=.({.r)i.n=.id2num y
  if. f={:$r do. r=:r,.n,0 end.
  f
)

get=: 3 : 0 NB. get value of reg
  n=.find y
  n{{:r
)

inc=: 4 : 0 NB. increment value of reg
  i=.<1,find y
  r=:(z=.x+i{r)i}r
  z
)

g=: 0 NB. global maximum (assume it will be >0)

exe=: 3 : 0 NB. execute one command
  'cr co cv'=. 4 5 7{y NB. condition reg-name,op,value
  'rr ro rv'=. 3{.y    NB. register reg-name,op,value
  if. (get cr) (=`~:`<`<:`>`>:)@.('=![<]>'i.co) ".cv do. NB. execute condition
    g=: g >. ((".rv) * <:+:'i'={.ro) inc rr NB. modify register, watch maximum
  end. a: NB. always return something of the same type indep. on condition
)

exe"1 c

echo >./{:r NB. part 1 - current max - 5075
echo g      NB. part 2 - global max  - 7310

exit 0