summaryrefslogtreecommitdiff
path: root/2019/19.hs
blob: 17598b2c3c089b758f50af00124f12a7d8bb6f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Main where

import Data.List
import Data.Maybe

import Input
import IntCode


main :: IO ()
main = do
    program <- parse . head <$> getInput 19

    let samplePoint x y = head (snd (run program [x, y]))

    print (sum [samplePoint x y | y <- [0..49], x <- [0..49]])

    let pollDist = 1000
        hitsAt d = let d' = fromIntegral d in [(x, y) | x <- [0..d'], let y = d' - x, samplePoint x y == 1]
        pollHits = hitsAt pollDist

        guessDist = 100 * pollDist `div` length pollHits
        (minDist, maxDist) = (995 * guessDist `div` 1000, 1005 * guessDist `div` 1000)

        hitsList = map hitsAt [minDist..maxDist]
        dist = fromJust (findIndex ((>= 100) . length) hitsList) + minDist
        finalHits = hitsList !! (dist - minDist)

        (lbX, lbY) = head finalHits
        ltY = lbY - 99

    print (10000 * lbX + ltY)