0
0

More than 5 years have passed since last update.

# 二値画像の回転(2012.7.23の過去問)

Posted at

``````{-# LANGUAGE Rank2Types #-}
module Doukaku.TwoTone  where
import Data.Bits.Lens (bitAt)
import Numeric.Lens (hex)
import Control.Lens (Lens', view, set, preview, review)

solve :: String -> String
solve input = (n' ++) . (':' :) . fill . pack . turnRight l n . unpack \$ tile'
where
(n', _:tile') = break (== ':') input
l = length tile'
fill x = replicate (l - length x) '0' ++ x

unpack :: String -> Integer
unpack = maybe 0 id . preview hex

pack :: Integer -> String
pack = review hex

point :: Int -> Int -> Int -> Int -> Lens' Integer Bool
point l n x y = bitAt index
where
bitLength = l * 4
index = (bitLength - 1) - (y * n + x)

turnRight :: Int -> Int -> Integer -> Integer
turnRight l n tile = foldr set' 0 points
where
points = [(x, y) | x <- [0 .. n - 1], y <- [0 .. n - 1]]
set' (x, y) = set (point' x y) (view (point' y (n - 1 - x)) tile)
point' :: Int -> Int -> Lens' Integer Bool
point' = point l n
``````

どう書くの鬼門であるビット演算。`Lens`を使ってやってみようと思って始めたのですが、かなり苦労しました。整数に起こして、各ビットへのレンズ`point l n x y`を定義して後はセット、ゲットしているだけです。

0
0
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
0
0