Skip to content

Commit 2fec672

Browse files
committed
Add 2019, 2020
0 parents  commit 2fec672

File tree

17 files changed

+3508
-0
lines changed

17 files changed

+3508
-0
lines changed

2019/1/input

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
114739
2+
132732
3+
123925
4+
146385
5+
72590
6+
51932
7+
64164
8+
72919
9+
97336
10+
101791
11+
101477
12+
95677
13+
89907
14+
76998
15+
60782
16+
109021
17+
138070
18+
113374
19+
68672
20+
133830
21+
134079
22+
89079
23+
72321
24+
142643
25+
102734
26+
103743
27+
77864
28+
111363
29+
72140
30+
64483
31+
139567
32+
141318
33+
132736
34+
87824
35+
81937
36+
123404
37+
56730
38+
54447
39+
63602
40+
84561
41+
145159
42+
93598
43+
137770
44+
52943
45+
146307
46+
91674
47+
143183
48+
52543
49+
143052
50+
107171
51+
80699
52+
130825
53+
141406
54+
75771
55+
123497
56+
87982
57+
144545
58+
125153
59+
147925
60+
106898
61+
60193
62+
111251
63+
102454
64+
135886
65+
141408
66+
84556
67+
94143
68+
97958
69+
86474
70+
96848
71+
126314
72+
51037
73+
140379
74+
142065
75+
73629
76+
102432
77+
123801
78+
68146
79+
107720
80+
51512
81+
123873
82+
104308
83+
59601
84+
149175
85+
80400
86+
76822
87+
116115
88+
87990
89+
90592
90+
68688
91+
133868
92+
78357
93+
91425
94+
149328
95+
123010
96+
93278
97+
101150
98+
120352
99+
107642
100+
136050

2019/1/main.hs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
input :: String -> [Int]
2+
input l = [read l :: Int | l <- lines l]
3+
4+
calc x = max (x `div` 3 - 2) 0
5+
6+
calc_rec x
7+
| x <= 0 = 0
8+
| otherwise = calc x + calc_rec (calc x)
9+
10+
main = do
11+
n <- readFile "input"
12+
let l = input n
13+
print (sum [calc x | x <- l])
14+
print (sum [calc_rec x | x <- l])

2019/2/main

994 KB
Binary file not shown.

2019/2/main.hi

2.24 KB
Binary file not shown.

2019/2/main.hs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import Data.Array.IO
2+
3+
--init_mem = [1, 0, 0, 0, 99]
4+
--init_mem = [2, 3, 0, 3, 99]
5+
--init_mem = [1,1,1,4,99,5,6,0,99]
6+
init_mem = [1, 0, 0, 3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,9,19,1,10,19,23,2,9,23,27,1,6,27,31,2,31,9,35,1,5,35,39,1,10,39,43,1,10,43,47,2,13,47,51,1,10,51,55,2,55,10,59,1,9,59,63,2,6,63,67,1,5,67,71,1,71,5,75,1,5,75,79,2,79,13,83,1,83,5,87,2,6,87,91,1,5,91,95,1,95,9,99,1,99,6,103,1,103,13,107,1,107,5,111,2,111,13,115,1,115,6,119,1,6,119,123,2,123,13,127,1,10,127,131,1,131,2,135,1,135,5,0,99,2,14,0,0]
7+
8+
-- Perform a single operation
9+
op' :: (Int -> Int -> Int) -> IOArray Int Int -> Int -> IO ()
10+
op' op mem i = do
11+
x <- readArray mem (i + 1)
12+
y <- readArray mem (i + 2)
13+
z <- readArray mem (i + 3)
14+
x <- readArray mem x
15+
y <- readArray mem y
16+
writeArray mem z (op x y)
17+
18+
-- Parse a sequence of operations
19+
parse :: IOArray Int Int -> Int -> IO ()
20+
parse mem i = do
21+
op <- readArray mem i
22+
case op :: Int of
23+
1 -> do
24+
op' (+) mem i
25+
parse mem (i + 4)
26+
2 -> do
27+
op' (*) mem i
28+
parse mem (i + 4)
29+
99 -> return ()
30+
_ -> error "invalid op"
31+
32+
-- Create a mutable array from init_array
33+
load' :: IO (IOArray Int Int)
34+
load' = do
35+
mem <- newArray (0, (length init_mem) - 1) 0 :: IO (IOArray Int Int)
36+
sequence_ $ do
37+
(i, x) <- zip [0..] init_mem
38+
return $ writeArray mem i x
39+
return mem
40+
41+
main = do
42+
43+
-- Part 1
44+
mem <- load'
45+
writeArray mem 1 12
46+
writeArray mem 2 2
47+
parse mem 0
48+
49+
-- Dump result
50+
readArray mem 0 >>= print
51+
52+
-- Bruteforce attempt to find 'search'
53+
let search = 19690720
54+
sequence_ $ do
55+
x <- [0..100]
56+
y <- [0..100]
57+
return $ do
58+
mem <- load'
59+
writeArray mem 1 x
60+
writeArray mem 2 y
61+
parse mem 0
62+
z <- readArray mem 0
63+
if z == search
64+
then print (x, y, z, 100 * x + y)
65+
else return ()

