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(-)
@@ -25,4 +25,6 @@
| 9 | Haskell |
| 10 | Haskell |
| 11 | Haskell |
| 12 | Rust |
| 13 | Haskell |
+---------------------+
@@ -15,11 +15,11 @@
parseMultipleGrids :: Parser [[[Bool]]]
parseMultipleGrids = parseGrid `sepBy` string "\n\n"
parseMultipleGrids = parseGrid `sepBy` string "\n"
parseGrid :: Parser [[Bool]]
parseGrid = parseGridRow `sepBy` char '\n'
parseGrid = parseGridRow `endBy` char '\n'
parseGridRow :: Parser [Bool]
@@ -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
@@ -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
@@ -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();
}
@@ -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
@@ -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)
@@ -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)
@@ -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)
@@ -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