Done several tasks
authorNeil Smith <NeilNjae@users.noreply.github.com>
Thu, 23 Nov 2023 15:46:54 +0000 (15:46 +0000)
committerNeil Smith <NeilNjae@users.noreply.github.com>
Thu, 23 Nov 2023 15:46:54 +0000 (15:46 +0000)
12 files changed:
app/cses1622.hs [new file with mode: 0644]
app/cses1623.hs [new file with mode: 0644]
app/cses1624.hs [new file with mode: 0644]
app/cses1755.hs [new file with mode: 0644]
app/cses2165.hs [new file with mode: 0644]
app/cses2205.hs [new file with mode: 0644]
app/cses2431.hs [new file with mode: 0644]
cses-programming-tasks.cabal
data/cses1623.txt [new file with mode: 0644]
data/cses1624.txt [new file with mode: 0644]
data/cses2431.txt [new file with mode: 0644]
data/cses2431a.txt [new file with mode: 0644]

diff --git a/app/cses1622.hs b/app/cses1622.hs
new file mode 100644 (file)
index 0000000..8925b05
--- /dev/null
@@ -0,0 +1,22 @@
+import Data.List (sort, inits, tails, nub)
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let base = sort line1
+  let perms = permutations base
+  print $ length perms
+  putStrLn $ unlines perms
+
+permutations :: String -> [String]
+
+permutations [] = []
+permutations (x:[]) = [[x]]
+permutations xs = concatMap (\(y, ys) -> map (y:) (permutations ys)) (selects xs)
+
+selects :: String -> [(Char, String)]
+selects [] = []
+selects xs = nub [(head t, h ++ (tail t))
+                 | (h, t) <- zip (inits xs) (tails xs)
+                 , not (null t)]
+
diff --git a/app/cses1623.hs b/app/cses1623.hs
new file mode 100644 (file)
index 0000000..7121018
--- /dev/null
@@ -0,0 +1,18 @@
+import Data.List (sort)
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  line2 <- getLine
+  let nums = reverse $ sort $ map read $ words line2 :: [Int]
+  print $ solve nums
+
+
+solve (x:xs) = minimum diffs
+  where s = x + sum xs
+        ys = powerSet xs
+        diffs = [abs (s - 2 * (sum y)) | y <- ys]
+
+powerSet :: [a] -> [[a]]
+powerSet [] = [[]]
+powerSet (x:xs) = powerSet xs ++ map (x:) (powerSet xs)
diff --git a/app/cses1624.hs b/app/cses1624.hs
new file mode 100644 (file)
index 0000000..d984ffe
--- /dev/null
@@ -0,0 +1,38 @@
+import Data.List
+import qualified Data.Set as S
+import Control.Monad
+
+data Queen = Queen Int Int -- row, column
+  deriving (Show, Eq, Ord)
+
+main :: IO ()
+main = do
+  text <- getContents
+  let grid = mkGrid text
+  -- print grid
+  let solutions = solve grid
+  print $ length solutions
+
+solve :: S.Set Queen -> [S.Set Queen]
+solve forbiddens =
+  do cols <- permutations [0..7]
+     let queens = S.fromList [uncurry Queen p | p <- zip [0..7] cols]
+     guard $ S.null $ S.intersection queens forbiddens
+     let posDiagonals = S.map posDiagonalOf queens
+     let negDiagonals = S.map negDiagonalOf queens
+     guard $ S.size posDiagonals == 8
+     guard $ S.size negDiagonals == 8
+     return queens
+
+posDiagonalOf, negDiagonalOf :: Queen -> Int
+posDiagonalOf (Queen r c) = c - r
+negDiagonalOf (Queen r c) = c + r
+
+mkGrid :: String -> S.Set Queen
+mkGrid text = S.fromList forbiddens
+  where rows = lines text
+        r = length rows - 1
+        c = (length $ head rows) - 1
+        forbiddens = [Queen i j | i <- [0..r], j <- [0..c], rows !! i !! j == '*' ]
+
+
diff --git a/app/cses1755.hs b/app/cses1755.hs
new file mode 100644 (file)
index 0000000..ff5d217
--- /dev/null
@@ -0,0 +1,26 @@
+import qualified Data.Map.Strict as M
+import Data.Map.Strict ((!))
+
+type LetterCount = M.Map Char Int
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let counts = countLetters line1
+  if palindromable counts
+    then putStrLn $ showPalindrome counts
+    else putStrLn "NO SOLUTION"
+
+countLetters :: String -> LetterCount
+countLetters = foldr (\c m -> M.insertWith (+) c 1 m) M.empty  
+
+palindromable :: LetterCount -> Bool
+palindromable = (<= 1) . M.size . M.filter odd
+
+showPalindrome :: LetterCount -> String
+showPalindrome counts = showStart ++ showMid ++ showEnd
+  where 
+    (odds, evens) = M.partition odd counts
+    showStart = mconcat [replicate (n `div` 2) l | (l, n) <- M.toAscList evens]
+    showMid = mconcat [replicate n l | (l, n) <- M.toAscList odds]
+    showEnd = mconcat [replicate (n `div` 2) l | (l, n) <- M.toDescList evens]
diff --git a/app/cses2165.hs b/app/cses2165.hs
new file mode 100644 (file)
index 0000000..1b6c71b
--- /dev/null
@@ -0,0 +1,17 @@
+data Move = Move Int Int
+  -- deriving (Show)
+
+instance Show Move where
+  show (Move a b) = show a ++ " " ++ show b
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let nDisks = read line1 :: Int
+  let moves = solve nDisks 1 2 3
+  print $ length moves
+  putStrLn $ unlines $ map show moves
+
+solve :: Int -> Int -> Int -> Int -> [Move]
+solve 0 _ _ _ = []
+solve n a b c = solve (n-1) a c b ++ [Move a c] ++ solve (n-1) b a c
diff --git a/app/cses2205.hs b/app/cses2205.hs
new file mode 100644 (file)
index 0000000..e9fa3d5
--- /dev/null
@@ -0,0 +1,20 @@
+import Data.Word
+import Data.Bits
+import Numeric (showIntAtBase)
+import Data.Char (intToDigit)
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let nBits = read line1 :: Int
+  let n = fromIntegral $ 2 ^ nBits
+  let codes = [toGray x | x <- [0..(n - 1)]]
+  putStrLn $ unlines $ map (showBits nBits) codes
+  
+toGray :: Word16 -> Word16
+toGray n = n `xor` (n `shiftR` 1)
+
+showBits :: Int -> Word16 -> String
+showBits k n = prefix ++ suffix
+  where suffix = showIntAtBase 2 intToDigit n ""
+        prefix = replicate (k - length suffix) '0'
\ No newline at end of file
diff --git a/app/cses2431.hs b/app/cses2431.hs
new file mode 100644 (file)
index 0000000..bebaf9d
--- /dev/null
@@ -0,0 +1,52 @@
+import Data.List
+import Control.Monad
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let numQuestions = read line1 :: Int
+  answers <- forM [1..numQuestions] $ \_ -> do
+    line <- getLine
+    -- print (">> " ++ line)
+    let question = read line
+    -- print $ ("<> " ++ (show $ skipPlaces question))
+    return $ skipPlaces question
+  putStrLn $ unlines $ map show answers
+
+
+-- lenUpTo, startOf, calcStartOfPiece, calcStartOf :: Integer -> Integer
+-- lenUpTo n = fromIntegral $ length $ mconcat $ map show $ takeWhile (< n) [1..]
+
+-- startOf n = (lenUpTo n) + 1
+
+-- calcStartOfPiece n 
+--   | n < 10 = n
+--   | n < 100 = 2 * (n - 10) + 9 + 1
+--   | n < 1000 = 3 * (n - 100) + (90 * 2) + 9 + 1
+--   | n < 10000 = 4 * (n - 1000) + (900 * 3) + (90 * 2) + 9 + 1
+
+-- calcStartOf n = (k + 1) * (n - 10 ^ k) + smaller
+--   where k = discreteLog n
+--         smaller = 1 + sum [9 * 10 ^ (i - 1) * i | i <- [1..k]]
+
+-- discreteLog :: Integer -> Integer
+-- discreteLog n = go n 0
+--   where go k p
+--           | k < 10 = p
+--           | otherwise = go (k `div` 10) (p + 1)
+
+
+-- cbStart n = (startOf n, calcStartOf n, calcStartOfPiece n)
+
+prefixLengths :: [Integer]
+prefixLengths = scanl' f 0 [1..]
+  where f acc i = acc + 9 * 10 ^ (i - 1) * i
+
+skipPlaces :: Integer -> Integer
+skipPlaces n = read [(show thisNum) !! (fromIntegral(n - (prevNumsLen + lowerOrders + 1)))]
+  where -- k = discreteLog n
+        k = fromIntegral $ (length $ takeWhile (< n) prefixLengths) - 1
+        lowerOrders = prefixLengths !! (fromIntegral k)
+        prevNums = ((n - (lowerOrders + 1)) `div` (k + 1))
+        prevNumsLen = prevNums * (k + 1)
+        thisNum = prevNums + (10 ^ k)
index 62ea30290f790f287efa99b85ff5ac409b4ab954..a1cde74c0db9d7ddf9281cc6f2c46039dd05cf2a 100644 (file)
@@ -133,4 +133,34 @@ executable cses1618
 executable cses1754
   import: build-directives
   main-is: cses1754.hs
