Parcourir la source

Day 7 Part 1

main
Jens Kadenbach il y a 2 ans
Parent
révision
7d1a8b8f5c
7 fichiers modifiés avec 1294 ajouts et 0 suppressions
  1. 6
    0
      aoc2022.cabal
  2. 1
    0
      package.yaml
  3. 986
    0
      ressources/day07-input
  4. 97
    0
      src/Day7.hs
  5. 68
    0
      src/Day7/Parser.hs
  6. 3
    0
      src/Lib.hs
  7. 133
    0
      test/Day7Spec.hs

+ 6
- 0
aoc2022.cabal Voir le fichier

@@ -44,6 +44,8 @@ library
44 44
       Day5.Part2
45 45
       Day5.Shared
46 46
       Day6
47
+      Day7
48
+      Day7.Parser
47 49
       Lib
48 50
   other-modules:
49 51
       Paths_aoc2022
@@ -57,6 +59,7 @@ library
57 59
     , heredoc
58 60
     , hspec
59 61
     , lens
62
+    , parsec
60 63
     , split
61 64
     , text
62 65
   default-language: Haskell2010
@@ -76,6 +79,7 @@ executable aoc2022-exe
76 79
     , heredoc
77 80
     , hspec
78 81
     , lens
82
+    , parsec
79 83
     , split
80 84
     , text
81 85
   default-language: Haskell2010
@@ -90,6 +94,7 @@ test-suite aoc2022-test
90 94
       Day4Spec
91 95
       Day5Spec
92 96
       Day6Spec
97
+      Day7Spec
93 98
       Paths_aoc2022
94 99
   hs-source-dirs:
95 100
       test
@@ -102,6 +107,7 @@ test-suite aoc2022-test
102 107
     , heredoc
103 108
     , hspec
104 109
     , lens
110
+    , parsec
105 111
     , split
106 112
     , text
107 113
   default-language: Haskell2010

+ 1
- 0
package.yaml Voir le fichier

@@ -29,6 +29,7 @@ dependencies:
29 29
 - split
30 30
 - lens
31 31
 - Ranged-sets
32
+- parsec
32 33
 
33 34
 ghc-options:
34 35
 - -Wall

+ 986
- 0
ressources/day07-input Voir le fichier

