untracked 2019 stuff
parent
d0d2e58f87
commit
21dee28618
@ -0,0 +1,104 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE BangPatterns #-}
|
||||
{-# LANGUAGE ParallelListComp #-}
|
||||
|
||||
import Data.Ratio
|
||||
import Data.List as L
|
||||
import qualified Data.Vector as V
|
||||
import Data.Ord
|
||||
import Data.Maybe
|
||||
|
||||
type Asteroid_old = (Int,Int)
|
||||
|
||||
data Asteroid = Asteroid Int Int deriving Show
|
||||
instance Eq Asteroid where
|
||||
Asteroid a b == Asteroid c d = reduce a b == reduce c d
|
||||
where reduce x y
|
||||
| y==0&&x==0 = (0,0)
|
||||
| x==0 = (0,signum y)
|
||||
| y==0 = (signum x,0)
|
||||
| otherwise = (signum x * (abs $numerator z), signum y * (abs $denominator z))
|
||||
where z = x%y
|
||||
instance Ord Asteroid where
|
||||
Asteroid a b `compare` Asteroid c d = phi a b `compare`phi c d
|
||||
where phi x y
|
||||
| x>=0 = acos (fromIntegral (-y)/sqrt(fromIntegral x^2+fromIntegral y^2))
|
||||
| otherwise = 2*pi + acos (fromIntegral (x)/sqrt(fromIntegral x^2+fromIntegral y^2))
|
||||
|
||||
(+|) :: Asteroid -> Asteroid -> Asteroid
|
||||
(+|) (Asteroid a b) (Asteroid c d) = Asteroid (a+c) (b+d)
|
||||
(-|) :: Asteroid -> Asteroid -> Asteroid
|
||||
(-|) (Asteroid a b) (Asteroid c d) = Asteroid (a-c) (b-d)
|
||||
neg :: Asteroid -> Asteroid
|
||||
neg (Asteroid a b) = Asteroid (-a) (-b)
|
||||
dist :: Asteroid -> Int
|
||||
dist (Asteroid x y) = abs x + abs y
|
||||
|
||||
|
||||
|
||||
main = do
|
||||
content <- lines <$> readFile "inputtest"
|
||||
print $ parse content
|
||||
let aMax = fst $ maxInSight $ parse content
|
||||
print aMax
|
||||
let newlist = listCycle $ sort $ sortOn (dist.fromJust) $ snd $ transformCoordinates (parse content) aMax
|
||||
print $ newlist !! 199
|
||||
let newnewlist = snd $ transformCoordinates newlist (neg aMax)
|
||||
print $ newnewlist !! 0
|
||||
print $ newnewlist !! 1
|
||||
print $ newnewlist !! 2
|
||||
print $ newnewlist !! 50
|
||||
print $ newnewlist !! 100
|
||||
print $ length newnewlist
|
||||
|
||||
|
||||
|
||||
listCycle :: [Maybe Asteroid] -> [Maybe Asteroid]
|
||||
listCycle [] = []
|
||||
listCycle xs = nub xs ++ listCycle (xs \\ nub xs)
|
||||
|
||||
-- alternative rotation function that also gets different results
|
||||
cycle2 :: Maybe Asteroid -> [Maybe Asteroid] -> [Maybe Asteroid]
|
||||
cycle2 _ [] = []
|
||||
cycle2 last (x:xs)
|
||||
| x==last&& length (filter (/=x) xs) > 0 = cycle2 x (xs++[x])
|
||||
| otherwise = x:cycle2 x xs
|
||||
|
||||
|
||||
parse :: [String] -> [Maybe Asteroid]
|
||||
parse = concat . V.toList . V.map V.toList . toAsteroids . V.map V.fromList . V.fromList
|
||||
where toAsteroids = V.imap g
|
||||
g x = V.imap (f x)
|
||||
f i j a = case a of '.' -> Nothing; _ -> Just $ Asteroid j i
|
||||
|
||||
|
||||
--inSight :: [Maybe Asteroid] -> Int
|
||||
transformCoordinates :: [Maybe Asteroid] -> Asteroid -> (Asteroid,[Maybe Asteroid])
|
||||
transformCoordinates xs a = (a,[Just $ x -| a |Just x<-xs])
|
||||
|
||||
maxInSight :: [Maybe Asteroid] -> (Asteroid, Int)
|
||||
maxInSight xs = maximumBy (comparing snd) $ map (f . transformCoordinates xs) (catMaybes xs)
|
||||
where f (x,y) = (x, pred . length . nub $ y)
|
||||
|
||||
|
||||
-- old shit be here
|
||||
--maxInSight xs = map ((\(x,y)->(x,pred . length . nub . snd $ y)). transformCoordinates xs) (catMaybes xs)
|
||||
--maxInSight xs = map ((pred . length . nub . snd) . transformCoordinates xs) (catMaybes xs)
|
||||
|
||||
-- transform2 :: Asteroid -> [Maybe Asteroid] -> [Maybe Asteroid]
|
||||
-- transform2 a xs = L.map (a -|) xs
|
||||
|
||||
|
||||
|
||||
-- maxInSight x = L.maximum $ inSight x `fmap` asts x
|
||||
|
||||
|
||||
-- sightTransform xs = map inSight xs
|
||||
|
||||
-- inSight xs a = (pred . L.length) $ L.nub [case a of
|
||||
-- Just (xa,ya) -> Just (reduceTuple (x-xa,y-ya))
|
||||
-- Nothing -> Nothing |Just (x,y)<-asts xs]
|
||||
--
|
||||
asts :: [String] -> [Maybe Asteroid_old]
|
||||
asts xxs = L.nub [if (xxs!!y)!!(x)=='.' then Nothing else Just (x,y)
|
||||
| y<-[0..(L.length $ L.head xxs) -1], x<-[0..L.length xxs -1] ]
|
@ -0,0 +1,20 @@
|
||||
.#..##.###...#######
|
||||
##.############..##.
|
||||
.#.######.########.#
|
||||
.###.#######.####.#.
|
||||
#####.##.#.##.###.##
|
||||
..#####..#.#########
|
||||
####################
|
||||
#.####....###.#.#.##
|
||||
##.#################
|
||||
#####.##.###..####..
|
||||
..######..##.#######
|
||||
####.##.####...##..#
|
||||
.#####..#.######.###
|
||||
##...#.##########...
|
||||
#.##########.#######
|
||||
.####.#.###.###.#.##
|
||||
....##.##.###..#####
|
||||
.#.#.###########.###
|
||||
#.#.#.#####.####.###
|
||||
###.##.####.##.#..##
|
@ -0,0 +1,4 @@
|
||||
<x=-1, y=0, z=2>
|
||||
<x=2, y=-10, z=-7>
|
||||
<x=4, y=-8, z=8>
|
||||
<x=3, y=5, z=-1>
|
@ -0,0 +1,17 @@
|
||||
171 ORE => 8 CNZTR
|
||||
7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL
|
||||
114 ORE => 4 BHXH
|
||||
14 VRPVC => 6 BMBT
|
||||
6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL
|
||||
6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT
|
||||
15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW
|
||||
13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW
|
||||
5 BMBT => 4 WPTQ
|
||||
189 ORE => 9 KTJDG
|
||||
1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP
|
||||
12 VRPVC, 27 CNZTR => 2 XDBXC
|
||||
15 KTJDG, 12 BHXH => 5 XCVML
|
||||
3 BHXH, 2 VRPVC => 7 MZWV
|
||||
121 ORE => 7 VRPVC
|
||||
7 XCVML => 6 RJRHP
|
||||
5 BHXH, 4 VRPVC => 5 LTCX
|
@ -0,0 +1,2 @@
|
||||
34659 too low
|
||||
2944566 too high
|
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day20"
|
||||
version = "0.1.0"
|
||||
authors = ["kageru <kageru@encode.moe>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,121 @@
|
||||
I U O M N O S
|
||||
X T X C D N A
|
||||
#########################################.#####.#######.###.###########.#####.#######.#######################################
|
||||
#.....#.......#.....#.....#.....#...#.#...#...#...#.....#.#...........#.....#.....#.............#.#...#...#...#.......#.....#
|
||||
#####.#######.#####.#####.#.###.###.#.#.###.#.###.#.#.###.###.###.#####.#.#######.#####.#.#######.###.#.###.###.#######.#####
|
||||
#.#.#...#...#.....#.........#...........#.#.#...#.#.#.....#...#.......#.#...#.#.....#.#.#...............#.........#.........#
|
||||
#.#.###.###.###.#####.#######.#####.#.###.#.###.#.#.#########.#.#########.###.#.#####.###.###.#####.#.#####.###.#########.###
|
||||
#.....#.........#.....#...#.#.....#.#.....#.#...#.#.#...#...#.#.....#...#.#.#.........#.....#.....#.#.........#.#...#...#...#
|
||||
#####.#####.#.#.###.#####.#.#.#####.#.#.#.#.###.#.#.###.###.#####.#####.#.#.#####.#######.###.#.#.#######.###.#####.###.#.###
|
||||
#...#.......#.#.......#.......#.#.#.#.#.#.#...#.#.#.......#.#.#.....#.........#.....#.....#.#.#.#.......#.#.........#.......#
|
||||
#.###############.#####.#####.#.#.#######.###.#.#.###.#.###.#.#.#.###.###.#####.#######.#.#.###.#.###.#.#.###.#####.#####.#.#
|
||||
#...#.#.#...#.......#.......#...#.....#...#...#.#.#...#.....#...#...#.#...#.......#...#.#.....#.#...#.#.#.#.#.#.....#.#.#.#.#
|
||||
#.###.#.#.#######.#.#.#.#####.###.###.#.#.#.###.#.#.###.#.###.###########.#####.#####.###.#.#.#.#.#########.###.#####.#.#.###
|
||||
#.#.......#...#...#.#.#...#...#.#.#.....#.#...#...#.#...#.#.......#.#.#.#.#.........#.....#.#.#.#...........#.#...#.#.#.#...#
|
||||
#.###.###.#.#####.###.#.###.###.###.#####.###.#.#.#.#############.#.#.#.#.#######.#.###.#.#####.#.#.#####.#.#.#####.#.#.#.###
|
||||
#.#...#...#.#.#.#...#.#...#.#.....#.#.....#...#.#.#...#.#.#...........#.....#.....#.#...#.....#.#.#.....#.#.....#...#.#.#.#.#
|
||||
#.#######.#.#.#.#.#############.#####.###.#.#######.#.#.#.#.###.#.#####.#########.#####.#################.#######.#.#.#.#.#.#
|
||||
#.....#.......#.#.#...#...#...#...#.#.#...#.....#...#.#.....#.#.#.#.......#.#.#...#.......#.#...#.#.....#.....#.#.#.....#...#
|
||||
###.#########.#.###.#####.#.###.###.###.#.#.#######.###.###.#.#######.###.#.#.#.#.#.#####.#.#.###.#.#.#####.###.#######.#.###
|
||||
#.#.....#.#...#...#...#.#.#...#...#...#.#.#.....#...#...#.......#...#.#.......#.#.#.#.........#.....#...#.#.#.#...#.........#
|
||||
#.###.###.###.###.#.###.#.###.#.#####.###.#.#.###.#.#########.###.#.#####.#######.###.###.#####.#.#.#####.###.###.#######.###
|
||||
#.........#.#...#.#.#...........#.#.......#.#...#.#.#...#.........#.#.....#.#.......#.#.#.#.#...#.#.#.....#.#...#.#.....#...#
|
||||
###.#.#####.#.###.#.#####.#####.#.#######.#.###.###.#.#####.#.#.#########.#.#####.#####.#.#.#.#.#######.###.#.###.#.#.###.###
|
||||
#.#.#.#...#.........#.#.#.#.#.#...#.#.....#.#.#.#.......#.#.#.#.#...........#.#.......#.....#.#.#.#...#...#...#.....#...#.#.#
|
||||
#.#.#####.#.###.###.#.#.###.#.#.#.#.#####.#.#.#.###.#.###.#####.#####.#######.###.#######.###.#.#.#.###.#####.###.#.###.#.#.#
|
||||
#...#.#.#.#.#.#.#.....#.........#.#...#...#...#.#...#.#...#.......#...#...#...........#.#.....#.#...#.#.#...#...#.#.#.......#
|
||||
###.#.#.#.###.#####.#.#####.#.#.###.###.#.#.#.#####.#####.#.#.#.#.#.###.#.#.###.#.#.###.#.#.#####.###.#.###.#.#.#########.###
|
||||
#...#.#...#...#.#.#.#.......#.#.........#.#.#.#...........#.#.#.#.#.#...#.#...#.#.#.#.#.#.#.#.............#.#.#.#.....#.#.#.#
|
||||
###.#.#.###.###.#.###.#.#.#####.###.#####.#.#######.#############.#.#.###.###.#######.#.#.###.###.#.#######.#.#######.#.#.#.#
|
||||
#.#.....#.#.#.#.#.....#.#.#.#...#.......#.#...#...........#...#...#...#...#...#...#...........#...#.#.#...#.....#...#.#.....#
|
||||
#.#####.#.#.#.#.#########.#.#.#.#.#.###.###.#######.#######.###.###.###.#.###.#.#####.###.#.#########.#.###.#######.#.###.###
|
||||
#...#.....#.#.#.#.#.#...#.#...#.#.#.#.#...#.......#.........#.....#.#...#.#.....#.#.#...#.#.#.#...#.....#.....#.#...#.#.....#
|
||||
#.#######.#.#.#.#.#.#.###.#.#.#.#.###.###.#.#.#####.#.###.#####.#######.###.#.###.#.#.#.#.#.#.###.###.#####.#.#.#.###.#.#####
|
||||
#...#.#...#...........#...#.#.#.#.#.......#.#...#...#.#...#.......#.......#.#...#.....#.#.#...#.......#...#.#.......#.....#.#
|
||||
#.#.#.###.###.###.#############.#########.###.#########.#####.#######.#####.#######.#################.#.#######.#####.###.#.#
|
||||
#.#.....#.#.#.#.#.....#.#.....#.# C S P R W B A #...#.#...#...#.#.....#...#...#.#
|
||||
###.#.###.#.#.#.#######.###.###.# S A P D A E D #.###.###.#.###.#.###########.#.#
|
||||
#...#.#...........#...#.....#...# #.#.......#.........#.#.#.....#.#
|
||||
###.#####.#####.###.###.###.##### #.#####.#.#.#.#.#.###.#.###.#.#.#
|
||||
#...#.#...#.........#.....#.#...# #.....#.#...#.#.#.#.#.......#...#
|
||||
###.#.###.#.###########.#####.### #.#.#.#.###.###.#.#.#######.#.#.#
|
||||
EY........#.#...#...#.#.......#.#.# #.#.#.#...#.#.#.#...#.......#.#.#
|
||||
#.###.#.#.#.#.#.#.#.###.#####.#.# #.#.#####.###.###.#####.#.###.###
|
||||
#.#...#...#.#...#................RB VN..#.......#.....#.......#.#......CS
|
||||
#############.#########.#.####### #############.#.###.#######.###.#
|
||||
#...#.#...#.....#.#.#...#.#.....# #.........#...#.#...#.#...#...#.#
|
||||
#.###.###.###.###.#.###.#####.#.# ###.###.###.#.#######.#.#########
|
||||
BE....#...#...#.#.......#.#.#...#.# ON..#.#...#...#.#...#...#.#.#...#.#
|
||||
#.#####.###.#######.#####.###.#.# #.#.#.#####.#.#.#####.#.#.#.###.#
|
||||
#.#.#.......#.........#.....#.#.# #.#.#.#...#.#.#.#.........#.#.#..MR
|
||||
#.#.#.#####.#####.#.#.###.###.#.# #.#.#.#####.#.#.###.#.#####.#.#.#
|
||||
#.....#...#.......#.#.........#..QG WP....#.......#.......#............VN
|
||||
#######.###.###.###.############# ###.###########.#.#######.#######
|
||||
ZM........#.....#...#.#...#.....#.# #.#...#...#...#.#.#.....#.#.....#
|
||||
#.#.#########.#.#####.#######.#.# #.#####.#.###.#######.#.###.#.###
|
||||
#.#.......#.#.#.....#.#.....#....ND UE........#...........#.#.#...#...#
|
||||
###.#######.#########.###.#.#.### #.###.#.#.#####.###.#.#.###.#.###
|
||||
#...#...#...#...#.......#.#.....# #.#.#.#.#...#...#...#.#.....#....GN
|
||||
#.#####.###.#.###.###.#####.#.### #.#.###########.#.###.#####.#.#.#
|
||||
#...................#.......#...# #.#.......#.#.#.#.......#...#.#.#
|
||||
#####.#####.###.#.#####.######### ###.#.#.###.#.###.###############
|
||||
#...#.....#.#...#.#...#.#.#.....# UT....#.#.#.#.#...#.#.#.......#....WA
|
||||
#.#.###############.###.#.#.##### ###.#####.#.#.###.#.#.###.#.#.#.#
|
||||
WP..#...#.....#.#.#.....#.#.......# #.....#.....#.#.....#.#...#...#.#
|
||||
#.#.#.###.###.#.###.#.###.#.##### #.#.#.###.###.#########.###.#.###
|
||||
#.#.#.....#.....#.#.#.....#.....# #.#.#.#.......#.#.#.....#...#.#.#
|
||||
#######.#.#.#.###.#.#.#.#.####### #.#####.###.#.#.#.#####.###.#.#.#
|
||||
#...#.#.#...#.......#.#.#........OX #.........#.#...........#...#.#.#
|
||||
#.###.###########.###.###.#.###.# #.#######.#####.###########.###.#
|
||||
#.......#.....#...#...#...#.#...# #.#.....#.#.#.#.#.......#...#....PP
|
||||
#.###.#.###.#.################### ###.###.###.#.###.#.###.#####.###
|
||||
NO..#...#.#...#.#.#.....#...#.....# ZR..#.#.#...#.....#.#.#.....#.....#
|
||||
###.#####.###.#.#.#####.###.###.# #.#.#.#.#####.#####.###.#####.#.#
|
||||
#...#...#...#.....#.#.........#..FK #.....#.....#.#...#.#...#...#.#.#
|
||||
#.###.#.###.#####.#.###.#.#.#.#.# #.###.###.###.###.#.###.#.#.###.#
|
||||
ZZ......#.......#.#.......#.#.#.#.# #.#...#.............#.....#.....#
|
||||
#.###.###.#.###.###.############# ###.#####.###.#################.#
|
||||
#.#...#.#.#.......#.#.#.........# #.....#.....#.#...............#.#
|
||||
#######.#####.#.#.###.#.###.###.# #########.#####.###.#######.#####
|
||||
RX..#.#.#.....#.#.#.#.#.....#.#....GN #.#...#...#.....#.....#.#........UE
|
||||
#.#.#.#.#.#########.###.#.###.#.# #.#.#####.###.#.###.###.###.###.#
|
||||
#.....#.#.....#.#.....#.#...#.#.# ZM....#...#.#...#.#.#.......#...#.#
|
||||
#.#.#.#.#.#####.###.#####.#.#.#.# #.#.###.###.#.###.#.#.#####.###.#
|
||||
#.#.#...#.................#.#.#.# #.#.........#...#...#...#.....#.#
|
||||
#.###.#.#####.###.#.#####.#####.# M P I R E N M #.###.#.#.###.#######.#####.###.#
|
||||
#.#...#.....#...#.#...#.#...#.#.# C G X X Y O R #...#.#.#.#...#.....#.....#...#.#
|
||||
#####.###.###.#.#.#####.#.###.#######.#######.###########.#####.#####.#####.###########.#########.#######.#.#####.#.#.###.###
|
||||
#.#.#...#.#...#.#.#.............#.....#.#.#...#...#.........#...#.......#.......#.#...#.#.......#.#.............#.#.#.#.#...#
|
||||
#.#.#.#.#.###.###.###.###.###.###.#.#.#.#.###.#.#.###.#########.#######.#######.#.###.#.###.#.#####.###.#.###.#########.#####
|
||||
#...#.#.#...#...#...#...#...#.#...#.#...#.#.....#.#...#.....#...#.#.#.#.....#.#.#.#.#.#.....#.#...#...#.#.#.............#...#
|
||||
###.#.#######.#.#####.###.#.#####.#.###.#.#######.#.###.#.###.#.#.#.#.###.###.#.#.#.#.#.#######.#######.#####.###.#######.###
|
||||
#.......#.....#.#.......#.#...#...#.#.....#...#.#.#.....#...#.#.#...........#.#...#...#...........#.#.#...#.....#...........#
|
||||
#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.###.###.#.#.#######.#####.###.###.#.###.#.###.#.#.#.###.#.#.#.#.#.###.#.###.###.###.###
|
||||
#.#.#.#.#.#.#.#.#.#.#.#.#...#...#.#.#.#.#.#.#.........#...#.#...#.....#.#...#...#...#...#...#.#.#.#.#...#...#.#.....#.#.#...#
|
||||
###.#.#####.#######.#.###.###.###.#####.#.#.#.#####.#####.#.###.#####.#.#.#####.#.#.###.#######.#.#.#.#######.#######.#.#####
|
||||
#...#...#...#.#.#...#...#.#.#...#.#.......#...#.#...#.....#.....#.....#.#...#...#.#.#.........#.#.#...#.........#...........#
|
||||
###.#####.###.#.#.#.#######.#.#########.#.#.###.#.#####.#.#.#.#####.#####.#####.#.#########.#########.#####.#.###.#####.###.#
|
||||
#.....#.....#.....#.......#.....#.......#.#.....#...#...#.#.#.....#.#.#.#.#...#.#.......#...........#.#.#.#.#...#...#.....#.#
|
||||
###.#.###.#######.###.###.###.###.###.###.#.#.###.###.###.#######.#.#.#.#####.#.#.#.#########.###.#.#.#.#.#####.#.#.#.#.#.#.#
|
||||
#...#...#...#.#.....#.#.....#.#...#.#.#...#.#...#.#.#.#.#.#.#...#.#.....#.#...#.#.#.........#.#.#.#.#.........#.#.#.#.#.#.#.#
|
||||
#.###.#.###.#.#.#.#.#####.#########.#####.#.#######.###.#.#.###.#.#.###.#.###.#.#####.#######.#.#######.#.#########.###.#.#.#
|
||||
#.#...#.#...#...#.#.#.......#...#.#.......#.#.......#...#...#...#.#...#.#.#.......#...#.#.#.#...#.....#.#.#.#...#.....#.#.#.#
|
||||
#.#.#.#.#####.#.#.#####.#####.###.#.#####.#.###.#######.#.#####.#.#####.#.###.#######.#.#.#.###.#.#####.###.#.#####.#.#######
|
||||
#.#.#.#...#...#.#.#.......#.#.#.#.#.#.#.#.#.#...#.#.#...#...#.#.......#.....#...#...........#.#...#.#.....#...#...#.#...#.#.#
|
||||
###.###.#.#.#########.#####.#.#.#.###.#.#.#.###.#.#.###.#.###.#.###.#.###.###.#####.###.#####.#####.#.#.###.#####.###.###.#.#
|
||||
#.....#.#.#.#.........#.......#.#...#.....#.....#.....#...#.......#.#...#.#.......#...#.......#.....#.#...#...#.....#.#.#...#
|
||||
#.#.###.#.#.#.###.#.#######.#.#.#.#####.#.#.###.###.#.#.#####.#.#####.###.###.#########.###.#.###.#####.###.###.#.#####.#.###
|
||||
#.#...#.#.#.#.#.#.#...#.....#.........#.#.#.#.....#.#.#...#.#.#...#.#.#.#.#.......#.......#.#...#...#.#.........#...........#
|
||||
#.#.###.#######.#.#.#####.#########.#####.#######.#.#.#.###.###.#.#.###.#.#####.#######.#########.###.#.###.#.#.###.#.#.###.#
|
||||
#.#.#.......#...#.#.#.#...#...#.....#.#...#.......#.#.......#...#.#.........#...#...............#.#.#.#.#.#.#.#...#.#.#.#.#.#
|
||||
#.#####.#######.#####.#######.#.###.#.###.#.#######.###########.#.#.###########.#####.###.###.###.#.#.###.###.#.#####.###.###
|
||||
#.#.....#...............#.......#.....#...#.......#...#...#...#.#.#...#.......#.....#...#...#...#.......#...#.#.....#.#...#.#
|
||||
#.#.###.#.#.#.#.#######.#.#.#.#######.###.#######.###.###.#.#.###.###.#####.#.#.#.###.###########.###.###.#####.#.#####.###.#
|
||||
#.#.#...#.#.#.#.#.#.......#.#...#.........#.........#...#.#.#...#.#.#...#.#.#...#.#.......#.........#.........#.#.#...#...#.#
|
||||
#.#.###.###.#####.#.#####.#.#####.###.#.###.#.#######.###.###.#.#.#.###.#.#.###.#####.#.###.#####.#.#.###.#####.#####.#.###.#
|
||||
#.#.#.#.#...#...#...#.....#.#.....#...#.#.#.#...#...#...#.#...#...#.......#...#.....#.#.......#.#.#.#.#.....#.#.............#
|
||||
#####.#.#.#.#.#.###.#.###.#####.#####.###.#.###.#.#.#.###.#.#######.###.#####.#######.#.#.#.###.#.#.###.#####.###.#.#.###.#.#
|
||||
#.......#.#.#.#.....#.#.....#...#.......#.....#.#.#.......#.......#...#.#...........#.#.#.#.....#.#.#...........#.#.#...#.#.#
|
||||
#####################################.#######.#########.#.#######.#.###########.#######.#####################################
|
||||
Z A R A F Q R P
|
||||
R D B A K G D G
|
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day24"
|
||||
version = "0.1.0"
|
||||
authors = ["kageru <kageru@encode.moe>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
grid = { path = "../grid" }
|
@ -0,0 +1,5 @@
|
||||
.#..#
|
||||
.#.#.
|
||||
#..##
|
||||
.#.##
|
||||
##..#
|
@ -0,0 +1,80 @@
|
||||
use grid::*;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
const FIELD_SIZE: usize = 5;
|
||||
|
||||
#[derive(Copy, Debug, Clone, PartialEq, Eq)]
|
||||
enum State {
|
||||
Alive,
|
||||
Dead,
|
||||
}
|
||||
|
||||
impl fmt::Display for State {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
State::Alive => '#',
|
||||
State::Dead => '.',
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<char> for State {
|
||||
fn from(c: char) -> Self {
|
||||
match c {
|
||||
'.' => Self::Dead,
|
||||
'#' => Self::Alive,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn state_for_live_neighbors(state: &State, n: usize) -> State {
|
||||
match (state, n) {
|
||||
(_, 1) => State::Alive,
|
||||
(State::Dead, 2) => State::Alive,
|
||||
_ => State::Dead,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut fields: HashMap<Position2D, State> = io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.map(|l| l.unwrap())
|
||||
.enumerate()
|
||||
.flat_map(move |(y, l)| {
|
||||
l.chars()
|
||||
.enumerate()
|
||||
.map(move |(x, c)| ((x, y).into(), c.into()))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect();
|
||||
loop {
|
||||
println!("{}", draw_ascii(&fields, State::Dead));
|
||||
fields = (0..FIELD_SIZE)
|
||||
.flat_map(|y| {
|
||||
(0..FIELD_SIZE)
|
||||
.map(|x| {
|
||||
let p = Position2D::from((x, y));
|
||||
let state = state_for_live_neighbors(
|
||||
fields.get(&p).unwrap(),
|
||||
p.moore()
|
||||
.iter()
|
||||
.map(|p| fields.get(&p).unwrap_or(&State::Dead))
|
||||
.filter(|&&s| s == State::Alive)
|
||||
.count(),
|
||||
);
|
||||
(Position2D::from((x, y)), state)
|
||||
})
|
||||
// .map(|alive_neighbors| (Position2D::from((x, y)), *state))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue