Day 13: Claw Contraption

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • ystael@beehaw.org
    link
    fedilink
    arrow-up
    2
    ·
    14 days ago

    J

    I think this puzzle is a bit of a missed opportunity. They could have provided inputs with no solution or with a line of solutions, so that the cost optimization becomes meaningful. As it is, you just have to carry out Cramer’s rule in extended precision rational arithmetic.

    load 'regex'
    
    data_file_name =: '13.data'
    raw =: cutopen fread data_file_name
    NB. a b sublist y gives elements [a..b) of y
    sublist =: ({~(+i.)/)~"1 _
    parse_button =: monad define
      match =. 'X\+([[:digit:]]+), Y\+([[:digit:]]+)' rxmatch y
      ". (}. match) sublist y
    )
    parse_prize =: monad define
      match =. 'X=([[:digit:]]+), Y=([[:digit:]]+)' rxmatch y
      ". (}. match) sublist y
    )
    parse_machine =: monad define
      3 2 $ (parse_button >0{y), (parse_button >1{y), (parse_prize >2{y)
    )
    NB. x: converts to extended precision, which gives us rational arithmetic
    machines =: x: (parse_machine"1) _3 ]\ raw
    
    NB. A machine is represented by an array 3 2 $ ax ay bx by tx ty, where button
    NB. A moves the claw by ax ay, button B by bx by, and the target is at tx ty.
    NB. We are looking for nonnegative integer solutions to ax*a + bx*b = tx,
    NB. ay*a + by*b = ty; if there is more than one, we want the least by the cost
    NB. function 3*a + b.
    
    solution_rank =: monad define
      if. 0 ~: -/ . * }: y do. 0  NB. system is nonsingular
      elseif. */ (=/"1) 2 ]\ ({. % {:) |: y do. 1  NB. one equation is a multiple of the other
      else. _1 end.
    )
    NB. solve0 yields the cost of solving a machine of solution rank 0
    solve0 =: monad define
      d =. -/ . * }: y
      a =. (-/ . * 2 1 { y) % d
      b =. (-/ . * 0 2 { y) % d
      if. (a >: 0) * (a = <. a) * (b >: 0) * (b = <. b) do. b + 3 * a else. 0 end.
    )
    NB. there are actually no machines of solution rank _1 or 1 in the test set
    result1 =: +/ solve0"_1 machines
    
    machines2 =: machines (+"2) 3 2 $ 0 0 0 0 10000000000000 10000000000000
    NB. there are no machines of solution rank _1 or 1 in the modified set either
    result2 =: +/ solve0"_1 machines2
    
    • lwhjp@lemmy.sdf.org
      link
      fedilink
      arrow-up
      1
      ·
      14 days ago

      Ooh, Cramer’s rule is new to me. That will come in handy if I can remember it next year!