summaryrefslogtreecommitdiff
path: root/day08
diff options
context:
space:
mode:
Diffstat (limited to 'day08')
-rw-r--r--day08/day08.hs83
-rw-r--r--day08/day08.input50
2 files changed, 133 insertions, 0 deletions
diff --git a/day08/day08.hs b/day08/day08.hs
new file mode 100644
index 0000000..728dd71
--- /dev/null
+++ b/day08/day08.hs
@@ -0,0 +1,83 @@
+import Data.List
+
+type Vec = (Int, Int)
+type Ant = (Int, Int, Char)
+type Map = (Vec, [Ant])
+
+consume2 :: [Ant] -> Int -> Int -> [Char] -> [Ant]
+consume2 acc _ _ [] = acc
+consume2 acc r c ('.':t) = consume2 acc r (c+1) t
+consume2 acc r c (h:t) = consume2 ((r, c, h):acc) r (c+1) t
+
+consume1 :: [Ant] -> Int -> [[Char]] -> [Ant]
+consume1 acc _ [] = acc
+consume1 acc r (h:t) = consume1 (acc ++ (consume2 [] r 0 h)) (r+1) t
+
+consume :: [Char] -> Map
+consume file =
+ ((length grid, length $ head grid), consume1 [] 0 grid)
+ where grid = lines file
+
+neg :: Vec -> Vec
+neg (r, c) = (-r, -c)
+add :: Vec -> Vec -> Vec
+add (fr, fc) (dr, dc) = (fr + dr, fc + dc)
+scale :: Int -> Vec -> Vec
+scale m (r, c) = (r * m, c * m)
+path :: (Vec, Vec) -> Vec
+path (f, t) = add t $ neg f
+
+inside :: Vec -> Vec -> Bool
+inside (mr, mc) (r, c) =
+ r >= 0 && r < mr && c >= 0 && c < mc
+
+simplify :: Vec -> Vec
+simplify (r, c) =
+ (r `div` d, c `div` d)
+ where d = gcd r c
+
+antiNodes :: Map -> Char -> [Vec]
+antiNodes (size, ants) freq =
+ filter (inside size) $ map (\p@(f, _) -> add f $ scale 2 $ path p) pairs
+ where
+ fants = filter (\(_, _, f) -> f == freq) ants
+ coos = map (\(r, c, _) -> (r, c)) fants
+ pairs = [(a1, a2) | a1 <- coos, a2 <- coos, a1 /= a2]
+
+antiNodes22 :: [Vec] -> Vec -> Vec -> Vec -> [Vec]
+antiNodes22 acc size from del
+ | inside size nxt = antiNodes22 (nxt:acc) size nxt del
+ | otherwise = acc
+ where nxt = add from del
+
+antiNodes21 :: Vec -> (Vec, Vec) -> [Vec]
+antiNodes21 s p@(f, t) =
+ antiNodes22 [] s f d ++
+ [f] ++
+ antiNodes22 [] s f (neg d)
+ where
+ d = simplify $ path p
+
+antiNodes2 :: Map -> Char -> [Vec]
+antiNodes2 (size, ants) freq =
+ nub $ concat $ map (antiNodes21 size) pairs
+ where
+ fants = filter (\(_, _, f) -> f == freq) ants
+ coos = map (\(r, c, _) -> (r, c)) fants
+ pairs = [(a1, a2) | a1 <- coos, a2 <- coos, a1 /= a2]
+
+part1 mp@(_, ants) =
+ length $ nub $ concat $ map (antiNodes mp) freqs
+ where
+ freqs = nub $ map (\(_, _, f) -> f) ants
+
+part2 mp@(_, ants) =
+ length $ nub $ concat $ map (antiNodes2 mp) freqs
+ where
+ freqs = nub $ map (\(_, _, f) -> f) ants
+
+main = do
+ file <- readFile "day08.input"
+ let mp = consume file
+ putStrLn ("Part 1: " ++ (show $ part1 mp))
+ putStrLn ("Part 2: " ++ (show $ part2 mp))
diff --git a/day08/day08.input b/day08/day08.input
new file mode 100644
index 0000000..a0e2233
--- /dev/null
+++ b/day08/day08.input
@@ -0,0 +1,50 @@
+............s...............1.....................
+......................E......3.....S..............
+.......................3.....S....................
+...e........T.t.......S.1...........I.............
+..................B..................I.....O......
+g.......z........i39......B..I....................
+.......s....S.......3......................i..I...
+....e.............2..........B....................
+.......tC...z.......g......1......................
+.E......s....R....................................
+..G...t..........2................................
+.........K...C.......2............................
+....T..e...........5...C..........................
+...T................................O...o.........
+...............................g..............o...
+.........z...................g......i............o
+...9.E............H...........Y.......O...........
+..........R..H...............7.O..................
+...........H.............v......7........B........
+..9.Q.......................W......1........Y.....
+.........................z.7.................Y....
+.....Q................................v...........
+....K.......E.....R...............2..........o....
+.n............H......v...........................Y
+.G.y..........................Q...................
+......G....A5.....................h...............
+..........D...5.w...9.............................
+......n....5...L..................................
+............................v.....................
+............L...0t..........7.....................
+..n....k............y....................W........
+..k..........0.........................W..........
+...n.......R..L..a........................W.......
+.........................................h........
+..0..L........c...b...............................
+.....................8.y..........................
+.......w.................6.............h.......N..
+..........y..4....................................
+...0....8...k.....Z........r......................
+..............a...8Z.........G......4.............
+........4..b.q.....................K..............
+.q...........kZ.K......b..D.........d.............
+.8.....................D................r.........
+.....w.........a...............d........A.........
+................................d.A.hV............
+................c..........D.....V....r...........
+.......Z......6.....l........................A.d..
+...................l..6..c....b......r...........N
+......a....4........q..l..V..c................N...
+l.....w...........q..6............V...............