2019/2/main.o

47.4 KB
Binary file not shown.

2019/3/better.hs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
-- Split a string of directions
2+
splitComma s = case dropWhile (== ',') s of
3+
"" -> []
4+
s' -> w : splitComma s''
5+
where
6+
(w, s'') = break (== ',') s'
7+
8+
-- Convert a direction token to a vector
9+
parseToken :: String -> (Int, Int)
10+
parseToken (d:n) = case d of
11+
'R' -> (read n, 0)
12+
'D' -> (0, -read n)
13+
'L' -> (-read n, 0)
14+
'U' -> (0, read n)
15+
16+
-- Add a vector to a point
17+
add (x, y) (dx, dy) = (x + dx, y + dy)
18+
19+
-- Generate a list of points from consecutive vectors
20+
createPath p [] = [p]
21+
createPath p (d:ds) = p : createPath (add p d) ds
22+
23+
between a m b = min a b <= m && m <= max a b
24+
25+
-- Check if 2 wires intersect, i.e.
26+
-- x
27+
--
28+
-- y1
29+
-- |
30+
-- y x1-+---x2
31+
-- |
32+
-- y2
33+
intersect (x, y1, y2) (y, x1, x2) =
34+
between x1 x x2 && between y1 y y2
35+
36+
-- Get all intersections with one straight wire
37+
intersection :: (Char, Int, Int, Int) -> [(Int, Int)] -> [(Int, Int)]
38+
intersection _ [_] = []
39+
intersection p@(d,c,a,b) ((ax,ay):q@(bx,by):wires) =
40+
case d of
41+
'x' ->
42+
if ay - by == 0
43+
then intersection p (q:wires)
44+
else if intersect (c, a, b) (ax, ay, by)
45+
then (ax, c) : intersection p (q:wires)
46+
else intersection p (q:wires)
47+
'y' ->
48+
if ax - bx == 0
49+
then intersection p (q:wires)
50+
else if intersect (c, a, b) (ay, ax, bx)
51+
then (c, ay) : intersection p (q:wires)
52+
else intersection p (q:wires)
53+
54+
-- Get all intersections between two wires
55+
intersections [_] _ = []
56+
intersections ((ax, ay):q@(bx, by):wires1) wires2
57+
| ax == bx = intersection ('y', ax, ay, by) wires2 ++ intersections (q:wires1) wires2
58+
| ay == by = intersection ('x', ay, ax, bx) wires2 ++ intersections (q:wires1) wires2
59+
60+
-- Get the distance of a point from the origin
61+
distance (x, y) = abs x + abs y
62+
63+
-- Get the point closest to the origin
64+
closestPoint (a:[]) = a
65+
closestPoint (a:b:ps)
66+
| distance a < distance b = closestPoint (a:ps)
67+
| otherwise = closestPoint (b:ps)
68+
69+
-- Get the amount of steps to take along a path to reach the given point
70+
stepsToPoint [_] _ = -1
71+
stepsToPoint ((ax, ay):b@(bx, by):path) p@(x, y)
72+
| ax == x && bx == x =
73+
if between ay y by
74+
then abs (ay - y)
75+
else ly + stepsToPoint (b:path) p
76+
| ay == y && by == y =
77+
if between ax x bx
78+
then abs (ax - x)
79+
else lx + stepsToPoint (b:path) p
80+
| ax == bx = ly + stepsToPoint (b:path) p
81+
| ay == by = lx + stepsToPoint (b:path) p
82+
where
83+
lx = abs (ax - bx)
84+
ly = abs (ay - by)
85+
86+
-- Find the best intersection between two wires such that signal latency is
87+
-- minimal
88+
leastSteps w1 w2 (p:[]) = stepsToPoint w1 p + stepsToPoint w2 p
89+
leastSteps w1 w2 (p:ps) = min (leastSteps w1 w2 [p]) (leastSteps w1 w2 ps)
90+
91+
main = do
92+
let f l = createPath (0, 0) $ [parseToken x | x <- splitComma l]
93+
let g a b = intersections a b
94+
95+
let a = f "R8,U5,L5,D3"
96+
b = f "U7,R6,D4,L4"
97+
98+
let a = f "R75,D30,R83,U83,L12,D49,R71,U7,L72"
99+
b = f "U62,R66,U55,R34,D71,R55,D58,R83"
100+
101+
let a = f "R990,U408,L583,U275,R483,U684,R437,U828,R108,U709,R378,U97,R252,D248,R413,U750,R428,D545,R570,D795,L204,D975,L557,U160,L861,U106,R436,U934,R81,D237,R660,U704,L451,U135,R282,D391,R39,D109,R125,U918,R214,U481,R853,U825,L91,D763,R335,U868,R42,U218,R152,D429,R414,D607,R28,U436,R7,U770,L215,D373,R209,U440,L536,U120,R900,D46,R635,D75,R58,U267,L581,U474,L858,U172,R725,U54,R291,D274,L583,D743,L130,U563,R137,U524,R659,D997,R131,D364,R883,D222,R628,U579,R801,D890,L519,D749,L620,U60,L759,D759,R376,U769,L910,D570,L814,U954,L153,D42,L784,D66,L844,U29,L794,D342,L924,U825,R447,U828,R404,D52,L330,D876,R125,U203,R245,U936,R866,D804,L186,U693,L620,D722,L32,D735,L191,D217,R68,U209,L736,U365,R280,U608,L450,D240,L282,U434,R589,U94,R470,D5,R49,U407,R552,D651,L69,U518,L358,D130,L710,D929,L315,U345,L511,D229,L557,U44,L890,D702,L181,D61,L208,U553,R878,U354,R787,U624,L961,D92,L891,U70,R203,U255,R532,U154,R299,U934,L609,D985,R115,U757,L13,D368,R936,D742,L412,U346,R56,D67,R371,D175,R868,U107,R806,D530,L40,U153,R374,D223,R517,D481,L194,U545,L356,U906,L999,D885,R967,U407,L141,U927,L489,U959,L992,U638,R332,U51,R256,U901,L891,U803,L885,U804,L242,U180,R277,U693,R935,D253,L68,D153,L614,D596,L999,D633,R995,D803,R17,U303,L569,U231,R737,D970,L45,D860,L225,D65,R41,D313,R698,D340,R599,D531,R55,D568,L911,D547,R196,D228,R868,D227,R262,U525,R104,D625,R570,U968,L276,D586,R690,D73,L336,U287,R294,U148,R781,D395,R478,D804,L429,U872,L351,D910,L597,U726,L320,D964,R928,U2,R540,D325,L222"
102+
b = f "L998,U662,R342,U104,R140,U92,R67,D102,L225,U265,R641,U592,L295,D77,R415,U908,L640,D381,R312,U44,R424,D847,R892,D625,L337,D344,L917,D914,R127,D273,L627,U812,L200,D262,R226,U273,R911,U597,L888,U28,R921,U464,R254,U771,R818,D808,L239,D225,L280,U785,R322,D831,L622,U506,R139,U12,L491,D572,L172,U685,R54,U747,L812,D717,R874,U428,L867,U174,R360,D36,R217,D539,R210,D791,L82,D665,L190,D313,R649,U849,R63,U385,R105,U806,L207,U697,L823,D272,R830,D952,L386,U987,R775,U517,R139,D756,R545,D973,L743,D286,R261,U448,R946,U884,L903,D142,R28,D374,R259,U403,R689,D245,L302,D134,R710,U762,L67,D561,R801,D140,L887,U346,L227,U682,L350,D218,L711,U755,R226,D277,R114,D61,R992,U602,L191,U640,R733,D329,R862,U242,R754,D161,L52,D974,L251,D444,L552,U977,R174,U483,R869,D955,R925,U693,R610,D353,L843,U148,L866,D167,R412,D31,L847,D979,L282,D797,L837,U473,L402,U193,L332,D603,R48,D589,L760,D673,L843,U428,R779,D592,L688,D141,R851,D642,R559,U939,R999,D64,L297,U817,R670,U322,L768,D936,L39,U95,L342,U849,L692,U714,L732,D734,L373,U66,L577,D453,R336,U760,L217,U542,R920,U24,R529,D594,L34,D79,R877,D965,R932,U460,R879,U26,R803,U876,L780,U956,L235,D270,L315,D577,R835,U750,R414,D584,L828,U335,L563,U238,L815,U780,L550,U18,R743,D54,L816,U344,L806,D197,L518,D682,L835,U255,L666,U442,L286,D543,R102,D52,L570,D787,L763,D223,R279,D892,L828,D111,L554,D452,R575,D299,R932,D187,L439,U616,L278,D701,L360,D524,L891,U953,L896,U788,R776,U782,L71,D741,L652,U121,R669,D809,L662,U319,R392,D313,R870,U794,R937,D469,R571,D761,R947"
103+
104+
print a
105+
print b
106+
let c = filter (/= (0,0)) $ g a b
107+
d = closestPoint c
108+
print $ c
109+
print $ d
110+
print $ distance d
111+
print $ leastSteps a b c

