ctrl k
  • 03-crossed-wires/README.md
    ■ ■ ■ ■ ■ ■
    skipped 44 lines
    45 45  Here are a few more examples:
    46 46   
    47 47   - `R75,D30,R83,U83,L12,D49,R71,U7,L72`
    48  - - `U62,R66,U55,R34,D71,R55,D58,R83` = distance `159`
     48 + `U62,R66,U55,R34,D71,R55,D58,R83` = distance `159`
    49 49   - `R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51`
    50  - - `U98,R91,D20,R16,D67,R40,U7,R15,U6,R7` = distance `135`
     50 + `U98,R91,D20,R16,D67,R40,U7,R15,U6,R7` = distance `135`
    51 51   
    52 52  What is the Manhattan distance from the central port to the closest intersection?
    53 53   
  • 03-crossed-wires/answer.hs
    ■ ■ ■ ■ ■ ■
     1 +import System.Environment
     2 +import Data.Sequence
     3 + 
     4 +compute :: Seq Int -> Int -> Seq Int
     5 +compute x y = do
     6 + let op = x `index` y
     7 + let indexOfIndex y = index x $ index x y
     8 + let left = indexOfIndex (y+1)
     9 + let right = indexOfIndex (y+2)
     10 + if op == 99
     11 + then x
     12 + else if op /= 1 && op /= 2
     13 + then fromList []
     14 + else
     15 + compute (update (x `index` (y+3)) ((if op == 1 then (+) else (*)) left right) x) (y+4)
     16 + 
     17 +-- Brute force to find answer based on any array input and any desired answer
     18 +findParamsBrute :: Seq Int -> Int -> [Int] -> Int
     19 +findParamsBrute x y params = do
     20 + let computed = compute (update 1 (params!!0) $ update 2 (params!!1) x) 0
     21 + let ans = computed `index` 0
     22 + if y == ans
     23 + then 100*params!!0 + params!!1
     24 + else if ans > y
     25 + then -1
     26 + else if y `mod` 1000 > ans `mod` 1000
     27 + then findParamsBrute x y [params!!0, params!!1 + 1]
     28 + else
     29 + findParamsBrute x y [params!!0+1, params!!1]
     30 + 
     31 +-- Use the gradients of each parameter in the goal to calculate the answer in O(1)
     32 +findParams :: Int -> Int
     33 +findParams goal = do
     34 + let first = (goal-493708) `div` 243000
     35 + let second = goal - 493708 - 243000*first
     36 + 100*first + second
     37 + 
     38 + 
     39 +main = do
     40 + let x = fromList [1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,9,23,1,5,23,27,1,27,9,31,1,6,31,35,2,35,9,39,1,39,6,43,2,9,43,47,1,47,6,51,2,51,9,55,1,5,55,59,2,59,6,63,1,9,63,67,1,67,10,71,1,71,13,75,2,13,75,79,1,6,79,83,2,9,83,87,1,87,6,91,2,10,91,95,2,13,95,99,1,9,99,103,1,5,103,107,2,9,107,111,1,111,5,115,1,115,5,119,1,10,119,123,1,13,123,127,1,2,127,131,1,131,13,0,99,2,14,0,0]
     41 + print $ (x `compute` 0) `index` 0
     42 + print $ findParams 19690720
     43 + 
  • 03-crossed-wires/run.sh
    ■ ■ ■ ■ ■ ■
     1 +#!/bin/sh
     2 + 
     3 +runhaskell answer.hs
     4 + 
Please wait...
Page is in error, reload to recover