Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

3
2017/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*\~
*.hi
*.o

18
2017/01/day1.hs Normal file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
sumMatchingOffset :: Int -> [Int] -> Int
sumMatchingOffset k xs =
foldr (\x acc -> if fst x == snd x then acc + fst x else acc) 0 xs'
where n = length xs
xs' = zip xs (take n . drop k . cycle $ xs)
toDigits :: String -> [Int]
toDigits = map (read . (:""))
main :: IO ()
main = do
digitsStr <- getLine
let digits = toDigits digitsStr
putStrLn . show $ sumMatchingOffset 1 digits
putStrLn . show $ sumMatchingOffset (length digits `div` 2) digits

19
2017/01/day1.scm Normal file
View file

@ -0,0 +1,19 @@
#!/usr/bin/csi -script
(use extras)
(use srfi-1)
(use list-comprehensions)
(define (sum-matching-offset k xs)
(let* ((n (length xs))
(newxs (zip xs (take (drop (concatenate ((repeat 3) xs)) k) n)))
(f (lambda (x acc) (if (= (first x) (second x)) (+ acc (first x)) acc))))
(foldr f 0 newxs)))
(define (main)
(let* ((digits-str (read-line))
(digits (map string->number (map string (string->list digits-str)))))
(print (sum-matching-offset 1 digits))
(print (sum-matching-offset (/ (length digits) 2) digits))))
(main)

1
2017/01/digits.txt Normal file
View file

@ -0,0 +1 @@
8231753674683997878179259195565332579493378483264978184143341284379682788518559178822225126625428318115396632681141871952894291898364781898929292614792884883249356728741993224889167928232261325123447569829932951268292953928766755779761837993812528527484487298117739869189415599461746944992651752768158611996715467871381527675219481185217357632445748912726487669881876129192932995282777848496561259839781188719233951619188388532698519298142112853776942545211859134185231768952888462471642851588368445761489225786919778983848113833773768236969923939838755997989537648222217996381757542964844337285428654375499359997792679256881378967852376848812795761118139288152799921176874256377615952758268844139579622754965461884862647423491918913628848748756595463191585555385849335742224855473769411212376446591654846168189278959857681336724221434846946124915271196433144335482787432683848594487648477532498952572515118864475621828118274911298396748213136426357769991314661642612786847135485969889237193822718111269561741563479116832364485724716242176288642371849569664594194674763319687735723517614962575592111286177553435651952853878775431234327919595595658641534765455489561934548474291254387229751472883423413196845162752716925199866591883313638846474321161569892518574346226751366315311145777448781862222126923449311838564685882695889397531413937666673233451216968414288135984394249684886554812761191289485457945866524228415191549168557957633386991931186773843869999284468773866221976873998168818944399661463963658784821796272987155278195355579386768156718813624559264574836134419725187881514665834441359644955768658663278765363789664721736533517774292478192143934318399418188298753351815388561359528533778996296279366394386455544446922653976725113889842749182361253582433319351193862788433113852782596161148992233558144692913791714859516653421917841295749163469751479835492713392861519993791967927773114713888458982796514977717987598165486967786989991998142488631168697963816156374216224386193941566358543266646516247854435356941566492841213424915682394928959116411457967897614457497279472661229548612777155998358618945222326558176486944695689777438164612198225816646583996426313832539918

20
2017/02/day2.hs Normal file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
range :: (Ord a, Num a) => [a] -> a
range l = maximum l - minimum l
divisors :: (Integral a) => [a] -> a
divisors [] = 0
divisors (x:xs) =
case filter (\y -> (x `mod` y == 0) || (y `mod` x == 0)) xs of
[] -> divisors xs
y:_ -> if y > x then y `div` x
else x `div` y
main :: IO ()
main = do
contents <- getContents
let numbers = map (map read . words) $ lines contents :: [[Int]]
putStrLn . show $ sum $ map range numbers
putStrLn . show $ sum $ map divisors numbers

27
2017/02/day2.scm Normal file
View file

@ -0,0 +1,27 @@
#!/usr/bin/csi -script
(use extras)
(define (range xs)
(- (apply max xs) (apply min xs)))
(define (find-divisors x xs)
(cond ((null? xs) 0)
((= 0 (remainder x (car xs))) (/ x (car xs)))
((= 0 (remainder (car xs) x)) (/ (car xs) x))
(else (find-divisors x (cdr xs)))))
(define (divisors xs)
(if (null? xs) 0
(let* ((y (car xs))
(ys (cdr xs))
(first-divisor (find-divisors y ys)))
(if (= 0 first-divisor) (divisors ys) first-divisor))))
(define (main)
(let* ((contents (read-lines))
(numbers (map (lambda (x) (map string->number x)) (map string-split contents))))
(print (apply + (map range numbers)))
(print (apply + (map divisors numbers)))))
(main)

16
2017/02/input.txt Normal file
View file

@ -0,0 +1,16 @@
790 99 345 1080 32 143 1085 984 553 98 123 97 197 886 125 947
302 463 59 58 55 87 508 54 472 63 469 419 424 331 337 72
899 962 77 1127 62 530 78 880 129 1014 93 148 239 288 357 424
2417 2755 254 3886 5336 3655 5798 3273 5016 178 270 6511 223 5391 1342 2377
68 3002 3307 166 275 1989 1611 364 157 144 3771 1267 3188 3149 156 3454
1088 1261 21 1063 1173 278 1164 207 237 1230 1185 431 232 660 195 1246
49 1100 136 1491 647 1486 112 1278 53 1564 1147 1068 809 1638 138 117
158 3216 1972 2646 3181 785 2937 365 611 1977 1199 2972 201 2432 186 160
244 86 61 38 58 71 243 52 245 264 209 265 308 80 126 129
1317 792 74 111 1721 252 1082 1881 1349 94 891 1458 331 1691 89 1724
3798 202 3140 3468 1486 2073 3872 3190 3481 3760 2876 182 2772 226 3753 188
2272 6876 6759 218 272 4095 4712 6244 4889 2037 234 223 6858 3499 2358 439
792 230 886 824 762 895 99 799 94 110 747 635 91 406 89 157
2074 237 1668 1961 170 2292 2079 1371 1909 221 2039 1022 193 2195 1395 2123
8447 203 1806 6777 278 2850 1232 6369 398 235 212 992 7520 7304 7852 520
3928 107 3406 123 2111 2749 223 125 134 146 3875 1357 508 1534 4002 4417

49
2017/03/day3.hs Normal file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
import qualified Data.Map.Strict as Map
import Data.Maybe
right, up, left, down :: (Int,Int) -> (Int,Int)
right (a,b) = (a+1,b )
up (a,b) = (a ,b+1)
left (a,b) = (a-1,b )
down (a,b) = (a ,b-1)
spiral :: [(Int, Int)]
spiral = scanl (flip ($)) (0,0) directions
where directions = concat $
zipWith replicate (concat (map (replicate 2) [1..])) $
cycle [right, up, left, down]
shortestPathLength :: Int -> Int
shortestPathLength n =
let (x,y) = spiral !! (n-1) in
(abs x) + (abs y)
neighbours :: (Int,Int) -> [(Int,Int)]
neighbours (a,b) =
[(a+k,b+l) | k <- [-1..1], l <- [-1..1]]
insertCell :: (Int,Int) -> Map.Map (Int,Int) Int -> Map.Map (Int,Int) Int
insertCell (a,b) m =
Map.insert (a,b) (fromJust sumNeighbours) m
where sumNeighbours = fmap sum $ sequence . (filter isJust) $ fmap (\k -> Map.lookup k m) (neighbours (a,b))
spiralSum :: Int -> Map.Map (Int,Int) Int
spiralSum n = foldl (flip insertCell) (Map.singleton (0,0) 1) (take n (tail spiral))
spiralSumValues :: Int -> [Int]
spiralSumValues n = fromJust . sequence $ fmap (\x -> Map.lookup x (spiralSum n)) (take n spiral)
firstValueLarger :: Int -> Int
firstValueLarger n = head $ filter (> n) (spiralSumValues (n `div` 1000))
main :: IO ()
main = do
input <- getLine
let n = read input :: Int
putStrLn . show $ shortestPathLength n
putStrLn . show $ firstValueLarger n

29
2017/04/day4.hs Normal file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
import Data.List
testDuplicates :: [String] -> Bool
testDuplicates xs = length (nub xs) /= (length xs)
countLinesWithDuplicates :: [String] -> Int
countLinesWithDuplicates = sum . map (fromEnum . testDuplicates . words)
isAnagram :: String -> String -> Bool
isAnagram s1 s2 = sort s1 == sort s2
testAnagrams :: [String] -> Bool
testAnagrams [] = True
testAnagrams (x:xs) =
if or (map (isAnagram x) xs)
then False
else testAnagrams xs
countValidLines :: [String] -> Int
countValidLines = sum . map (fromEnum . testAnagrams . words)
main :: IO ()
main = do
contents <- getContents
putStrLn . show . (\xs -> length xs - countLinesWithDuplicates xs) . lines $ contents
putStrLn . show . countValidLines . lines $ contents

512
2017/04/input.txt Normal file
View file

