Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 6: Store only northward turns #193

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hs/src/Day22.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}

-- |
-- Module: Day22
Expand Down
17 changes: 7 additions & 10 deletions hs/src/Day6.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Maybe (catMaybes, isJust)
import Data.Semigroup (Max (Max), sconcat)
import Data.Set (Set)
import Data.Set qualified as Set (empty, insert, lookupGE, member, notMember, singleton)
import Data.Set qualified as Set (empty, insert, member, singleton)
import Data.Text (Text)
import Data.Text qualified as T (lines, unpack)

Expand Down Expand Up @@ -52,14 +52,11 @@ part1 input = length $ nubOrd $ map fst $ start >>= visited maxes blocks

part2 :: Text -> Int
part2 input =
length . filter id . parMap rseq isLoop $
start >>= (zip `ap` scanl (flip Set.insert) Set.empty) . visited maxes blocks
length $ filter id $ start >>= (parMap rseq . isLoop) `ap` (nubOrd . map fst . visited maxes blocks)
where
(maxes, blocks, start) = parse input
isLoop (start'@((y, x), (dy, dx)), seen) =
pos' `Set.notMember` blocks
&& Just pos' /= (fst <$> Set.lookupGE (pos', minBound) seen)
&& or (zipWith Set.member `ap` scanl (flip Set.insert) seen $ visited maxes blocks' start')
where
pos' = (y + dy, x + dx)
blocks' = Set.insert pos' blocks
isLoop start' block = isLoop' 0 Set.empty $ visited maxes (Set.insert block blocks) start'
isLoop' _ _ [] = False
isLoop' (-1) seen ((_, (dy, _)) : rest) = isLoop' dy seen rest
isLoop' _ seen ((pos, (-1, _)) : rest) = pos `Set.member` seen || isLoop' (-1) (Set.insert pos seen) rest
isLoop' _ seen ((_, (dy, _)) : rest) = isLoop' dy seen rest
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ class Day6(input: String) {
suspend fun part2() = initialWalk.asFlow().drop(1).flatMapMerge { (y, x) ->
val lines = lines.toMutableList()
lines[y] = StringBuilder(lines[y]).apply { set(x, '#') }.toString()
flowOf(Unit).filter { !lines.walk(initialPosition).all(mutableSetOf<Any?>()::add) }
flowOf(Unit).filter {
var last = 0
val visited = mutableSetOf<IntPair>()
!lines.walk(initialPosition).all { (pos, dir) ->
(last == -1 || dir.first != -1 || visited.add(pos)).also { last = dir.first }
}
}
}.count()

companion object {
Expand Down
12 changes: 7 additions & 5 deletions py/aoc2024/day6.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ def _part2(
max_bounds: tuple[int, int],
obstructions: Iterable[tuple[int, int]],
) -> bool:
visited = set()
for key in _visit(initial_pos, max_bounds, obstructions):
if key in visited:
return True
visited.add(key)
last_dy, visited = None, set()
for pos, (dy, _) in _visit(initial_pos, max_bounds, obstructions):
if last_dy != -1 and dy == -1:
if pos in visited:
return True
visited.add(pos)
last_dy = dy
return False


Expand Down
8 changes: 6 additions & 2 deletions rs/src/day6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ pub fn part2(data: &str) -> Option<usize> {
.filter(|candidate| {
let mut data = data.clone();
assert!(data.obstacles.insert(*candidate));
let mut last_dy = 0;
let mut visited = BTreeSet::new();
let ok = data.iter().any(|state| !visited.insert(state));
ok
!data.iter().all(|(pos, (dy, _))| {
let ok = last_dy == -1 || dy != -1 || visited.insert(pos);
last_dy = dy;
ok
})
})
.count(),
)
Expand Down
Loading