+
+executable cses1755
+  import: build-directives
+  main-is: cses1755.hs
+  build-depends: containers
+  
+executable cses2205
+  import: build-directives
+  main-is: cses2205.hs
+  
+executable cses2165
+  import: build-directives
+  main-is: cses2165.hs
+
+executable cses1622
+  import: build-directives
+  main-is: cses1622.hs
+
+executable cses1623
+  import: build-directives
+  main-is: cses1623.hs
+
+executable cses1624
+  import: build-directives
+  main-is: cses1624.hs
+  build-depends: containers
+
+executable cses2431
+  import: build-directives
+  main-is: cses2431.hs
   
\ No newline at end of file
diff --git a/data/cses1623.txt b/data/cses1623.txt
new file mode 100644 (file)
index 0000000..b9679f8
--- /dev/null
@@ -0,0 +1,2 @@
+5
+3 2 7 4 1
diff --git a/data/cses1624.txt b/data/cses1624.txt
new file mode 100644 (file)
index 0000000..994f68d
--- /dev/null
@@ -0,0 +1,8 @@
+........
+........
+..*.....
+........
+........
+.....**.
+...*....
+........
diff --git a/data/cses2431.txt b/data/cses2431.txt
new file mode 100644 (file)
index 0000000..3737d84
--- /dev/null
@@ -0,0 +1,4 @@
+3
+7
+19
+12
diff --git a/data/cses2431a.txt b/data/cses2431a.txt
new file mode 100644 (file)
index 0000000..32415cd
--- /dev/null
@@ -0,0 +1,1001 @@
+1000
+582
+214
+723
+273
+480
+280
+237
+204
+134
+210
+565
+640
+784
+508
+846
+532
+465
+952
+205
+737
+11
+166
+320
+400
+754
+301
+200
+91
+337
+813
+663
+473
+858
+533
+329
+245
+240
+531
+25
+889
+47
+763
+935
+696
+314
+490
+20
+449
+550
+586
+56
+691
+665
+50
+808
+729
+544
+367
+924
+573
+988
+627
+823
+545
+652
+269
+959
+700
+859
+29
+799
+779
+591
+250
+711
+902
+22
+568
+832
+419
+429
+297
+295
+983
+669
+36
+222
+926
+416
+823
+614
+606
+598
+936
+270
+926
+675
+533
+134
+724
+277
+484
+66
+775
+400
+544
+547
+954
+998
+547
+642
+213
+920
+807
+334
+647
+488
+596
+120
+557
+314
+915
+286
+288
+90
+320
+914
+953
+431
+839
+79
+300
+422
+486
+81
+377
+216
+65
+317
+810
+667
+321
+920
+301
+93
+367
+772
+525
+428
+288
+267
+169
+906
+193
+895
+813
+286
+612
+119
+951
+814
+148
+581
+801
+142
+909
+313
+546
+717
+432
+845
+910
+793
+862
+288
+298
+698
+445
+951
+632
+668
+467
+80
+343
+204
+927
+827
+829
+278
+528
+603
+233
+286
+691
+742
+840
+176
+452
+549
+217
+838
+544
+971
+222
+282
+308
+48
+77
+196
+133
+362
+945
+793
+1000
+732
+646
+167
+905
+100
+445
+426
+959
+593
+410
+775
+662
+312
+923
+461
+29
+707
+901
+232
+495
+506
+74
+803
+390
+14
+570
+497
+568
+531
+909
+700
+186
+661
+724
+632
+666
+128
+261
+544
+723
+655
+676
+881
+888
+108
+520
+303
+790
+88
+86
+679
+43
+554
+518
+197
+11
+244
+332
+289
+704
+522
+123
+110
+522
+56
+609
+732
+122
+932
+354
+122
+61
+747
+366
+871
+693
+131
+543
+459
+360
+956
+63
+970
+528
+439
+419
+845
+621
+523
+170
+600
+838
+65
+650
+110
+995
+174
+622
+937
+802
+636
+3
+321
+621
+677
+209
+208
+618
+625
+56
+453
+858
+805
+748
+655
+573
+700
+28
+569
+942
+281
+935
+157
+261
+826
+251
+985
+131
+444
+963
+64
+273
+156
+112
+711
+223
+105
+368
+181
+672
+346
+989
+787
+456
+986
+380
+727
+130
+205
+484
+324
+390
+470
+44
+777
+527
+246
+401
+551
+937
+112
+363
+248
+239
+492
+485
+854
+797
+952
+691
+945
+263
+181
+425
+696
+722
+620
+994
+294
+588
+272
+529
+698
+375
+157
+89
+948
+410
+248
+749
+998
+948
+979
+318
+178
+958
+266
+485
+88
+837
+298
+435
+75
+116
+277
+21
+441
+656
+602
+166
+163
+361
+465
+5
+727
+974
+430
+982
+745
+483
+456
+429
+277
+154
+365
+795
+996
+112
+316
+509
+584
+538
+693
+111
+275
+132
+458
+774
+164
+381
+530
+421
+103
+43
+206
+719
+770
+666
+741
+232
+589
+257
+989
+559
+412
+299
+417
+460
+92
+691
+33
+630
+308
+18
+904
+466
+807
+352
+703
+841
+928
+458
+817
+480
+758
+13
+245
+536
+583
+172
+955
+335
+229
+902
+292
+987
+936
+246
+961
+730
+884
+731
+827
+595
+496
+205
+607
+801
+996
+955
+434
+58
+934
+49
+26
+184
+275
+676
+332
+435
+654
+583
+870
+986
+915
+931
+585
+139
+836
+753
+356
+402
+510
+35
+888
+488
+853
+415
+5
+200
+174
+226
+145
+122
+405
+171
+293
+325
+780
+428
+456
+359
+176
+796
+107
+504
+312
+12
+194
+7
+344
+227
+618
+929
+756
+398
+51
+804
+189
+783
+780
+173
+145
+330
+336
+756
+611
+468
+641
+530
+994
+349
+443
+295
+404
+136
+253
+421
+522
+305
+861
+128
+63
+517
+521
+241
+348
+125
+396
+36
+862
+129
+924
+553
+289
+57
+796
+9
+14
+653
+964
+830
+69
+232
+923
+662
+792
+48
+478
+258
+905
+606
+897
+836
+315
+310
+224
+616
+347
+207
+50
+394
+276
+677
+863
+898
+277
+552
+833
+654
+194
+717
+632
+881
+672
+337
+426
+435
+122
+653
+818
+667
+795
+264
+152
+708
+405
+228
+641
+908
+837
+562
+245
+279
+150
+582
+772
+429
+318
+987
+197
+41
+902
+70
+203
+815
+395
+189
+188
+211
+709
+312
+234
+561
+197
+921
+291
+694
+564
+88
+265
+129
+831
+790
+859
+907
+862
+445
+367
+169
+754
+430
+846
+113
+10
+2
+34
+264
+625
+833
+970
+261
+34
+199
+410
+684
+118
+783
+887
+639
+988
+89
+20
+575
+575
+227
+744
+164
+738
+649
+205
+147
+855
+814
+30
+608
+525
+860
+194
+248
+384
+645
+790
+229
+639
+782
+703
+532
+803
+144
+440
+550
+112
+429
+874
+723
+648
+279
+651
+443
+862
+631
+655
+845
+922
+543
+213
+820
+670
+554
+222
+513
+561
+522
+408
+536
+62
+495
+126
+606
+293
+444
+136
+236
+965
+789
+881
+577
+308
+666
+164
+897
+26
+400
+492
+209
+348
+808
+79
+894
+799
+4
+659
+906
+881
+109
+40
+388
+570
+45
+88
+597
+632
+35
+628
+798
+666
+5
+24
+797
+967
+875
+34
+617
+91
+816
+475
+97
+132
+457
+3
+240
+371
+880
+966
+182
+982
+143
+850
+676
+458
+806
+619
+324
+815
+865
+832
+703
+77
+170
+356
+693
+536
+91
+547
+318
+691
+467
+688
+104
+868
+56
+607
+422
+302
+823
+780
+542
+406
+261
+170
+590
+275
+574
+231
+911
+317
+124
+64
+218
+401
+172
+529
+99
+161
+494
+268
+49
+939
+942
+874
+861
+791
+806
+743
+134
+759
+994
+674
+195
+5
+52
+909
+16
+688
+181
+971
+538
+247
+60
+987
+103
+25
+360
+548
+411
+94
+233
+226
+39
+117
+644
+300
+117
+245
+125
+610
+932
+638
+27
+310
+272
+808
+86
+991
+233
+495
+26
+116
+910
+749
+751
+695
+80
+717
+78
+738
+21
+942
+153
+590
+872
+97
+255
+364
+756
+70
+993
+26
+103
+593
+786
+75
+48
+185
+965
+937
+55
+407
+264
+454
+262
+345
+582
+785
+316
+916
+465
+501
+872
+427
+416
+188
+314
+543
+177
+819
+889
+597
+638
+666
+150
+959
+23
+176
+403