🏡 index : ~doyle/aoc.git

author Jordan Doyle <jordan@doyle.la> 2023-12-21 4:17:45.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-12-21 4:17:45.0 +00:00:00
commit
a5804b6f392062589674387ee31cc67855efcb3d [patch]
tree
99d4033fefe3b9a92600bea5e7f73110570a4d10
parent
4b685ca6b215d59282832ee6f5550bdaa7b41e49
download
a5804b6f392062589674387ee31cc67855efcb3d.tar.gz

Add day 13



Diff

 README     |  2 ++
 aoc.hs     |  4 ++--
 2023/10.hs |  2 +-
 2023/11.hs |  2 +-
 2023/12.rs | 14 +++++++++-----
 2023/13.hs | 39 +++++++++++++++++++++++++++++++++++++++
 2023/6.hs  |  2 +-
 2023/7.hs  |  2 +-
 2023/8.hs  |  2 +-
 2023/9.hs  |  2 +-
 10 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/README b/README
index afc2a1c..0299bd5 100644
--- a/README
+++ a/README
@@ -25,4 +25,6 @@
|        9 | Haskell  |
|       10 | Haskell  |
|       11 | Haskell  |
|       12 | Rust     |
|       13 | Haskell  |
+---------------------+
diff --git a/aoc.hs b/aoc.hs
index dda9f26..55a9494 100644
--- a/aoc.hs
+++ a/aoc.hs
@@ -15,11 +15,11 @@

-- parse multiple grids
parseMultipleGrids :: Parser [[[Bool]]]
parseMultipleGrids = parseGrid `sepBy` string "\n\n"
parseMultipleGrids = parseGrid `sepBy` string "\n"

-- parse an entire grid
parseGrid :: Parser [[Bool]]
parseGrid = parseGridRow `sepBy` char '\n'
parseGrid = parseGridRow `endBy` char '\n'

-- parse an incoming grow
parseGridRow :: Parser [Bool]
diff --git a/2023/10.hs b/2023/10.hs
index e4c560f..39d22ba 100755
--- a/2023/10.hs
+++ a/2023/10.hs
@@ -1,6 +1,7 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (readAndParseStdin)
import Data.List
import Data.Maybe (listToMaybe)
import qualified Data.Set as Set
@@ -8,7 +9,6 @@
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
import Aoc (readAndParseStdin)

main = do
  input <- buildPipeLoop <$> readAndParseStdin parseInput
diff --git a/2023/11.hs b/2023/11.hs
index 53f5cfb..bc3ae24 100755
--- a/2023/11.hs
+++ a/2023/11.hs
@@ -1,12 +1,12 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (parseGrid, readAndParseStdin)
import Data.List (tails, transpose)
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
import Aoc (parseGrid, readAndParseStdin)

main = do
  input <- readAndParseStdin parseGrid
diff --git a/2023/12.rs b/2023/12.rs
index 122d022..d79ea3a 100644
--- a/2023/12.rs
+++ a/2023/12.rs
@@ -64,11 +64,14 @@
    let mut seen = String::new();
    out_file.read_to_string(&mut seen).unwrap();

    let seen = seen
        .lines()
        .filter_map(|v| v.split_once(','))
        .map(|(idx, _)| usize::from_str(idx).unwrap())
        .collect::<Vec<_>>();
    let (mut acc, seen) = seen.lines().filter_map(|v| v.split_once(',')).fold(
        (0, Vec::new()),
        |(mut acc, mut seen), (idx, val)| {
            acc += u64::from_str(val).unwrap();
            seen.push(usize::from_str(idx).unwrap());
            (acc, seen)
        },
    );
    eprintln!("recovered state - completed {seen:?}");

    let (completed_send, completed_recv) = std::sync::mpsc::channel();
@@ -144,7 +147,6 @@

    drop(completed_send);

    let mut acc = 0;
    for h in processing_handles {
        acc += h.join().unwrap();
    }
diff --git a/2023/13.hs b/2023/13.hs
new file mode 100755
index 0000000..2546538 100755
--- /dev/null
+++ a/2023/13.hs
@@ -1,0 +1,39 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (parseMultipleGrids, readAndParseStdin)
import Data.Bifunctor (Bifunctor (first))
import Data.List (transpose)

main = do
  input <- readAndParseStdin parseMultipleGrids
  print $ part1 input
  print $ part2 input

part1 :: [[[Bool]]] -> Int
part1 = sum . map (findReflectionAny 0)

part2 :: [[[Bool]]] -> Int
part2 = sum . map (findReflectionAny 1)

findReflectionAny :: Int -> [[Bool]] -> Int
findReflectionAny diffs xs =

  let hl = findReflection diffs xs
      hr = findReflection diffs . reverse $ xs
      vl = findReflection diffs . transpose $ xs
      vr = findReflection diffs . reverse . transpose $ xs
   in case (hl, hr, vl, vr) of
        (v, 0, 0, 0) -> v * 100
        (0, v, 0, 0) -> (length xs - v) * 100
        (0, 0, v, 0) -> v
        (0, 0, 0, v) -> (length . head) xs - v

findReflection :: Int -> [[Bool]] -> Int
findReflection diffs xs = go xs
  where
    go [] = 0
    go xs
      | cmp (first reverse $ splitAt (length xs `div` 2) xs) == diffs = length xs `div` 2
      | otherwise = go (init xs)
    cmp (x1, x2) = sum $ zipWith cmpInner x1 x2
    cmpInner x1 x2 = length . filter id $ zipWith (/=) x1 x2
diff --git a/2023/6.hs b/2023/6.hs
index 6b9afa8..5836634 100755
--- a/2023/6.hs
+++ a/2023/6.hs
@@ -1,12 +1,12 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (readAndParseStdin)
import Control.Applicative ((<*))
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
import Aoc (readAndParseStdin)

{- https://adventofcode.com/2023/day/6 -}

diff --git a/2023/7.hs b/2023/7.hs
index 135e12d..564ce30 100755
--- a/2023/7.hs
+++ a/2023/7.hs
@@ -1,13 +1,13 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (readAndParseStdin)
import Data.List
import Data.Ord
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
import Aoc (readAndParseStdin)

{- https://adventofcode.com/2023/day/7 -}

diff --git a/2023/8.hs b/2023/8.hs
index b137bcd..87781ec 100755
--- a/2023/8.hs
+++ a/2023/8.hs
@@ -1,13 +1,13 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (readAndParseStdin)
import Data.List
import qualified Data.Map as Map
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
import Aoc (readAndParseStdin)

{- https://adventofcode.com/2023/day/8 -}

diff --git a/2023/9.hs b/2023/9.hs
index 370b6d2..9e9dc8d 100755
--- a/2023/9.hs
+++ a/2023/9.hs
@@ -1,13 +1,13 @@
#!/usr/bin/env nix-shell
#!nix-shell --pure -i "runghc -- -i../" -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ ])"

import Aoc (readAndParseStdin)
import Control.Monad (guard)
import Data.List (unfoldr)
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
import Aoc (readAndParseStdin)

main = do
  input <- readAndParseStdin parseInput