@@ -0,0 +1,986 @@
1
+$ cd /
2
+$ ls
3
+dir bnl
4
+dir dmpsnhdh
5
+272080 dncdssn.hdr
6
+dir fcnqg
7
+6067 hjpmqrq
8
+dir jvwtm
9
+dir ldztz
10
+dir lmmw
11
+dir wthvqw
12
+dir zpdnprb
13
+$ cd bnl
14
+$ ls
15
+dir dhw
16
+dir dmpsnhdh
17
+dir lmw
18
+dir vgbqbrst
19
+$ cd dhw
20
+$ ls
21
+237421 vccwmhl
22
+$ cd ..
23
+$ cd dmpsnhdh
24
+$ ls
25
+dir chf
26
+dir mjpbhjm
27
+dir zwhpwp
28
+$ cd chf
29
+$ ls
30
+4679 lmw.wmp
31
+217367 wwnfv.qqr
32
+dir zfgznbz
33
+$ cd zfgznbz
34
+$ ls
35
+179409 cnj.gdn
36
+171574 vglqg
37
+$ cd ..
38
+$ cd ..
39
+$ cd mjpbhjm
40
+$ ls
41
+dir crf
42
+dir hqnj
43
+dir lmw
44
+18783 lmw.rwr
45
+302608 twpq
46
+166891 vqczlg
47
+$ cd crf
48
+$ ls
49
+32183 dltmqht
50
+240428 frqqdsr.hbf
51
+224910 sgtnrvrt
52
+$ cd ..
53
+$ cd hqnj
54
+$ ls
55
+261723 cgstb
56
+77979 dmpsnhdh.cmd
57
+$ cd ..
58
+$ cd lmw
59
+$ ls
60
+50307 fcqrwd
61
+$ cd ..
62
+$ cd ..
63
+$ cd zwhpwp
64
+$ ls
65
+141133 gdngm.mps
66
+$ cd ..
67
+$ cd ..
68
+$ cd lmw
69
+$ ls
70
+dir dvv
71
+267473 jmqgrh.dlz
72
+295139 rrqjwpm
73
+$ cd dvv
74
+$ ls
75
+114536 gmlmbrrw.wdm
76
+102061 lmw
77
+$ cd ..
78
+$ cd ..
79
+$ cd vgbqbrst
80
+$ ls
81
+105102 dmpsnhdh.bgl
82
+269054 gmwgjf.fzz
83
+dir jbdtpnw
84
+245266 jzsjvgl
85
+216220 lmw.gtb
86
+dir rflp
87
+dir twpq
88
+$ cd jbdtpnw
89
+$ ls
90
+27543 cjvvmzp
91
+$ cd ..
92
+$ cd rflp
93
+$ ls
94
+137601 frqqdsr.hbf
95
+83444 rrqjwpm
96
+$ cd ..
97
+$ cd twpq
98
+$ ls
99
+dir rlbsdj
100
+36846 tnrqzjdd
101
+$ cd rlbsdj
102
+$ ls
103
+56078 bvndq
104
+$ cd ..
105
+$ cd ..
106
+$ cd ..
107
+$ cd ..
108
+$ cd dmpsnhdh
109
+$ ls
110
+dir fnpwwhtj
111
+dir lmw
112
+9090 mgjpsvl.jlh
113
+186374 pbb.zln
114
+$ cd fnpwwhtj
115
+$ ls
116
+dir cgp
117
+$ cd cgp
118
+$ ls
119
+81938 hjpmqrq
120
+281971 jvszf
121
+151057 wmr.bnf
122
+$ cd ..
123
+$ cd ..
124
+$ cd lmw
125
+$ ls
126
+dir bfbv
127
+56929 pbb.zln
128
+dir rrqjwpm
129
+dir sngm
130
+$ cd bfbv
131
+$ ls
132
+92667 qrrttb.jgp
133
+$ cd ..
134
+$ cd rrqjwpm
135
+$ ls
136
+25739 cqljn.zqw
137
+91325 dncdssn.hdr
138
+$ cd ..
139
+$ cd sngm
140
+$ ls
141
+282163 jgrj
142
+dir lmw
143
+237524 lmw.dff
144
+153497 lmw.ntg
145
+dir lqd
146
+dir szn
147
+143535 tvpvc.qpr
148
+98326 vbfgh
149
+$ cd lmw
150
+$ ls
151
+32484 dncdssn.hdr
152
+dir glwr
153
+$ cd glwr
154
+$ ls
155
+144719 frqqdsr.hbf
156
+$ cd ..
157
+$ cd ..
158
+$ cd lqd
159
+$ ls
160
+231401 dncdssn.hdr
161
+dir jnjqmvg
162
+dir lmw
163
+199704 rrqjwpm
164
+$ cd jnjqmvg
165
+$ ls
166
+104947 trpsrfjz.brg
167
+$ cd ..
168
+$ cd lmw
169
+$ ls
170
+230298 rrqjwpm.nnv
171
+158947 wfv.qrb
172
+$ cd ..
173
+$ cd ..
174
+$ cd szn
175
+$ ls
176
+197974 frqqdsr.hbf
177
+$ cd ..
178
+$ cd ..
179
+$ cd ..
180
+$ cd ..
181
+$ cd fcnqg
182
+$ ls
183
+251609 dncdssn.hdr
184
+289497 jdjmftqs
185
+228459 qbmthcq
186
+$ cd ..
187
+$ cd jvwtm
188
+$ ls
189
+dir dmpsnhdh
190
+47959 pbb.zln
191
+dir tlr
192
+dir twpq
193
+dir wbgcsw
194
+dir zjmldjdh
195
+$ cd dmpsnhdh
196
+$ ls
197
+247567 bnl
198
+102471 bnl.wdm
199
+80054 fhqvp.hfm
200
+dir llhp
201
+dir mnsbh
202
+dir mpplsfjp
203
+20844 mtvl.lmp
204
+$ cd llhp
205
+$ ls
206
+180255 dmpsnhdh
207
+$ cd ..
208
+$ cd mnsbh
209
+$ ls
210
+267627 dmpsnhdh
211
+$ cd ..
212
+$ cd mpplsfjp
213
+$ ls
214
+dir bnl
215
+233742 tcnpvqc.tdr
216
+$ cd bnl
217
+$ ls
218
+243223 fcqrwd
219
+$ cd ..
220
+$ cd ..
221
+$ cd ..
222
+$ cd tlr
223
+$ ls
224
+dir vcsngm
225
+dir wndmt
226
+$ cd vcsngm
227
+$ ls
228
+36434 czs.dnv
229
+$ cd ..
230
+$ cd wndmt
231
+$ ls
232
+dir fvmtfcqd
233
+dir nvdb
234
+dir nwqqgl
235
+dir sbspgnpm
236
+$ cd fvmtfcqd
237
+$ ls
238
+237025 nzttjt.rzh
239
+$ cd ..
240
+$ cd nvdb
241
+$ ls
242
+235328 dnrqwqtp.vfc
243
+51984 rhblt.mfz
244
+51332 rjhvhw
245
+$ cd ..
246
+$ cd nwqqgl
247
+$ ls
248
+203534 cjghw
249
+dir cljbrh
250
+$ cd cljbrh
251
+$ ls
252
+133820 lmw.dnd
253
+$ cd ..
254
+$ cd ..
255
+$ cd sbspgnpm
256
+$ ls
257
+270010 hjpmqrq
258
+$ cd ..
259
+$ cd ..
260
+$ cd ..
261
+$ cd twpq
262
+$ ls
263
+dir dmpsnhdh
264
+dir hbchdjjp
265
+247649 hjpmqrq
266
+29891 rrqjwpm
267
+72407 twpq.crb
268
+$ cd dmpsnhdh
269
+$ ls
270
+251490 dncdssn.hdr
271
+222231 hjpmqrq
272
+102058 pbb.zln
273
+$ cd ..
274
+$ cd hbchdjjp
275
+$ ls
276
+70058 dprrmd.qcd
277
+230958 tsdbl.bnq
278
+$ cd ..
279
+$ cd ..
280
+$ cd wbgcsw
281
+$ ls
282
+292028 bhtfcf
283
+dir bnl
284
+dir bqq
285
+dir ctnlpgt
286
+247888 hblhfvwj
287
+dir hbqm
288
+277949 pbb.zln
289
+106225 rrqjwpm
290
+125927 ssqpmlfb.gwm
291
+dir zqmjwsgz
292
+dir zwwphs
293
+$ cd bnl
294
+$ ls
295
+dir fbbr
296
+240500 frqqdsr.hbf
297
+dir mzfrdl
298
+25137 srqlww.mcj
299
+dir tqgrdz
300
+dir ztrnq
301
+$ cd fbbr
302
+$ ls
303
+84414 mjbw.dhs
304
+$ cd ..
305
+$ cd mzfrdl
306
+$ ls
307
+135647 bccwgn
308
+dir cjdptqgh
309
+dir hwdnrqns
310
+dir prq
311
+$ cd cjdptqgh
312
+$ ls
313
+147946 mdgl.drz
314
+11972 pbb.zln
315
+$ cd ..
316
+$ cd hwdnrqns
317
+$ ls
318
+dir dmpsnhdh
319
+$ cd dmpsnhdh
320
+$ ls
321
+254432 phthmn
322
+$ cd ..
323
+$ cd ..
324
+$ cd prq
325
+$ ls
326
+75827 dmpsnhdh.rtl
327
+$ cd ..
328
+$ cd ..
329
+$ cd tqgrdz
330
+$ ls
331
+251475 bjwnll.rlw
332
+$ cd ..
333
+$ cd ztrnq
334
+$ ls
335
+208497 bnl.dtr
336
+179376 gqnbswcj.hht
337
+$ cd ..
338
+$ cd ..
339
+$ cd bqq
340
+$ ls
341
+202201 bnl.lbm
342
+$ cd ..
343
+$ cd ctnlpgt
344
+$ ls
345
+269484 vsfvzrpr
346
+$ cd ..
347
+$ cd hbqm
348
+$ ls
349
+74455 bvnfz
350
+42748 pbb.zln
351
+$ cd ..
352
+$ cd zqmjwsgz
353
+$ ls
354
+146194 pbb.zln
355
+$ cd ..
356
+$ cd zwwphs
357
+$ ls
358
+209587 mtbzd.nwb
359
+$ cd ..
360
+$ cd ..
361
+$ cd zjmldjdh
362
+$ ls
363
+dir cdq
364
+dir mdclfbs
365
+dir tfc
366
+132043 wrm
367
+$ cd cdq
368
+$ ls
369
+289173 twpq.mrn
370
+$ cd ..
371
+$ cd mdclfbs
372
+$ ls
373
+64639 bnl.jwf
374
+dir hpdgt
375
+72868 hznfj.nmj
376
+159467 lmw.bfz
377
+$ cd hpdgt
378
+$ ls
379
+52760 fcqrwd
380
+54661 tzgt.hvh
381
+$ cd ..
382
+$ cd ..
383
+$ cd tfc
384
+$ ls
385
+185481 bwntlh
386
+18925 fcqrwd
387
+$ cd ..
388
+$ cd ..
389
+$ cd ..
390
+$ cd ldztz
391
+$ ls
392
+128430 bwz.fcz
393
+dir dmpsnhdh
394
+dir lbqgz
395
+dir znrnj
396
+$ cd dmpsnhdh
397
+$ ls
398
+238193 dncdssn.hdr
399
+285939 hwfngq.dpw
400
+$ cd ..
401
+$ cd lbqgz
402
+$ ls
403
+171931 vgrp
404
+$ cd ..
405
+$ cd znrnj
406
+$ ls
407
+153738 vmwwbjqd
408
+$ cd ..
409
+$ cd ..
410
+$ cd lmmw
411
+$ ls
412
+dir bqqnsfdj
413
+163303 fcqrwd
414
+43453 frqqdsr.hbf
415
+33319 hjpmqrq
416
+dir rlpcqtzg
417
+$ cd bqqnsfdj
418
+$ ls
419
+dir bnl
420
+2251 hjpmqrq
421
+14707 rrqjwpm
422
+dir tlnbvhdl
423
+$ cd bnl
424
+$ ls
425
+33357 bnl.fqp
426
+151237 bnl.vbs
427
+40294 dmpsnhdh.hwz
428
+76455 dncdssn.hdr
429
+290341 hjpmqrq
430
+dir lmw
431
+dir nqw
432
+$ cd lmw
433
+$ ls
434
+dir sfj
435
+$ cd sfj
436
+$ ls
437
+156532 fcqrwd
438
+$ cd ..
439
+$ cd ..
440
+$ cd nqw
441
+$ ls
442
+59928 dncdssn.hdr
443
+$ cd ..
444
+$ cd ..
445
+$ cd tlnbvhdl
446
+$ ls
447
+183301 hjpmqrq
448
+$ cd ..
449
+$ cd ..
450
+$ cd rlpcqtzg
451
+$ ls
452
+258638 dqt.mlc
453
+$ cd ..
454
+$ cd ..
455
+$ cd wthvqw
456
+$ ls
457
+224501 pbb.zln
458
+$ cd ..
459
+$ cd zpdnprb
460
+$ ls
461
+dir bnl
462
+dir ffg
463
+dir jljlwpsv
464
+212081 lrzc.lhj
465
+dir rrqjwpm
466
+dir twpq
467
+dir vlgsrtm
468
+$ cd bnl
469
+$ ls
470
+124009 hjgjf
471
+74860 hjpmqrq
472
+84996 lrdl.swf
473
+dir pnzmp
474
+$ cd pnzmp
475
+$ ls
476
+dir btbtlrs
477
+128636 nfzf
478
+$ cd btbtlrs
479
+$ ls
480
+107651 hhzbwd.wzj
481
+$ cd ..
482
+$ cd ..
483
+$ cd ..
484
+$ cd ffg
485
+$ ls
486
+57918 jwzbs.tnt
487
+$ cd ..
488
+$ cd jljlwpsv
489
+$ ls
490
+188175 dmpsnhdh.nnb
491
+46693 fcqrwd
492
+111557 pbb.zln
493
+$ cd ..
494
+$ cd rrqjwpm
495
+$ ls
496
+dir bftw
497
+dir ccsfws
498
+87225 mccw
499
+290654 pbb.zln
500
+147394 twzqc.pbz
501
+52983 wsvgf
502
+dir wwfgbzqh
503
+$ cd bftw
504
+$ ls
505
+dir brl
506
+167154 crs
507
+dir lmw
508
+dir rrqjwpm
509
+dir twpq
510
+174963 twpq.wjl
511
+dir vnfhb
512
+dir wcldzp
513
+$ cd brl
514
+$ ls
515
+297937 wspcnp
516
+$ cd ..
517
+$ cd lmw
518
+$ ls
519
+166695 mcjql.jrv
520
+$ cd ..
521
+$ cd rrqjwpm
522
+$ ls
523
+198762 mwn
524
+$ cd ..
525
+$ cd twpq
526
+$ ls
527
+141835 jlwf.hcd
528
+$ cd ..
529
+$ cd vnfhb
530
+$ ls
531
+128626 tvmwhq.wfn
532
+$ cd ..
533
+$ cd wcldzp
534
+$ ls
535
+dir ncq
536
+dir twpq
537
+$ cd ncq
538
+$ ls
539
+dir wrtw
540
+$ cd wrtw
541
+$ ls
542
+133331 fcqrwd
543
+$ cd ..
544
+$ cd ..
545
+$ cd twpq
546
+$ ls
547
+151811 fcqrwd
548
+$ cd ..
549
+$ cd ..
550
+$ cd ..
551
+$ cd ccsfws
552
+$ ls
553
+100548 twpq.ppm
554
+$ cd ..
555
+$ cd wwfgbzqh
556
+$ ls
557
+dir lmw
558
+dir mfms
559
+dir pjbjgbcl
560
+204154 qtflzwm
561
+226500 vdmjj.htj
562
+dir wzqbwr
563
+$ cd lmw
564
+$ ls
565
+dir bgl
566
+95150 dncdssn.hdr
567
+119653 frqqdsr.hbf
568
+97941 hjpmqrq
569
+dir jqthwzj
570
+$ cd bgl
571
+$ ls
572
+dir lmw
573
+dir rrqjwpm
574
+$ cd lmw
575
+$ ls
576
+233655 wmdldvbz
577
+$ cd ..
578
+$ cd rrqjwpm
579
+$ ls
580
+242918 frqqdsr.hbf
581
+227581 hjpmqrq
582
+dir hsvnmlp
583
+dir nsch
584
+25524 pbb.zln
585
+dir qlgg
586
+dir twpq
587
+67453 twpq.fms
588
+$ cd hsvnmlp
589
+$ ls
590
+264517 pbb.zln
591
+$ cd ..
592
+$ cd nsch
593
+$ ls
594
+7898 cmsdzh
595
+233270 dmpsnhdh.bsq
596
+101256 frl
597
+133902 jzvh.vdv
598
+dir lmw
599
+dir sgjsg
600
+130245 wcftvft
601
+$ cd lmw
602
+$ ls
603
+69572 bnjnc.csp
604
+$ cd ..
605
+$ cd sgjsg
606
+$ ls
607
+38856 tnzpz.tbq
608
+$ cd ..
609
+$ cd ..
610
+$ cd qlgg
611
+$ ls
612
+276013 frbstg.pzb
613
+$ cd ..
614
+$ cd twpq
615
+$ ls
616
+136454 fhwz.bqb
617
+94099 rglp
618
+114026 tsrt.cbd
619
+26252 zhclpzm.rqf
620
+$ cd ..
621
+$ cd ..
622
+$ cd ..
623
+$ cd jqthwzj
624
+$ ls
625
+128200 lmw.btl
626
+$ cd ..
627
+$ cd ..
628
+$ cd mfms
629
+$ ls
630
+274935 dmpsnhdh
631
+76547 lchwq.dsd
632
+215701 pbb.zln
633
+dir rmwtvjt
634
+$ cd rmwtvjt
635
+$ ls
636
+74490 hjpmqrq
637
+$ cd ..
638
+$ cd ..
639
+$ cd pjbjgbcl
640
+$ ls
641
+231757 cjcpwwc.wbf
642
+dir cswvftzs
643
+dir jtvtg
644
+dir lmw
645
+dir tnctbjr
646
+dir tqsrfhdr
647
+$ cd cswvftzs
648
+$ ls
649
+dir dchqnbns
650
+dir smf
651
+$ cd dchqnbns
652
+$ ls
653
+94111 szl.hqs
654
+$ cd ..
655
+$ cd smf
656
+$ ls
657
+dir dlnsgvl
658
+dir zglt
659
+$ cd dlnsgvl
660
+$ ls
661
+dir dsz
662
+$ cd dsz
663
+$ ls
664
+156473 hjpmqrq
665
+$ cd ..
666
+$ cd ..
667
+$ cd zglt
668
+$ ls
669
+295383 frgg.sdp
670
+$ cd ..
671
+$ cd ..
672
+$ cd ..
673
+$ cd jtvtg
674
+$ ls
675
+202254 bftv.rqb
676
+58419 lmw
677
+$ cd ..
678
+$ cd lmw
679
+$ ls
680
+8097 fcqrwd
681
+$ cd ..
682
+$ cd tnctbjr
683
+$ ls
684
+250830 frqqdsr.hbf
685
+dir gzrcqr
686
+$ cd gzrcqr
687
+$ ls
688
+dir fnzgsnv
689
+$ cd fnzgsnv
690
+$ ls
691
+117215 hjpmqrq
692
+$ cd ..
693
+$ cd ..
694
+$ cd ..
695
+$ cd tqsrfhdr
696
+$ ls
697
+96381 lmw
698
+$ cd ..
699
+$ cd ..
700
+$ cd wzqbwr
701
+$ ls
702
+149066 dmpsnhdh.vnd
703
+dir dpbcgfdr
704
+dir swp
705
+14495 twpq.gsb
706
+dir zhj
707
+$ cd dpbcgfdr
708
+$ ls
709
+12909 dmpsnhdh
710
+dir jvn
711
+173491 mnhpr.lpr
712
+222018 rfqfjmd.jqq
713
+205077 wbbdrpr.hzj
714
+dir wzpbbbhm
715
+$ cd jvn
716
+$ ls
717
+117656 vqddrqlq.nfd
718
+233109 vqqvh.swz
719
+$ cd ..
720
+$ cd wzpbbbhm
721
+$ ls
722
+143534 pbb.zln
723
+$ cd ..
724
+$ cd ..
725
+$ cd swp
726
+$ ls
727
+131295 pbb.zln
728
+$ cd ..
729
+$ cd zhj
730
+$ ls
731
+166268 pbb.zln
732
+33734 rrqjwpm.blg
733
+$ cd ..
734
+$ cd ..
735
+$ cd ..
736
+$ cd ..
737
+$ cd twpq
738
+$ ls
739
+dir bppvlwqs
740
+dir bvh
741
+dir rmcdr
742
+dir tdn
743
+2230 whb.lfb
744
+dir wwtwnvh
745
+$ cd bppvlwqs
746
+$ ls
747
+195026 hvlhgsw
748
+279259 rrqjwpm
749
+$ cd ..
750
+$ cd bvh
751
+$ ls
752
+dir lmw
753
+$ cd lmw
754
+$ ls
755
+66958 pdqnd
756
+$ cd ..
757
+$ cd ..
758
+$ cd rmcdr
759
+$ ls
760
+dir dmpsnhdh
761
+182930 grj
762
+dir pmrdhrth
763
+119725 qpcqclqh
764
+77890 sjgfjz
765
+142855 twpq
766
+dir zbmcrvbh
767
+$ cd dmpsnhdh
768
+$ ls
769
+dir rrqjwpm
770
+188474 zgjzpbl.vgv
771
+$ cd rrqjwpm
772
+$ ls
773
+dir bnl
774
+dir lmw
775
+7598 vsntvs.pdv
776
+$ cd bnl
777
+$ ls
778
+245600 lmw.mgf
779
+$ cd ..
780
+$ cd lmw
781
+$ ls
782
+73396 hjpmqrq
783
+$ cd ..
784
+$ cd ..
785
+$ cd ..
786
+$ cd pmrdhrth
787
+$ ls
788
+173155 rrqjwpm.pjw
789
+178530 smgpzs.qtj
790
+$ cd ..
791
+$ cd zbmcrvbh
792
+$ ls
793
+124201 fcqrwd
794
+135578 hjpmqrq
795
+54356 hnztplsp.qlh
796
+dir lmw
797
+58350 pbb.zln
798
+dir qfrvdm
799
+dir rcg
800
+15267 rwbzjpt.djn
801
+$ cd lmw
802
+$ ls
803
+dir bbbll
804
+28362 bfgfwlf.wvg
805
+229637 dmpndms.fln
806
+146121 dncdssn.hdr
807
+131039 frqqdsr.hbf
808
+152805 hjpmqrq
809
+dir mlz
810
+$ cd bbbll
811
+$ ls
812
+169940 dncdssn.hdr
813
+216888 pbb.zln
814
+248369 tjpmlr.vmf
815
+$ cd ..
816
+$ cd mlz
817
+$ ls
818
+115167 bhfv.fts
819
+$ cd ..
820
+$ cd ..
821
+$ cd qfrvdm
822
+$ ls
823
+284564 pbb.zln
824
+$ cd ..
825
+$ cd rcg
826
+$ ls
827
+dir sqzjz
828
+$ cd sqzjz
829
+$ ls
830
+116435 jrstpcpl.zsq
831
+$ cd ..
832
+$ cd ..
833
+$ cd ..
834
+$ cd ..
835
+$ cd tdn
836
+$ ls
837
+143971 cmg
838
+133317 fcqrwd
839
+dir pstpclp
840
+133161 tddv
841
+$ cd pstpclp
842
+$ ls
843
+267351 hjpmqrq
844
+86930 rrqjwpm.dvl
845
+$ cd ..
846
+$ cd ..
847
+$ cd wwtwnvh
848
+$ ls
849
+256584 frqqdsr.hbf
850
+114564 twpq.wrd
851
+$ cd ..
852
+$ cd ..
853
+$ cd vlgsrtm
854
+$ ls
855
+148089 bnl.jzj
856
+119796 cjfphsfw.hnd
857
+197668 cpw
858
+dir dmpsnhdh
859
+dir fjsglr
860
+dir lmw
861
+dir lqgrft
862
+9231 pltdltrs
863
+dir rmdp
864
+109777 rncfff.fll
865
+dir vgjzqjpq
866
+dir ztnqnfnq
867
+$ cd dmpsnhdh
868
+$ ls
869
+dir lltnrdtv
870
+dir scthsg
871
+$ cd lltnrdtv
872
+$ ls
873
+179511 pcvmpz
874
+90913 tbr
875
+$ cd ..
876
+$ cd scthsg
877
+$ ls
878
+dir rrqjwpm
879
+$ cd rrqjwpm
880
+$ ls
881
+188629 fcqrwd
882
+$ cd ..
883
+$ cd ..
884
+$ cd ..
885
+$ cd fjsglr
886
+$ ls
887
+139754 fcqrwd
888
+dir pnsjwfzc
889
+$ cd pnsjwfzc
890
+$ ls
891
+113848 lmw
892
+$ cd ..
893
+$ cd ..
894
+$ cd lmw
895
+$ ls
896
+54999 dmpsnhdh
897
+dir ffhcf
898
+251476 frqqdsr.hbf
899
+dir jpgqspqw
900
+198972 nhfclq.pbh
901
+180380 nqmjnvc.fvr
902
+dir pfsjwmbc
903
+213768 rcvccgcd
904
+$ cd ffhcf
905
+$ ls
906
+40478 svmwstq.sjj
907
+$ cd ..
908
+$ cd jpgqspqw
909
+$ ls
910
+22181 hjpmqrq
911
+$ cd ..
912
+$ cd pfsjwmbc
913
+$ ls
914
+dir bcvchw
915
+$ cd bcvchw
916
+$ ls
917
+225892 bnl.nwc
918
+$ cd ..
919
+$ cd ..
920
+$ cd ..
921
+$ cd lqgrft
922
+$ ls
923
+dir rrqjwpm
924
+dir twpq
925
+$ cd rrqjwpm
926
+$ ls
927
+54786 fcqrwd
928
+3053 tthhqjm.ntd
929
+$ cd ..
930
+$ cd twpq
931
+$ ls
932
+109355 bnl
933
+dir lmw
934
+dir mhgqt
935
+301291 rrqjwpm.lrm
936
+271233 twpq.srp
937
+$ cd lmw
938
+$ ls
939
+dir lmw
940
+dir lngbszqm
941
+$ cd lmw
942
+$ ls
943
+139640 pbb.zln
944
+$ cd ..
945
+$ cd lngbszqm
946
+$ ls
947
+98279 mqvq.gsj
948
+283599 rvjd.dvt
949
+$ cd ..
950
+$ cd ..
951
+$ cd mhgqt
952
+$ ls
953
+208165 fcqrwd
954
+$ cd ..
955
+$ cd ..
956
+$ cd ..
957
+$ cd rmdp
958
+$ ls
959
+dir bqn
960
+170956 fcqrwd
961
+90954 snnttp.gld
962
+$ cd bqn
963
+$ ls
964
+75628 hdrgbrpc
965
+$ cd ..
966
+$ cd ..
967
+$ cd vgjzqjpq
968
+$ ls
969
+dir bnl
970
+dir wpfw
971
+$ cd bnl
972
+$ ls
973
+25911 pbb.zln
974
+$ cd ..
975
+$ cd wpfw
976
+$ ls
977
+247784 bzll.ltc
978
+$ cd ..
979
+$ cd ..
980
+$ cd ztnqnfnq
981
+$ ls
982
+dir dtpzsrfc
983
+214055 srgzhp.nlr
984
+$ cd dtpzsrfc
985
+$ ls
986
+142652 bhgwj