@ -0,0 +1,512 @@
sayndz zfxlkl attjtww cti sokkmty brx fhh suelqbp
xmuf znkhaes pggrlp zia znkhaes znkhaes
nti rxr bogebb zdwrin
sryookh unrudn zrkz jxhrdo gctlyz
bssqn wbmdc rigc zketu ketichh enkixg bmdwc stnsdf jnz mqovwg ixgken
flawt cpott xth ucwgg xce jcubx wvl qsysa nlg
qovcqn zxcz vojsno nqoqvc hnf gqewlkd uevax vuna fxjkbll vfge
qrzf phwuf ligf xgen vkig elptd njdm gvqiu epfzsvk urbltg dqg
sfpku viwihi fje umdkwvi ejzhzj qrbl sfpku sad nawnow ksnku
nzhj mfudick ueaa jnhz kpy pzk
euiin xvl elaoelu wbdd xlv jtm nohtq gfdbgdg gdfggdb edtym
xfmkn wyww woe hwysuh gjw dtk utryasc dela eluk vmmun
nmag qfwe cwslmgd nlhf hpf
ifs sszo iod isf jna
pjptwg wreera leyb hmlbpf qcrbma ylgue
rwlpo jhla rprxvgs quguh pyybwgl qqvcb
rxtcpdy wmpci mpcwi vwvdzdn nfpnj rcsxinl itatg ycy hrctg ron wveju
zmkfn wip pyiz pyiz tnyg dvftf elks ezhotbj wip
sgmtfdd xdl sch sch yaxzh wphgksh knzrixp yaxzh etm czqbaa jldta
gnbr rnpd upe eeb sbq sbq oxc rwvugoj
cshk thcc emfxx emfxx pbtcf jpim vltkqar czy iudkac jhpcc nqs
uzbvx fkiuyk izxdiu yutntvn dixuzi hkyfnud oyz ynutntv
ewl mfns idy fphu yqccb pte unukirt unukirt fdx
lzn tin fgena qbql qycbdw gbtn lctlysx adhjfq blu aiv
ites ites pbxzunl vljzh lqgerta pbxzunl
vmk wjfzvhn pqkidze qfwh
tqprmc exypl caf kwikh mdyyljc pbo hhxxo skna
sqxgejb ejc fvup hpesvs luz fcxqwhr ypxof fxlcp pxyk xiczjri
vjg qcw fsyqaoj mxf jha feclqqr vzdqnk verw mvmvm pvdqtcd xsfu
fwwn ktvdh aecfv acfve yjozxwo cnujw bcgde lphnk knlph bqwlqju
uwwapm dlln uwwapm uwwapm
huupab ewetcte huupab ewetcte
wjs zipivpd klwafr ipcczg wbv uujstsw hykn
mgojdyh hlm xruta lbmaxit pabqrnp vkyigd ptpzr glin gfau pbo
tbwx baqxq vtz jwex tvz tzv
efopwx wfknzb ogaxln tqzrh jne zugd zpxikma
rdjsa arjds hqdldw fjrtl midt qjv jfrlt
dggqohj bidaaty iah lgmug wwmlbc lggmu laonaoq erkqrb tqolnns iygv qnonlst
msc glwn xjfnij itt pka irrafjd euazydj silo
zsyut znsht ldky zsyut dzcdft znsht
iit cyteu pib fgvrs iux ffctql pib zuzp zsbb ieoi
xxtwlu kqfxjhq isj xqjhfkq dohrs haovzc cgfwfrt munqon vuyexz nouqnm
eptpqgi uiflvd acj livzq ejtt bniud cjdh jkrcken lspfy tpxri zibj
zxme dpo fumup gly bkdcwxn lsly eglhe
uoshw ijoyiql rcskaa vjbqv roiinzi fppqdu
xuw vdbxie oypcx khxq xebjt oypcx uitqep vdbxie hoz
lrjv tdksk uebo wktebvx nlapmp udhhxh uliqbm cklyyf jlzw xrmdlvx
fosspck fosspck fosspck qyidyur hxnxmb dkpj
rmrvlms susvos idw hzy idw tjgxbc lhgqxr tjgxbc uuq
etjmbdr hwqe lnfwzni lnfwzni good eisci etjmbdr
yqde bmlcc yuel vpyplss vyvhho kslgiu lllhc jtkijdj uclz hfbqsf
tlohlvv tlohlvv bdqahw tlohlvv qavcqrn penia saafy
lvtzyt qffe eaikhv eaikhv wbnl mdkg mdkg utfrm
luowwk magp luowwk oyao oyao hsb yms
gnxply vsdqum nprf jik axdp ariqjpc hjqcc
izbo nkqkb xpqg pgxq qpxg gpm jxbkvu resj
hsgyxar hvsl ner zzmcn lcpdvqn ern
rfjlhu xkyh hafs cvvk drg vjsk mymc iab ycmlubx kpwemiw
wlci qhhpr vhpr oyrili cnynh sivdso ldjya wilc ioiyrl
cdfwd mbtk sienxui jsmxzo yxbeyl bybtc covxaq yuxn ktbvztl ktbvztl rcekjhk
ptenhqv tzdt phetqvn mfkdz
hmezeak pqvqld amsih jxqacc uferfyh nfqjsz rtuqdzz pohcx qia cpxho hgpqs
iygny dluc uxqz nlujm xkdtlm xbdgepg jwn ohl wpfll
lnqf pcxy cpit enp zpj lqfn oowgw yoxdff ohvcfcf fuvz qkpmb
oydu jlscilm pzxen nmtdngu tslcupx ntdgmun uztpx nlhh jqn llsv
euyx epn gyvg cwtoe ruyap yaurp uryap obbl ovo
pgo irm ksrxe qotuygd afwh qfhzfsr wafh dqjbwce dzfo hew skrxe
dpvel dpvel ipljjxs vrrsob iakey uheuu swxu qmnmn mpjkb jqrwfmv jozj
sempz plzxqe qvyg sempz fejux
cqgqvg zhqir rqzih vyu fmb mfb
uejl kjh ayz dzimg yzafvg dem vlogg
htfkd htfkd hwykmm htfkd
oxvgq wtai rkyyxya ldkecdv
lvlrsu rsullv pptnrwi slvulr vxrk dpzti
gde ixwoz nnsx nhc nzi
dsadkj qtgya wco psyondq jayad crc lswwm purrad pof
nocibgs hxqdejv nlqxdpu dvzd
jfaws aiwnjm tqjpgs fuiobz gwnemv hjevs xkbbgiq sakgv zmwpkuq grjllw
xrr jwhtchs boaqkg wjhdr xrr
vyapct tgw juzgwkz odddvof juzgwkz
unuu kubdd dxr drwg
qpefzz iemo fwa vhdcxx
hseqy copss gytzub lxi mrxtwc hxqqdfx ijt kcy tafjs jit
uevse rrq zmwyjfe xljx lhgnyzt rngvwqd
gfvpyhq xpdhind eocgpiz ebs pcmsgjy swni iwns thspnh yvbzxz fgb
hxr ehw ekfd ncxcs gxjmd oqszdjp fgu gwuoafw zumenf qltbw whzuxov
wfc pawqo pim jxgt dtiwzil hdptivc slkazm htafjih hzheez rkk amy
mgoatiy pkec ddvwyni zuya aqrcjes ubkaeus nuhhad upe qfem bpcc
rmyeg qfq bia lzk fusqfb ltvgry vggr xaxi avwdkbg zhlzt
zkjoeee dyi sxdwfqa irqljmw gek dgdb mrakr ddaznn zlh ajzzacf juv
kmqcy pohbej hujdgao rsxfkn vlu
scnpa hvl cybql lvh lbcyq msw deqqb yjpsndq
ndhjooo dpf ziey jtjlc eesag ldhgoif
tysbae wkpst kjz stpkw sil yetsba
ghvlfq flhvgq tgkjie gqlvfh
oimn vlmsljl ocala vokhrs odyv msn dzly wcky
cfjwmh rpsdor bttnkg jxenm mwdk mer jgsusdz cslf
ialvxk bvc qjfikr caw puhmmfl xpmsx
tyoey egcf dijg vywd enued uxkshz nav bdrn hjugffi iobqwiy
eykhxck shpfjhk vlqg alkenz kuj okxs oeth mqbr nfvqvkv xfvyi mboo
zbw curcajm mel jxqcw mpdscxq rhadty zrddeh wmedc wkcwt yvwm
iee hzeofmh pqlkkb azlam fpj hzeofmh ripi
sawaqek oyoiwtb npq pisadk nnd bzgo wiqme lxnvn
obqx ffiegn obxq for xobq
zwway wwazy aqxg gaxq
ebssilw nuscati mofyc sogyacc yujmdwu ehxsx qcaf udvoo nlcfaz eov
vnbe wtzzjn bczyxt crmvas zujy kukq zujy kukq
gvltk kgltv kglvt zflikic
hby pium gut fjqn sksoqyq kcliapa
tbonrr prf vga jqgw ulze ukfig
zafixw hia omgwoi noeiox fqbket iviidgp bebune kwcuotp slvy wcx
fjq cyecn fhxvj byv kojvj iaqd aaxva rkogp
vqbbt sjmr mxu mxu rlfj yqhtzv cuar yde yrs sjmr
iyxiyp auepgw dtpbyvu thuoai fpsfkpn bemgbsk lni ozy jogp xldyvvx fpsfkpn
jtha ibn ahbkh xzxkei tql mycvmyh ioyw
mpsc pvdiuu wqixxlo cqwmlrw cttoz lad
srl xxlnofu dqf snxd zjlp htxzd
fkv berlbyh kyna wkme qjzgh thpw frup
irhreaj udkpbza qmgp ormlipa lbyuc
empizc apcb ossmtj awk ttsgi bfoymzd ftx jkicph qqjv tywp fwzfe
zaqkd ysn zaluvs rljdk ast fjp amjqr uabrya ufswzjg vcldkxt hzsmrbl
qvy tqgnwj akibr tfjevhv vav
mhe sxg hacoa emh kasf hid jklfy ijk dih
qvwbenk akdctm jztmsx aqvpodu vmknns nck letcrk poba
lhve kkvff iiixid vtsun uvgte mmlxk pgd
gktphd aaoqwz lrvsuw ofcyvmi suvwrl dpqiol wjgj uqigjx
tbp xoc lmz dyzlvp bjleh pxj xjp xbil
gpzgvj tctszm tctszm pnp upqtmm rribg tctszm sllsbr
hpm qvjnd lyqg bybpwn etz pwfigbg uqgrvpg cvniubo
tpowus bdncyxg gmm ebfg zwoue izgkwtx gmtfeg xvudp xgmjp atrvn aqgl
wlrxvo wvonohi owxlvr owhnvoi
knyo aiixyi sjtqb kukhgv qkj qiuefb syhfc aoana okmot tdsmnoj eyzqjn
szhto szhto szhto fxpsavu dtcz hnwqdvk iza
poykme rboczge tuyiw sxr
lpgbp bpmf aiqy exzqt gxdoow yjp fxwdmt eoklc jnps zbnbiwr ppvl
huecy jjhyz pwcea ffofmj tts
ahbmkw brz xdenmw mwexnd ncdxgf gcxnfd
yhfnra vqljz bkyxzt vhtsyde ysaxt qbw
gqhiej rofhmp soeebdp rcuiblb rcuiblb rrnh nses
pxrwe suil iihzf lhcgmfm mqasxh ttpp kqitdyf cuabaa
cxl cwsp qyseogj dimvv igsoxu ncrexla ubrvpp oum usluv
rkmo jqqcdjb mobqcta pbcmoi afjlh mork
nmohoeq fezpxh fezpxh yec
yxlncrt ivi dajo tjpim tjpim
hzhy rcjs uhyvwz tdpxlqw itoiyf
ded apfmhe stfk ugyujv drwks zagqnw mbbzmvc aoupemq
iezre wivdwif xzytxe xwytd vpnol pljx aot phln ztncw
ozblu asda tkxh xqe pvijnl qwwh uvp bdhtgjt uynwtav cdz uqmvp
eukgtsy kdfb bdfk tnv dfkb ewdemb
rsf cxnk cid qsa zwk oetnggn
fpq oim zetbmlk fpq oim xgv cbaj cjrqm
phgldt fhmkc efkztj qidri vsv bvjf lfwfgm wfuoln toamg wfuoln idrs
iuc rrdnk rrdnk asqhnz qxkigmo eeoim mmdtgif akk
rfvsyy kopfhmd tnv ibo demeqm gxrxw hwk ukorln bep
ialo eogif sxlj xfegx nanch egoif eymwt
kttrpjq gbnyiat kptg oarewx vkmt gbnyiat szyokf
tjll xviodi tjll efc rliugl wfbbpq wsqvdli jur tjll bguqyu
uecm yzjhn vqf labnc xyaksj
hjtef zzq ellr wtrodcg drwqo ernt uzx sqiokam
izmh ddutl bdzft jvfthh
ecr xqrp qlxstu kgprd gqvtwni mkughf bulabe bvoxkx
jwsna vjwq swkycg cpp dvmyal xotxviy qkiva ffa eakwp fww yirri
ufnl lpuxw rjki nggh ajdkpvo oeuaemy bjisma vsjzc
ctxu aavlw rap fzxtcp msufn fzxtcp sdlaom vgvdvpc
rftw cyf twyxi orifavd
ogiht ertz wcw jnqdup phvp lbw
tplpyq jeh aobamqe bvaim qptac gssi mkjbaj
nmklyg iitx iczojzr vjspqb uooky uooky hjk
ggnekbb bnebggk sepzjd fvqfgr
wnfwrn yaiogv mbusuy cpbcgs thjea
atndjc dbjgdz guedeay rasa kfhame pusuu dbjgdz
xivzyml xivzyml eqsykxo bshvz xivzyml
nfe ayx gscy ylyp oqyl isatnpx poaelm zsrw dpd eyrdjpq yllk
feqktz mlm jhi yxigeu xzqa qwv yquxw emken jgqsp rojfcu
ruvfcud poubal xswer hfhpyp guf pzgzoq pzgzoq jwgxafi guf kqzzlu apg
rxwcsdc rxwcsdc ywu rxwcsdc
dmgsey xrtx wldwyxz avi
yxnqv ewlx fvif ozfcbxb zqapa yudqksk wlxe mjpvgz
ozoa ozoa hwkbp ozoa
qcv drtqn uqv kcsavgn ybzs tkw
njmloq wapa srm srm ifurca
ezm ccj rub yuaww xhee liikjee kcabgic sbgqx vrpyo pzmesdp ksvv
hycyne raaksm nylsc lcpgn akasrm vxwoaum
zhugs pqquitv bae lyozb fhij pcdcc bae rygsgm pqquitv pizz
oxx bzk grpis qiqljwh svkn
qcq qqc fzgn sqg
lclad motw ukz zghp
glr okzfs zgv ygsvv sauuog glr amxr vvmwmu khy eyh
ukpxpy rgnqyaw ncm coeblf
qdbr ortzo spvnrnq uomtj vffbeva
miwar bidfxp eibo qyee
yldec ghwj mxlemvi imac klkvmg fekxhp kevlzfr fcgnoq fncgqo
hlm vlol qdic rltij nlzxfys rzpoh
krpwspb yrosr hioqla dbpgzgu dvkvvc vvdckv lcjzb qbsbr acbi rtnk
iqtvk jcldzuv smly whmnte mdwlse mkxw mfnkv mkxw kes owkfh
iwcjmkt rnb bjcdjl furhzuu exs
kjwu iuaj ixkujoa jzeau whpn
tvj zrdy fwsbagh zrdy czuzum lxotprx wbohaai
crsyzod jouf osxntw iwzzie bodu scze gjxn vgxvqo gjxn mmthykb
dabjfb vjqz cvr gsymwoe qzpusj twvwhw gyvlqd kdrdkzm bdljp cvr
vmswdz lgjsvxz yjkgqkg tzmjkfp uzbmwxe kuqa dzomt hep jjlibs oxvpvq cix
iqgd btwdjd ncdrovj ltxqc orwhdlo orwhdlo
nxro uxj ovgha elvzl xmlzssr wonimvb urecfx dbfn kope
tbes cgyh fypswue fgxjqtd dxdrfm pzhnaeu kugspa
eouzw qrpokyb fyhpb bcvfvze brdwey gpaa fpqutw pbqkroy axtc egamku gxk
xdrovpt peeww wkcin suir gvrbix
hgsjks juvod jtii iijt
yaw hzifa wpagkd tgvmc iru yyeuy mgcvt fhiza
lsk lks kls edypaxo
tjz qjs mgoyd gomyd ztjbex nprwk vvw rtjsq quvf vuziqtb oygdm
kftodz xua lyxt zfadf fgdwt zfadf xua ehwykd wniahd mqoarg
qgiapb xptk iscyf zfspn qvrpva egufqte zfspn hksw xwxrs dkdruku vegfs
wqifs wfsevg iwnjjpi oajju tkvhpl lemuw
rzbmhso pbvb lfgpq fzjwxxh pqlgf rbhsomz
ufi aiyd gxozgx hygjp dtma uughdc ojumcf yuadt
caami tqzkvor tqzkvor tqzkvor
vhtnvyx myxdywi mwpwq hjxadd qkcj vvytxnh dmbea
jvjtcjg mbiwyad cup xkrfk puz uxpmutf rjxyxyn mfchc
ocrak zprfbgu pjjzl zoehfkm xqn qki uxq tcv emknqjp wvmkas
nxg myr myr vnfzpoy
gwu ezt kbmeouj sxue cxax gcquz ieegnal xecusia vxf
xermi xermi qporwc mzemns ticltnz ddpsstr ddpsstr slgbn
xnujwtw bvzv xjwntuw unxwtjw
tipo akp fkmcls wglmjq fnrtsv
fan dfbya qrp lcvxqqu ldpm gucmeky mrzy fixaph rygneb ocm pjh
ovtrqs ujmbnal geihpe mijhy eewuic toaxbp ipy tvb evlmrtd lbujmna
lsmbwwd hvurk ihbuek hvoyq erzomhn gue lpq dihon dgzvst
fuoshq hfrzeu zfrhue ufqohs
icgwnbi gmhogxu gmguohx toixb hfwj haxlav hbe jdpxeyi xtgfi
vfakk ioil hddqu sdztx hduqd bmiuyr vmas
mcvjjhf sfgt sfgt lambvp dnqc pfecquk
xgr omy bmoadg afbna mar nicpazd iveku zdioyo
rpipon dwg wgd pironp
fkyx wjefuy mfesst ztlf gnnceb rsbvuk ckilt kliqnm iuifcvu
lmgzx oknwr wmttry luipa vcttj nuqdmy
iota efrxkk daqzm certtoi nnvqrwz qrqgza tllwp efrxkk
alde wqmdjy erh txrtqm zuljg hspbnrd pvsnebh bkue pvsnebh txrtqm txtthn
hgggm rswwfpj uctzrv bylqeen dpbnw ostsjwn jtjiyuh ofxu mmmqlg ayhza opbgdrv
qmhkh orbeokv agosach lhujcju jzpp wmxtcy jcxglu iuwmzrv xwkgz sxlzld
dzcdm lwal xpujjm xpujjm lpfojz lqqcon qmqrg
gmwugq ceslt rxcogaq jwkraq
joxr brdy yixlou brdy lnr lnr
wbut pxlsclt igigapq zeacg jxiezn hvws wwz ujpbl fdjtfjw opod kea
tsodswf pufo zqrt zvcpu
nyy mrqmg zkt tslzsf zkt
hxywv lbmogd hhv npyzgjy whfvv mlfqjr ggjz owijo zmesslo gtvizw
xzz dvpzxbd wxwlp cye rcqpgrr gynzo nhy gzpk fpfmb
nhaakbv iazpdc yadqbe kmqm dffq lidnh cegjosw kgd hwivd wijj
cwmdyf huoy awihev qav cwmdyf rdwck hahj pesfyk uoju zrirjdu
qabl vwcwbb phnd xnp huuzwxl rukbp kod sfu ngcvgrt buncnfw
regyd gjzfwf hpuv zmm vphu gwffjz
rdf emo crsoeo bksetj aqfzm pphny
opbmboi iakvj ymjwm vxoq qvox yafk zkch adlusz
qhm jul zasv xhu qnhjwzx
mjmyvd mezfuls upbdpzw awc qxta bzrx tjpjmj dxfyewc zorm
bko kfokm htcpoqc liuvj xhmpcu ccqphot dthvo pfj dtxpmu xoocm cmxoo
kxv eenns qhpfsvo gqoyv jzjho aoscl fetug agxmfea aygpt
javmegf jlmt epdwy egfs hwv uszcqvn foixpz iukh dbuhqgs zgb
zrex zrex xtx ydan maomp hqdhh mfvan broh wvwhqbu
phatsot joipm pmniq arqzmbe vurl bgy iwbwk oyhngcv vnzbzgm bgy
xprufgn vhca nrs abuh zwsxmhk mqrj tyslsij ojkdzom wepxg koodzv ypvyy
vop nnpz mcod mlli ntyhz laqztb kauqkla gmrfte pcuhaci
vrenj lypors prknc djbdkzv amofdx
lgig lojnrw obusoc fkwe ggnv pydcraq bvdivl vev mrojjs rxa
qeg tap jocwlsm vqxa lmjscow
gptlrgq vdasm erdc oparmw
rgbsa nacqhvm pczf anupcp upudwgp
jbnobi ifhzrd ihrkkf osw wos lrnwv
aiuntpl fcxpmz fplacs fplacs tipm gfotkx
fsbnd qoc ozmbi rqv fmbxh tuso kfoxvjn ocja zzs jwplx
muaklvq ghozoxh nwxbh mgoou ufptl ouhh reyuf jougckd dgprag
gwbnqwv dtrd mkzxinl erxl zmfa skuu crxmp wwao wwvdpk nxbn lglzy
qeejk wvnypc yfzyfcr eeqkj
nmcp fmkgfyi grfthau azw
kkallxz rjke ukbt ixkhfb bktu jkre
pxj mnwe djrjde gpsc enqz pdbydx cktfs jjeddr
mgplj yyunujc vis odee ccesa yyg yjcnuyu doo utse
flyy juvxomm vcdcyva lfyy ozxnuzw bmgns
kmsypi zpbyiv rrycnb qos sslwyeo jgbyv njltzt fuwk nwfb ozcf xqnf
sdcvgmy sdcvgmy hzv uyq sdcvgmy
fyox vmgxahj ywaxbmm ugy ruwc mys yrjwr ozsxb vaq
gjpyc sgdn kgm fbvq cziui nzy bwu ezjkkus jrag
kxcr tgjxss xkcr bembjv rbbiw bwbri
dcz rrhvdc zbonfzy ubjt
rvq yjnzswt vatkopb xlj dwxig dqlt qts iva
lylclc jptz rbidu lbt byxk
lwre vwriwh afixsi vwriwh
kmvbflr nfptw fbglxh pyas dxmn hemf segaz zrs
dvbey zmj xfoi bma udtxhb
yryng geiwgz bbrvjp ala
olzicp olzicp qhhslry olzicp
exf xdmwh xdwhm nhjsssn rmlkdb excguia fex
xkwgeso htys sjdk jizciy gjjl phgqdjh wzdb izew zcrumu llxfp
frkohf oifsm aisebkt ijsfkot ukk
koqf xvoior tpe erfpnp npnx
sneysk nsxki wpmhd mdor akrpvgz moicncj sbsj owfhj exw
oqqbvk xztx gtxlms icmo
lfy ltq dlzqlvi ovbrsa gzm nhcjq umbtgm nhcjq
iuopdzq cqaeuu xuzngq kxlx laml slvvr frtml tvioiez vyoomw xickbqh
ckahov mepeku gtaf gtaf
tlto cnnz kzsbkjo kzsbkjo
kqf comkf dvrkyl jdsqi rnwvb vxvd pok
hncq xcx yuykfs egrruvw yqh smcou
tywyq xeq cix yywqt jhzptci hybcoe
zsw zsgot wnu sumd azmuos qawjaz rpf zkxgwdu iom igh
vmxmelt gll ysbbt yboqoyz ykdglk cnypf otn owsz ipn epfeka bkmy
wxjpce etzyavi whb sxzft bfu dgwnbgc nfw sxcteis qqpk
kofv dgoyme vlza oxhbo lrqt uic tvfqiyy iaqm afnk
nsmpg wkibdcz dxbw tlxzm zgwe nqwjji eacbhn blk
shlgws eencr rtufah kjyvqw transt ecsq otbf
obs xsjceex ffqj sob djpq jcda zlskve
rfqtle klarp mtzrx rasr eisqovk rpt vymibt zwrif ilsnd
ldu ffd ldu tizfexr fwpmyan
flxso tzec pzn flxso kzdouon tkvkj
tvd arh qywql uev btvnpm
wtwx kzafvk ybyzmhv mdbrphy vamlvr gbxhod tyulba krcqj ikotmla qfhpa
bnfin ebngj agfdfzu rhjtj aaqzh fsyp nilar uwurjnu hhmso hhmso
uanmesj vshh syosjdt xkormf syosjdt ifvytwl qnw vshh jkg
epyzcn pgdxgye lecnx nebg jzdhvge hfy imiyft
zonbcnv vuvg sxtuty zdhmiow lmud cuegzg
bxgft mxhzrh unqd pqpsnce khykn qlb oujdxpq pxrd jzxjuxr tij
qss mqirowz ijjswjm jjer utwn kuedqxx bxshuok qkfag dmfwcr
jgln zdohd xitfbge xbokj xxeuv wqhvhjo erg cua fhc mhwy
euo ousht ipxt tpzq vnbmlo wvbjpb yjg bwpjbv nzvsea aerhsqv
axhmi bcf zdx vplso xhmai qsk psolv
ydnpmyo pfba zmo nat ykwxvm ydnpmyo rtd uuvqqr hcfccbd rtd
ytp guw ydmyf rww oucmpf gemhpj labc
edpbefn awgg qzpe aat cupig
mmi ghdaoh ibx fbyj gge vmmssen nplt mmqcra omcvm uwa fxypxfc
kjaw mtijne cfmsigd zwcjjd ajxjlqr tbp bnilc
fse ele vcsyiv bfe udny vznrao mgrjfgw
hadl nikvvpf gmdg bkmgt ugj
xkis qmr cgz nresp gms zrii coxkke vfsqiil
wmicbf bkk wcwklfg vpcbeg kfmjab vabc dax tnao tnao fvvzeyq fqm
bct tvj tra soo stqao kqua ikupoy wulcu nauxkkb pvqxy bfu
wpz txdduxq gaehfki kxo lvjzpxu iqon swr eyihl nbbec
fuphnbj bdtz huwu zdtb ilgzpa uyaut vpy viff tuuya
cvusbh bgy apsao qsupha
jtzlbd ljfvh wkjrw xsah sef jygb pqym zbcwok zdmug qpym
hbibuax iorqc dqjrs daeb iorqc qiw sagyt rkc sagyt khbr
shz mgn pqrdbm jvace gfhnq ann zosq wdwzmuf kswsg dzt brlavyo
qiw cdvwds dckpruy pybjra lfvgfn cwj bajtud pojehb rzrzvwe
txfyk zkgeeu zkgeeu zkgeeu wskcv nccoz
eettnxq gbgr uiqonyz wqtgs ozfjbn gbgr
svd thmmr rbbtxn sxkq isxlnhf tamdlbe bqrgvu nmpvlkc spko
qmn rspbjme ikjddkq kdb ugpegi egipgu
ufffijo revqpep zfw kwd pnya blqo rnntzx anpy
piaeyf vbeye uuqd vbeye
hamd hap ekk lgla twto
isniinr crz sjpmfxn uskwj
lzeofk tavbq ijcglqy lvy jliqcyg lwlip
uhyyyw itlrf tdc iabeocv jzwnjh vqxll nefze pyrxmx eispxnm hzlksce
ucuh mlam bhyej rgzkew ctbo iswqnvg
ytmb toppqgp ytmb gqgpr gqgpr vps ebv
eavn atkqltv bjvojs kaskr vqltakt uiktr xglc eyb rkkas fhnf eaorqm
jmfipc ujggeh hdxpfa xtab ydkibi ycxn ujggeh icheh vpznael oprbf
xazqxg khlemu awh uwz vhnixk vdcty hkk
gcl kayi hfozask grpseyn zviy tzoum qywnr wqkhq
ctrrcpw wqfbylp wqfbylp wqfbylp
gtk lqohf hqeaku mdj zrfkmxn bcqgf msing
luhpel kexokpx vojap ldaexs bbbtz
oimnqb esg zyjmbfh dfyhcf khpo zjtgm yelztbs ugj zjtgm mxro xyfxpk
dgtsu vvk wwfugbx aai zlxab beyxcg bpx chc bnxui
irrwbo orwibr lqt qtl tqknh
ihjsg ihjsg powwy pycyqo ihjsg
xdcu outh fnqrc eihkss bdylm sjunib eihkss
jpnw ycimse rffu ismyce uhxl feai
yyodnh dvwshkx vulh pvxj ydhyno hyodny
vuuweg pfguvyu orhei orhei wrm amkr xecja lmnveth
wriwe xgtnvj tdmxf gadtqh bezjvz lifu
euft tchbm xmtlwji tchbm
cfi zudn zludl pwiu axe psed
dbtfwf ajxcudj uaxdjcj dxuajjc zouyy
fmycmej bqhe jyfecmm kkrv kcdvjoy
grtb uzs rkxzt hivhic brtg hwyc lsl iivhch qbcp
ymn xfpka hqm sldz dblvsoe
qrcapma hntgmy difrkpk difrkpk xlsph
flvqh akcw boxrz ywhq boxrz esnxzv boxrz
zrvh jskaw mfs fkj
abveb qxfnlfq abveb kbwiyvd abveb
pgarl nbfrenx rnxgx bdlkix liltdm dzcokeg fubupcg iwp xfayp obfaz nevfw
nuhvaci blyv fcsp adlanka sjy syj ysxl
avwakn dkoya yzuszuk lqrr oqfyd dmgbhd lqrr
pxa mcvtoug nlweso yffqc dtuagcd ovvrkz ggfhw wnlseo bpqbn ohxzs rxzo
djkcl kbgyfir ogquot uoqotg jtmyd ohudvle xrnbt yvsln wykqt hntc xlrhqrb
ykt tkxfmd exas kty
zebstke msbbndq itmli ubexmht vekvd xbmb iajbj wac sta
ptdg oftwo goiulah tfmsrqs jffxvnv ozaluj qlhqjy wyffa
xeq ezmlpw xgno xorvfo yzq vwif wsi
hdove hqbzhu pjrxlj uafuh rizlb advmkca
jzk ddoisdh tfjh yuvikps ixpkf hnu
kixa djx uksr ogxty dxj clda ukrs
xgiy diwbvn vphdbg qnelyz tqptqig lenyzq ecsswj
alx awj fpasmmg zukuh qaanvb too nvskuk too gnria
suo suo brw nazq suo dqv
tan uxiz oqa xyezcd lsaicjr bosiak rmmh
bidpomf dimcj qekero wbrc lewt kmgmlao
bciacj eye lxfpef cbdshd dhdsbc qwnhil iuokc
zduefht lrgfjn nclksm wpjpjr hkeqd oprsjcw
chhdr bram swdfjr yikqra xkzsloc otptp agec hhdrc uofljf toppt wpbyrwo
bwlpb nishr knnrysj bvr ftnb iedskch weo
czo hsfp wblh cru kzalun intt
jvob rppz rkwv hgyhrqg
sgo hued jnygge izf ztan kjgpcn fagff jsi ijcxzoi tgqjjp tgqjjp
ltjq zidjy rfmy yevuaa nlhfyg xytdtle wsqvzzx wfflboo nawhv golhf xhsti
bmtzlml xcbsquq vnfsux voep lkss ioim
ntfffh gcncwu mmymn wkwlswa gcncwu iaeyumz
kcgdm rbaau cwsoya pznnnn xzz zbbdlhw zxuelq xzz pjeq
xrmnuct kwvykx khxr ioua xnmtrcu xrnctum ujq imnt ecee
xjsgx fby fby fby ggtpgdm jqvuj qshewki tkml ymsazcq
sdbyhwg kewtrte novhdcp wbuaoh dtytgtx zez whygbds hpg
tjvaqo yrycda yrycda ldbp yrycda
kloi tmsocmx dza sqtxc wgevs zlevs vtm
ftnx drvdm ryjfdgw nerynh cwfjpa mddvr
wsqjyn svg ncw aesn hvuq vybajti aesn bql atxhp ipu
eye romgxj gumuke jwi jrf dtt kcj wmg waw
ptltud oymklv fgnmbc ete apanovb vpt vyospi
clkguhu rbxs lxtnmy ferdx qbmrpg pvojnj zbcffbp
itngp dvtlq fzxp cxrf gbxxqp aafls pfe bpxgxq
nmikrui ddsq srfilr gnuvghu mwnacz nlbdm zcjm uylgev umzu mftz nmikrui
bow jmnxyen bow hvz
lksibxk lefzh lksibxk nkxsi nkxsi pldvhk
osjlzns pihvr zpeu zxjgjb xplykfk xplykfk
hajmfss cardd kaddjw uicfde taue
rgwdjra sgifh ggt mpzx usghkos oob fvzx ghnyxr sblcif
dtu gnihpry kjdpiny xvax itmluk fxvgaap bei xuq wzcy rhb hailtgo
wwob ueldq ueldq glxc umimwv onu dxhmhis ebottoa lnysfiu
zfbyi eyq etaj idpbkf
qshcfjb ozzqigv raztm ymcv sgivwoc kightf dcaglk udah fdm
jmxr jrcnck enffwfl jycc jmxr cylnigo enffwfl
bkslhv tykqw tykqw mbeqrbt tykqw
vogf nhqltpt nhqltpt vogf kpc
ryayz ddktu rfhkmx xok xninjcm ijcrw fxu
cmezfj zaamjrs whlcuoo mug lcaqhkb ymkdci qexa onhgk pgy
hcrcok qri fki wbiog ptj pmgtdt
xsl mpfxwbz bmzxpwf hrysu bmfxwzp xfja
gybzho ktokndy rzkbr jcnp ahicq weccg pgrodkt che vaglyn omhmpo
vdv bngjox srs faymg xrmf enseu aygfm gvsd
nuzi xodkbag eevovl bfjuv nuzi xmejqn
kcswegw bpa dgil insf insf
stg tklrut poi knurfpf
pcs dgirfie yep lvkfk ype hntt athvad clfybsq ofjhegj epy qwawns
wjtpgd wjtpgd vxnapp mwyfsm vxnapp rvcswcs jksa
ckzslrg wdzeimw cqhp nfgk zgukvd yyt tra erkx wdzeimw
hsww avl vkmzej hsww
mum oczj jfew rag zjoc wjfe yqynjqt cbkcsgo mri
vjhfqdi vjhfqdi npfa pzdmy utlyw bwvbfm nqdv iiap ygpky bwvbfm eocya
ewkqi ckb yviuro mqz vtrdam yzkqzv ppbj lhmj blkafo juxvwke lvewc
ljrewgx sutnb hfsavbu jofr ltml mjzkzz nmjii sutnb eonegt
cxzv nepyrb wmejdo vwqi aeqys
sbx fmne obzdz rdnfb gmb sbx ykcae hbzom ncwju rhpiao obzdz
lsgfun cbmfjwk fya ktzxbwt
ica bpsk bwjwkp obloxdx uwoqdo bnnhjuc tlsx qtaacp bdooxxl jamy ade
psus wmtkg ikvfx fkvesj upqlhfs ueje nyt abxvo
adlbl hzskbrp ooht nps
wtcgnvy nvqtvx tvgnycw ntvcygw kkxcp zyjmpbh
xfxww xsddqe ewvmgw qxqwy wpabtz ppe zuiw zubcc onaqii
kkaeec xhcakul wrrvi dtlqfy ahqdilw bnt gwimw espaivx nam yfv
lxz jtc nkwgz nbgsao olsck emtltf xidwcvm lcjxq
eav dzh hnbp hnbp yeg
egaq yvat kavsige csar zsi sptai
pofijc ibdnoe caoazp azlnjk dqp chik lowll iby gpvjv ohm
ors lexk zcneaj rmesx jman uqkb kvkq zfufmn
qgsyzxd hlm juerg ortfzw hxjzg
fxwy lcoc fyxw pzhynp yfn zdzrz
datmws ckwghgr gbtyf lqrpfgl mbgpd dyjilr fgybt hxpg
mxw facxdnu wxm urltwtf qfo wtpwrj
esa srypq jauwv dpm wdgqq hrke icvudq bdmubb ellhfjh ttpjjd gxmg
gvwvqwj cbzzuvj eckube adqinpa djutlue wcpw vrt ucqwu ekruwsn
fhj fst zmtb yhwk dxlbozs fcb vjvuxin dxlbozs rixdvu
egfoep cvq icd prwj icyg
aojaa ezmcuf udreyi bja cyrtpl wjl
gjeka bsbufp tbqqq vbmnqg sfqtgac odhq xzsxt
yse gujdr ugjdr sye
tax hntqw phf eixjwfh qkylnu nkyuql ugsuj
wyh egum zizhfc jrq htbyug lop dsu
exh vfdoosj ajrna jbiaz lqsgvks xklqgjv abtmdud
juqc ormfa sab tucsfln detqfo feg kifsion juqc ovhra
hvcrh oddhme omzmu vmy she xulvfa fecmgi
ayo gspge nkmy yblsj lrsre nkmy pwocjz gdexqqx ovovm
acy sqcz ijl htt yjsi rly vea bck
bniafe yore xnh rkcfd hxfuzw xlr nkzmmcs ekwggiu kgoboi wfuzxh hwfxuz
weq crkeq cccphe dtozviy kzkkdr yku cephcc ctq zbau dewpi
vfla rzpl bnmx uvggon foivrb fval
ziaove lawkpdn ddwl sxj krroj rqmffxv babb
bdw dsifr kuueet hugddwt piz dwb sjixveg kmsoknq
czl feyxf soyvbj tnmpjn kklwi akx nqepntc
nrmhc tkkn jrxgc jrxgc tkkn
ufzn mrhiapi qrme kjlf qrme xpp qrme loyzizz xqm coli
qvaoye mysv ydfxr iixrw
dql tqarux fxqfn haoinu lyati xml
kyve obatly dgfjt fjz sqrz xlbst lgwlt zovih aepy otrpl oifid
ymawam afgye lcnpkmv feilfws vonseh rxrdco
tqij kuawg dmova slds imdtb sjsafo ffkzzl pxxenva wuakg efbgx
yrwoaos vpw ijjpua jnbxl sev yvgdxzr mpqa vpe lboh sev
krwdtd uglxtcz mljcgdk lqj fgpfle nuui cqk exr nuu oyn
dwd nwt idhclm vgkh rpubq wybhapp
hskhgpy gzvz jztbr jwv vcx vdjmnjr jrsp
ikml ceuhcng biu zoo gra bnnforx abzan hwsmd lmki tsl yvogo
kqfc younaz azvgfz gesajr tmwxvyb vmcdu dclwh rfjwhic slfym
pbrhjml rsacryg jga qvgks neh fcq qmi mwb juezk mjteeg alkb
pcj ujstl fkrqm eeczrle hbkcvm upbo mrb qrspjt
jbq rrk xjl rgokbnx hor ogg szxqu hysy vqj piorq wtrtrdk
bnq ntvhcrf vrm puer kde xaxkja sfxgjf
pgcicus hqeqkkx xqekqhk qqkxhke
puquxi hmeaehh oxe tasipw qzyg hyvy wcmpwe
hvs fxq wvfy zjepsl dvrfxnc xnvg
xle crcuc qkhnv crcuc oedez bjw pmwq
xzzpiy cjwss jwscs apb bpa
ydjhhf yeltadb lwi cjdcb ovaox xrdm vkxub
zax xza admbc lvpzfeh auxn rwasj
kebx eild nrskdr meja jxczomh gcne

