first commit

This commit is contained in:
Quinten Kock 2021-12-06 14:50:47 +01:00
commit 4cfb172f6a
12 changed files with 5300 additions and 0 deletions

16
2021/day01.hs Normal file
View File

@ -0,0 +1,16 @@
module Main where
main :: IO ()
main = do
input <- getContents
let numbers = map (read :: String -> Integer) (lines input)
putStr "part 1: "
let zippedInts = zip numbers (tail numbers)
-- let count = filter (\(x,y) -> y > x) zippedInts
let count = filter (<0) (zipWith (-) numbers (tail numbers))
putStrLn $ show $ length count
putStr "part 2: "
let sums = zipWith3 (\x y z -> x + y + z) numbers (tail numbers) (tail (tail numbers))
putStrLn $ show $ length $ filter (<0) $ zipWith (-) sums (tail sums)
return ()

2000
2021/day01.input Normal file

File diff suppressed because it is too large Load Diff

37
2021/day02.hs Normal file
View File

@ -0,0 +1,37 @@
data Command = Forward Int | Down Int | Up Int
type Position = (Int,Int)
type AimPosition = (Int,Int,Int)
readCmd :: String -> Command
readCmd s = case cmd of
"forward" -> Forward $ read num
"down" -> Down $ read num
"up" -> Up $ read num
_ -> error $ "unknown command" ++ cmd
where (cmd:num:xs) = words s
moveSub :: Position -> Command -> Position
moveSub (h,d) (Forward f) = (h+f,d)
moveSub (h,d) (Down f) = (h,d+f)
moveSub (h,d) (Up f) = (h,d-f)
applyCommands :: [Command] -> Position
applyCommands = foldl moveSub (0,0)
moveSubAim :: AimPosition -> Command -> AimPosition
moveSubAim (h,d,a) (Forward f) = (h+f,d+a*f,a)
moveSubAim (h,d,a) (Down f) = (h,d,a+f)
moveSubAim (h,d,a) (Up f) = (h,d,a-f)
applyCommandsAim :: [Command] -> AimPosition
applyCommandsAim = foldl moveSubAim (0,0,0)
main :: IO ()
main = do
input <- getContents
let commands = map readCmd $ lines input
putStr "part 1: "
print $ uncurry (*) $ applyCommands commands
putStr "part 2: "
print $ (\(x,y,_) -> x*y) $ applyCommandsAim commands
return ()

1000
2021/day02.input Normal file

File diff suppressed because it is too large Load Diff

35
2021/day03.hs Normal file
View File