+ 97
- 0
src/Day7.hs Voir le fichier

@@ -0,0 +1,97 @@
1
+module Day7 (
2
+  buildTree,
3
+  buildTree',
4
+  mkdir,
5
+  Directory (..),
6
+  calculateSize,
7
+  filterDirectories,
8
+  sumUp,
9
+  day7
10
+) where
11
+
12
+import Day7.Parser
13
+import Data.Map (Map)
14
+import qualified Data.Map as Map
15
+
16
+data Directory = Directory
17
+  { sub :: Map String Directory
18
+  , files :: Map String Int
19
+  , isRoot :: Bool
20
+} deriving (Eq)
21
+
22
+
23
+instance Show Directory where
24
+  show d = show (files d) ++ " - " ++ show (sub d) ++ "\n"
25
+
26
+mkdir :: Directory
27
+mkdir = Directory { sub = Map.empty, files = Map.empty, isRoot = False }
28
+
29
+rootDirectory :: Directory
30
+rootDirectory = Directory { sub = Map.empty, files = Map.empty, isRoot = True  }
31
+
32
+buildTree :: [TerminalCommand] -> Directory
33
+buildTree commands = fst (buildTree' rootDirectory commands)
34
+
35
+buildTree' :: Directory -> [TerminalCommand] -> (Directory, [TerminalCommand])
36
+buildTree'  dir [] = (dir, [])
37
+buildTree' dir (command:cs) =  case command of
38
+  Listing entries ->
39
+    let asFiles = Map.fromList $ toFiles entries
40
+        newDir = dir { files = asFiles }
41
+    in buildTree' newDir cs
42
+  In dirName ->
43
+   let subFolders = sub dir
44
+       selectedDir = Map.findWithDefault mkdir  dirName subFolders
45
+       (replacement, rest) = buildTree' selectedDir cs
46
+       updatedSub = Map.insert dirName replacement subFolders
47
+   in buildTree' dir { sub = updatedSub} rest
48
+  Out -> (dir, cs)
49
+  Root -> if isRoot dir
50
+            then buildTree' dir cs
51
+            else (dir, command:cs)
52
+
53
+toFiles :: [ListingEntry] -> [(String, Int)]
54
+toFiles ((FileListing name size):rest) = (name, size): toFiles rest
55
+toFiles (_:rest) = toFiles rest
56
+toFiles [] = []
57
+
58
+sizeThreshold :: Int
59
+sizeThreshold = 100000
60
+
61
+calculateSize :: Directory -> Int
62
+calculateSize dir = sum (Map.elems (files dir)) + sum sizes
63
+  where
64
+    subFolders = Map.elems (sub dir)
65
+    sizes = map calculateSize subFolders
66
+
67
+flatten :: Directory -> [(String, Directory)]
68
+flatten dir = flatten' ("/", dir)
69
+  where
70
+  flatten' (name, d) = (name, d) : concatMap flatten' (Map.toList (sub d))
71
+
72
+sizeOfDirectories :: Directory -> [(String, Int)]
73
+sizeOfDirectories dir = map withSize allDirectories
74
+  where
75
+    allDirectories = flatten dir
76
+    withSize (name, directory) = (name, calculateSize directory)
77
+
78
+filterDirectories :: (Int -> Bool) -> Directory -> [(String, Int)]
79
+filterDirectories predicate dir = filter (\(_, size) -> predicate size) $ sizeOfDirectories dir
80
+
81
+sumUp :: [(String, Int)] -> Int
82
+sumUp = sum . map snd
83
+
84
+
85
+forceRight :: Either a b -> b
86
+forceRight (Left _) = error "forced Right but got Left"
87
+forceRight (Right b) = b
88
+
89
+day7 :: IO ()
90
+day7 = do
91
+   input <- readFile "ressources/day07-input"
92
+   putStrLn "Day7"
93
+   let parsed = forceRight $ parseTerminalLines input
94
+   let tree = buildTree parsed
95
+   let filtered = filterDirectories (<= 100000) tree
96
+   let summed = sumUp filtered
97
+   putStrLn ("Sum of those directories is " ++ show summed)

+ 68
- 0
src/Day7/Parser.hs Voir le fichier

@@ -0,0 +1,68 @@
1
+module Day7.Parser (
2
+  parseTerminalLines,
3
+  TerminalCommand (..),
4
+  ListingEntry (..),
5
+  DirName,
6
+  FileName,
7
+  Size
8
+) where
9
+
10
+import Text.ParserCombinators.Parsec
11
+
12
+type DirName = String
13
+type FileName = String
14
+type Size = Int
15
+
16
+data ListingEntry = DirListing DirName | FileListing FileName Size
17
+  deriving (Show, Eq)
18
+
19
+data TerminalCommand = In DirName | Out | Root | Listing [ListingEntry]
20
+ deriving (Show, Eq)
21
+ 
22
+parseTerminalLines :: String -> Either ParseError  [TerminalCommand]
23
+parseTerminalLines = parse terminal "(error)"
24
+
25
+terminal :: GenParser Char st [TerminalCommand]
26
+terminal = many command
27
+
28
+command :: GenParser Char st TerminalCommand
29
+command = do
30
+  _ <- string "$ "
31
+  cd <|> ls
32
+
33
+ls :: GenParser Char st TerminalCommand
34
+ls = do
35
+  _ <- string "ls\n"
36
+  entries <- many listingEntry
37
+  return $ Listing entries
38
+
39
+listingEntry :: GenParser Char st ListingEntry
40
+listingEntry = dirListing <|> fileListing
41
+
42
+dirListing :: GenParser Char st ListingEntry
43
+dirListing = do
44
+  _ <- string "dir "
45
+  dirName <-  many $ noneOf "\n"
46
+  _ <- char '\n'
47
+  return $ DirListing dirName
48
+
49
+fileListing :: GenParser Char st ListingEntry
50
+fileListing = do
51
+  fileSize <- many1 digit
52
+  _ <- char ' '
53
+  fileName <- many $ noneOf "\n"
54
+  _ <- char '\n'
55
+  return $ FileListing fileName (read fileSize)
56
+
57
+cd :: GenParser Char st TerminalCommand
58
+cd = do
59
+  _ <- string "cd "
60
+  name <- many $ noneOf "\n"
61
+  _ <- char '\n'
62
+  return (nameToDir name)
63
+
64
+nameToDir :: DirName -> TerminalCommand
65
+nameToDir "/" = Root
66
+nameToDir ".." = Out
67
+nameToDir ds = In ds
68
+

+ 3
- 0
src/Lib.hs Voir le fichier

@@ -8,6 +8,7 @@ import Day3
8 8
 import Day4
9 9
 import Day5
10 10
 import Day6
11
+import Day7
11 12
 
12 13
 someFunc :: IO ()
13 14
 someFunc = do
@@ -22,5 +23,7 @@ someFunc = do
22 23
   day5
23 24
   putStrLn "-----------"
24 25
   day6
26
+  putStrLn "-----------"
27
+  day7
25 28
   
26 29
 

+ 133
- 0
test/Day7Spec.hs Voir le fichier

@@ -0,0 +1,133 @@
1
+{-# LANGUAGE QuasiQuotes #-}
2
+module Day7Spec (spec) where
3
+
4
+import Test.Hspec
5
+import Text.Heredoc
6
+
7
+import Day7
8
+import Day7.Parser
9
+import qualified Data.Map as Map
10
+
11
+inputPart1 :: String
12
+inputPart1 = [str|$ cd /
13
+                 |$ ls
14
+                 |dir a
15
+                 |14848514 b.txt
16
+                 |8504156 c.dat
17
+                 |dir d
18
+                 |$ cd a
19
+                 |$ ls
20
+                 |dir e
21
+                 |29116 f
22
+                 |2557 g
23
+                 |62596 h.lst
24
+                 |$ cd e
25
+                 |$ ls
26
+                 |584 i
27
+                 |$ cd ..
28
+                 |$ cd ..
29
+                 |$ cd d
30
+                 |$ ls
31
+                 |4060174 j
32
+                 |8033020 d.log
33
+                 |5626152 d.ext
34
+                 |7214296 k
35
+                 |]
36
+
37
+cdTerm :: [TerminalCommand]
38
+cdTerm = forceRight $ parseTerminalLines
39
+         [str|$ cd foo
40
+             |$ ls
41
+             |23 f
42
+             |34 f2
43
+             |dir d
44
+             |$ cd ..
45
+             |]
46
+
47
+cdOutTerm :: [TerminalCommand]
48
+cdOutTerm = forceRight $ parseTerminalLines
49
+         [str|$ cd foo
50
+             |$ ls
51
+             |42 f
52
+             |$ cd ..
53
+             |$ cd bar
54
+             |$ ls
55
+             |100 baba
56
+             |$ cd x
57
+             |$ cd /
58
+             |$ ls
59
+             |23 f
60
+             |dir foo
61
+             |dir bar
62
+             |]
63
+cdOutParsed :: [TerminalCommand]
64
+cdOutParsed = [
65
+    In "foo",
66
+    Listing [FileListing "f" 42],
67
+    Out,
68
+    In "bar",
69
+    Listing [FileListing "baba" 100],
70
+    In "x",
71
+    Root,
72
+    Listing [FileListing "f" 23, DirListing "foo", DirListing "bar"]
73
+  ]
74
+
75
+forceRight :: Either a b -> b
76
+forceRight (Left _) = error "forced Right but got Left"
77
+forceRight (Right b) = b
78
+
79
+spec :: Spec
80
+spec =
81
+  describe "Day7" $ do
82
+    describe "Part1" $ do
83
+      describe "parser" $ do
84
+        it "parses an ls command" $ do
85
+          parseTerminalLines "$ ls\n" `shouldBe` Right [Listing []]
86
+        it "parses a cd up command" $ do
87
+          parseTerminalLines "$ cd ..\n" `shouldBe` Right [Out]
88
+        it "parses a cd root command" $ do
89
+          parseTerminalLines "$ cd /\n" `shouldBe` Right [Root]
90
+        it "parses a cd in command" $ do
91
+          parseTerminalLines "$ cd dirname\n" `shouldBe` Right [In "dirname"]
92
+        it "parses a file listing line" $ do
93
+          parseTerminalLines "$ ls\n1234 f\n$ cd foo\n" `shouldBe` Right [Listing [FileListing "f" 1234], In "foo"]
94
+        it "parses a larger shell log" $ do
95
+          cdOutTerm `shouldBe` cdOutParsed
96
+      describe "tree" $ do
97
+        it "reads a listing" $ do
98
+          buildTree [Listing [FileListing "f" 123]] `shouldBe`
99
+           mkdir { files = Map.singleton "f" 123, isRoot = True }
100
+        it "changes dir" $ do
101
+          buildTree cdTerm `shouldBe`
102
+           mkdir {
103
+             sub = Map.fromList [
104
+               ("foo", mkdir { files = Map.fromList [("f", 23), ("f2", 34)] })
105
+             ],
106
+             files = Map.empty,
107
+             isRoot = True
108
+           }
109
+        it "changes dir outwards" $ do
110
+          buildTree cdOutTerm `shouldBe`
111
+            mkdir {
112
+               sub = Map.fromList [
113
+                 ("foo", mkdir { files = Map.fromList [("f", 42)] } ),
114
+                 ("bar", mkdir {
115
+                    sub = Map.fromList [("x", mkdir)],
116
+                    files = Map.fromList [("baba", 100)]
117
+                    } )
118
+                  ],
119
+               files = Map.fromList [("f", 23)],
120
+               isRoot = True
121
+             }
122
+        it "calculates size of directories" $ do
123
+          calculateSize mkdir { files = Map.fromList [("f", 23), ("f2", 34)] }
124
+           `shouldBe` 23 + 34
125
+          calculateSize mkdir {
126
+           sub = Map.fromList [ ("d", mkdir { files = Map.fromList [("f", 23), ("f2", 34)] })] }
127
+            `shouldBe` 23 + 34
128
+        it "sums stuff up" $ do
129
+           let parsed = forceRight $ parseTerminalLines inputPart1
130
+           let tree = buildTree parsed
131
+           let filtered = filterDirectories (<= 100000) tree
132
+           let summed = sumUp filtered
133
+           summed `shouldBe` 95437

Chargement…
Annuler
Enregistrer