36
2017/05/day5.hs Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
jump :: Int -> [Int] -> [Int] -> Maybe ([Int], [Int])
jump n back front =
if n == 0 then
Just (back, front)
else if n > 0 then
case front of
[] -> Nothing
x:xs -> jump (n-1) (x:back) xs
else
case back of
[] -> Nothing
x:xs -> jump (n+1) xs (x:front)
countJumps :: Int -> [Int] -> [Int] -> Int
countJumps count back [] = count
countJumps count back (x:xs) =
case jump x back ((x+1):xs) of
Nothing -> count + 1
Just (back', front') -> countJumps (count+1) back' front'
countJumps2 :: Int -> [Int] -> [Int] -> Int
countJumps2 count back [] = count
countJumps2 count back (x:xs) =
case jump x back (newx:xs) of
Nothing -> count + 1
Just (back', front') -> countJumps2 (count+1) back' front'
where newx = if x >= 3 then x-1 else x+1
main :: IO ()
main = do
front <- map read <$> lines <$> getContents
putStrLn . show $ countJumps2 0 [] front

1003
2017/05/input.txt Normal file

File diff suppressed because it is too large Load diff

35
2017/06/day6.hs Normal file
View file

@ -0,0 +1,35 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
import Data.List
distribute :: Int -> Int -> [Int]
distribute _ 0 = []
distribute n len =
map (+1) (take k baselist) ++ (drop k baselist)
where baselist = replicate len (n `div` len)
k = n `mod` len
redistribute :: [Int] -> [Int]
redistribute xs =
zipWith (+) d xs'
where n = maximum xs
(a,_:b) = span (< n) xs
xs' = a ++ (0:b)
d = let l = distribute n (length xs)
k = length xs - length a - 1
in (drop k l) ++ (take k l)
findCycle :: [Int] -> (Int, Int)
findCycle xs =
aux 0 [] xs
where aux k acc xs =
case elemIndex xs' acc of
Nothing -> aux (k+1) (xs':acc) xs'
Just i -> (k+1, i + 1)
where xs' = redistribute xs
main :: IO ()
main = do
xs <- map read . words <$> getLine
putStrLn . show $ findCycle xs

1
2017/06/input.txt Normal file
View file

@ -0,0 +1 @@
5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6

60
2017/07/day7.hs Normal file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.14 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Text
import Data.Text (Text)
import qualified Data.Text as T
import Data.List
import Data.Maybe
data Program = Program
{ programName :: Text
, programWeight :: Int
, programChildren :: [Text]
} deriving (Show, Eq)
parseProgram :: Parser Program
parseProgram = do
name <- many1 letter
skipSpace
char '('
weight <- decimal
char ')'
option "" $ count 4 anyChar
children <- many' (option "" (string ", ") *> many1 letter)
return $ Program (T.pack name) weight (map T.pack children)
findRoots :: [Program] -> [Program]
findRoots xs =
filter (\x -> programName x `notElem` l) xs
where l = concat $ map programChildren xs
totalWeight :: [Program] -> Program -> Int
totalWeight _ (Program _ w []) = w
totalWeight xs (Program _ w cs) =
w + (sum $ totalWeight xs <$> (filter (\x -> programName x `elem` cs) xs))
childrenWeights :: [Program] -> Program -> [Int]
childrenWeights xs (Program _ _ children) =
totalWeight xs <$> (filter (\x -> programName x `elem` children) xs)
isUnbalanced :: [Int] -> Bool
isUnbalanced [] = False
isUnbalanced weights =
not . and $ map (== head weights) (tail weights)
main :: IO ()
main = do
contents <- T.lines . T.pack <$> getContents
let programs = map (parseOnly parseProgram) contents
--mapM_ (mapM_ (putStrLn . show)) $ programs
putStrLn "Root: "
mapM_ (mapM_ (putStrLn . show)) $ findRoots <$> sequence programs
putStrLn "Unbalanced: "
let Right programs' = sequence programs
let weights = zip programs' $ childrenWeights programs' <$> programs'
let unbalanced = filter (isUnbalanced . snd) weights
mapM_ (putStrLn . show) unbalanced

1241
2017/07/input.txt Normal file

File diff suppressed because it is too large Load diff

13
2017/07/test.txt Normal file
View file

@ -0,0 +1,13 @@
pbga (66)
xhth (57)
ebii (61)
havc (66)
ktlj (57)
fwft (72) -> ktlj, cntj, xhth
qoyq (66)
padx (45) -> pbga, havc, qoyq
tknk (41) -> ugml, padx, fwft
jptl (61)
ugml (68) -> gyxo, ebii, jptl
gyxo (61)
cntj (57)

85
2017/08/day8.hs Normal file
View file

@ -0,0 +1,85 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.17 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Text
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Map.Strict as Map
import Control.Applicative
import Data.List
type Register = Text
data Order = L | LEQ | EEQ | GEQ | G | NEQ
deriving (Show, Eq)
data Operation = Inc | Dec
deriving (Show, Eq)
data Instruction = Instruction
{ register :: Register
, op :: Operation
, value :: Int
, condition :: (Register, Order, Int)
} deriving (Show, Eq)
parseOperation :: Parser Operation
parseOperation =
(string "inc" >> return Inc) <|> (string "dec" >> return Dec)
parseOrder :: Parser Order
parseOrder =
(string ">=" >> return GEQ)
<|> (string ">" >> return G)
<|> (string "<=" >> return LEQ)
<|> (string "<" >> return L)
<|> (string "==" >> return EEQ)
<|> (string "!=" >> return NEQ)
parseInstruction :: Parser Instruction
parseInstruction = do
reg <- many1 letter
skipSpace
op <- parseOperation
skipSpace
val <- signed decimal
skipSpace
string "if"
skipSpace
condReg <- many1 letter
skipSpace
condOrd <- parseOrder
skipSpace
condVal <- signed decimal
return $ Instruction (T.pack reg) op val ((T.pack condReg), condOrd, condVal)
executeInstruction :: Map.Map Register Int -> Instruction -> Map.Map Register Int
executeInstruction map i =
if cond then
case op i of
Inc -> Map.insertWith (+) (register i) (value i) map
Dec -> Map.insertWith (+) (register i) (-(value i)) map
else map
where (condReg, condOrd, condVal) = condition i
curCondVal = Map.findWithDefault 0 condReg map
cond = case condOrd of
L -> curCondVal < condVal
LEQ -> curCondVal <= condVal
EEQ -> curCondVal == condVal
GEQ -> curCondVal >= condVal
G -> curCondVal > condVal
NEQ -> curCondVal /= condVal
executeInstructions :: [Instruction] -> [Map.Map Register Int]
executeInstructions = scanl' executeInstruction Map.empty
main :: IO ()
main = do
contents <- T.lines . T.pack <$> getContents
let Right instructions = sequence $ parseOnly parseInstruction <$> contents
--mapM_ (putStrLn . show) instructions
let maps = executeInstructions instructions
putStrLn . show . maximum . Map.elems . last $ maps
putStrLn . show . maximum . concat $ Map.elems <$> maps

1000
2017/08/input.txt Normal file

File diff suppressed because it is too large Load diff

4
2017/08/test.txt Normal file
View file

@ -0,0 +1,4 @@
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10

38
2017/09/day9.hs Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.17 script
import Data.List
data Stream = Stream
{ depth :: Int
, score :: Int
, ignoreNextChar :: Bool
, garbage :: Bool
, countGarbage :: Int
} deriving (Show, Eq)
processChar :: Stream -> Char -> Stream
processChar stream c =
if ignoreNextChar stream then
stream { ignoreNextChar = False }
else if garbage stream then
case c of
'!' -> stream { ignoreNextChar = True }
'>' -> stream { garbage = False }
_ -> stream { countGarbage = countGarbage stream + 1 }
else if c == '}' then
stream { depth = depth stream - 1, score = score stream + depth stream }
else if c == '{' then
stream { depth = depth stream + 1 }
else if c == '<' then
stream { garbage = True }
else stream
processStream :: String -> Stream
processStream = foldl' processChar (Stream 0 0 False False 0)
main :: IO ()
main = do
stream <- processStream <$> getContents
putStrLn . show $ score stream
putStrLn . show $ countGarbage stream

1
2017/09/input.txt Normal file

File diff suppressed because one or more lines are too long

41
2017/10/day10.hs Normal file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.17 script
import Data.List
import Data.List.Split
import Data.Char
import Data.Bits
import Text.Printf
twist :: [Int] -> Int -> Int -> [Int]
twist xs len skip =
end ++ start
where (knot, keep) = splitAt len xs
(start, end) = splitAt skip $ keep ++ reverse knot
twists :: [Int] -> [Int] -> Int -> [Int]
twists list [] k = list
twists list (x:xs) k = twists (twist list x k) xs (k+1)
hashRound :: Int -> [Int] -> [Int]
hashRound n lens = (drop rot list) ++ (take rot list)
where list = foldl' (\acc (x,k) -> twist acc x k) [0..(n-1)] $ zip lens (cycle [0..(n-1)])
rot = n - (sum (zipWith (+) [0..] lens) `rem` n)
sparseHash :: [Int] -> [Int]
sparseHash lengths =
hashRound 256 . concat . replicate 64 $ lengths ++ [17, 31, 73, 47, 23]
denseHash :: [Int] -> [Int]
denseHash h = foldr xor 0 <$> chunksOf 16 h
main :: IO ()
main = do
line <- getLine
let lengths = map read . splitOn "," $ line
-- let a:b:_ = hashRound 5 [3,4,1,5]
-- putStrLn . show $ a*b
let a:b:_ = hashRound 256 lengths
putStrLn . show $ a*b
let h = denseHash . sparseHash . map ord $ line
putStrLn . concat . map (printf "%02x") $ h

1
2017/10/input.txt Normal file
View file

@ -0,0 +1 @@
192,69,168,160,78,1,166,28,0,83,198,2,254,255,41,12

56
2017/11/day11.hs Normal file
View file

@ -0,0 +1,56 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.17 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Text
import Data.Text (Text)
import qualified Data.Text as T
import Control.Applicative
import Data.List
data Direction = N | NE | NW | S | SE | SW
deriving (Eq, Show)
type Path = [Direction]
type Coord = (Int, Int, Int)
parseDirection :: Parser Direction
parseDirection =
(string "ne" >> return NE)
<|> (string "nw" >> return NW)
<|> (string "se" >> return SE)
<|> (string "sw" >> return SW)
<|> (string "n" >> return N)
<|> (string "s" >> return S)
parsePath :: Parser Path
parsePath = do
path <- many' (option "" (string ",") *> parseDirection)
return path
step :: Coord -> Direction -> Coord
step (x,y,z) dir = case dir of
N -> (x, y+1, z-1)
NE -> (x+1, y, z-1)
NW -> (x-1, y+1, z)
S -> (x, y-1, z+1)
SE -> (x+1, y-1, z)
SW -> (x-1, y, z+1)
finalCoord :: Path -> Coord
finalCoord = foldl' step (0,0,0)
allSteps :: Path -> [Coord]
allSteps = scanl' step (0,0,0)
distance :: Coord -> Int
distance (x,y,z) = maximum $ map abs [x,y,z]
main :: IO ()
main = do
Right path <- parseOnly parsePath . T.pack <$> getLine
--putStrLn . show $ path
putStrLn . show . distance .finalCoord $ path
putStrLn . show . maximum . map distance . allSteps $ path

1
2017/11/input.txt Normal file

File diff suppressed because one or more lines are too long

39
2017/12/day12.hs Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.18 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Text
import Data.Text (Text)
import qualified Data.Text as T
import Control.Applicative
import Data.List
parseAdjacencyList :: Parser (Int, [Int])
parseAdjacencyList = do
node <- decimal
skipSpace
string "<->"
skipSpace
neighbours <- many1 (option "" (string ", ") *> decimal)
return (node, neighbours)
dfs :: [(Int, [Int])] -> [Int] -> Int -> [Int]
dfs graph visited vertex =
case lookup vertex graph of
Nothing -> vertex : visited
Just neighbours -> concat $ map f neighbours
where f n = if n `elem` visited then visited
else dfs graph (neighbours ++ visited) n
main :: IO ()
main = do
contents <- T.lines . T.pack <$> getContents
let Right graph = sequence $ parseOnly parseAdjacencyList <$> contents
-- mapM_ (putStrLn . show) graph
let cc0 = nub $ dfs graph [] 0
-- putStrLn . show $ cc0
putStrLn . show . length $ cc0
let ccs = nub $ map (sort . nub . dfs graph []) (map fst graph)
-- putStrLn . show $ ccs
putStrLn . show . length $ ccs

2000
2017/12/input.txt Normal file

File diff suppressed because it is too large Load diff

7
2017/12/test.txt Normal file
View file

@ -0,0 +1,7 @@
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5

31
2017/13/day13.hs Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.18 script --package ghc --package text
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import qualified Data.Text as T
import Util (filterByList)
import Data.List
caught :: [Int] -> [Int] -> [Bool]
caught = zipWith (\d k -> d `rem` (2*(k-1)) == 0)
severity :: [Int] -> [Int] -> [Bool] -> Int
severity depths ranges catches =
sum $ zipWith (*) depths' ranges'
where depths' = filterByList catches depths
ranges' = filterByList catches ranges
findDelay :: Int -> [Int] -> [Int] -> Int
findDelay dt depths ranges =
if not . or $ caught (map (+dt) depths) ranges then
dt
else findDelay (dt+1) depths ranges
main :: IO ()
main = do
contents <- T.lines . T.pack <$> getContents
let depths:ranges:_ = transpose $ map (read . T.unpack) . T.splitOn ": " <$> contents
putStrLn . show $ severity depths ranges (caught depths ranges)
putStrLn . show $ findDelay 0 depths ranges

43
2017/13/input.txt Normal file
View file

@ -0,0 +1,43 @@
0: 3
1: 2
2: 4
4: 4
6: 5
8: 6
10: 6
12: 8
14: 6
16: 6
18: 9
20: 8
22: 8
24: 8
26: 12
28: 8
30: 12
32: 12
34: 12
36: 10
38: 14
40: 12
42: 10
44: 8
46: 12
48: 14
50: 12
52: 14
54: 14
56: 14
58: 12
62: 14
64: 12
66: 12
68: 14
70: 14
72: 14
74: 17
76: 14
78: 18
84: 14
90: 20
92: 14

41
2017/14/day14.hs Normal file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.18 script
import Data.List
import Data.List.Split
import Data.Char
import Data.Bits
import Text.Printf
twist :: [Int] -> Int -> Int -> [Int]
twist xs len skip =
end ++ start
where (knot, keep) = splitAt len xs
(start, end) = splitAt skip $ keep ++ reverse knot
twists :: [Int] -> [Int] -> Int -> [Int]
twists list [] k = list
twists list (x:xs) k = twists (twist list x k) xs (k+1)
hashRound :: Int -> [Int] -> [Int]
hashRound n lens = drop rot list ++ take rot list
where list = foldl' (\acc (x,k) -> twist acc x k) [0..(n-1)] $ zip lens (cycle [0..(n-1)])
rot = n - (sum (zipWith (+) [0..] lens) `rem` n)
sparseHash :: [Int] -> [Int]
sparseHash lengths =
hashRound 256 . concat . replicate 64 $ lengths ++ [17, 31, 73, 47, 23]
denseHash :: [Int] -> [Int]
denseHash h = foldr xor 0 <$> chunksOf 16 h
grid :: String -> [[Int]]
grid input =
map (denseHash . sparseHash . map ord . ((input++"-")++) . show) [0..127]
main :: IO ()
main = do
let input = "stpzcrnm"
let g = grid input
mapM_ (putStrLn . concatMap (printf "%08b")) g
print . length . filter (=='1') . concat $ map (concatMap (printf "%08b")) g

29
2017/15/day15.hs Normal file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.18 script
import Data.Bits
import Data.List
genA :: Integral a => a -> a
genA n = (16807 * n) `rem` 2147483647
genB :: Integral a => a -> a
genB n = (48271 * n) `rem` 2147483647
matchLowest16Bits :: (Bits a, Num a) => a -> a -> Bool
matchLowest16Bits n1 n2 =
n1 .&. 0xFFFF == n2 .&. 0xFFFF
main :: IO ()
main = do
let (inputA, inputB) = (783, 325) :: (Int, Int)
let n = 40000000
let seqA = take n $ iterate genA inputA
let seqB = take n $ iterate genB inputB
print . foldl' (flip ((+) . fromEnum)) 0 $ zipWith matchLowest16Bits seqA seqB
let n' = 5000000
let seqA' = take n' . filter (\k -> k `rem` 4 == 0)
$ iterate genA inputA
let seqB' = take n' . filter (\k -> k `rem` 8 == 0)
$ iterate genB inputB
print . foldl' (flip ((+) . fromEnum)) 0 $ zipWith matchLowest16Bits seqA' seqB'

112
2017/16/day16.hs Normal file
View file

@ -0,0 +1,112 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.18 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import qualified Data.Text as T
import Data.Attoparsec.Text
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Monoid
data Move = Spin Int | Exchange Int Int | Partner Char Char
deriving (Eq, Show)
isPartner :: Move -> Bool
isPartner (Partner _ _) = True
isPartner _ = False
parseSpin :: Parser Move
parseSpin = string "s" *> decimal >>= return . Spin
parseExchange :: Parser Move
parseExchange = do
string "x"
a <- decimal
string "/"
b <- decimal
return $ Exchange a b
parsePartner :: Parser Move
parsePartner = do
string "p"
a <- letter
string "/"
b <- letter
return $ Partner a b
parseMove :: Parser Move
parseMove = parseSpin <|> parseExchange <|> parsePartner
parseMoves :: Parser [Move]
parseMoves = parseMove `sepBy` (skipSpace *> string "," *> skipSpace)
spin :: Int -> [a] -> [a]
spin n xs = end ++ start
where (start, end) = splitAt (length xs - n) xs
exchange :: Int -> Int -> [a] -> [a]
exchange m n xs
| m == n = xs
| m > n = exchange n m xs
| otherwise = start ++ b:as ++ a:bs
where (start, end) = splitAt m xs
(a:as, b:bs) = splitAt (n-m) end
partner :: Eq t => t -> t -> [t] -> [t]
partner a b xs = exchange m n xs
where Just m = elemIndex a xs
Just n = elemIndex b xs
move :: [Char] -> Move -> [Char]
move xs (Spin n) = spin n xs
move xs (Exchange m n) = exchange m n xs
move xs (Partner a b) = partner a b xs
findRepetition :: Eq a => [a] -> Maybe Int
findRepetition [] = Nothing
findRepetition (x:xs) = case elemIndices x xs of
[] -> Nothing
inds -> Just $ last inds
moveMany 0 _ xs _ = xs
moveMany n seen xs (m:ms) =
if xs `elem` seen
then concat $ intersperse "\n" seen-- !! (length seen - n `rem` length seen)
else let xs' = move xs m in
moveMany (n-1) (xs:seen) xs' ms
newtype Perm16 = Perm16 [Int]
deriving (Show, Eq)
toPermutation :: Move -> Perm16
toPermutation (Spin n) = Perm16 $ spin n [0..15]
toPermutation (Exchange m n) = Perm16 $ exchange m n [0..15]
toPermutation (Partner _ _) = Perm16 [0..15]
instance Monoid Perm16 where
mempty = Perm16 [0..15]
mappend (Perm16 p1) (Perm16 p2) = Perm16 $ map (p1 !!) p2
applyPerm :: [b] -> Perm16 -> [b]
applyPerm xs (Perm16 p) = map (xs !!) p
main :: IO ()
main = do
contents <- T.pack <$> getContents
let Right moves = parseOnly parseMoves contents
let start = ['a'..'p']
let partnerMoves = filter isPartner moves
-- print (length moves)
let positions = scanl' move start . concat $ replicate 1 moves
putStrLn $ last positions
let dance = mconcat . map toPermutation $ moves
putStrLn $ foldl' move (applyPerm start dance) partnerMoves
let thousanddance = iterate (<> dance) dance !! 1000
let milliondance = iterate (<> thousanddance) thousanddance !! 1000
let billiondance = iterate (<> milliondance) milliondance !! 1000
print billiondance
let billionPartnerMoves = concat $ replicate 1000000000 partnerMoves
putStrLn $ foldl' move (applyPerm start billiondance) billionPartnerMoves

1
2017/16/input.txt Normal file

File diff suppressed because one or more lines are too long

26
2017/17/day17.hs Normal file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.18 script
{-# LANGUAGE BangPatterns #-}
import Data.List
rotate :: Int -> [a] -> [a]
rotate n xs = take (length xs) (drop n (cycle xs))
spinlock :: (Num a, Enum a) => a -> Int -> [a] -> [a]
spinlock n steps buf = foldl' f buf [1..n]
where f !curbuf !i = let !newbuf = rotate (steps+1) curbuf in i:newbuf
valueAfter :: Eq t => t -> [t] -> t
valueAfter n buf = buf !! ((i + 1) `rem` length buf)
where Just i = elemIndex n buf
main :: IO ()
main = do
let input = 312
n = 2017
start = [0]
print $ valueAfter n $ spinlock n input start
-- let n = 50000000
-- print $ valueAfter 0 $ spinlock n input start

25
2017/17/day17.scm Normal file
View file

@ -0,0 +1,25 @@
;; $ csc day17.scm -o day17 -O5
;; $ ./day17 -:hm8g
(use srfi-1)
(declare (fixnum))
(define (prepend! x list)
(set-cdr! list (cons (car list) (cdr list)))
(set-car! list x))
(define (spin steps n after)
(let ((buffer (circular-list 0)))
(do ((i 1 (+ i 1)))
((> i n) (cadr (member after buffer)))
(let ((new (drop buffer (+ steps 1))))
(prepend! i new)
(set! buffer new)))))
(write (spin 3 2017 2017))
(newline)
(write (spin 312 2017 2017))
(newline)
(write (spin 312 50000000 0))
(newline)

70
2017/20/day20.hs Normal file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env stack
-- stack --resolver lts-9.20 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Text hiding (take)
import Data.Text (Text)
import qualified Data.Text as T
import Data.List (minimumBy, (\\))
import Data.Function (on)
data Point a =
Point { position :: (a,a,a)
, velocity :: (a,a,a)
, acceleration :: (a,a,a)
}
deriving (Show, Eq)
parseVector :: Text -> Parser (Int,Int,Int)
parseVector id = do
string id
string "=<"
x <- signed decimal
string ","
y <- signed decimal
string ","
z <- signed decimal
string ">"
return (x,y,z)
parsePoint :: Parser (Point Int)
parsePoint = do
pos <- parseVector "p"
string ","
skipSpace
vel <- parseVector "v"
string ","
skipSpace
acc <- parseVector "a"
return $ Point pos vel acc
update :: Num a => Point a -> Point a
update (Point (posx,posy,posz) (velx,vely,velz) (accx,accy,accz)) =
Point (posx',posy',posz') (velx',vely',velz') (accx,accy,accz)
where velx' = velx + accx
vely' = vely + accy
velz' = velz + accz
posx' = posx + velx'
posy' = posy + vely'
posz' = posz + velz'
removeCollisions :: Eq a => [Point a] -> [Point a]
removeCollisions [] = []
removeCollisions (p:ps) = case filter (\x -> position x == position p) ps of
[] -> p : removeCollisions ps
xs -> removeCollisions $ ps \\ xs
distance :: Num a => Point a -> a
distance (Point (x,y,z) _ _) = abs x + abs y + abs z
closestZero :: [Point Int] -> Integer
closestZero = fst . minimumBy (compare `on` (distance . snd)) . zip [0..]
main :: IO ()
main = do
contents <- T.lines . T.pack <$> getContents
let Right points = mapM (parseOnly parsePoint) contents
-- mapM_ print points
print . take 1000 . map closestZero $ iterate (map update) points
print . take 200 . map length $ iterate (removeCollisions . map update) points

1000
2017/20/input.txt Normal file

File diff suppressed because it is too large Load diff

3
2017/README.org Normal file
View file

@ -0,0 +1,3 @@
* Advent of Code 2017
My answers for [[http://adventofcode.com/2017][Advent of Code 2017]].

1
2018/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*\~

7
2018/day01/day01.dyalog Normal file
View file

@ -0,0 +1,7 @@
⎕IO←0
x←,⍎¨⎕csv'input.txt'
+/x
y←+\(200×x)x
pos←{⍸⍵=y}¨y
y[1⌷{⍵[⍋⍵]}{1⌷⍵}¨(⊃¨1<¨pos)/pos]

23
2018/day01/day01.ss Normal file
View file

@ -0,0 +1,23 @@
(import :gerbil/gambit/ports)
(export main)
(def freqs (with-input-from-file "input.txt" read-all))
(def (repeat n l)
(match n
(0 [])
(else (append l (repeat (1- n) l)))))
(def (main . args)
(displayln (apply + freqs))
(let/cc exit
(foldl
(lambda (x acc)
(let ((new-x (+ x (car acc))))
(if (memq new-x acc)
(begin (displayln new-x) (exit))
(cons new-x acc))))
[0]
(repeat 200 freqs))))

1029
2018/day01/input.txt Normal file

File diff suppressed because it is too large Load diff

4
2018/day02/day02.dyalog Normal file
View file

@ -0,0 +1,4 @@
⎕IO←0
x←⊃⎕nget'input.txt'1
×/+/2 3∘.∊{≢⍵}⌸¨x
⊃x/⍨+/1=x∘.{+/⍺≠⍵}x

39
2018/day02/day02.ss Normal file
View file

@ -0,0 +1,39 @@
(import :gerbil/gambit/ports)
(export main)
(def (unique-counts l)
(def hash (make-hash-table))
(for-each (lambda (x) (hash-update! hash x 1+ 0)) l)
(hash-values hash))
(def (checksum l)
(let ((twice (length (filter (lambda (x) (memq 2 (unique-counts x))) l)))
(thrice (length (filter (lambda (x) (memq 3 (unique-counts x))) l))))
(* twice thrice)))
(def (similarity id1 id2)
(apply +
(map (lambda (x y) (if (eq? x y) 0 1)) id1 id2)))
(def (find-similar id id-list)
(filter (lambda (x) (eq? 1 (similarity id x))) id-list))
(def (intersection id1 id2)
(reverse (foldl (lambda (x y acc) (if (eq? x y) (cons x acc) acc)) [] id1 id2)))
(def (main . args)
(def ids (call-with-input-file "input.txt" (lambda (p) (read-all p read-line))))
(displayln (checksum (map string->list ids)))
(def common
(let/cc exit
(foldl
(lambda (id acc)
(let ((similar (find-similar id acc)))
(if (< 0 (length similar))
(exit (intersection id (car similar)))
(cons id acc))))
[]
(map string->list ids))))
(displayln (list->string common)))

250
2018/day02/input.txt Normal file
View file

@ -0,0 +1,250 @@
ubkfmdjxyzlbgkrotcepvswaqx
uikfmdkuyzlbgerotcepvswaqh
uikfmdpxyzlbgnrotcepvswoeh
nikfmdjxyzlbgnrotqepvswyqh
qgkfmdjxyzlbgnmotcepvswaqh
uikfmdjxyzqbgnrytcepvsbaqh
uikfmdjxyzibgprotcecvswaqh
uikfmajxyzlcgnrojcepvswaqh
uvkfsdjxyzlbgnrotcepvjwaqh
uikfmdjxfzlbggrotcepvswawh
uikfmhjxyzlbgnuotcepvjwaqh
uikfmdjxyzlbuzcotcepvswaqh
uikfmdwxyzlbgnrotcepvfwamh
uikfmdexyzlbgnroecepvswhqh
uikfmdjuyzlbgnrotcqpvswafh
uikfddjxyzvbgnrotceppswaqh
yikfwdjxyzlbgnrotcepvswagh
uiktmdjxyzlbgnrotceposwajh
uikfmdsxyzlbgnroteetvswaqh
uikfpdjxyzlbgnroncepvswuqh
uikfmtjxyzlbgurotcepvswaoh
eikfmdjxyjlbgnrotcepyswaqh
uikfkdjxyzlbgnrotcepvszaqv
uikfrdjxtwlbgnrotcepvswaqh
uikfmdjxyzlbgnrotpepwswahh
kikfmdjxyzlbgnkotcepvswqqh
uikfkduxyzlbgnrotcepvswafh
uikfxhjxyzlbgnrotcegvswaqh
uikfmdjxyzlmgnrotcenvawaqh
uzkfmddxyzlbgnrltcepvswaqh
uikfmdjxyzlbgnrobcepisqaqh
uijfmdjxyzlbgnrotcexvrwaqh
uiwjmdjxyzlbgnrotceprswaqh
uhkqmdjxwzlbgnrotcepvswaqh
uiktmsjxyzwbgnrotcepvswaqh
uikfmdjxyztbgnqotcepvswcqh
uibfmdjxyzlbgnroqcepvswaqx
uwkfmdjxyxlbgnrotcfpvswaqh
uikumdjxyzlbgnrstceposwaqh
uikfzdjxyznhgnrotcepvswaqh
uikuydjxyzlbgnrotqepvswaqh
uikfmdqxyzlbgnrotfefvswaqh
yikfmdjxyzlbrnrqtcepvswaqh
uiifmdjxyzlbenrotcfpvswaqh
uxkjmdjxyzlbgnrotcmpvswaqh
uikgmdjxyzlbgnrotceovlwaqh
uikfmdjxyzvbgzrotcenvswaqh
uiufmdjxyzlbgnrotceposwazh
uiafmdjxyzlignmotcepvswaqh
uikfmdjxyzwbgnsotlepvswaqh
uikjmdjvyzlbgnrotcenvswaqh
uikfmdjxyzlbonroteepvswaqi
uikfmdjxyzldgnroxcepviwaqh
uikvmdjxyzlbgdrotrepvswaqh
uikfmdjyyzwbgnrotcepvzwaqh
uikfmdjxyzzbnnvotcepvswaqh
uikomdjxyzlbgnrotcepvuwhqh
uikfmmjxyzbbgnrotcepvswayh
uikfmdjfezlbgprotcepvswaqh
uzkfmojxmzlbgnrotcepvswaqh
uipfmdjxyzlbgnrotceyuswaqh
uikfmdjxyzlkgnmotcepvswadh
uikfmdjxyzlbgnuctcbpvswaqh
uikfqdjxyzlbogrotcepvswaqh
uikfmdjxyzlfynrotcepvswash
uikfmdjxizzmgnrotcepvswaqh
uhkfmdjxyzlbhnrotcenvswaqh
uipfmdjxyzlbgorotcepfswaqh
uikfmdjxyznbgnrotcepvswfah
uikfmujxyzlbgnrotnepvssaqh
uikfmdjxyzlxgnrotcepvsrwqh
uikfmdjxszlbgnrottvpvswaqh
umkfmdskyzlbgnrotcepvswaqh
uikfmdjxyzlbgorotcwpzswaqh
uikfmdhxyzzbgnzotcepvswaqh
jikfmdjxyzlbgnrotcepveyaqh
uirfmdlxyzlbgnjotcepvswaqh
uikfmdjxyzlbgnrorxepvswazh
uikfmdjxyzltgnrotcepvsxaqi
uikfmdjxyzlbxlrotcepkswaqh
uvkfmdjxyzlbgnrotcopvswxqh
uxkfmdjxkzlbgnrotcepvswqqh
uikfmdjxyzlbqnrotcepvhwrqh
uikfmdjxyzlvgnrolcepvswrqh
urkfmdixyzlbgnrotcepvsjaqh
uikfmdjxymlbsnrotcepvswcqh
uikfmdjxyilbgnrotcepvcwzqh
uikfadjxlzlbgnrotcepvswaql
uikfmdjxuzlbgdrotcgpvswaqh
yikfmdgxyzlbgnrotcepvswarh
uikfmdjxyzlbgorotcepcswaqv
uikkmdjxyzlbvnrotcepvvwaqh
uwzfmdjxyxlbgnrotcfpvswaqh
uikfmdjxyztbgnrotcrtvswaqh
uiufmdjxyzhbgnrotcupvswaqh
uikfzdjcyzlbgnrotcfpvswaqh
uipfmdjxyzlbgnrotavpvswaqh
uikfmajxyzlbgnrotcepbsxaqh
uikfmdjfyzlbgnrotbepvswmqh
gikkmdjxyzlbgnrptcepvswaqh
uikfmdjxqvlbgnrotsepvswaqh
fikfmdjxyzlbgnrotcegvswoqh
idkfmdjxyzlbgnrotcepwswaqh
uikfmdqxyzlbmnrobcepvswaqh
uikfmdjxyzpbgnroicepvsyaqh
uikfmkoxyzlbgnrotcgpvswaqh
unkfmdjxyzlbpnrolcepvswaqh
uikfmdjmyzlbgfrotcupvswaqh
ujkfmdjxynlbgnroteepvswaqh
uikfmljxyzlbgnaotcepvsiaqh
uikfmdjdyzlbgnrotcepvtwaqd
uikfmdjxyzlbgnyotcepimwaqh
uikfmdjxyzfbjnrotcepvxwaqh
eiwfmdjxyzlbgnrdtcepvswaqh
umkhmdjxyzlbgnrotceivswaqh
uikfmdjxyzlbgnrotcwpvswneh
uckfmdjxyzlbknrotcepvswauh
uiofmdjoyzlbgnrbtcepvswaqh
uikfmdbxyzlbgnrotcepaslaqh
uimfmdjxyalbgnrotcepvswaxh
uqkfmdjxyzlbwnrotcepmswaqh
uiyfmdjxyzlbgnrotcepvswxuh
uikfmdjxyzlbgmrotgepvswamh
uikfmdjxyqlbgarozcepvswaqh
uikfmdjxyzabanpotcepvswaqh
uikfmdjgyzlbsnrotcepvswaqj
uikfmdjxyzlbwnrottepvsvaqh
uikfmdjxyzlbbnrotcepvofaqh
uikfudjxyzlbgnustcepvswaqh
cskfmqjxyzlbgnrotcepvswaqh
uiwfmddxyzlbgnrotccpvswaqh
eikpmdjxyzlbgnrotcesvswaqh
uikfmdzxyzlngnrrtcepvswaqh
uikfmdjxyzlbgnrotcepaswtph
uirfmdjxyzlbgnrotcepvswsqe
uikjmdjxqzlbgirotcepvswaqh
uikfmdjxsllbknrotcepvswaqh
uikfmdjxyqlbgcrotcepvswaqu
uikfmdjsymlbgnrotcebvswaqh
uikfmdjxezlbgnroccepviwaqh
uikfmdjiyzjbgnrotcepvswarh
uqkfmdjxyzmbgnrotcepvslaqh
unkfmdjxyzlbinrotceplswaqh
uikfmdjxyzpbgnrjtcedvswaqh
uicfmdjxyzlbgrrotcepvswamh
ukknmdjxyzlbgnqotcepvswaqh
uikfudjxyzlbdnrztcepvswaqh
uikfmdjxyzlbgnrozcepvswawk
uikfmduxyzsbgnrotcepvswauh
uikfmdjxyzljbnrotcenvswaqh
uukfmdjxyzlbgnrotcckvswaqh
uilfldjxyzlbgnrotcdpvswaqh
uckfmdjxyvybgnrotcepvswaqh
qikfmdjxyglbgnrotcepvrwaqh
uikfmhjxyzltgnrotcepvswbqh
uikfmdjxipabgnrotcepvswaqh
uikfmdjxyzlbgnrotceovswanl
uikfmdjxyzlbgirotcapvswahh
uikfucjxyflbgnrotcepvswaqh
pikfmdjxyzpbgnrotcepvswaeh
uikfmdjiyylbgnrotcervswaqh
uikfmdjgyzlbgnrotcaevswaqh
uikvmdjxyzlbunrotcepvswarh
uikfadjuyzpbgnrotcepvswaqh
uikfmdjxyzlbgnrotcepsawaqj
eikfmdjxyflbgnrotcepvswaeh
uisfmdaxyzlbgnrotcepvswlqh
vikfmdjxyzlzgnrotcepvswanh
ukkfmdoxyzlbgnrotcewvswaqh
uikfmdhxyzlbgnrotcrpvbwaqh
uhkfmdjwezlbgnrotcepvswaqh
uikfddjxyzlbgnroteepvpwaqh
uikfmdjxczlbgncotiepvswaqh
uikfvdjxyzlbgnrotcnpvsaaqh
uikfmdjxyzlbgnritcepvlwmqh
uikfmdjxczlcgnrotcecvswaqh
mikfmdjxyzlbgnrotcepvswasi
uifvmdjxyzlbgnrotpepvswaqh
uikzmdjxyzlbgnrotrepvswaqs
uikfmqjqyzlbunrotcepvswaqh
uikfpdyxyzlbgnrotcepvswagh
uikfmdjxyzobgnrotrlpvswaqh
zisdmdjxyzlbgnrotcepvswaqh
uikfmdjxyzlbgnlotiesvswaqh
uikfddixyzlbgnroucepvswaqh
uijfmdrxyzlbgnrotoepvswaqh
uikfmdcxbzlbgnrotcepvpwaqh
uikfmdjxxzlbfnrotcecvswaqh
upkfmdjxyzmtgnrotcepvswaqh
uikfmdrxyzlbgnrjtcepvswaqp
uizfmdjxyzlbsnrotcepviwaqh
uidfmdjxyslbgnrotcxpvswaqh
uikfmdjxyzlbgnrovyepvsdaqh
uiafmdjxyzlbgnrxtcepvsdaqh
ugkfmdjxyzlbgnrodcepvswawh
pikfmtjxyzhbgnrotcepvswaqh
uikfmdjxyzlfgnvotcepvswtqh
uikbmdjxyzlbgerotcepvswaqm
uikfmdjxyhlbdnrotcepvswaqy
uikfgdjxyzlbgnhotcepvswdqh
uikfmdpxyzlbgnrotcepvscanh
uikfmdjxyzsbgnretcepvswaqn
uikfddjxyzlrgnrotcepvsbaqh
uikfmdjxyzlbgnrotcqnrswaqh
uhkfmejxyzlbgnrotvepvswaqh
uikimdjxyzlngnrotceprswaqh
uikfmdjxyzwbunrotiepvswaqh
rikfmdjxyzlbgnrotcypvssaqh
uikfmdjxyzlbdnrotcrpvswsqh
uekfmdjxkzlbznrotcepvswaqh
uikfmdjxyglbgvrotcepvswaqv
uikfmcjxyzlbgnrotmeovswaqh
uikfmdjxyznbgnrozcepvswaqm
uikfmdjxyzlbdnrotcepdsyaqh
umkfmdjxfzlbgnrotiepvswaqh
uitfmdjxyzvbcnrotcepvswaqh
uikfmdjqyzlbgnrvtcepvlwaqh
uikfmdjxyzkbworotcepvswaqh
uikfmdzxyzlbwnrotcypvswaqh
uikfmdjxyklbgnrftyepvswaqh
uinfmsjxyzlbgnrotcepsswaqh
xisfmdjxymlbgnrotcepvswaqh
uikfmdjxjzlbgnropcepvswaqv
uikfmdjxyalegyrotcepvswaqh
uikfydjxyzlbgnrotcekvswtqh
uikwmojxyzlbgnromcepvswaqh
uikdmdjayzlbgnrotcepvswzqh
uikfmdjxyzlmvnrotctpvswaqh
uikfmbjxyzlbgnrotceptsweqh
yvkfmdjxyzlbgqrotcepvswaqh
uikomdjxfxlbgnrotcepvswaqh
uikfmdjxczlbgnroocepgswaqh
uikemdjxizlbgnrotcegvswaqh
uikdmdjxyzlbgwrotceprswaqh
tiyfmdjfyzlbgnrotcepvswaqh
tmkfmdjxyzlbgirotcepvswaqh
uikfmdjxyzebgnzotcepqswaqh
uikfmljxyzlbgnrwtcepvswaeh
uikfmojxyzlbgnrotcepbswwqh
uikfmdjxyzlbgsrotcewvswwqh
uikfmdjhyzlbgdrogcepvswaqh
uikfmvjxyzlbrnrltcepvswaqh
jikfmdjxyzlbgnrotcepvgcaqh
uikhtdjxyzlbgnrotcepvswarh
uikfmdjxyezbgnritcepvswaqh
uikfwdjxyzlbgnrotzepvsnaqh
uikfmdjxqylbgnrobcepvswaqh
zikzmdjxyzlbgnrhtcepvswaqh
uiksmzjxyzlbgnrftcepvswaqh
uikfmdjxuzlbgnrotcepvsvaqc

12
2018/day03/day03.dyalog Normal file
View file

@ -0,0 +1,12 @@
⎕IO←0
x←⊃⎕nget'input.txt'1
x←⍎¨¨'\d+'⎕S'&'¨x
⍝ Part 1
claims←{id x y w h←⍵ ⋄ (x+w)∘.,y+h}¨x
fabric←1000 10000
{fabric[⍵]+←1}¨claims
+/,1<fabric
⍝ Part 2
⊃⊃x/⍨{∧/,1=fabric[⍵]}¨claims

30
2018/day03/day03.ss Normal file
View file

@ -0,0 +1,30 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/iter)
(export main)
(def (claim-area! fabric claim)
(match claim
([id x y w h] (for* ((i (in-range x w))
(j (in-range y h)))
(hash-update! fabric [i j] (lambda (l) (cons id l)) [])))))
(def (non-overlapping-claim fabric)
(def overlapping (make-hash-table))
(hash-for-each (lambda (k v) (for ((id v)) (hash-put! overlapping id #t))) fabric)
(hash-for-each (lambda (k v) (when (< 1 (length v))
(for ((id v)) (hash-put! overlapping id #f))))
fabric)
(hash->list overlapping)
(hash-find (lambda (k v) (if v k #f)) overlapping))
(def (main . args)
(def claims-str (call-with-input-file "input.txt" (lambda (p) (read-all p read-line))))
(def claims (map (lambda (x) (filter-map string->number (pregexp-split "[ #@,:x]" x))) claims-str))
(def fabric (make-hash-table))
(for ((claim claims))
(claim-area! fabric claim))
(displayln (hash-fold (lambda (k v acc) (if (> (length v) 1) (1+ acc) acc)) 0 fabric))
(displayln (non-overlapping-claim fabric)))

1307
2018/day03/input.txt Normal file

File diff suppressed because it is too large Load diff

77
2018/day04/day04.ss Normal file
View file

@ -0,0 +1,77 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/iter)
(import :std/sort)
(import :std/srfi/1)
(import :std/srfi/43)
(export main)
(defstruct date (year month day))
(defstruct shift (date id sleep))
(def (parse-date str)
(def d (apply make-date (filter-map string->number (pregexp-split "[-\\[ ]" str))))
(let ((parsed-hour (string->number (cadr (pregexp-match "\\s(\\d\\d):\\d\\d" str)))))
(if (< 12 parsed-hour) (set! (date-day d) (1+ (date-day d)))))
d)
(def (parse-minute str)
(string->number (cadr (pregexp-match "\\s\\d\\d:(\\d\\d)" str))))
(def (parse-id str)
(let ((parsed-id (pregexp-match "\\s\\#(\\d+)\\s" str)))
(if parsed-id (string->number (cadr parsed-id)) #f)))
(def (partition-shifts records-str)
(if (null? records-str)
'()
(let-values (((shift rest) (break (lambda (x) (pregexp-match "begins shift" x)) (cdr records-str))))
(cons (cons (car records-str) shift) (partition-shifts rest)))))
(def (parse-shift records-str)
(def id (parse-id (car records-str)))
(def date (parse-date (car records-str)))
(def sleep (make-vector 60 0))
(for ((record (cdr records-str)))
(let ((minute (parse-minute record)))
(if (pregexp-match "falls asleep" record)
(for ((i (in-range minute (- 60 minute)))) (vector-set! sleep i 1))
(for ((i (in-range minute (- 60 minute)))) (vector-set! sleep i 0)))))
(make-shift date id sleep))
(def (find-sleepiest-guard shifts)
(def guards (make-hash-table))
(for ((shift shifts))
(let ((time-asleep (apply + (vector->list (shift-sleep shift)))))
(hash-update! guards (shift-id shift) (lambda (x) (+ x time-asleep)) 0)))
(car (hash-fold (lambda (k v acc) (if (> v (cadr acc)) (list k v) acc)) '(0 0) guards)))
(def (find-sleepiest-minute id shifts)
(def total (make-list 60 0))
(for ((shift shifts))
(when (= id (shift-id shift))
(set! total (map + total (vector->list (shift-sleep shift))))))
(list-index (lambda (x) (= x (apply max total))) total))
(def (find-sleepy shifts)
(def hash (make-hash-table))
(for* ((shift shifts)
(i (in-range 60)))
(hash-update! hash
[(shift-id shift) i]
(lambda (x) (+ x (vector-ref (shift-sleep shift) i)))
0))
(car (hash-fold (lambda (k v acc) (if (> v (cadr acc)) (list k v) acc)) '(0 0) hash)))
(def (main . args)
(def records-str
(sort
(call-with-input-file "input.txt" (lambda (p) (read-all p read-line)))
string<?))
(def shifts (map parse-shift (partition-shifts records-str)))
(def sleepy-guard (find-sleepiest-guard shifts))
(def sleepy-minute (find-sleepiest-minute sleepy-guard shifts))
(displayln (* sleepy-guard sleepy-minute))
(displayln (apply * (find-sleepy shifts))))

1088
2018/day04/input.txt Normal file

File diff suppressed because it is too large Load diff

28
2018/day05/day05.ss Normal file
View file

@ -0,0 +1,28 @@
(import :gerbil/gambit/ports)
(import :std/iter)
(import :std/srfi/13)
(export main)
(def (opposite? x y)
(= (abs (- (char->integer x) (char->integer y))) 32))
(def (react-polymer polymer)
(foldl (lambda (x acc) (match acc
((cons y rest) (if (opposite? x y) rest (cons x acc)))
(else (cons x acc))))
'()
polymer))
(def (remove-unit unit polymer)
(string-delete (lambda (x) (char=? (char-downcase x) (char-downcase unit))) polymer))
(def (find-minimal polymer)
(apply min
(for/collect ((c (in-range (char->integer #\a) 26)))
(length (react-polymer (string->list (remove-unit (integer->char c) polymer)))))))
(def (main . args)
(def polymer (call-with-input-file "input.txt" read-line))
(displayln (length (react-polymer (string->list polymer))))
(displayln (find-minimal polymer)))

1
2018/day05/input.txt Normal file

File diff suppressed because one or more lines are too long

35
2018/day07/day07.ss Normal file
View file

@ -0,0 +1,35 @@
(import :gerbil/gambit/ports)
(import :std/iter)
(import :std/pregexp)
(import :std/srfi/1)
(import :std/sort)
(export main)
(def (no-incoming? edges m)
(not (member m (map cadr edges))))
(def (find-no-incoming edges)
(sort
(filter (lambda (m) (no-incoming? edges m)) (delete-duplicates (apply append edges)))
char<?))
(def (topological-sort edges)
(def (aux edges l s)
(match s
([] l)
([n . rest] (let* ((new-edges (filter (lambda (e) (not (eq? n (car e)))) edges))
(additional-s (map cadr (filter (lambda (e)
(and (eq? n (car e)) (no-incoming? new-edges (cadr e))))
edges)))
(new-s (sort (delete-duplicates (append rest additional-s)) char<?)))
(aux new-edges (cons n l) new-s)))))
(reverse (aux edges [] (find-no-incoming edges))))
(def (main . args)
(def edges
(map (lambda (e)
(map (lambda (s) (car (string->list s)))
(cdr (pregexp-match "Step (.) must be finished before step (.) can begin." e))))
(call-with-input-file "input.txt" (lambda (p) (read-all p read-line)))))
(displayln (list->string (topological-sort edges))))

101
2018/day07/input.txt Normal file
View file

@ -0,0 +1,101 @@
Step C must be finished before step P can begin.
Step V must be finished before step Q can begin.
Step T must be finished before step X can begin.
Step B must be finished before step U can begin.
Step Z must be finished before step O can begin.
Step P must be finished before step I can begin.
Step D must be finished before step G can begin.
Step A must be finished before step Y can begin.
Step R must be finished before step O can begin.
Step J must be finished before step E can begin.
Step N must be finished before step S can begin.
Step X must be finished before step H can begin.
Step F must be finished before step L can begin.
Step S must be finished before step I can begin.
Step W must be finished before step Q can begin.
Step H must be finished before step K can begin.
Step K must be finished before step Q can begin.
Step E must be finished before step L can begin.
Step Q must be finished before step O can begin.
Step U must be finished before step G can begin.
Step L must be finished before step O can begin.
Step Y must be finished before step G can begin.
Step G must be finished before step I can begin.
Step M must be finished before step I can begin.
Step I must be finished before step O can begin.
Step A must be finished before step N can begin.
Step H must be finished before step O can begin.
Step T must be finished before step O can begin.
Step H must be finished before step U can begin.
Step A must be finished before step I can begin.
Step B must be finished before step R can begin.
Step V must be finished before step T can begin.
Step H must be finished before step M can begin.
Step C must be finished before step A can begin.
Step B must be finished before step G can begin.
Step L must be finished before step Y can begin.
Step T must be finished before step J can begin.
Step A must be finished before step R can begin.
Step X must be finished before step L can begin.
Step B must be finished before step L can begin.
Step A must be finished before step F can begin.
Step K must be finished before step O can begin.
Step W must be finished before step M can begin.
Step Z must be finished before step N can begin.
Step Z must be finished before step S can begin.
Step R must be finished before step K can begin.
Step Q must be finished before step L can begin.
Step G must be finished before step O can begin.
Step F must be finished before step Y can begin.
Step V must be finished before step H can begin.
Step E must be finished before step I can begin.
Step W must be finished before step Y can begin.
Step U must be finished before step I can begin.
Step F must be finished before step K can begin.
Step M must be finished before step O can begin.
Step Z must be finished before step H can begin.
Step X must be finished before step S can begin.
Step J must be finished before step O can begin.
Step B must be finished before step I can begin.
Step F must be finished before step H can begin.
Step D must be finished before step U can begin.
Step E must be finished before step M can begin.
Step Z must be finished before step X can begin.
Step P must be finished before step L can begin.
Step W must be finished before step H can begin.
Step C must be finished before step D can begin.
Step A must be finished before step X can begin.
Step Q must be finished before step I can begin.
Step R must be finished before step Y can begin.
Step B must be finished before step A can begin.
Step N must be finished before step L can begin.
Step H must be finished before step G can begin.
Step Y must be finished before step M can begin.
Step L must be finished before step G can begin.
Step G must be finished before step M can begin.
Step Z must be finished before step R can begin.
Step S must be finished before step Q can begin.
Step P must be finished before step J can begin.
Step V must be finished before step J can begin.
Step J must be finished before step I can begin.
Step J must be finished before step X can begin.
Step W must be finished before step O can begin.
Step B must be finished before step F can begin.
Step R must be finished before step M can begin.
Step V must be finished before step S can begin.
Step H must be finished before step E can begin.
Step E must be finished before step U can begin.
Step R must be finished before step W can begin.
Step X must be finished before step Q can begin.
Step N must be finished before step G can begin.
Step T must be finished before step I can begin.
Step L must be finished before step M can begin.
Step H must be finished before step I can begin.
Step U must be finished before step M can begin.
Step C must be finished before step H can begin.
Step P must be finished before step H can begin.
Step J must be finished before step F can begin.
Step A must be finished before step O can begin.
Step X must be finished before step M can begin.
Step H must be finished before step L can begin.
Step W must be finished before step K can begin.

46
2018/day08/day08.ss Normal file
View file

@ -0,0 +1,46 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/srfi/1)
(import :std/iter)
(export main)
(defstruct node (children metadata))
(def (parse-node lst)
(match lst
([m n . rest]
(let*-values (((children rest) (parse-children rest m))
((metadata rest) (split-at rest n)))
(values (make-node children metadata) rest)))))
(def (parse-children lst m)
(if (= m 0)
(values [] lst)
(let*-values (((children rest) (parse-children lst (1- m)))
((child rest) (parse-node rest)))
(values (cons child children) rest))))
(def (dfs node f g)
(if (null? (node-children node))
(f (node-metadata node))
(g (cons (f (node-metadata node))
(map (lambda (n) (dfs n f g)) (node-children node))))))
(def (node-value node)
(if (null? (node-children node))
(apply + (node-metadata node))
(apply + (for/collect ((m (node-metadata node)))
(if (and (> m 0) (< (1- m) (length (node-children node))))
(node-value (list-ref (reverse (node-children node)) (1- m)))
0)))))
(def (main . args)
(def example-lst [2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2])
(define-values (example _) (parse-node example-lst))
(displayln (dfs example (lambda (m) (apply + m)) (lambda (m) (apply + m))))
(displayln (node-value example))
(def lst (map string->number (pregexp-split " " (call-with-input-file "input.txt" read-line))))
(define-values (tree _) (parse-node lst))
(displayln (dfs tree (lambda (m) (apply + m)) (lambda (m) (apply + m))))
(displayln (node-value tree)))

1
2018/day08/input.txt Normal file

File diff suppressed because one or more lines are too long

61
2018/day09/day09.ss Normal file
View file

@ -0,0 +1,61 @@
(import :std/srfi/1)
(export main)
(def (rotate-right deque k)
(def left (car deque))
(def right (cadr deque))
(if (= k 0)
deque
(if (null? right)
(rotate-right [[] (reverse left)] k)
(rotate-right [(cons (car right) left) (cdr right)] (1- k)))))
(def (rotate-left deque k)
(def left (car deque))
(def right (cadr deque))
(if (= k 0)
deque
(if (null? left)
(rotate-left [(reverse right) '()] k)
(rotate-left [(cdr left) (cons (car left) right)] (1- k)))))
(def (deque-car deque)
(match deque
([left right] (car right))))
(def (deque-cdr deque)
(match deque
([left right] [left (cdr right)])))
(def (deque-cons x deque)
(match deque
([left right] [left (cons x right)])))
(def (display-deque deque)
(displayln (reverse (car deque)) " " (cadr deque)))
(def (play circle marbles scores player)
;; (display-deque circle)
(match marbles
([] scores)
([marble . rest]
(def new-player (remainder (1+ player) (vector-length scores)))
(if (= 0 (remainder marble 23))
(let ((new-circle (rotate-left circle 7)))
(vector-set! scores player (+ marble (deque-car new-circle) (vector-ref scores player)))
(play (deque-cdr new-circle) (cdr marbles) scores new-player))
(let ((new-circle (deque-cons marble (rotate-right circle 2))))
(play new-circle (cdr marbles) scores new-player))))))
(def (marbles-game n-players last-marble)
(apply max (vector->list (play [[] [0]] (iota last-marble 1) (make-vector n-players 0) 0))))
(def (main (n-players "9") (last-marble "25") . args)
(displayln (marbles-game 9 25))
(displayln (marbles-game 10 1618))
(displayln (marbles-game 13 7999))
(displayln (marbles-game 17 1104))
(displayln (marbles-game 21 6111))
(displayln (marbles-game 30 5807))
(displayln (marbles-game (string->number n-players) (string->number last-marble))))

1
2018/day09/input.txt Normal file
View file

@ -0,0 +1 @@
463 players; last marble is worth 71787 points

51
2018/day10/day10.ss Normal file
View file

@ -0,0 +1,51 @@
(import :gerbil/gambit/ports)
(import :std/pregexp)
(import :std/srfi/1)
(import :std/srfi/13)
(import :std/iter)
(export main)
(defstruct point (x y vx vy))
(def (step-point p n)
(make-point (+ (* n (point-vx p)) (point-x p))
(+ (* n (point-vy p)) (point-y p))
(point-vx p)
(point-vy p)))
(def (step-points points n)
(map (lambda (p) (step-point p n)) points))
(def (bounding-box points)
(let ((xs (map point-x points))
(ys (map point-y points)))
[(- (apply max xs) (apply min xs) -1)
(- (apply max ys) (apply min ys) -1)]))
(def (plot-points points)
(def box (bounding-box points))
(def min-x (apply min (map point-x points)))
(def min-y (apply min (map point-y points)))
(def new-points
(delete-duplicates
(map (lambda (p) [(- (point-x p) min-x) (- (point-y p) min-y)]) points)))
(def plot (make-vector (cadr box) #f))
(for ((i (in-range (cadr box)))) (vector-set! plot i (make-string (car box) #\.)))
(for ((p new-points))
(string-set! (vector-ref plot (cadr p)) (car p) #\#))
(for ((i (in-range (cadr box))))
(displayln (vector-ref plot i))))
(def (main (n "10240") . args)
(def params
(map (lambda (s) (filter-map string->number (pregexp-split "[<>, ]" s)))
(call-with-input-file "input.txt" (lambda (p) (read-all p read-line)))))
(def points (map (lambda (p) (apply make-point p)) params))
;; (def sizes (for/collect ((i (in-range 20000)))
;; [i (apply * (bounding-box (step-points points i)))]))
;; (displayln
;; (foldl (lambda (s acc) (if (< (cadr s) (cadr acc)) s acc))
;; (car sizes)
;; (cdr sizes)))
(plot-points (step-points points (string->number n))))

379
2018/day10/input.txt Normal file
View file

@ -0,0 +1,379 @@
position=<-50948, 20587> velocity=< 5, -2>
position=< 20732, -51094> velocity=<-2, 5>
position=<-30471, -10131> velocity=< 3, 1>
position=<-40758, 10355> velocity=< 4, -1>
position=< 30912, 20590> velocity=<-3, -2>
position=< 30927, 30827> velocity=<-3, -3>
position=<-20237, 20591> velocity=< 2, -2>
position=< 51423, -51086> velocity=<-5, 5>
position=<-40732, -20370> velocity=< 4, 2>
position=<-20271, -51094> velocity=< 2, 5>
position=<-40764, -30605> velocity=< 4, 3>
position=<-30492, 41073> velocity=< 3, -4>
position=<-30473, -30614> velocity=< 3, 3>
position=<-51004, 41074> velocity=< 5, -4>
position=<-30481, 30829> velocity=< 3, -3>
position=<-50961, -10130> velocity=< 5, 1>
position=<-50968, 51306> velocity=< 5, -5>
position=<-30489, -40850> velocity=< 3, 4>
position=<-20273, 51307> velocity=< 2, -5>
position=<-40732, 10351> velocity=< 4, -1>
position=<-40764, -30612> velocity=< 4, 3>
position=< 10483, 51307> velocity=<-1, -5>
position=<-30511, -30614> velocity=< 3, 3>
position=< 20719, -40848> velocity=<-2, 4>
position=<-20249, -10129> velocity=< 2, 1>
position=< 30919, -51090> velocity=<-3, 5>
position=<-20281, 20595> velocity=< 2, -2>
position=< 30931, 30830> velocity=<-3, -3>
position=<-20240, 41071> velocity=< 2, -4>
position=< -9988, -51087> velocity=< 1, 5>
position=<-50991, 51315> velocity=< 5, -5>
position=<-10022, 30826> velocity=< 1, -3>
position=<-20241, 30830> velocity=< 2, -3>
position=< 10492, -30610> velocity=<-1, 3>
position=<-30525, 51313> velocity=< 3, -5>
position=< 10492, -20370> velocity=<-1, 2>
position=< 10492, -20371> velocity=<-1, 2>
position=<-20255, 30830> velocity=< 2, -3>
position=< 51403, 20595> velocity=<-5, -2>
position=< 10489, -20370> velocity=<-1, 2>
position=< 10492, 41067> velocity=<-1, -4>
position=< 20722, -40854> velocity=<-2, 4>
position=< 10451, 30830> velocity=<-1, -3>
position=< 10452, -10125> velocity=<-1, 1>
position=< 41151, 51306> velocity=<-4, -5>
position=< 51410, -30610> velocity=<-5, 3>
position=<-20289, -51093> velocity=< 2, 5>
position=<-40708, 30828> velocity=< 4, -3>
position=<-20244, -40851> velocity=< 2, 4>
position=<-20257, -30612> velocity=< 2, 3>
position=< 30943, -10131> velocity=<-3, 1>
position=<-40718, 10346> velocity=< 4, -1>
position=<-40769, -51091> velocity=< 4, 5>
position=< 30967, 41074> velocity=<-3, -4>
position=<-40728, -30614> velocity=< 4, 3>
position=<-40711, 20589> velocity=< 4, -2>
position=< 10487, -51090> velocity=<-1, 5>
position=< 10459, 20595> velocity=<-1, -2>
position=<-10025, 10346> velocity=< 1, -1>
position=< 30913, 41066> velocity=<-3, -4>
position=< 51407, 20595> velocity=<-5, -2>
position=< 30911, -10134> velocity=<-3, 1>
position=<-40769, 51310> velocity=< 4, -5>
position=<-10001, -51085> velocity=< 1, 5>
position=<-10004, 20594> velocity=< 1, -2>
position=<-20281, -40849> velocity=< 2, 4>
position=< 51444, -20365> velocity=<-5, 2>
position=<-40769, 20593> velocity=< 4, -2>
position=< 41183, 20588> velocity=<-4, -2>
position=< 30932, 51315> velocity=<-3, -5>
position=< 41192, -10130> velocity=<-4, 1>
position=< 10488, 41068> velocity=<-1, -4>
position=<-10022, -20370> velocity=< 1, 2>
position=< 30954, 30831> velocity=<-3, -3>
position=<-30513, -40845> velocity=< 3, 4>
position=< 51428, 30831> velocity=<-5, -3>
position=<-40760, -40845> velocity=< 4, 4>
position=<-10009, 30832> velocity=< 1, -3>
position=< 41151, 20586> velocity=<-4, -2>
position=< 30972, 30831> velocity=<-3, -3>
position=< 20675, -30607> velocity=<-2, 3>
position=< 51436, -30606> velocity=<-5, 3>
position=<-50977, -40852> velocity=< 5, 4>
position=< 10471, -40847> velocity=<-1, 4>
position=< 10463, 20590> velocity=<-1, -2>
position=< 30911, -10129> velocity=<-3, 1>
position=< 10476, 41074> velocity=<-1, -4>
position=< 51423, 51309> velocity=<-5, -5>
position=<-50948, -20367> velocity=< 5, 2>
position=< 51419, 30835> velocity=<-5, -3>
position=< 10474, 51306> velocity=<-1, -5>
position=< 10431, 51308> velocity=<-1, -5>
position=<-40733, 41074> velocity=< 4, -4>
position=< 30915, 51306> velocity=<-3, -5>
position=< 30959, 10354> velocity=<-3, -1>
position=< 30972, 20595> velocity=<-3, -2>
position=< 30913, -30610> velocity=<-3, 3>
position=<-10041, -20368> velocity=< 1, 2>
position=<-50972, -20368> velocity=< 5, 2>
position=<-30476, 30828> velocity=< 3, -3>
position=< 41196, -40851> velocity=<-4, 4>
position=<-40753, -20374> velocity=< 4, 2>
position=< 30927, -20368> velocity=<-3, 2>
position=<-20271, -10130> velocity=< 2, 1>
position=<-10046, -40850> velocity=< 1, 4>
position=<-20244, 51308> velocity=< 2, -5>
position=<-20280, 30835> velocity=< 2, -3>
position=< 51391, -30610> velocity=<-5, 3>
position=< 20724, -20365> velocity=<-2, 2>
position=<-40710, 10352> velocity=< 4, -1>
position=<-20289, 41075> velocity=< 2, -4>
position=< 20690, -51085> velocity=<-2, 5>
position=<-40729, 30834> velocity=< 4, -3>
position=< 51428, 51313> velocity=<-5, -5>
position=< -9998, -10129> velocity=< 1, 1>
position=< 20728, -30612> velocity=<-2, 3>
position=< 51447, 41075> velocity=<-5, -4>
position=< 41159, -30605> velocity=<-4, 3>
position=<-40744, -20374> velocity=< 4, 2>
position=< 41188, -51085> velocity=<-4, 5>
position=< 30943, -10126> velocity=<-3, 1>
position=<-10006, -10134> velocity=< 1, 1>
position=< 51399, -10131> velocity=<-5, 1>
position=< 41204, 30834> velocity=<-4, -3>
position=< 30914, -40850> velocity=<-3, 4>
position=< 30948, -51093> velocity=<-3, 5>
position=<-30486, 30826> velocity=< 3, -3>
position=< 10439, 10346> velocity=<-1, -1>
position=< 20721, -51089> velocity=<-2, 5>
position=<-50956, -10130> velocity=< 5, 1>
position=< 10464, -30613> velocity=<-1, 3>
position=< 10492, -30608> velocity=<-1, 3>
position=<-10031, 10346> velocity=< 1, -1>
position=< 30953, 30830> velocity=<-3, -3>
position=< 41204, 51308> velocity=<-4, -5>
position=< 20695, -10127> velocity=<-2, 1>
position=<-40709, -10126> velocity=< 4, 1>
position=<-30497, -20369> velocity=< 3, 2>
position=< 51396, 10354> velocity=<-5, -1>
position=< 20698, 41075> velocity=<-2, -4>
position=<-20286, -51089> velocity=< 2, 5>
position=< 30955, 41066> velocity=<-3, -4>
position=<-10046, 30830> velocity=< 1, -3>
position=<-20254, 10352> velocity=< 2, -1>
position=< -9993, -51094> velocity=< 1, 5>
position=<-50967, -40850> velocity=< 5, 4>
position=<-40753, -51088> velocity=< 4, 5>
position=<-20272, 30835> velocity=< 2, -3>
position=< 10475, 51313> velocity=<-1, -5>
position=<-40769, -40851> velocity=< 4, 4>
position=< 51447, -10128> velocity=<-5, 1>
position=<-10036, 20595> velocity=< 1, -2>
position=< 51431, -40852> velocity=<-5, 4>
position=< 41204, -10127> velocity=<-4, 1>
position=< 51448, 51307> velocity=<-5, -5>
position=<-51008, -51094> velocity=< 5, 5>
position=< 20727, -20369> velocity=<-2, 2>
position=<-50969, 41075> velocity=< 5, -4>
position=< 20707, 10353> velocity=<-2, -1>
position=< 51447, -10130> velocity=<-5, 1>
position=< 10439, 20588> velocity=<-1, -2>
position=< 10490, -20368> velocity=<-1, 2>
position=<-40769, 41068> velocity=< 4, -4>
position=< 30915, -51090> velocity=<-3, 5>
position=<-20273, 30831> velocity=< 2, -3>
position=< 10463, 20586> velocity=<-1, -2>
position=< 51417, 41075> velocity=<-5, -4>
position=<-50948, 41071> velocity=< 5, -4>
position=<-30470, 20591> velocity=< 3, -2>
position=< 51439, 51311> velocity=<-5, -5>
position=< 51399, 51307> velocity=<-5, -5>
position=<-30478, -40849> velocity=< 3, 4>
position=<-40727, 41070> velocity=< 4, -4>
position=<-30503, 51306> velocity=< 3, -5>
position=<-20252, 51308> velocity=< 2, -5>
position=<-20265, -51094> velocity=< 2, 5>
position=<-30468, -40846> velocity=< 3, 4>
position=<-30501, -30611> velocity=< 3, 3>
position=<-20263, -40849> velocity=< 2, 4>
position=< 20714, 20591> velocity=<-2, -2>
position=<-20252, -40854> velocity=< 2, 4>
position=<-40729, -40853> velocity=< 4, 4>
position=<-10047, 30826> velocity=< 1, -3>
position=< 10479, -51088> velocity=<-1, 5>
position=< 51443, 41071> velocity=<-5, -4>
position=<-50969, -40854> velocity=< 5, 4>
position=<-40742, -20374> velocity=< 4, 2>
position=< 41155, 20592> velocity=<-4, -2>
position=< 41175, 41074> velocity=<-4, -4>
position=< 20679, 20594> velocity=<-2, -2>
position=<-20265, 10355> velocity=< 2, -1>
position=< 41184, 20587> velocity=<-4, -2>
position=< 41177, -40845> velocity=<-4, 4>
position=< 30938, -20374> velocity=<-3, 2>
position=< 20732, 30830> velocity=<-2, -3>
position=<-20273, 51314> velocity=< 2, -5>
position=< 41178, 51310> velocity=<-4, -5>
position=< 20687, 30829> velocity=<-2, -3>
position=<-50964, 10347> velocity=< 5, -1>
position=< 51391, -51093> velocity=<-5, 5>
position=<-30485, -51088> velocity=< 3, 5>
position=<-30468, -30611> velocity=< 3, 3>
position=<-30513, 51312> velocity=< 3, -5>
position=<-40753, 10347> velocity=< 4, -1>
position=<-50993, 51308> velocity=< 5, -5>
position=< 41194, -51089> velocity=<-4, 5>
position=< 30919, -30607> velocity=<-3, 3>
position=< 51436, 51315> velocity=<-5, -5>
position=<-20245, -20368> velocity=< 2, 2>
position=< 30913, -20370> velocity=<-3, 2>
position=< 30943, -51086> velocity=<-3, 5>
position=<-20273, 41073> velocity=< 2, -4>
position=< 10484, -40850> velocity=<-1, 4>
position=< 10492, -40848> velocity=<-1, 4>
position=<-10009, 30826> velocity=< 1, -3>
position=< 20679, -20368> velocity=<-2, 2>
position=<-50959, -30614> velocity=< 5, 3>
position=<-40741, -10134> velocity=< 4, 1>
position=< 30919, 30834> velocity=<-3, -3>
position=<-10033, 30833> velocity=< 1, -3>
position=<-30489, 30831> velocity=< 3, -3>
position=<-30492, -40848> velocity=< 3, 4>
position=<-40720, -30613> velocity=< 4, 3>
position=<-50993, -30612> velocity=< 5, 3>
position=<-40745, -20365> velocity=< 4, 2>
position=<-10024, -20368> velocity=< 1, 2>
position=< 41152, 30830> velocity=<-4, -3>
position=< 30951, 20586> velocity=<-3, -2>
position=<-40740, 41075> velocity=< 4, -4>
position=< 51416, -40845> velocity=<-5, 4>
position=<-30527, -51090> velocity=< 3, 5>
position=< 41161, 51315> velocity=<-4, -5>
position=<-40764, -20365> velocity=< 4, 2>
position=< 51448, -51093> velocity=<-5, 5>
position=<-30521, 10349> velocity=< 3, -1>
position=<-20249, 51312> velocity=< 2, -5>
position=<-30513, 10354> velocity=< 3, -1>
position=< 30945, 30830> velocity=<-3, -3>
position=< -9996, 30832> velocity=< 1, -3>
position=< 10468, -51085> velocity=<-1, 5>
position=<-30487, 10346> velocity=< 3, -1>
position=< 51391, 10352> velocity=<-5, -1>
position=< 10471, 41067> velocity=<-1, -4>
position=<-10033, 30830> velocity=< 1, -3>
position=< 30932, 30835> velocity=<-3, -3>
position=< 41199, 51308> velocity=<-4, -5>
position=<-40713, -10134> velocity=< 4, 1>
position=<-10005, -51088> velocity=< 1, 5>
position=< 10475, -20370> velocity=<-1, 2>
position=< 10484, 41071> velocity=<-1, -4>
position=< 10439, -20367> velocity=<-1, 2>
position=< 10448, -51090> velocity=<-1, 5>
position=<-30529, 20591> velocity=< 3, -2>
position=< 41188, 41068> velocity=<-4, -4>
position=< 51419, 10349> velocity=<-5, -1>
position=<-20289, 10348> velocity=< 2, -1>
position=<-20265, -10134> velocity=< 2, 1>
position=< 30927, 30831> velocity=<-3, -3>
position=< 41183, -40849> velocity=<-4, 4>
position=< 10487, -10128> velocity=<-1, 1>
position=<-20236, -51086> velocity=< 2, 5>
position=< 30943, -20371> velocity=<-3, 2>
position=< 30948, 20589> velocity=<-3, -2>
position=<-10015, -30611> velocity=< 1, 3>
position=< 41208, -20372> velocity=<-4, 2>
position=< 51431, -20372> velocity=<-5, 2>
position=<-10016, 41068> velocity=< 1, -4>
position=< 41180, -40852> velocity=<-4, 4>
position=<-20230, -10129> velocity=< 2, 1>
position=<-40713, -51092> velocity=< 4, 5>
position=<-40712, 41067> velocity=< 4, -4>
position=< 51418, 20595> velocity=<-5, -2>
position=< 41191, 30827> velocity=<-4, -3>
position=<-20233, -10133> velocity=< 2, 1>
position=< 51395, 10352> velocity=<-5, -1>
position=< 41212, -30609> velocity=<-4, 3>
position=< 41167, -51086> velocity=<-4, 5>
position=<-40737, 20595> velocity=< 4, -2>
position=<-10040, -10125> velocity=< 1, 1>
position=<-40753, -10129> velocity=< 4, 1>
position=< 30927, 51310> velocity=<-3, -5>
position=< 20676, 20589> velocity=<-2, -2>
position=<-30529, -10128> velocity=< 3, 1>
position=< 20729, 51310> velocity=<-2, -5>
position=<-20229, -40847> velocity=< 2, 4>
position=< 41196, 20587> velocity=<-4, -2>
position=< 10468, 51314> velocity=<-1, -5>
position=< 20676, 41067> velocity=<-2, -4>
position=< -9993, 30834> velocity=< 1, -3>
position=<-51004, 51307> velocity=< 5, -5>
position=< 51410, 30835> velocity=<-5, -3>
position=<-30476, 41075> velocity=< 3, -4>
position=<-20281, 20590> velocity=< 2, -2>
position=< 20727, -40852> velocity=<-2, 4>
position=<-10001, -30609> velocity=< 1, 3>
position=< 20691, 30826> velocity=<-2, -3>
position=<-51001, 20587> velocity=< 5, -2>
position=<-51001, 51310> velocity=< 5, -5>
position=<-20252, -30612> velocity=< 2, 3>
position=<-20257, -30610> velocity=< 2, 3>
position=< 20703, -20370> velocity=<-2, 2>
position=< 41207, -51093> velocity=<-4, 5>
position=<-30481, 20593> velocity=< 3, -2>
position=<-30510, -30614> velocity=< 3, 3>
position=<-10001, -20366> velocity=< 1, 2>
position=<-30485, 20590> velocity=< 3, -2>
position=< 10460, -51094> velocity=<-1, 5>
position=< 51410, -40850> velocity=<-5, 4>
position=<-20236, 20589> velocity=< 2, -2>
position=<-20241, 20594> velocity=< 2, -2>
position=<-10044, -10132> velocity=< 1, 1>
position=<-10048, 20586> velocity=< 1, -2>
position=<-20245, -10130> velocity=< 2, 1>
position=< 30951, -10131> velocity=<-3, 1>
position=< -9993, -40852> velocity=< 1, 4>
position=< 30971, -30607> velocity=<-3, 3>
position=< 51391, 20594> velocity=<-5, -2>
position=<-50951, -51091> velocity=< 5, 5>
position=< -9993, 20593> velocity=< 1, -2>
position=< 10480, -10129> velocity=<-1, 1>
position=< 41211, 51314> velocity=<-4, -5>
position=< 30912, -10134> velocity=<-3, 1>
position=< 30944, 30828> velocity=<-3, -3>
position=< 30915, -51090> velocity=<-3, 5>
position=< -9999, 10351> velocity=< 1, -1>
position=< 51395, 51312> velocity=<-5, -5>
position=<-40729, 20588> velocity=< 4, -2>
position=<-20229, -40846> velocity=< 2, 4>
position=<-10033, 30833> velocity=< 1, -3>
position=< 41168, 41066> velocity=<-4, -4>
position=< 20703, 51306> velocity=<-2, -5>
position=< 20732, 51309> velocity=<-2, -5>
position=<-10020, 10347> velocity=< 1, -1>
position=<-40716, -10130> velocity=< 4, 1>
position=< 41212, 10352> velocity=<-4, -1>
position=< 10464, -51093> velocity=<-1, 5>
position=< 10434, 41066> velocity=<-1, -4>
position=< 10451, 51315> velocity=<-1, -5>
position=<-40764, -10133> velocity=< 4, 1>
position=<-10049, -10126> velocity=< 1, 1>
position=< 10463, 10347> velocity=<-1, -1>
position=< 20732, 41075> velocity=<-2, -4>
position=<-30488, -10130> velocity=< 3, 1>
position=<-10009, 10349> velocity=< 1, -1>
position=<-40744, -10134> velocity=< 4, 1>
position=< 20703, 41073> velocity=<-2, -4>
position=< 41204, -51092> velocity=<-4, 5>
position=<-50960, -51093> velocity=< 5, 5>
position=<-40748, 20586> velocity=< 4, -2>
position=< 51428, 20591> velocity=<-5, -2>
position=<-30489, 30831> velocity=< 3, -3>
position=< 10472, 10350> velocity=<-1, -1>
position=< 41199, -20371> velocity=<-4, 2>
position=<-30489, 41069> velocity=< 3, -4>
position=<-20241, 10352> velocity=< 2, -1>
position=< 41188, -51090> velocity=<-4, 5>
position=<-10004, -40845> velocity=< 1, 4>
position=<-30497, -51088> velocity=< 3, 5>
position=< 30943, 20591> velocity=<-3, -2>
position=<-50956, -10129> velocity=< 5, 1>
position=< 41186, -20369> velocity=<-4, 2>
position=< 30948, 41066> velocity=<-3, -4>
position=< 30919, 51309> velocity=<-3, -5>
position=< 10451, 10355> velocity=<-1, -1>
position=< 10475, 20586> velocity=<-1, -2>
position=< 30959, 20588> velocity=<-3, -2>
position=< 41187, -51087> velocity=<-4, 5>
position=< 30956, 51309> velocity=<-3, -5>
position=<-30497, 41075> velocity=< 3, -4>
position=< 51396, 51309> velocity=<-5, -5>
position=< 30962, -51094> velocity=<-3, 5>
position=< 30967, -51091> velocity=<-3, 5>
position=< 10487, 20587> velocity=<-1, -2>
position=<-40732, 30826> velocity=< 4, -3>
position=<-50985, 10354> velocity=< 5, -1>
position=<-40753, -40850> velocity=< 4, 4>
position=< 10476, -51092> velocity=<-1, 5>
position=<-50966, 10350> velocity=< 5, -1>
position=< 30930, 51306> velocity=<-3, -5>

75
2018/day11/day11.ss Normal file
View file

@ -0,0 +1,75 @@
(import :std/srfi/1)
(import :std/srfi/43)
(import :std/iter)
(import :gerbil/gambit/hvectors)
(export main)
(def (hundreds-digit n)
(remainder (quotient n 100) 10))
(def (power serial-number x y)
(def rack-id (+ x 10))
(- (hundreds-digit (* rack-id (+ (* rack-id y) serial-number))) 5))
(def (build-grid serial-number grid-size)
(def grid (make-vector grid-size #f))
(for ((i (in-range grid-size)))
(vector-set! grid i (make-s32vector grid-size 0)))
(for* ((i (in-range grid-size))
(j (in-range grid-size)))
(s32vector-set! (vector-ref grid i)
j
(power serial-number i j)))
(for* ((i (in-range grid-size))
(j (in-range grid-size)))
(if (> i 0)
(let ((cur-row (vector-ref grid i))
(prev-row (vector-ref grid (- i 1))))
(s32vector-set! cur-row j
(+ (s32vector-ref cur-row j)
(if (> j 0) (s32vector-ref cur-row (- j 1)) 0)
(s32vector-ref prev-row j)
(if (> j 0) (- (s32vector-ref prev-row (- j 1))) 0))))
(let ((cur-row (vector-ref grid i)))
(s32vector-set! cur-row j
(+ (s32vector-ref cur-row j)
(if (> j 0) (s32vector-ref cur-row (- j 1)) 0))))))
grid)
(def (largest-power-cell grid cell-size)
(def grid-size (vector-length grid))
(def max -10000)
(def max-idx [0 0])
(for* ((i (in-range (- grid-size cell-size)))
(j (in-range (- grid-size cell-size))))
(let* ((top-row (vector-ref grid i))
(bottom-row (vector-ref grid (+ i cell-size)))
(sum (- (+ (s32vector-ref bottom-row (+ j cell-size))
(s32vector-ref top-row j))
(+ (s32vector-ref bottom-row j)
(s32vector-ref top-row (+ j cell-size))))))
(when (> sum max)
(begin
(set! max sum)
(set! max-idx [(1+ i) (1+ j)])))))
(cons max max-idx))
(def (largest-power-cell-size grid)
(def grid-size (vector-length grid))
(def max-size 0)
(def max -1000)
(def res #f)
(for ((size (in-range grid-size)))
(let ((x (largest-power-cell grid size)))
(when (> (car x) max)
(begin
(set! max (car x))
(set! max-size size)
(set! res (cons size x))))))
res)
(def (main (serial-number "9005") . args)
(def grid (build-grid (string->number serial-number) 300))
(displayln (largest-power-cell grid 3))
(displayln (largest-power-cell-size grid)))

3
2019/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*\~
*.fasl
compiled/

10
2019/README.org Normal file
View file

@ -0,0 +1,10 @@
* Advent of Code 2019
My answers for [[https://adventofcode.com/2019][Advent of Code 2019]].
Racket tests:
#+begin_src sh
raco make **/*.rkt
raco test **/*.rkt
#+end_src

4
2019/day01/day01.dyalog Normal file
View file

@ -0,0 +1,4 @@
p←⍎¨⊃⎕nget'input.txt'1
f←{0⌈⌊2-⍨⍵÷3}
+/f p ⍝ part 1
+/,↑{(f⍣⍵)p}¨10 ⍝ part 2

16
2019/day01/day01.lisp Normal file
View file

@ -0,0 +1,16 @@
(defparameter *input-file* #P"input.txt")
(defparameter *input* (uiop:read-file-lines *input-file*))
(defun part1 ()
(loop for line in *input*
sum (- (floor (/ (parse-integer line) 3)) 2)))
(defun total-fuel-requirements (total-fuel fuel)
(let ((new-fuel (max 0 (- (floor (/ fuel 3)) 2))))
(if (= new-fuel 0)
(+ total-fuel fuel)
(total-fuel-requirements (+ total-fuel fuel) new-fuel))))
(defun part2 ()
(loop for line in *input*
sum (total-fuel-requirements 0 (- (floor (/ (parse-integer line) 3)) 2))))

100
2019/day01/input.txt Normal file
View file

@ -0,0 +1,100 @@
120847
60347
63340
72773
57020
133960
98163
121548
87233
59150
59712
146816
93205
61936
75319
141998
97125
73450
106250
129939
94854
113782
112044
127923
67114
119770
130034
70876
82204
101939
132604
142836
117066
95861
75597
94630
50085
101107
77937
94286
74091
140875
118543
52767
54544
93062
115681
142065
111737
131214
75160
115432
140504
115376
86589
104631
133012
108690
85993
99533
133725
87698
133480
68379
107852
111209
116623
98717
130227
114356
144516
89663
118355
77816
149914
105080
116793
65259
143900
67838
148389
129753
140524
90005
147305
118428
79940
59110
78120
87066
64722
142066
81410
106958
92984
95584
108534
66362
126340
143660

5
2019/day02/day02.dyalog Normal file
View file

@ -0,0 +1,5 @@
⎕io←0
p←⍎¨','(≠⊆⊢)⊃⊃⎕nget'input.txt'1
f←{i←0 ⊣ s[1 2]←⍵ ⊣ s←p ⋄ ⊃s⊣{i+←4 ⊣ a x y z←s[i+4] ⋄ s[z]←(a-1)⌷s[x](+,×)s[y]}⍣{99=i⌷s}0}
f 12 2 ⍝ part 1
nv←1+,99 99 ⋄ +/100 1×⊃nv[(19690720=f¨nv)1] ⍝ part 2

32
2019/day02/day02.lisp Normal file
View file

@ -0,0 +1,32 @@
(ql:quickload "str")
(defparameter *input-file* #P"input.txt")
(defparameter *input* (uiop:read-file-string *input-file*))
(defun execute (program)
(loop for i from 0 by 4 until (= 99 (aref program i))
do (let ((opcode (aref program i)))
(cond ((= opcode 1)
(setf (aref program (aref program (+ 3 i)))
(+ (aref program (aref program (+ 1 i)))
(aref program (aref program (+ 2 i))))))
((= opcode 2)
(setf (aref program (aref program (+ 3 i)))
(* (aref program (aref program (+ 1 i)))
(aref program (aref program (+ 2 i)))))))))
program)
(defun execute-with-inputs (program-string noun verb)
(let ((program (map 'vector #'parse-integer (str:split "," program-string))))
(setf (aref program 1) noun)
(setf (aref program 2) verb)
(aref (execute program) 0)))
(defun part1 (program-string)
(execute-with-inputs program-string 12 2))
(defun part2 (program-string)
(dotimes (noun 99)
(dotimes (verb 99)
(when (= 19690720 (execute-with-inputs program-string noun verb))
(return-from part2 (+ (* 100 noun) verb))))))

34
2019/day02/day02.rkt Normal file
View file

@ -0,0 +1,34 @@
#lang racket/base
(require "../intcode.rkt"
racket/vector)
(module+ test
(require rackunit))
(define (part1 filename)
(define program (parse-file filename))
(vector-set! program 1 12)
(vector-set! program 2 2)
(define vm (execute (start-machine program '())))
(vector-ref (machine-program vm) 0))
(module+ test
(check-equal? (part1 "input.txt") 6627023))
(define (try-inputs program noun verb)
(define my-program (vector-copy program))
(vector-set! my-program 1 noun)
(vector-set! my-program 2 verb)
(define vm (execute (start-machine my-program '())))
(vector-ref (machine-program vm) 0))
(define (part2 filename)
(define program (parse-file filename))
(for*/first ([noun (in-range 100)]
[verb (in-range 100)]
#:when (eq? 19690720 (try-inputs program noun verb)))
(+ (* 100 noun) verb)))
(module+ test
(check-equal? (part2 "input.txt") 4019))

1
2019/day02/input.txt Normal file
View file

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,9,1,19,1,9,19,23,1,23,5,27,2,27,10,31,1,6,31,35,1,6,35,39,2,9,39,43,1,6,43,47,1,47,5,51,1,51,13,55,1,55,13,59,1,59,5,63,2,63,6,67,1,5,67,71,1,71,13,75,1,10,75,79,2,79,6,83,2,9,83,87,1,5,87,91,1,91,5,95,2,9,95,99,1,6,99,103,1,9,103,107,2,9,107,111,1,111,6,115,2,9,115,119,1,119,6,123,1,123,9,127,2,127,13,131,1,131,9,135,1,10,135,139,2,139,10,143,1,143,5,147,2,147,6,151,1,151,5,155,1,2,155,159,1,6,159,0,99,2,0,14,0

7
2019/day03/day03.dyalog Normal file
View file

@ -0,0 +1,7 @@
⎕io←0
p←','(≠⊆⊢)¨⊃⎕nget'input.txt'1
segment←{x y← ⋄ 'R'=⊃⍵:{x y+⍵}¨1↓,(1+⍎1↓⍵),1 ⋄ 'L'=⊃⍵:{x y+⍵}¨1↓,-(1+⍎1↓⍵),1 ⋄ 'D'=⊃⍵:{x y+⍵}¨1↓,-1,1+⍎1↓⍵ ⋄ 'U'=⊃⍵:{x y+⍵}¨1↓,1,1+⍎1↓⍵}
path←{x←⊂0 0 ⋄ 1↓x⊣{x,←(⊃⌽x) segment ⍵}¨⍵}
⌊/+/↑(path ⊃0⌷p) ∩ path ⊃1⌷p ⍝ part 1
c←(path ⊃0⌷p)∩path ⊃1⌷p
2+⌊/((path ⊃0⌷p)c)+(path ⊃1⌷p)c ⍝ part 2

2
2019/day03/input.txt Normal file
View file

@ -0,0 +1,2 @@
R990,U803,R777,U157,R629,D493,R498,D606,R344,U241,L708,D403,R943,U961,L107,D755,R145,D77,L654,D297,L263,D904,R405,U676,R674,U139,L746,U935,R186,U433,L739,D774,R470,D459,R865,D209,L217,U525,R747,D218,R432,U769,L876,D477,R606,D161,R991,D338,R647,D958,R777,D148,R593,D873,L95,U707,R468,U518,R845,U285,R221,U771,R989,D107,R44,U833,L343,D420,R468,D954,L604,D270,L691,U401,R850,U70,R441,U461,R638,D743,R65,U673,L999,U110,R266,U759,R768,U569,L250,D577,R247,U420,L227,U437,L80,D647,L778,U935,R585,U35,L735,D201,R694,U635,L597,U215,R743,D542,L701,U946,L503,U589,R836,D687,L444,U409,L473,U132,L570,U374,R193,D908,L800,U294,L252,U851,R947,D647,L37,D20,L27,U620,L534,D356,L291,U611,L128,D670,L364,U200,L749,D708,R776,U99,R606,D999,L810,D373,R212,D138,R856,D966,L206,D23,L860,D731,L914,U716,L212,U225,R766,U348,L220,D69,L766,D15,L557,U71,R734,D295,R884,D822,R300,D152,L986,D170,R764,U24,R394,D710,L860,U830,L305,U431,R201,D44,R882,U667,R37,D727,R916,U460,L834,D771,R373,U96,L707,D576,R607,D351,R577,D200,L402,U364,L32,D512,L152,D283,L232,U804,R827,U352,R104,D323,L254,U273,L451,D967,R739,D53,L908,D866,R998,U897,L581,U538,R206,U644,L70,D17,L481,U912,L377,D922,L286,U547,R35,U292,L318,U256,R79,D52,R92,U160,R763,U428,R663,D634,R212,D325,R460,U142,L375,U382,R20,D321,L220,D578,R915,D465,L797,D849,L281,D491,L911,D624,R800,U629,L675,U428,L219,U694,R680,U350,R113,D903,L22,D683,L787,D1,R93,U315,L562,U756,R622,D533,L587,D216,L933,U972,R506,U536,R797,U828,L12,D965,L641,U165,R937,D675,R259
L998,D197,L301,D874,L221,U985,L213,D288,R142,D635,R333,D328,R405,D988,L23,D917,R412,D971,R876,U527,R987,D884,R39,D485,L971,U200,R931,U79,L271,U183,R354,D18,R346,D866,L752,D204,L863,U784,R292,U676,R811,U721,L53,U983,L993,U822,R871,U539,L782,D749,R417,U667,R882,U467,R321,U894,R912,U756,L102,U154,L57,D316,R200,U372,L44,U406,L426,D613,R847,U977,R303,U469,R509,U839,L633,D267,L487,D976,R325,U399,L359,U161,L305,U935,R522,D848,R784,D273,L337,D55,L266,U406,L858,D650,L176,D124,R231,U513,L462,U328,L674,D598,R568,D742,L39,D438,L643,D254,R577,U519,R325,U124,R91,U129,L79,D52,R480,D46,R129,D997,R452,D992,L721,U490,L595,D666,R372,D198,R813,U624,L469,U59,R578,U184,R117,D749,L745,U302,R398,D951,L683,D360,R476,D788,R70,U693,R295,D547,L61,U782,R440,D818,L330,D321,L968,U622,R160,U571,L886,D43,L855,U272,R530,D267,L312,D519,L741,D697,R206,U148,L445,U857,R983,D192,L788,U826,R805,U932,R888,D250,L682,D52,R406,D176,R984,D637,L947,D416,L687,U699,L544,D710,L933,D171,L357,D134,L968,D538,R496,D240,L730,U771,R554,U708,R265,D748,L839,U668,L333,U335,R526,U809,L653,D6,R234,D130,R871,U911,R538,U372,L960,D535,L196,U236,L966,D185,L166,U789,L885,U453,R627,D586,R501,U222,L280,U124,R755,D159,L759,U78,R669,D889,L150,D888,L71,D917,L126,D97,L138,U726,R160,D971,R527,D988,R455,D413,R539,U923,R258,U734,L459,D954,R877,U613,R343,D98,R238,U478,R514,U814,L274,U119,L958,U698,R761,U693,R367,D111,L800,D531,L91,U616,R208,D255,R169,U145,R671,U969,L468,U566,R589,D455,R323,D303,R374,D890,R377,D262,L40,U85,L719

14
2019/day04/day04.dyalog Normal file
View file

@ -0,0 +1,14 @@
⎕io←0
a b←245182 790572
p←⊃(,(∘.,))/6/⊂10
x←{(1↓⍵)-¯1↓⍵}¨p
n←10⊥¨((/¨0=x)∧∧/¨0≤x)/p
≢((n>a)∧n<b)/n ⍝ part 1
n2←10⊥¨(({/2=⍵}¨(⊢∘≢⌸)¨p)∧∧/¨0≤x)/p
≢((n2>a)∧n2<b)/n2 ⍝ part 2
⍝ better version (inspired from
⍝ https://github.com/jayfoad/aoc2019apl/blob/master/p4.dyalog)
p←⍉10⊥⍣¯1⊢a+1+b-a
+/({∧/2≤/⍵}∧{/2=/⍵})p ⍝ part 1
+/({∧/2≤/⍵}∧{/¨2=(⊢∘≢⌸)¨↓⍵})p ⍝ part 2

61
2019/day05/day05.lisp Normal file
View file

@ -0,0 +1,61 @@
(defparameter *input-file* #P"input.txt")
(defparameter *input* (uiop:read-file-string *input-file*))
(defvar *program*)
(defun opcode (pc)
(mod (aref *program* pc) 100))
(defun parameter-mode (pc index)
(mod (truncate (aref *program* pc) (expt 10 (+ 1 index))) 10))
(defun parameter (pc index)
(ecase (parameter-mode pc index)
(1 (aref *program* (+ index pc)))
(0 (aref *program* (aref *program* (+ pc index))))))
(defun (setf parameter) (new-value pc index)
(ecase (parameter-mode pc index)
(0 (setf (aref *program* (aref *program* (+ pc index))) new-value))
(1 (error "Cannot write with a parameter in immediate mode"))))
(defun execute (inputs)
(defun execute-instruction (pc inputs outputs)
(ecase (opcode pc)
(99 (return-from execute-instruction outputs))
(1 (setf (parameter pc 3) (+ (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4) inputs outputs))
(2 (setf (parameter pc 3) (* (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4) inputs outputs))
(3 (setf (parameter pc 1) (car inputs))
(execute-instruction (+ pc 2) (cdr inputs) outputs))
(4 (execute-instruction (+ pc 2) inputs (cons (parameter pc 1) outputs)))
(5 (if (/= 0 (parameter pc 1))
(execute-instruction (parameter pc 2) inputs outputs)
(execute-instruction (+ pc 3) inputs outputs)))
(6 (if (= 0 (parameter pc 1))
(execute-instruction (parameter pc 2) inputs outputs)
(execute-instruction (+ pc 3) inputs outputs)))
(7 (if (< (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4) inputs outputs))
(8 (if (= (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4) inputs outputs))))
(execute-instruction 0 inputs nil))
(defun parse-program (program-string)
(map 'vector #'parse-integer (uiop:split-string program-string :separator ",")))
(defun part1 ()
(let ((*program* (parse-program *input*)))
(car (execute '(1)))))
(defun part2 ()
(let ((*program* (parse-program *input*)))
(car (execute '(5)))))
(assert (= (part1) 12896948))
(assert (= (part2) 7704130))

22
2019/day05/day05.rkt Normal file
View file

@ -0,0 +1,22 @@
#lang racket/base
(require "../intcode.rkt")
(module+ test
(require rackunit))
(define (part1 filename)
(define program (parse-file filename))
(define vm (execute (start-machine program '(1))))
(car (machine-outputs vm)))
(module+ test
(check-equal? (part1 "input.txt") 12896948))
(define (part2 filename)
(define program (parse-file filename))
(define vm (execute (start-machine program '(5))))
(car (machine-outputs vm)))
(module+ test
(check-equal? (part2 "input.txt") 7704130))

1
2019/day05/input.txt Normal file
View file

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1102,46,47,225,2,122,130,224,101,-1998,224,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1102,61,51,225,102,32,92,224,101,-800,224,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,1101,61,64,225,1001,118,25,224,101,-106,224,224,4,224,1002,223,8,223,101,1,224,224,1,224,223,223,1102,33,25,225,1102,73,67,224,101,-4891,224,224,4,224,1002,223,8,223,1001,224,4,224,1,224,223,223,1101,14,81,225,1102,17,74,225,1102,52,67,225,1101,94,27,225,101,71,39,224,101,-132,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1002,14,38,224,101,-1786,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1,65,126,224,1001,224,-128,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1101,81,40,224,1001,224,-121,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,226,224,1002,223,2,223,1005,224,329,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,344,101,1,223,223,1107,677,677,224,102,2,223,223,1005,224,359,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,374,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,389,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,419,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,434,1001,223,1,223,108,226,677,224,102,2,223,223,1006,224,449,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,479,1001,223,1,223,1007,677,677,224,1002,223,2,223,1005,224,494,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,7,226,226,224,102,2,223,223,1005,224,539,1001,223,1,223,8,677,677,224,1002,223,2,223,1005,224,554,101,1,223,223,107,677,226,224,102,2,223,223,1006,224,569,1001,223,1,223,7,226,677,224,1002,223,2,223,1005,224,584,1001,223,1,223,1008,226,226,224,1002,223,2,223,1006,224,599,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,614,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,629,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,659,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226

23
2019/day06/day06.rkt Normal file
View file

@ -0,0 +1,23 @@
#lang racket
(require racket/string
graph)
(define (read-graph filename)
(with-input-from-file filename
(lambda ()
(define g (unweighted-graph/directed '()))
(for ([line (in-lines)])
(define bodies (string-split line ")"))
(add-edge! g (car bodies) (cadr bodies)))
g)))
(define (part1 filename)
(define g (read-graph filename))
(define-values (dists _) (bfs g "COM"))
(for/sum ([i (in-hash-values dists)]) i))
(define (part2 filename)
(define g (read-graph filename))
(define-values (dists _) (bfs g "YOU"))
(- (hash-ref dists "SAN") 2))

1514
2019/day06/input Normal file

File diff suppressed because it is too large Load diff

11
2019/day06/test Normal file
View file

@ -0,0 +1,11 @@
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L

13
2019/day06/test2 Normal file
View file

@ -0,0 +1,13 @@
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN

87
2019/day07/day07.lisp Normal file
View file

@ -0,0 +1,87 @@
(ql:quickload :alexandria)
(defparameter *input-file* #P"input.txt")
(defvar *input*)
(defvar *program*)
(defvar *input-fn*)
(defvar *output-fn*)
(defun opcode (pc)
(mod (aref *program* pc) 100))
(defun parameter-mode (pc index)
(mod (truncate (aref *program* pc) (expt 10 (+ 1 index))) 10))
(defun parameter (pc index)
(ecase (parameter-mode pc index)
(1 (aref *program* (+ index pc)))
(0 (aref *program* (aref *program* (+ pc index))))))
(defun (setf parameter) (new-value pc index)
(ecase (parameter-mode pc index)
(0 (setf (aref *program* (aref *program* (+ pc index))) new-value))
(1 (error "Cannot write with a parameter in immediate mode"))))
(defun execute-instruction (pc)
(ecase (opcode pc)
(99 (return-from execute-instruction))
(1 (setf (parameter pc 3) (+ (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4)))
(2 (setf (parameter pc 3) (* (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4)))
(3 (setf (parameter pc 1) (funcall *input-fn*))
(execute-instruction (+ pc 2)))
(4 (funcall *output-fn* (parameter pc 1))
(execute-instruction (+ pc 2)))
(5 (if (/= 0 (parameter pc 1))
(execute-instruction (parameter pc 2))
(execute-instruction (+ pc 3))))
(6 (if (= 0 (parameter pc 1))
(execute-instruction (parameter pc 2))
(execute-instruction (+ pc 3))))
(7 (if (< (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4)))
(8 (if (= (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4)))))
(defun execute (program &key (input 'input) (output 'output))
(let ((*program* program)
(*input-fn* input)
(*output-fn* output))
(execute-instruction 0)))
(defun parse-program (program-string)
(map 'vector #'parse-integer (uiop:split-string program-string :separator ",")))
(defun amplify (phases)
(let ((amp-input 0))
(loop for phase in phases
do (let ((program (alexandria:copy-array *program*))
(inputs (list phase amp-input))
(output nil))
(execute program :input (lambda () (pop inputs)) :output (lambda (x) (setf output x)))
(setf amp-input output)))
amp-input))
(defun max-thrust ()
(let ((res nil))
(alexandria:map-permutations (lambda (x) (push (amplify x) res)) '(0 1 2 3 4))
(apply #'max res)))
(assert (= 43210 (let ((*program* (parse-program "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")))
(max-thrust))))
(assert (= 54321 (let ((*program* (parse-program "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0")))
(max-thrust))))
(assert (= 65210 (let ((*program* (parse-program "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")))
(max-thrust))))
(defun part1 ()
(let ((*program* (parse-program (uiop:read-file-string *input-file*))))
(max-thrust)))

51
2019/day07/day07.rkt Normal file
View file

@ -0,0 +1,51 @@
#lang racket/base
(require "../intcode.rkt"
racket/list)
(module+ test
(require rackunit))
(define (part1 program)
(apply max
(for/list ([phases (in-permutations (range 5))])
(for/fold ([signal 0])
([phase (in-list phases)])
(define vm (execute (start-machine program (list phase signal))))
(car (machine-outputs vm))))))
(module+ test
(check-equal? (part1 (parse "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")) 43210)
(check-equal? (part1 (parse "3,23,3,24,1002,24,10,24,1002,23,-1,23,
101,5,23,23,1,24,23,23,4,23,99,0,0")) 54321)
(check-equal? (part1 (parse "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")) 65210)
(check-equal? (part1 (parse-file "input.txt")) 298586))
(define (part2 program)
(define signals
(for/list ([phases (in-permutations (range 5 10))])
(define amplifiers
(for/list ([phase (in-list phases)])
(define vm (execute (start-machine program (list phase))))
vm))
(let loop ([signal 0]
[amplifiers amplifiers])
(if (empty? amplifiers)
signal
(let* ([amp (machine-program (car amplifiers))]
[pc (machine-pc (car amplifiers))]
[new-vm (execute (machine amp (list signal) pc 0 #f '()))]
[new-amplifiers (if (machine-terminated new-vm)
(cdr amplifiers)
(append (cdr amplifiers) (list new-vm)))])
(loop (car (machine-outputs new-vm)) new-amplifiers))))))
(apply max signals))
(module+ test
(check-equal? (part2 (parse "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,
27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5")) 139629729)
(check-equal? (part2 (parse "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,
-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,
53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10")) 18216)
(check-equal? (part2 (parse-file "input.txt")) 9246095))

1
2019/day07/input.txt Normal file
View file

@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,42,67,84,109,126,207,288,369,450,99999,3,9,102,4,9,9,1001,9,4,9,102,2,9,9,101,2,9,9,4,9,99,3,9,1001,9,5,9,1002,9,5,9,1001,9,5,9,1002,9,5,9,101,5,9,9,4,9,99,3,9,101,5,9,9,1002,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,2,9,102,4,9,9,101,2,9,9,102,4,9,9,1001,9,2,9,4,9,99,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99

4
2019/day08/day08.dyalog Normal file
View file

@ -0,0 +1,4 @@
p←100 6 25⍎¨⊃⊃⎕nget'input.txt'1
x←,(⊃⍋+/+/0=p)⌷p
(+/1=x)×+/2=x ⍝ part 1
' ⌺'[{1⌷⍋⍵0 1}¨⊂[1]p] ⍝ part 2

1
2019/day08/input.txt Normal file

File diff suppressed because one or more lines are too long

29
2019/day09/day09.rkt Normal file
View file

@ -0,0 +1,29 @@
#lang racket/base
(require "../intcode.rkt")
(module+ test
(require rackunit))
(define (part12 program input)
(define vm (execute (start-machine program (list input))))
(car (machine-outputs vm)))
(module+ test
(test-case "quine"
(define quine (parse "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99"))
(define vm (execute (start-machine quine '())))
(check-true (machine-terminated vm))
(check-equal? (reverse (machine-outputs vm)) (vector->list quine)))
(test-case "16-digit number"
(define program (parse "1102,34915192,34915192,7,4,7,99,0"))
(define vm (execute (start-machine program '())))
(check-true (machine-terminated vm))
(check-equal? (string-length (number->string (car (machine-outputs vm)))) 16))
(test-case "large number"
(define program (parse "104,1125899906842624,99"))
(define vm (execute (start-machine program '())))
(check-true (machine-terminated vm))
(check-equal? (car (machine-outputs vm)) 1125899906842624))
(check-equal? (part12 (parse-file "input") 1) 3601950151)
(check-equal? (part12 (parse-file "input") 2) 64236))

1
2019/day09/input Normal file
View file

@ -0,0 +1 @@
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,0,3,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,30,1016,1101,37,0,1005,1101,362,0,1023,1101,0,20,1014,1101,39,0,1013,1102,34,1,1007,1101,682,0,1027,1102,664,1,1025,1102,1,655,1028,1101,0,26,1002,1102,1,38,1015,1101,669,0,1024,1101,0,28,1017,1102,1,21,1000,1101,0,27,1012,1102,1,29,1008,1102,1,23,1019,1101,0,24,1011,1101,685,0,1026,1102,646,1,1029,1102,1,369,1022,1101,0,31,1003,1102,1,36,1001,1101,0,0,1020,1102,1,35,1009,1101,32,0,1010,1101,0,1,1021,1102,33,1,1004,1101,22,0,1006,1102,1,25,1018,109,14,1205,6,197,1001,64,1,64,1105,1,199,4,187,1002,64,2,64,109,-4,21107,40,39,9,1005,1019,219,1001,64,1,64,1105,1,221,4,205,1002,64,2,64,109,9,1206,1,239,4,227,1001,64,1,64,1106,0,239,1002,64,2,64,109,-9,2101,0,-8,63,1008,63,26,63,1005,63,261,4,245,1106,0,265,1001,64,1,64,1002,64,2,64,109,-6,2108,37,1,63,1005,63,287,4,271,1001,64,1,64,1105,1,287,1002,64,2,64,109,15,21108,41,44,-2,1005,1017,307,1001,64,1,64,1106,0,309,4,293,1002,64,2,64,109,-16,1207,1,34,63,1005,63,327,4,315,1105,1,331,1001,64,1,64,1002,64,2,64,109,8,1208,-9,29,63,1005,63,347,1106,0,353,4,337,1001,64,1,64,1002,64,2,64,109,4,2105,1,8,1001,64,1,64,1105,1,371,4,359,1002,64,2,64,109,-22,1201,9,0,63,1008,63,27,63,1005,63,391,1106,0,397,4,377,1001,64,1,64,1002,64,2,64,109,18,21107,42,43,5,1005,1016,415,4,403,1106,0,419,1001,64,1,64,1002,64,2,64,109,-8,1201,2,0,63,1008,63,37,63,1005,63,441,4,425,1105,1,445,1001,64,1,64,1002,64,2,64,109,27,1205,-9,463,4,451,1001,64,1,64,1106,0,463,1002,64,2,64,109,-1,1206,-8,475,1105,1,481,4,469,1001,64,1,64,1002,64,2,64,109,-6,21101,43,0,-8,1008,1015,43,63,1005,63,507,4,487,1001,64,1,64,1106,0,507,1002,64,2,64,109,-15,2101,0,-3,63,1008,63,35,63,1005,63,531,1001,64,1,64,1106,0,533,4,513,1002,64,2,64,109,-2,2102,1,-6,63,1008,63,18,63,1005,63,553,1105,1,559,4,539,1001,64,1,64,1002,64,2,64,109,7,21102,44,1,3,1008,1016,44,63,1005,63,581,4,565,1105,1,585,1001,64,1,64,1002,64,2,64,109,-11,1202,7,1,63,1008,63,34,63,1005,63,609,1001,64,1,64,1105,1,611,4,591,1002,64,2,64,109,6,1202,1,1,63,1008,63,35,63,1005,63,637,4,617,1001,64,1,64,1106,0,637,1002,64,2,64,109,16,2106,0,4,4,643,1001,64,1,64,1106,0,655,1002,64,2,64,109,-1,2105,1,1,4,661,1106,0,673,1001,64,1,64,1002,64,2,64,109,5,2106,0,-1,1105,1,691,4,679,1001,64,1,64,1002,64,2,64,109,-24,1208,-2,26,63,1005,63,709,4,697,1105,1,713,1001,64,1,64,1002,64,2,64,109,-10,2102,1,6,63,1008,63,21,63,1005,63,735,4,719,1105,1,739,1001,64,1,64,1002,64,2,64,109,25,21108,45,45,-9,1005,1010,757,4,745,1106,0,761,1001,64,1,64,1002,64,2,64,109,-12,1207,-7,20,63,1005,63,777,1106,0,783,4,767,1001,64,1,64,1002,64,2,64,109,-13,2108,22,6,63,1005,63,799,1106,0,805,4,789,1001,64,1,64,1002,64,2,64,109,17,21102,46,1,0,1008,1011,45,63,1005,63,825,1105,1,831,4,811,1001,64,1,64,1002,64,2,64,109,-6,2107,21,1,63,1005,63,849,4,837,1105,1,853,1001,64,1,64,1002,64,2,64,109,-3,2107,27,0,63,1005,63,873,1001,64,1,64,1105,1,875,4,859,1002,64,2,64,109,12,21101,47,0,0,1008,1014,48,63,1005,63,899,1001,64,1,64,1105,1,901,4,881,4,64,99,21102,27,1,1,21101,0,915,0,1105,1,922,21201,1,42931,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,942,0,0,1106,0,922,21202,1,1,-1,21201,-2,-3,1,21102,1,957,0,1106,0,922,22201,1,-1,-2,1106,0,968,22101,0,-2,-2,109,-3,2106,0,0

87
2019/day10/day10.rkt Normal file
View file

@ -0,0 +1,87 @@
#lang racket
(module+ test
(require rackunit))
(define (read-input filename)
(string->grid (file->string filename)))
(define (string->grid str)
(for/set ([line (string-split str)]
[i (in-naturals)]
#:when #t
[c (in-string line)]
[j (in-naturals)]
#:when (eq? c #\#))
(make-rectangular j i)))
(define (segment->direction x)
(define a (real-part x))
(define b (imag-part x))
(define r (gcd a b))
(make-rectangular (/ a r) (/ b r)))
(define (detected grid)
(for/hash ([x (in-set grid)])
(values x (for/set ([y (in-set grid)]
#:unless (= x y))
(segment->direction (- y x))))))
(define (find-station grid)
(define visible (detected grid))
(define max-visible (apply max (map set-count (hash-values visible))))
(define pos (for/first ([(k v) (in-hash visible)]
#:when (= max-visible (set-count v)))
k))
(values pos (hash-ref visible pos)))
(define (part1 filename)
(define grid (read-input filename))
(define-values (loc dirs) (find-station grid))
(set-count dirs))
(module+ test
(check-equal? (part1 "test") 8)
(check-equal? (part1 "input") 286))
(define (find-target grid station direction)
(define targets
(for/list ([x (in-set grid)]
#:unless (= x station)
#:when (real? (/ (- x station) direction))
#:when (<= 0 (/ (- x station) direction)))
x))
(car (sort targets
(λ (x y) (< (magnitude (- x station))
(magnitude (- y station)))))))
(define (rotate-left lst)
(if (empty? lst)
'()
(append (cdr lst) (list (car lst)))))
(define (rotate-right lst)
(reverse (rotate-left (reverse lst))))
(define (find-nth-target grid n)
(define-values (station directions) (find-station grid))
(define directions-sorted (rotate-right
(sort (set->list directions)
(λ (x y) (< (angle (* -i x)) (angle (* -i y)))))))
(define-values (final-grid targets)
(for/fold ([grid grid]
[targets '()])
([i (in-range n)]
[dir (in-cycle directions-sorted)])
(define target (find-target grid station dir))
(values (set-remove grid target)
(cons target targets))))
(car targets))
(define (part2 filename)
(define grid (read-input filename))
(define target (find-nth-target grid 200))
(+ (* 100 (real-part target)) (imag-part target)))
(module+ test
(check-equal? (part2 "input") 504))

30
2019/day10/input Normal file
View file

@ -0,0 +1,30 @@
.#.####..#.#...#...##..#.#.##.
..#####.##..#..##....#..#...#.
......#.......##.##.#....##..#
..#..##..#.###.....#.#..###.#.
..#..#..##..#.#.##..###.......
...##....#.##.#.#..##.##.#...#
.##...#.#.##..#.#........#.#..
.##...##.##..#.#.##.#.#.#.##.#
#..##....#...###.#..##.#...##.
.###.###..##......#..#...###.#
.#..#.####.#..#....#.##..#.#.#
..#...#..#.#######....###.....
####..#.#.#...##...##....#..##
##..#.##.#.#..##.###.#.##.##..
..#.........#.#.#.#.......#..#
...##.#.....#.#.##........#..#
##..###.....#.............#.##
.#...#....#..####.#.#......##.
..#..##..###...#.....#...##..#
...####..#.#.##..#....#.#.....
####.#####.#.#....#.#....##.#.
#.#..#......#.........##..#.#.
#....##.....#........#..##.##.
.###.##...##..#.##.#.#...#.#.#
##.###....##....#.#.....#.###.
..#...#......#........####..#.
#....#.###.##.#...#.#.#.#.....
.........##....#...#.....#..##
###....#.........#..#..#.#.#..
##...#...###.#..#.###....#.##.

5
2019/day10/test Normal file
View file

@ -0,0 +1,5 @@
.#..#
.....
#####
....#
...##

58
2019/day11/day11.rkt Normal file
View file

@ -0,0 +1,58 @@
#lang racket/base
(require "../intcode.rkt"
racket/set)
(module+ test
(require rackunit))
(define (part12 program start-panel [show-grid #f])
(define vm (execute (start-machine program '())))
(define grid (make-hash))
(hash-set! grid 0 start-panel)
(define painted (mutable-set))
(let loop ([vm vm]
[direction 0+1i]
[position 0])
(define new-vm
(execute (machine (machine-program vm)
(list (hash-ref grid position 0))
(machine-pc vm)
(machine-relative-base vm)
#f
'())))
(unless (machine-terminated new-vm)
(hash-set! grid position (cadr (machine-outputs new-vm)))
(set-add! painted position)
(define new-direction (if (= 0 (car (machine-outputs new-vm)))
(* direction 0+1i)
(* direction 0-1i)))
(define new-position (+ position new-direction))
(loop new-vm new-direction new-position)))
(when show-grid
(display-grid grid))
(set-count painted))
(define (display-grid grid)
(define min-x (apply min (map real-part (hash-keys grid))))
(define max-x (apply max (map real-part (hash-keys grid))))
(define min-y (apply min (map imag-part (hash-keys grid))))
(define max-y (apply max (map imag-part (hash-keys grid))))
(for* ([j (in-range max-y (sub1 min-y) -1)]
[i (in-range min-x max-x)])
(if (= 1 (hash-ref grid (make-rectangular i j) 0))
(printf "#")
(printf " "))
(when (= i (sub1 max-x))
(printf "\n"))))
(module+ test
(check-equal? (part12 (parse-file "input") 0) 2511))
;; > (part12 (parse-file "input") 1 #t)
;; # # ## # # ## # # ## ### # #
;; # # # # # # # # # # # # # #
;; #### # ## # ## # # # ####
;; # # # # # # # # # ## ### # #
;; # # # # # # # # # # # # # # #
;; # # ## # # ## # # ### # # #

1
2019/day11/input Normal file
View file

@ -0,0 +1 @@
3,8,1005,8,328,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,29,1,104,7,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,55,1,2,7,10,1006,0,23,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,84,1006,0,40,1,1103,14,10,1,1006,16,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,116,1006,0,53,1,1104,16,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,102,1,8,146,2,1104,9,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,172,1006,0,65,1,1005,8,10,1,1002,16,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,204,2,1104,9,10,1006,0,30,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,102,1,8,233,2,1109,6,10,1006,0,17,1,2,6,10,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,266,1,106,7,10,2,109,2,10,2,9,8,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,301,1,109,9,10,1006,0,14,101,1,9,9,1007,9,1083,10,1005,10,15,99,109,650,104,0,104,1,21102,1,837548789788,1,21101,0,345,0,1106,0,449,21101,0,846801511180,1,21101,0,356,0,1106,0,449,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,235244981271,0,1,21101,403,0,0,1105,1,449,21102,1,206182744295,1,21101,0,414,0,1105,1,449,3,10,104,0,104,0,3,10,104,0,104,0,21102,837896937832,1,1,21101,0,437,0,1106,0,449,21101,867965862668,0,1,21102,448,1,0,1106,0,449,99,109,2,22102,1,-1,1,21101,40,0,2,21102,1,480,3,21101,0,470,0,1106,0,513,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,475,476,491,4,0,1001,475,1,475,108,4,475,10,1006,10,507,1101,0,0,475,109,-2,2106,0,0,0,109,4,1201,-1,0,512,1207,-3,0,10,1006,10,530,21102,1,0,-3,22102,1,-3,1,21201,-2,0,2,21102,1,1,3,21102,549,1,0,1106,0,554,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,577,2207,-4,-2,10,1006,10,577,21202,-4,1,-4,1106,0,645,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21101,596,0,0,1106,0,554,21201,1,0,-4,21102,1,1,-1,2207,-4,-2,10,1006,10,615,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,637,22102,1,-1,1,21101,637,0,0,105,1,512,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2106,0,0

64
2019/day12/day12.rkt Normal file
View file

@ -0,0 +1,64 @@
#lang racket
(struct moon
(x y z vx vy vz)
#:transparent)
(define (read-input filename)
(with-input-from-file filename
(lambda ()
(for/list ([line (in-lines)])
(define positions-str
(cdr (regexp-match #rx"<x=(-?[0-9]+), y=(-?[0-9]+), z=(-?[0-9]+)>" line)))
(define positions (map string->number positions-str))
(apply moon (append positions '(0 0 0)))))))
(define (step moons)
(for/list ([cur moons])
(define new-vx (moon-vx cur))
(define new-vy (moon-vy cur))
(define new-vz (moon-vz cur))
(define (gravity-pull pos-cur pos-other)
(cond
[(> pos-other pos-cur) 1]
[(< pos-other pos-cur) -1]
[else 0]))
(for ([other moons] #:unless (eq? other cur))
(set! new-vx (+ new-vx (gravity-pull (moon-x cur) (moon-x other))))
(set! new-vy (+ new-vy (gravity-pull (moon-y cur) (moon-y other))))
(set! new-vz (+ new-vz (gravity-pull (moon-z cur) (moon-z other)))))
(moon (+ (moon-x cur) new-vx)
(+ (moon-y cur) new-vy)
(+ (moon-z cur) new-vz)
new-vx
new-vy
new-vz)))
(define (total-energy moons)
(for/sum ([m moons])
(* (+ (abs (moon-x m))
(abs (moon-y m))
(abs (moon-z m)))
(+ (abs (moon-vx m))
(abs (moon-vy m))
(abs (moon-vz m))))))
(define (part1 filename)
(define moons (read-input filename))
(total-energy (for/fold ([ms moons]) ([i (in-range 1000)]) (step ms))))
(define (cycle-length moons pos speed)
(define-values (states final)
(for/fold ([states (mutable-set)] [ms moons])
([_ (in-naturals)]
#:break (set-member? states (append (map pos ms) (map speed ms))))
(set-add! states (append (map pos ms) (map speed ms)))
(values states (step ms))))
(set-count states))
(define (part2 filename)
(define moons (read-input filename))
(define cycle-x (cycle-length moons moon-x moon-vx))
(define cycle-y (cycle-length moons moon-y moon-vy))
(define cycle-z (cycle-length moons moon-z moon-vz))
(lcm cycle-x cycle-y cycle-z))

4
2019/day12/input Normal file
View file

@ -0,0 +1,4 @@
<x=-17, y=9, z=-5>
<x=-1, y=7, z=13>
<x=-19, y=12, z=5>
<x=-6, y=-6, z=-4>

4
2019/day12/test Normal file
View file

@ -0,0 +1,4 @@
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>

4
2019/day12/test2 Normal file
View file

@ -0,0 +1,4 @@
<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>

74
2019/day13/day13.rkt Normal file
View file

@ -0,0 +1,74 @@
#lang racket/base
(require "../intcode.rkt"
racket/list)
(module+ test
(require rackunit))
(define (part1 program)
(define vm (execute (start-machine program '())))
(define screen
(let loop ([screen (hash)]
[instructions (reverse (machine-outputs vm))])
(if (empty? instructions)
screen
(let ([x (car instructions)]
[y (cadr instructions)]
[tile-id (caddr instructions)])
(loop (hash-set screen (list x y) tile-id)
(cdddr instructions))))))
(for/sum ([(k v) (in-hash screen)]
#:when (= v 2))
1))
(module+ test
(check-equal? (part1 (parse-file "input")) 363))
(define (ball-x screen)
(for/first ([(k v) (in-hash screen)]
#:when (= v 4))
(car k)))
(define (paddle-x screen)
(for/first ([(k v) (in-hash screen)]
#:when (= v 3))
(car k)))
(define (block-count screen)
(for/sum ([(k v) (in-hash screen)]
#:when (= v 2))
1))
(define (draw-screen screen instructions)
(if (empty? instructions)
screen
(let ([x (car instructions)]
[y (cadr instructions)]
[tile-id (caddr instructions)])
(draw-screen (hash-set screen (list x y) tile-id)
(cdddr instructions)))))
(define (part2 program)
(vector-set! program 0 2)
(define vm (execute (start-machine program '())))
(define screen
(let loop ([vm vm]
[screen (hash)])
(define new-screen (draw-screen screen (reverse (machine-outputs vm))))
(define ball-pos (ball-x new-screen))
(define paddle-pos (paddle-x new-screen))
(define joystick (cond
[(< ball-pos paddle-pos) -1]
[(= ball-pos paddle-pos) 0]
[(> ball-pos paddle-pos) 1]))
(define new-vm
(execute (machine (machine-program vm) (list joystick)
(machine-pc vm) (machine-relative-base vm) #f '())))
(if (= 0 (block-count new-screen))
new-screen
(loop new-vm new-screen))))
(hash-ref screen (list -1 0)))
(module+ test
(check-equal? (part2 (parse-file "input")) 17159))

1
2019/day13/input Normal file

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show more