2019/3/main

1.14 MB
Binary file not shown.

2019/3/main.hi

2.42 KB
Binary file not shown.

2019/3/main.hs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
-- I know this is utter crap and doesn't work, I just wanted to understand
2+
-- mutability in Haskell :(
3+
4+
import Control.Monad
5+
import Data.Array.IO
6+
7+
newField :: IO (IOArray (Int, Int) Int)
8+
newField = newArray ((-20000, -20000), (20000, 20000)) 0
9+
10+
splitComma s = case dropWhile (== ',') s of
11+
"" -> []
12+
s' -> w : splitComma s'' where (w, s'') = break (== ',') s'
13+
14+
parseToken :: String -> (Int, Int)
15+
parseToken (d:n) = case d of
16+
'R' -> (read n, 0)
17+
'D' -> (0, -read n)
18+
'L' -> (-read n, 0)
19+
'U' -> (0, read n)
20+
21+
addDelta (x, y) (dx, dy) = (x + dx, y + dy)
22+
23+
iter a b = [min a b..max a b]
24+
25+
iter2 (ax, ay) (bx, by) = [(a, b) | a <- iter ax bx, b <- iter ay by]
26+
27+
visit field p = do
28+
v <- readArray field p
29+
writeArray field p (v + 1)
30+
31+
visitList :: (IOArray (Int, Int) Int) -> [(Int, Int)] -> IO ()
32+
visitList field [] = do
33+
return ()
34+
visitList field (p:ps) = do
35+
visit field p
36+
visitList field ps
37+
return ()
38+
39+
step :: (IOArray (Int, Int) Int) -> (Int, Int) -> [(Int, Int)] -> IO ()
40+
step field start [] = do
41+
return ()
42+
step field start (p:ps) = do
43+
let s = addDelta start p
44+
visitList field $ filter (/= start) $ iter2 start s
45+
step field s ps
46+
47+
crosses :: (Ix i) => (IOArray i Int) -> i -> IO Bool
48+
crosses field p = do
49+
v <- readArray field p
50+
return $ v >= 2
51+
52+
run field start walk = do
53+
step field start wlk
54+
where
55+
wlk = [parseToken x | x <- splitComma walk]
56+
noop x = x
57+
58+
printField :: (IOArray (Int, Int) Int) -> (Int, Int) -> (Int, Int) -> IO ()
59+
printField field (dimBX, dimBY) (dimAX, dimAY) = do
60+
sequence_ $ do
61+
y <- iter dimAY dimBY
62+
return $ do
63+
sequence $ do
64+
x <- iter dimAX dimBX
65+
return $ do
66+
v <- readArray field (x, y)
67+
putStr (show v)
68+
putStrLn ""
69+
return ()
70+
71+
72+
solve field (ox, oy) a b = do
73+
sequence_ $ do
74+
(x, y) <- iter2 a b
75+
return $ do
76+
b <- crosses field (x, y)
77+
if b
78+
then print (x - ox, y - oy, x - ox + y - oy)
79+
else return ()
80+
81+
82+
main = do
83+
let a = "R8,U5,L5,D3"
84+
b = "U7,R6,D4,L4"
85+
ldim = (0, 0)
86+
udim = (10, 10)
87+
let a = "R75,D30,R83,U83,L12,D49,R71,U7,L72"
88+
b = "U62,R66,U55,R34,D71,R55,D58,R83"
89+
ldim = (-20, -70)
90+
udim = (295, 225)
91+
let a = "R990,U408,L583,U275,R483,U684,R437,U828,R108,U709,R378,U97,R252,D248,R413,U750,R428,D545,R570,D795,L204,D975,L557,U160,L861,U106,R436,U934,R81,D237,R660,U704,L451,U135,R282,D391,R39,D109,R125,U918,R214,U481,R853,U825,L91,D763,R335,U868,R42,U218,R152,D429,R414,D607,R28,U436,R7,U770,L215,D373,R209,U440,L536,U120,R900,D46,R635,D75,R58,U267,L581,U474,L858,U172,R725,U54,R291,D274,L583,D743,L130,U563,R137,U524,R659,D997,R131,D364,R883,D222,R628,U579,R801,D890,L519,D749,L620,U60,L759,D759,R376,U769,L910,D570,L814,U954,L153,D42,L784,D66,L844,U29,L794,D342,L924,U825,R447,U828,R404,D52,L330,D876,R125,U203,R245,U936,R866,D804,L186,U693,L620,D722,L32,D735,L191,D217,R68,U209,L736,U365,R280,U608,L450,D240,L282,U434,R589,U94,R470,D5,R49,U407,R552,D651,L69,U518,L358,D130,L710,D929,L315,U345,L511,D229,L557,U44,L890,D702,L181,D61,L208,U553,R878,U354,R787,U624,L961,D92,L891,U70,R203,U255,R532,U154,R299,U934,L609,D985,R115,U757,L13,D368,R936,D742,L412,U346,R56,D67,R371,D175,R868,U107,R806,D530,L40,U153,R374,D223,R517,D481,L194,U545,L356,U906,L999,D885,R967,U407,L141,U927,L489,U959,L992,U638,R332,U51,R256,U901,L891,U803,L885,U804,L242,U180,R277,U693,R935,D253,L68,D153,L614,D596,L999,D633,R995,D803,R17,U303,L569,U231,R737,D970,L45,D860,L225,D65,R41,D313,R698,D340,R599,D531,R55,D568,L911,D547,R196,D228,R868,D227,R262,U525,R104,D625,R570,U968,L276,D586,R690,D73,L336,U287,R294,U148,R781,D395,R478,D804,L429,U872,L351,D910,L597,U726,L320,D964,R928,U2,R540,D325,L222"
92+
b = "L998,U662,R342,U104,R140,U92,R67,D102,L225,U265,R641,U592,L295,D77,R415,U908,L640,D381,R312,U44,R424,D847,R892,D625,L337,D344,L917,D914,R127,D273,L627,U812,L200,D262,R226,U273,R911,U597,L888,U28,R921,U464,R254,U771,R818,D808,L239,D225,L280,U785,R322,D831,L622,U506,R139,U12,L491,D572,L172,U685,R54,U747,L812,D717,R874,U428,L867,U174,R360,D36,R217,D539,R210,D791,L82,D665,L190,D313,R649,U849,R63,U385,R105,U806,L207,U697,L823,D272,R830,D952,L386,U987,R775,U517,R139,D756,R545,D973,L743,D286,R261,U448,R946,U884,L903,D142,R28,D374,R259,U403,R689,D245,L302,D134,R710,U762,L67,D561,R801,D140,L887,U346,L227,U682,L350,D218,L711,U755,R226,D277,R114,D61,R992,U602,L191,U640,R733,D329,R862,U242,R754,D161,L52,D974,L251,D444,L552,U977,R174,U483,R869,D955,R925,U693,R610,D353,L843,U148,L866,D167,R412,D31,L847,D979,L282,D797,L837,U473,L402,U193,L332,D603,R48,D589,L760,D673,L843,U428,R779,D592,L688,D141,R851,D642,R559,U939,R999,D64,L297,U817,R670,U322,L768,D936,L39,U95,L342,U849,L692,U714,L732,D734,L373,U66,L577,D453,R336,U760,L217,U542,R920,U24,R529,D594,L34,D79,R877,D965,R932,U460,R879,U26,R803,U876,L780,U956,L235,D270,L315,D577,R835,U750,R414,D584,L828,U335,L563,U238,L815,U780,L550,U18,R743,D54,L816,U344,L806,D197,L518,D682,L835,U255,L666,U442,L286,D543,R102,D52,L570,D787,L763,D223,R279,D892,L828,D111,L554,D452,R575,D299,R932,D187,L439,U616,L278,D701,L360,D524,L891,U953,L896,U788,R776,U782,L71,D741,L652,U121,R669,D809,L662,U319,R392,D313,R870,U794,R937,D469,R571,D761,R947"
93+
ldim = (-20000, -20000)
94+
udim = (20000, 20000)
95+
96+
field <- newField
97+
let port = (1, 1)
98+
print ""
99+
run field port a
100+
--printField field ldim udim
101+
print ""
102+
run field port b
103+
--printField field ldim udim
104+
print ""
105+
solve field port ldim udim

2019/3/main.o

41.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)