Skip to content

Commit

Permalink
2024-08
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 8, 2024
1 parent a0e34cb commit 05931bc
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
50 changes: 50 additions & 0 deletions inputs/2024/08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
...........g......................................
......e.g......s.............R...........I........
............g........................I............
..b......Q....s.....................P.............
.......e..T...................K...........P...F...
.....g................U.............4.............
.........b..........4..RU..................1.F....
....a.....Q..........b........R..U...............1
.S...T............s.............I.........f.......
...A....T...............................I.........
.....Qa............A.G...K...........P............
...........................G................1.....
...D...................................4.f........
..................................................
................k.......R....................t....
.........T.e..............K........u.......t......
....................A.............................
......S....a.............F...........KG...........
....D...h......k..................................
..D...............k............................4..
..............................................i...
.........S..................d.....................
......QU....S......s..d...G............i..........
...........d....9...F.h...E.......................
.................d....B...........................
...h........................H.......t.............
.........h.B..3.E.............H..r................
.......E......B......2...5.....H..................
.z...........9................................t...
.....9...D........................................
.....Z.......39..a................................
..........3.............r.........................
...............Er.............................7...
.........................J...k.r.q.......i.8.p....
.......................u...............H.p..q.....
..............................i.u6........p.......
....................................0.............
...............3..J....P...0......................
........................2j........................
...............................j2B0...............
................J..2...5.....6......p....8........
............y.........................7...........
..............5.........y...........6.............
................................j.................
.........................Y.J.....0................
.........................................y........
..................Z...uy...................q......
.......z.........Z............Y.6.............8...
z.........................Y..........7............
....................Z...5..Y......................
5 changes: 5 additions & 0 deletions solutions/solutions.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -1147,3 +1147,8 @@ executable sln_2024_06
executable sln_2024_07
import: day
main-is: 2024/07.hs

executable sln_2024_08
import: day
main-is: 2024/08.hs
build-depends: containers
49 changes: 49 additions & 0 deletions solutions/src/2024/08.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# Language QuasiQuotes, ImportQualifiedPost #-}
{-|
Module : Main
Description : Day 8 solution
Copyright : (c) Eric Mertens, 2024
License : ISC
Maintainer : emertens@gmail.com
<https://adventofcode.com/2024/day/8>
-}
module Main (main) where

import Advent (getInputMap)
import Advent.Coord (coordRow, coordCol, Coord (C))
import Control.Monad (guard)
import Data.List (nub, tails)
import Data.Map qualified as Map

-- | >>> :main
-- 303
-- 1045
main :: IO ()
main =
do input <- getInputMap 2024 8
let locs = Map.filter (\x -> length x > 1)
$ Map.fromListWith (++) [(v, [k]) | (k,v) <- Map.assocs input, v /= '.']
print $ length $ nub $
do posns <- Map.elems locs
x:ys <- tails posns
y <- ys
node <- [y + (y - x), x + (x - y)]
guard (Map.member node input)
pure node
print $ length $ nub $ concat (Map.elems locs) ++
do posns <- Map.elems locs
x:ys <- tails posns
y <- ys
z <- Map.keys input
guard (inlined x y z)
[z,x,y]

inlined :: Coord -> Coord -> Coord -> Bool
inlined x y z
| coordCol x == coordCol z = coordCol x == coordCol y
| otherwise = coordCol z /= coordCol y && slope z x == slope y x

slope :: Coord -> Coord -> Rational
slope (C y1 x1) (C y2 x2) = fromIntegral (y2-y1) / fromIntegral (x2-x1)

0 comments on commit 05931bc

Please sign in to comment.