@ -0,0 +1,35 @@
module Day03 where
import Data.Char (digitToInt)
import Data.List (foldl', transpose)
count :: Eq a => a -> [a] -> Int
count x = length . filter (x==)
binToDec :: String -> Int
binToDec = foldl' (\acc x -> acc * 2 + digitToInt x) 0
getMostCommon :: String -> Char
getMostCommon x = if count '0' x > count '1' x then '0' else '1'
getLeastCommon :: String -> Char
getLeastCommon x = if count '0' x > count '1' x then '1' else '0'
filterNumbers :: (String->Char) -> Int -> [String] -> String
filterNumbers _ _ [x] = x
filterNumbers f i nums = filterNumbers f (i+1) $ filter (\w -> w!!i == filterVal) nums
where filterVal = f $ map (!!i) nums
main :: IO ()
main = do
input <- getContents
let bits = lines input
putStr "part 1: "
let gamma = binToDec $ map getMostCommon $ transpose bits
let epsilon = binToDec $ map getLeastCommon $ transpose bits
print $ gamma*epsilon
putStr "part 2: "
let oxygen = binToDec $ filterNumbers getMostCommon 0 bits
let co2 = binToDec $ filterNumbers getLeastCommon 0 bits
print $ oxygen*co2

1000
2021/day03.input Normal file

File diff suppressed because it is too large Load Diff

42
2021/day04.hs Normal file
View File

@ -0,0 +1,42 @@
module Day04 where
import Data.List.Split ( splitOn )
import Data.Maybe (catMaybes, mapMaybe, listToMaybe)
import Data.List (transpose, sort)
type Board = [[Int]]
checkRowTime :: [Int] -> [Int] -> Maybe Int
checkRowTime values row = fmap maximum elemTimes
where
elemTimes = mapM (`lookup` times) row
times = zip values [0..]
checkBingoTime :: [Int] -> Board -> Maybe Int
checkBingoTime values board = listToMaybe $ sort $ catMaybes $ horTimes ++ verTimes
where
horTimes = map (checkRowTime values) board
verTimes = map (checkRowTime values) (transpose board)
calculateScore :: [Int] -> Int -> Board -> Int
calculateScore values time card = values!!time * sum unmarked
where
marked = take (time+1) values
unmarked = filter (`notElem` marked) $ concat card
main :: IO ()
main = do
firstLine <- getLine
let values = map (read :: String->Int) $ splitOn "," firstLine
_ <- getLine
input <- getContents
let cards = map (map (map (read::String->Int))) $ map (map words) $ splitOn [""] $ lines input
let cardsWithTime = mapMaybe (\card -> fmap (\t -> (t,card)) (checkBingoTime values card)) cards
putStr "part 1: "
let (time,card) = minimum cardsWithTime
print $ calculateScore values time card
putStr "part 2: "
let (time,card) = maximum cardsWithTime
print $ calculateScore values time card

601
2021/day04.input Normal file
View File

@ -0,0 +1,601 @@
31,50,68,16,25,15,28,80,41,8,75,45,96,9,3,98,83,27,62,42,59,99,95,13,55,10,23,84,18,76,87,56,88,66,1,58,92,89,19,54,85,74,39,93,77,26,30,52,69,48,91,73,72,38,64,53,32,51,6,29,17,90,34,61,70,4,7,57,44,97,82,37,43,14,81,65,11,22,5,36,71,35,78,12,0,94,47,49,33,79,63,86,40,21,24,46,20,2,67,60
95 91 54 75 45
46 94 39 44 85
31 43 24 2 70
90 58 4 30 77
13 26 38 52 34
68 14 99 63 46
67 16 82 10 8
55 52 41 51 4
90 17 32 44 74
89 94 73 56 36
6 91 2 28 71
7 88 37 21 36
95 32 84 57 8
13 79 89 75 48
47 81 66 17 5
44 55 48 16 41
35 5 43 53 11
0 79 19 82 49
30 85 31 72 39
27 68 65 66 95
6 46 55 24 40
87 16 62 11 64
8 49 18 59 91
72 28 48 19 96
39 73 9 88 0
46 1 18 93 6
50 96 41 81 58
62 66 49 32 55
29 11 65 2 10
71 30 17 69 43
84 57 6 56 95
39 15 32 1 10
55 43 40 3 22
89 54 13 80 38
72 12 59 71 77
3 51 72 88 6
56 64 99 82 36
75 81 42 67 24
41 79 35 40 73
2 14 61 50 87
72 7 0 99 24
29 81 92 74 38
84 53 20 56 91
93 64 44 48 2
37 40 88 96 97
39 87 99 75 38
45 81 46 72 62
40 8 24 67 79
96 1 77 73 70
34 13 55 83 59
18 38 55 84 58
35 97 31 26 44
80 1 28 71 85
45 46 10 32 99
60 48 72 11 53
53 44 6 30 82
65 64 10 0 86
81 50 5 21 60
16 19 97 66 73
20 24 96 68 69
33 85 88 58 20
19 2 99 40 70
69 10 17 67 23
48 9 29 34 3
89 30 43 41 94
30 83 37 14 78
57 42 98 90 28
5 82 51 15 55
77 79 96 67 53
22 76 0 59 47
10 93 84 54 11
59 30 6 94 75
9 62 68 37 24
61 3 66 58 74
64 0 83 32 13
24 77 15 5 49
23 11 70 17 10
62 33 38 36 12
3 61 0 26 65
63 47 54 50 9
34 28 24 55 38
69 37 36 32 72
91 85 9 64 68
94 22 96 90 82
30 20 97 19 56
35 90 58 93 2
72 8 27 88 16
25 54 69 60 61
1 15 9 71 78
89 42 46 57 91
43 38 47 1 82
19 32 2 95 51
78 74 91 10 22
80 77 33 60 14
0 98 70 54 50
71 94 3 84 31
10 59 12 28 46
68 69 13 48 36
37 49 24 26 61
72 65 38 16 25
68 78 10 90 29
40 14 45 43 44
27 75 72 39 87
96 93 53 12 11
37 64 56 59 76
19 78 84 68 41
36 96 87 15 55
47 33 10 91 85
11 94 31 49 35
83 63 86 4 76
5 89 55 46 96
67 22 95 82 56
61 94 84 99 28
71 70 16 57 63
98 92 86 73 83
56 24 93 88 29
74 89 50 44 79
97 49 87 31 8
70 6 57 3 82
20 25 99 48 78
80 20 45 22 57
43 35 48 44 52
29 98 38 27 11
64 49 84 60 31
7 97 17 85 74
21 1 18 41 62
76 38 13 46 27
0 16 43 28 53
54 42 22 83 20
4 25 50 11 56
21 71 73 30 51
92 27 14 16 11
89 43 38 62 31
65 4 36 84 94
56 13 59 22 6
46 28 45 57 6
16 67 33 20 44
36 86 92 3 11
98 27 26 84 31
5 76 13 65 25
49 55 99 31 73
80 0 83 26 65
3 96 60 37 50
57 34 7 20 23
70 52 93 13 71
78 41 81 32 76
37 36 16 99 48
3 93 33 85 35
60 38 74 80 56
86 29 87 20 7
98 24 4 9 82
22 37 52 81 30
51 70 17 23 8
60 61 75 35 85
33 48 88 13 57
49 52 91 21 46
90 51 70 5 35
57 68 31 86 87
42 36 25 95 3
73 64 17 96 80
6 42 13 16 24
51 72 55 78 47
67 28 7 97 64
44 77 98 71 25
82 8 31 76 73
63 79 18 80 27
57 66 47 89 14
15 61 94 38 29
21 53 73 17 76
65 7 23 36 3
93 22 8 52 4
63 31 90 46 68
51 14 95 50 23
17 34 60 75 36
88 55 7 0 73
42 67 91 15 83
1 37 76 12 25
29 82 50 70 86
74 66 39 90 16
31 71 47 5 92
98 48 27 87 2
60 0 25 30 14
44 61 94 82 16
17 19 53 49 59
15 85 50 63 8
63 12 52 81 97
87 68 44 31 41
39 24 99 27 6
1 21 35 59 51
4 92 3 90 62
66 43 98 25 47
59 16 75 44 82
40 58 48 60 37
13 81 61 45 83
24 89 93 19 67
8 86 47 81 83
1 91 57 59 68
0 52 25 93 95
27 84 63 45 36
39 62 20 99 88
67 28 65 4 17
44 33 12 99 84
27 36 34 5 10
38 63 97 82 64
94 19 58 41 6
28 41 96 16 39
93 78 56 69 75
74 60 9 30 7
12 81 35 84 94
45 20 6 49 2
33 7 5 43 9
97 65 50 2 1
26 86 76 47 98
83 31 30 58 52
93 51 34 63 12
65 81 10 46 59
51 89 85 2 52
13 54 18 70 4
37 94 75 92 56
58 99 91 32 41
44 23 18 16 75
5 3 45 76 90
20 71 69 37 99
49 91 43 32 82
4 79 51 2 39
54 80 71 61 76
81 52 6 89 34
58 14 20 26 40
25 60 45 0 3
36 33 65 41 94
53 23 56 55 92
3 81 59 0 35
32 30 93 70 40
45 91 24 65 6
95 88 16 47 8
24 23 10 47 63
38 79 18 77 62
13 88 72 68 36
96 27 12 69 89
28 94 6 98 20
70 1 61 77 28
92 22 30 48 96
66 3 34 29 45
85 6 17 87 5
97 89 14 80 20
40 86 97 5 54
91 57 12 19 7
62 88 92 20 32
50 77 11 39 22
33 2 21 95 35
51 79 64 24 28
95 19 82 58 76
69 30 9 14 42
27 16 56 80 83
52 5 49 43 45
26 65 68 18 55
58 83 30 48 4
69 42 78 96 22
91 13 82 38 41
67 33 10 98 53
74 97 43 1 18
49 30 28 10 15
42 12 80 20 96
41 92 67 22 36
5 26 13 47 4
85 60 72 19 48
61 21 43 34 83
51 75 94 11 78
27 89 45 49 79
97 15 5 28 50
69 99 97 77 98
37 80 86 7 34
85 28 31 23 16
32 47 8 40 4
46 91 71 54 74
23 43 29 7 21
58 34 42 4 38
64 96 16 73 49
36 92 52 81 31
85 71 78 13 5
65 71 46 5 11
24 4 6 0 23
13 17 76 15 34
27 79 10 56 85
90 19 30 94 78
81 40 65 12 35
23 93 24 96 53
27 60 87 59 33
76 56 4 17 6
78 49 90 3 80
7 25 42 85 39
40 6 26 11 63
48 20 62 61 89
78 15 18 21 53
69 4 87 47 5
38 65 87 72 26
88 75 10 30 3
89 58 7 20 32
85 2 97 63 15
28 77 82 57 64
16 89 6 31 27
45 69 61 5 15
40 75 64 94 98
36 18 26 58 90
10 44 59 13 67
80 13 39 49 42
50 94 12 14 88
97 48 15 68 69
21 18 51 99 91
89 64 35 63 84
81 96 26 43 30
5 74 9 93 60
63 41 1 14 22
4 77 33 53 3
38 90 50 25 10
58 66 17 0 16
5 67 41 48 40
43 93 76 95 68
4 13 14 51 8
56 74 23 57 94
18 15 92 4 20
69 26 84 23 2
72 35 56 96 9
61 41 85 91 25
13 70 1 14 77
15 20 77 80 47
45 34 63 55 75
65 12 66 44 22
74 3 25 38 93
9 31 68 87 67
54 95 79 50 75
18 36 20 34 43
17 65 55 98 61
27 46 56 6 52
91 31 30 40 0
25 30 24 64 98
8 72 53 45 3
27 77 0 33 44
89 39 34 71 38
54 21 20 80 23
69 38 63 60 4
55 37 83 68 61
67 25 86 24 18
22 59 65 28 70
84 46 44 91 96
35 27 65 31 0
46 97 44 74 4
56 61 7 49 88
5 38 50 20 26
62 9 45 64 51
3 28 43 97 7
88 57 17 82 73
16 94 74 22 39
84 99 31 1 47
92 91 55 38 78
60 31 29 49 72
89 41 5 79 22
58 28 90 76 95
93 45 14 47 37
65 25 7 59 62
58 1 73 30 55
95 46 5 80 63
52 16 70 20 71
84 60 15 0 77
99 89 17 72 31
83 21 68 18 42
1 28 97 31 35
2 38 67 63 74
77 27 48 90 86
12 52 26 29 60
81 53 80 85 96
19 32 31 15 88
91 92 66 37 34
74 75 33 39 78
42 40 30 83 58
40 41 80 69 67
24 63 97 33 5
28 84 34 72 11
36 79 91 14 92
55 89 59 10 44
0 51 49 24 60
48 65 28 70 66
86 58 78 77 18
6 44 50 37 36
4 73 91 97 43
66 42 76 12 48
77 83 35 18 50
30 87 95 99 11
0 52 92 16 51
26 56 39 64 62
83 89 52 61 45
74 77 66 59 30
85 17 38 10 8
97 67 54 21 26
34 63 11 94 40
93 91 14 49 38
77 53 29 36 39
12 52 0 48 92
95 82 71 76 37
19 70 75 9 74
80 49 59 67 27
43 66 97 81 12
10 77 24 75 68
16 57 14 28 85
41 52 23 15 2
39 89 66 27 11
63 84 21 44 69
56 51 58 70 83
14 0 8 41 31
98 18 61 97 74
97 18 63 29 24
75 73 80 8 7
81 37 46 93 68
57 32 2 41 5
4 65 88 45 54
87 26 81 12 50
19 9 68 23 71
74 38 4 34 16
18 99 86 45 7
35 8 11 98 57
13 31 78 59 58
51 61 96 68 44
41 85 26 55 92
11 62 30 7 64
18 19 73 27 17
48 85 71 39 29
33 79 24 0 72
95 52 12 77 40
70 31 46 44 43
61 88 73 49 65
60 81 51 24 27
23 28 47 85 67
35 57 45 76 84
19 48 30 37 74
15 78 56 87 16
12 5 45 26 21
1 71 51 10 25
37 68 73 46 56
55 20 63 87 91
22 32 66 90 19
20 59 12 29 97
92 30 77 27 49
14 98 23 50 6
11 47 61 34 36
55 82 13 22 21
29 37 0 40 71
2 43 97 18 59
32 72 89 99 24
58 90 73 60 85
69 53 95 78 27
58 28 32 52 55
86 33 1 41 60
8 53 42 92 5
43 69 96 54 24
74 10 17 89 85
51 74 99 21 64
54 27 60 32 37
14 45 50 81 94
28 11 77 17 23
93 95 53 57 79
16 5 80 45 71
22 57 9 90 43
3 52 47 59 84
28 53 14 15 7
50 76 46 56 34
83 62 77 56 26
82 35 11 6 51
96 97 15 1 78
92 45 55 84 94
20 8 70 21 31
4 47 68 81 12
66 23 35 8 39
73 94 27 69 22
59 11 53 26 99
7 20 87 60 88
90 0 88 81 43
47 54 42 29 97
60 13 85 51 71
56 14 94 80 41
75 8 35 69 61
51 38 40 17 42
19 26 92 64 67
33 66 82 27 55
62 2 68 59 31
7 24 20 91 79
21 83 45 35 88
85 11 5 86 72
78 3 58 0 89
67 1 39 59 63
79 87 19 4 57
25 16 40 17 27
96 72 29 32 87
50 63 35 81 66
7 11 92 68 69
54 83 12 51 95
64 98 67 54 75
8 10 31 5 57
89 23 25 34 47
72 74 37 48 94
39 59 15 55 87

46
2021/day05.hs Normal file
View File

@ -0,0 +1,46 @@
module Day05 where
import Data.Map (Map, insertWith, empty, elems)
import Data.List.Split (splitOn)
data Line = Line (Int,Int) (Int,Int) deriving Show
type Field = Map (Int,Int) Int
strToLine :: String -> Line
strToLine s = Line (x1,y1) (x2,y2) where
[x1,y1] = map read $ splitOn "," p1
[x2,y2] = map read $ splitOn "," p2
[p1,"->",p2] = words s
coverPoint :: (Int,Int) -> Field -> Field
coverPoint point = Data.Map.insertWith (+) point 1
coverLine :: Line -> Field -> Field
coverLine (Line (x1,y1) (x2,y2)) field
| y1 == y2 = let points = map (\x -> (x,y1)) [xMin..xMax] in foldr coverPoint field points
| x1 == x2 = let points = map (\y -> (x1,y)) [yMin..yMax] in foldr coverPoint field points
| otherwise = field
where xMin = min x1 x2; xMax = max x1 x2; yMin = min y1 y2; yMax = max y1 y2
coverLineDiag :: Line -> Field -> Field
coverLineDiag (Line (x1,y1) (x2,y2)) field
| y1 == y2 = let points = map (\x -> (x,y1)) [xMin..xMax] in foldr coverPoint field points
| x1 == x2 = let points = map (\y -> (x1,y)) [yMin..yMax] in foldr coverPoint field points
| x1 == xMin && y1 == yMin = let points = zip [x1..x2] [y1..y2] in foldr coverPoint field points
| x1 == xMin && y1 == yMax = let points = zip [x1..x2] (reverse [y2..y1]) in foldr coverPoint field points
| otherwise = coverLineDiag (Line (x2,y2) (x1,y1)) field
where xMin = min x1 x2; xMax = max x1 x2; yMin = min y1 y2; yMax = max y1 y2
main :: IO ()
main = do
lines <- getContents >>= ((return . map strToLine) . lines)
putStr "part 1: "
let covered = foldr coverLine Data.Map.empty lines
let num_overlap = length $ filter (>=2) $ Data.Map.elems covered
print num_overlap
putStr "part 2: "
let covered = foldr coverLineDiag Data.Map.empty lines
let num_overlap = length $ filter (>=2) $ Data.Map.elems covered
print num_overlap
return ()

500
2021/day05.input Normal file
View File

@ -0,0 +1,500 @@
593,10 -> 593,98
777,236 -> 964,236
650,575 -> 476,575
120,612 -> 715,17
508,707 -> 508,89
98,834 -> 751,834
623,554 -> 623,701
929,976 -> 62,109
368,893 -> 330,931
495,335 -> 40,335
44,704 -> 423,704
683,711 -> 683,487
26,940 -> 833,133
961,183 -> 454,183
301,306 -> 301,935
973,822 -> 398,822
639,911 -> 515,911
861,180 -> 184,857
31,97 -> 857,923
966,376 -> 966,114
881,485 -> 881,377
930,98 -> 110,918
841,889 -> 841,35
512,261 -> 880,261
48,533 -> 48,674
207,226 -> 52,226
823,952 -> 177,306
331,566 -> 423,566
422,418 -> 422,130
699,517 -> 699,567
757,784 -> 241,784
508,445 -> 560,393
866,275 -> 435,706
74,41 -> 74,258
386,369 -> 334,317
240,94 -> 240,969
851,197 -> 577,197
28,906 -> 741,193
286,227 -> 286,293
849,800 -> 849,665
736,307 -> 336,307
69,701 -> 494,276
421,823 -> 96,823
121,626 -> 121,393
318,351 -> 194,351
670,671 -> 439,671
603,914 -> 603,272
61,507 -> 61,889
266,39 -> 157,39
543,664 -> 869,664
382,709 -> 884,709
499,80 -> 548,80
489,79 -> 878,79
695,86 -> 644,86
987,585 -> 987,557
287,67 -> 551,67
975,983 -> 35,43
707,351 -> 232,351
529,175 -> 852,175
32,811 -> 604,811
106,153 -> 815,153
195,268 -> 509,582
50,922 -> 312,922
220,500 -> 872,500
473,33 -> 569,33
858,847 -> 162,151
937,947 -> 26,36
726,435 -> 402,435
686,601 -> 474,813
764,880 -> 84,200
850,950 -> 850,464
413,620 -> 413,285
893,560 -> 229,560
149,100 -> 149,901
358,613 -> 243,613
202,445 -> 202,411
127,153 -> 513,539
147,846 -> 53,940
139,920 -> 679,380
913,953 -> 913,735
339,466 -> 339,177
113,882 -> 647,882
18,880 -> 134,880
897,152 -> 897,428
473,511 -> 636,511
880,370 -> 358,370
400,244 -> 721,244
419,987 -> 120,688
872,224 -> 481,224
335,302 -> 730,302
961,324 -> 961,157
769,301 -> 959,301
829,124 -> 144,124
523,372 -> 985,372
520,33 -> 520,685
554,644 -> 808,898
82,676 -> 870,676
303,612 -> 303,705
338,40 -> 338,939
836,47 -> 72,811
371,751 -> 575,955
929,505 -> 929,324
273,181 -> 275,183
347,595 -> 347,463
95,629 -> 95,606
809,188 -> 126,871
857,924 -> 145,212
668,277 -> 668,63
700,904 -> 700,45
814,899 -> 22,899
205,98 -> 714,607
943,28 -> 40,931
282,620 -> 773,129
424,803 -> 285,803
688,329 -> 299,329
146,628 -> 34,628
573,417 -> 164,826
292,232 -> 412,112
412,508 -> 145,508
632,648 -> 632,92
885,904 -> 885,513
295,981 -> 132,818
134,681 -> 41,681
810,531 -> 959,531
188,590 -> 188,215
960,795 -> 189,24
729,211 -> 729,833
214,817 -> 845,817
196,609 -> 584,609
384,908 -> 384,101
770,907 -> 770,530
451,469 -> 451,812
571,261 -> 834,261
799,436 -> 799,983
248,105 -> 248,879
783,906 -> 783,903
955,670 -> 790,670
723,750 -> 723,429
572,427 -> 546,427
610,341 -> 527,341
925,426 -> 816,317
151,403 -> 151,684
408,969 -> 408,369
276,425 -> 276,75
186,86 -> 186,758
412,420 -> 412,531
361,60 -> 976,60
787,649 -> 667,769
45,866 -> 91,866
319,963 -> 51,963
112,866 -> 112,747
291,475 -> 504,475
175,116 -> 357,116
968,961 -> 968,213
13,12 -> 987,986
640,728 -> 767,728
981,505 -> 246,505
864,981 -> 128,981
91,66 -> 931,906
798,116 -> 91,823
552,74 -> 88,538
620,872 -> 232,872
45,229 -> 658,229
413,75 -> 413,436
815,257 -> 815,686
989,22 -> 36,975
178,904 -> 233,849
635,128 -> 635,96
640,820 -> 640,313
890,787 -> 167,64
221,22 -> 826,22
914,132 -> 60,986
848,31 -> 392,487
105,969 -> 858,969
903,868 -> 143,108
38,941 -> 621,358
171,340 -> 14,497
286,460 -> 81,255
726,688 -> 857,819
494,689 -> 510,689
517,913 -> 598,913
932,66 -> 932,431
977,982 -> 18,23
95,101 -> 95,278
574,467 -> 349,467
63,803 -> 63,882
838,874 -> 255,874
900,752 -> 181,33
102,897 -> 989,10
374,439 -> 374,277
513,504 -> 513,885
814,932 -> 814,407
824,656 -> 959,521
415,570 -> 616,570
577,880 -> 577,181
287,524 -> 986,524
955,665 -> 323,665
556,365 -> 263,658
154,226 -> 886,226
803,750 -> 866,750
558,725 -> 558,395
941,115 -> 941,150
180,410 -> 180,874
458,753 -> 112,753
199,253 -> 363,253
423,650 -> 22,650
892,851 -> 279,238
611,109 -> 611,198
983,344 -> 339,988
299,47 -> 299,934
435,652 -> 700,387
186,775 -> 677,284
136,576 -> 136,368
818,744 -> 305,744
767,171 -> 767,431
930,842 -> 259,171
342,831 -> 342,601
193,672 -> 46,525
925,164 -> 528,164
725,92 -> 617,200
67,729 -> 67,739
547,153 -> 547,245
763,434 -> 763,509
314,888 -> 357,888
72,645 -> 491,645
92,67 -> 240,67
827,936 -> 788,897
852,378 -> 77,378
448,337 -> 668,337
846,739 -> 499,739
465,691 -> 315,541
716,163 -> 18,861
78,965 -> 983,60
114,952 -> 820,246
950,351 -> 419,882
266,36 -> 266,482
773,841 -> 773,66
742,198 -> 742,46
417,512 -> 304,625
900,277 -> 900,338
983,431 -> 473,941
986,282 -> 734,30
742,19 -> 769,19
952,320 -> 948,324
92,590 -> 548,590
107,39 -> 107,696
603,749 -> 603,26
55,282 -> 888,282
670,848 -> 985,533
981,982 -> 92,93
147,428 -> 649,930
773,737 -> 821,785
791,576 -> 791,852
327,672 -> 530,469
847,122 -> 381,122
419,493 -> 498,572
879,842 -> 879,239
267,717 -> 267,869
142,449 -> 174,417
342,718 -> 342,397
603,207 -> 314,207
612,648 -> 735,771
37,10 -> 971,944
891,716 -> 891,86
252,217 -> 662,627
185,165 -> 941,921
854,717 -> 676,717
158,791 -> 336,791
762,226 -> 98,890
73,189 -> 92,189
649,511 -> 253,115
719,456 -> 514,251
605,286 -> 325,286
454,609 -> 454,489
374,541 -> 783,541
599,177 -> 94,682
600,384 -> 32,384
810,933 -> 39,162
780,871 -> 409,871
24,639 -> 24,316
454,80 -> 454,95
556,541 -> 907,541
627,295 -> 750,295
245,71 -> 214,102
725,445 -> 614,445
779,538 -> 779,390
746,667 -> 351,272
117,776 -> 117,660
498,495 -> 88,905
697,721 -> 697,919
580,314 -> 580,166
22,656 -> 641,37
413,433 -> 44,802
182,305 -> 805,928
739,277 -> 739,499
172,210 -> 172,259
894,576 -> 894,322
265,263 -> 265,437
430,228 -> 780,578
464,531 -> 798,531
713,63 -> 668,63
918,831 -> 256,169
414,375 -> 467,375
440,32 -> 391,32
439,806 -> 955,806
335,820 -> 335,279
727,458 -> 422,458
312,274 -> 619,581
136,724 -> 538,322
589,680 -> 589,850
335,648 -> 232,545
499,216 -> 405,216
942,710 -> 942,455
969,556 -> 721,556
756,552 -> 756,902
98,870 -> 445,870
476,833 -> 476,269
820,127 -> 407,127
337,519 -> 714,519
756,95 -> 11,840
317,339 -> 317,286
353,86 -> 43,86
93,950 -> 938,105
705,509 -> 705,319
244,879 -> 721,402
434,794 -> 711,517
272,381 -> 431,381
652,104 -> 652,587
850,866 -> 34,50
645,902 -> 79,336
701,39 -> 701,295
492,793 -> 95,396
352,554 -> 395,554
123,405 -> 322,206
941,745 -> 716,520
450,512 -> 569,631
42,25 -> 817,800
909,387 -> 909,863
919,934 -> 919,546
439,881 -> 569,881
167,866 -> 167,669
242,264 -> 242,694
981,786 -> 228,33
452,434 -> 452,660
22,26 -> 22,29
26,155 -> 677,806
801,627 -> 313,627
657,135 -> 657,270
872,875 -> 440,443
636,248 -> 636,338
776,51 -> 93,51
498,600 -> 894,600
263,984 -> 263,807
416,390 -> 899,873
269,137 -> 976,137
752,12 -> 752,617
55,925 -> 548,925
856,551 -> 771,551
653,93 -> 653,587
403,286 -> 403,417
895,706 -> 221,32
139,822 -> 139,928
696,194 -> 696,143
270,678 -> 710,678
879,353 -> 879,360
949,712 -> 752,712
665,661 -> 817,661
462,952 -> 980,434
692,766 -> 692,478
157,117 -> 144,117
438,701 -> 408,701
401,703 -> 401,724
876,831 -> 108,63
749,892 -> 832,892
455,124 -> 455,776
551,222 -> 551,372
533,80 -> 726,80
342,740 -> 56,740
793,370 -> 34,370
949,614 -> 949,623
610,287 -> 610,760
978,834 -> 85,834
644,894 -> 644,341
35,887 -> 176,887
168,958 -> 964,162
341,886 -> 341,470
417,845 -> 417,702
338,347 -> 304,313
651,10 -> 72,10
853,160 -> 853,85
381,568 -> 436,623
794,437 -> 250,437
861,72 -> 206,72
807,813 -> 807,827
820,502 -> 820,329
547,508 -> 547,773
160,129 -> 160,175
756,468 -> 756,80
442,661 -> 405,661
304,817 -> 304,765
99,42 -> 957,900
212,110 -> 854,752
44,620 -> 661,620
212,311 -> 784,883
329,671 -> 329,908
86,359 -> 553,826
257,799 -> 934,122
409,663 -> 409,367
528,623 -> 593,688
957,525 -> 544,938
846,766 -> 113,33
176,680 -> 176,102
167,287 -> 167,929
932,870 -> 834,968
86,774 -> 49,774
745,231 -> 70,906
435,760 -> 138,463
776,810 -> 625,810
928,930 -> 76,78
602,24 -> 602,688
394,424 -> 65,424
946,966 -> 93,113
494,39 -> 951,39
607,699 -> 832,699
13,403 -> 391,403
726,475 -> 726,29
828,625 -> 836,617
396,770 -> 167,770
28,546 -> 374,200
56,113 -> 837,894
290,589 -> 740,139
930,805 -> 296,171
646,895 -> 49,895
111,15 -> 111,497
11,274 -> 570,833
257,624 -> 603,624
63,844 -> 666,844
846,661 -> 846,464
431,72 -> 431,674
726,674 -> 726,40
286,660 -> 286,909
847,222 -> 847,861
325,896 -> 325,416
793,953 -> 365,953
987,956 -> 62,31
845,853 -> 363,371
79,782 -> 506,782
424,21 -> 424,369
938,162 -> 177,923
86,193 -> 799,906
320,164 -> 320,654
840,306 -> 840,711
852,736 -> 852,690
876,966 -> 143,233
787,926 -> 38,177
374,112 -> 340,112
132,541 -> 740,541
29,28 -> 968,967
916,212 -> 170,958
371,553 -> 521,403
88,796 -> 870,796
656,367 -> 71,367
785,166 -> 785,427
320,30 -> 320,549
909,527 -> 816,620
832,965 -> 302,965
672,259 -> 80,259
578,513 -> 578,243
975,561 -> 537,123
135,330 -> 188,330
501,695 -> 501,573
717,230 -> 878,230
854,501 -> 27,501
705,885 -> 950,885
704,338 -> 704,630
477,485 -> 864,485
901,42 -> 305,638
660,540 -> 660,546
555,79 -> 190,79
226,126 -> 800,700
575,908 -> 944,908
94,478 -> 94,746
461,425 -> 929,893
861,429 -> 451,19
832,825 -> 179,172
186,133 -> 298,133
684,270 -> 558,270
786,872 -> 125,872
649,178 -> 649,595
893,738 -> 412,257
760,854 -> 901,713
16,914 -> 866,64
935,928 -> 266,259
323,229 -> 32,229
608,828 -> 608,49
715,892 -> 74,251
787,187 -> 787,903
405,793 -> 405,183
232,704 -> 232,389
130,706 -> 130,657

22
2021/day06.hs Normal file
View File

@ -0,0 +1,22 @@
module Day06 where
import Data.Array
import Data.List.Split
updateArray :: Array Int Int -> Array Int Int
updateArray a = array (0,8) $ map (\i -> (i,calculate i)) [0..8] where
calculate 8 = a ! 0
calculate 6 = (a ! 0) + (a ! 7)
calculate x = a ! (x+1)
calcArray :: Int -> Array Int Int -> Array Int Int
calcArray n = foldr (.) id $ replicate n updateArray
main :: IO ()
main = do
input <- map (read :: String -> Int) . splitOn "," <$> getContents
let starting_array = array (0,8) $ map (\val -> (val,length $ filter (==val) input)) [0..8]
putStr "part 1: "
print $ sum $ elems $ calcArray 80 starting_array
putStr "part 2: "
print $ sum $ elems $ calcArray 256 starting_array

1
2021/day06.input Normal file
View File

@ -0,0 +1 @@
4,5,3,2,3,3,2,4,2,1,2,4,5,2,2,2,4,1,1,1,5,1,1,2,5,2,1,1,4,4,5,5,1,2,1,1,5,3,5,2,4,3,2,4,5,3,2,1,4,1,3,1,2,4,1,1,4,1,4,2,5,1,4,3,5,2,4,5,4,2,2,5,1,1,2,4,1,4,4,1,1,3,1,2,3,2,5,5,1,1,5,2,4,2,2,4,1,1,1,4,2,2,3,1,2,4,5,4,5,4,2,3,1,4,1,3,1,2,3,3,2,4,3,3,3,1,4,2,3,4,2,1,5,4,2,4,4,3,2,1,5,3,1,4,1,1,5,4,2,4,2,2,4,4,4,1,4,2,4,1,1,3,5,1,5,5,1,3,2,2,3,5,3,1,1,4,4,1,3,3,3,5,1,1,2,5,5,5,2,4,1,5,1,2,1,1,1,4,3,1,5,2,3,1,3,1,4,1,3,5,4,5,1,3,4,2,1,5,1,3,4,5,5,2,1,2,1,1,1,4,3,1,4,2,3,1,3,5,1,4,5,3,1,3,3,2,2,1,5,5,4,3,2,1,5,1,3,1,3,5,1,1,2,1,1,1,5,2,1,1,3,2,1,5,5,5,1,1,5,1,4,1,5,4,2,4,5,2,4,3,2,5,4,1,1,2,4,3,2,1