Day 5: 428

This commit is contained in:
Christoph Stahl 2021-12-05 13:57:09 +01:00
parent 5e2434cff2
commit 2157214c8a

View file

@ -1,16 +1,11 @@
import Data.List
type Board = [[Int]]
c x=0<$[0..x]<$[0..x]
a=concat;m=map;(!)=splitAt
v[[a,b],[c,d]]=a==c||b==d
p[s,_,t]=m(m read.(&','))[s,t]
x1?x2|x1>x2=x1-1|x1<x2=x1+1|let=x1
l[[x,y],[a,b]]m=foldr d m$n x y a b
a&x|(u,_:v)<-break(==x)a=u:v&x|let=[a]
printfield :: Board -> String
printfield = intercalate "\n".map printrow
printrow :: [Int] -> String
printrow = concatMap show
createField :: [Int] -> Board
createField [x,y] = 0<$[0..x]<$[0..y]
drawPixel :: (Int,Int) -> Board -> Board
drawPixel (x,y) m|(l,h:r)<-splitAt x m,(u,v:d)<-splitAt y h=l++(u++(v+1):d):r
drawLine [x,y] [a,b] m = foldr drawPixel m [(u,v)|u<-[x..a],v<-[y..b]]
minmax x=map(maximum.(x>>=))[head,last]
p::[String] -> [[Int]]
p[s,_,t]=map(map read.(&','))[s,t]
main=interact$printfield.drawLine [1,0][1,2].drawLine [0,0] [4,0].createField.minmax.map(p.words).lines
q x=sum[1|v<-a$foldr l(c$maximum$a$a x)x,v>1]
d(x,y)m|(l,h:r)<-y!m,(u,v:d)<-x!h=l++(u++(v+1):d):r
n x y a b|x/=a||y/=b=(x,y):n(x?a)(y?b)a b|let=[(x,y)]
main=interact$show.m q.(\x->[x,filter v x]).m(p.words).lines