Commit fb79b0c5 authored by Neil Smith's avatar Neil Smith
Browse files

Done day 9

parent 90918214
......@@ -47,13 +47,6 @@ part2 program = filter terminates $ map runProgram programs
executeMany visited machine
-- if currentIP `S.member` visited
-- then Looped (machineAcc machine)
-- else if currentIP == programSize
-- then Terminated (machineAcc machine)
-- else if currentIP > programSize
-- then OutOfBounds (machineAcc machine) currentIP
-- else executeMany visited' machine'
| currentIP `S.member` visited = Looped (machineAcc machine)
| currentIP == programSize = Terminated (machineAcc machine)
| currentIP > programSize = OutOfBounds (machineAcc machine) currentIP
......
# This YAML file describes your package. Stack will automatically generate a
# Cabal file when you run `stack build`. See the hpack website for help with
# this file: <https://github.com/sol/hpack>.
name: advent09
synopsis: Advent of Code
version: '0.0.1'
default-extensions:
- AllowAmbiguousTypes
- ApplicativeDo
- BangPatterns
- BlockArguments
- DataKinds
- DeriveFoldable
- DeriveFunctor
- DeriveGeneric
- DeriveTraversable
- EmptyCase
- FlexibleContexts
- FlexibleInstances
- FunctionalDependencies
- GADTs
- GeneralizedNewtypeDeriving
- ImplicitParams
- KindSignatures
- LambdaCase
- MonadComprehensions
- MonoLocalBinds
- MultiParamTypeClasses
- MultiWayIf
- NamedFieldPuns
- NegativeLiterals
- NumDecimals
# - OverloadedLists
- OverloadedStrings
- PartialTypeSignatures
- PatternGuards
- PatternSynonyms
- PolyKinds
- RankNTypes
- RecordWildCards
- ScopedTypeVariables
- TemplateHaskell
- TransformListComp
- TupleSections
- TypeApplications
- TypeFamilies
- TypeInType
- TypeOperators
- ViewPatterns
executables:
advent09:
main: advent09.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
-- import Debug.Trace
import Data.List
main :: IO ()
main =
do numStrs <- readFile "data/advent09.txt"
let nums = map (read @Int) $ lines numStrs
let firstInvalid = part1 nums
print firstInvalid
print $ part2 firstInvalid nums
-- print $ head $ part2 nums
part1 nums = fst $ head $ filter (not . valid) $ slidingWindow 25 nums
slidingWindow winSize nums =
zip (drop winSize nums) $ map (take winSize) $ tails nums
valid (target, window) = not $ null [(x, y) | x <- window, y <- window, x + y == target]
part2 target nums = (maximum section) + (minimum section)
where section = head $ filter (sumsToTarget target) $ subStrings nums
-- subStrings :: [a] -> [[a]]
subStrings = (concatMap inits) . tails
-- sumsToTarget :: Int -> [Int] -> Bool
sumsToTarget target ns = sum ns == target
11
1
33
5
20
31
49
26
3
35
25
39
29
28
23
44
19
4
21
2
7
41
10
32
14
68
6
11
8
18
27
20
71
13
9
16
73
52
34
25
79
15
12
17
113
94
19
24
21
22
114
31
23
26
28
29
32
47
27
39
33
80
35
38
34
36
127
40
41
227
42
43
45
44
48
54
70
71
65
55
110
60
72
74
114
77
69
90
75
187
78
100
81
83
85
86
118
93
92
102
115
120
127
175
129
160
132
144
169
147
252
150
153
156
179
178
164
166
168
185
204
208
222
194
284
235
276
256
261
273
330
297
291
447
300
303
306
378
320
332
334
374
351
485
379
398
455
478
634
491
647
517
553
896
977
750
588
591
606
603
623
626
652
654
685
829
725
1132
777
1084
946
969
995
1008
1120
1123
1070
1707
1722
1431
1273
1179
1217
1876
1857
1249
1337
1306
1339
1502
1554
1671
1785
2625
1964
2319
2039
2901
2078
2190
3215
2886
2704
2396
4375
3149
2428
2466
2555
2588
2645
2643
2808
2841
3456
3225
4358
3749
4951
4229
4435
4117
4778
5363
5681
4824
5100
7333
5274
12433
5016
5288
5021
6304
7664
5451
5484
7759
9386
6681
9138
7978
7866
8552
8664
10500
8895
9602
9840
14426
9845
10037
19175
10290
14346
11697
10309
10472
10935
12165
12132
14036
19447
14547
17818
15844
16418
28111
22202
27420
18497
18735
19442
20154
26134
19882
20327
24837
20762
24655
20781
21244
27353
45850
24297
44991
28583
32262
34429
34236
46016
35153
38617
37232
39741
37939
38177
39596
40036
59183
66612
41089
45078
41543
49827
78278
63736
68665
78112
75849
73574
79482
66498
108406
69389
119821
72385
78321
75171
81579
77535
98779
79632
85114
82632
122613
116260
86621
133125
147501
156390
130234
177795
135887
138883
151109
144560
141669
159006
141774
147556
157167
200934
152706
183893
209866
167746
212866
219746
228395
268966
202881
481832
410635
283443
266121
269117
274770
280447
354535
286229
344655
289225
535238
637978
300262
1048613
320452
907095
351639
370627
436712
415747
644917
431276
469002
492106
540891
543887
549564
751728
786374
736974
846641
836761
575454
589487
609677
1015544
875716
620714
946081
672091
1507650
722266
801903
847023
1566795
900278
1292805
961108
1032997
1090455
1093451
1125018
1583997
1164941
1210201
1185131
1196168
1199164
2085917
1774618
1342980
1714165
1868259
1705088
2540350
2025296
1524169
1834900
2783161
1861386
1933275
2433435
1994105
2928351
2183906
2350072
2324182
4307330
2553181
4639098
3575551
6332626
3392428
3048068
2867149
4716436
3229257
3359069
3549465
3385555
5733371
3457444
3696286
3794661
3855491
5579329
6633203
4178011
4674254
5398140
6778722
6961106
5420330
5601249
5915217
6096406
6252704
8782366
6226218
6563435
6588326
6614812
7081841
6842999
7972672
7635455
7153730
13411925
7650152
8033502
8852265
11262580
9576151
10072394
13932452
11021579
11335547
11516466
11697655
12011623
13696653
19803945
12789653
12841030
13151761
13431325
17950359
13924840
13996729
15126402
19166618
31501600
17226303
15683654
19731157
21588860
43513223
23997234
21093973
22719234
28247882
23033202
25513195
24487308
30922956
25630683
25941414
35513700
21806024
37272514
27356165
32909957
75014823
29123131
45091207
34850272
41713611
35414811
76014163
40825130
42682833
42899997
43813207
44525258
52156333
44839226
46293332
60928006
66766544
47436707
47747438
89364484
49162189
69553462
104039058
60266122
73648389
70836742
63973403
70265083
127694550
112422455
76239941
95455521
83507963
87208091