diff --git a/2019/10/day10.hs b/2019/10/day10.hs new file mode 100644 index 0000000..6531d54 --- /dev/null +++ b/2019/10/day10.hs @@ -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] ] diff --git a/2019/10/inputtest b/2019/10/inputtest new file mode 100644 index 0000000..33437ba --- /dev/null +++ b/2019/10/inputtest @@ -0,0 +1,20 @@ +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..## diff --git a/2019/12/input2 b/2019/12/input2 new file mode 100644 index 0000000..4e4b09c --- /dev/null +++ b/2019/12/input2 @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/2019/14/input2 b/2019/14/input2 new file mode 100644 index 0000000..753fa4e --- /dev/null +++ b/2019/14/input2 @@ -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 \ No newline at end of file diff --git a/2019/14/notes b/2019/14/notes new file mode 100644 index 0000000..3a7aa06 --- /dev/null +++ b/2019/14/notes @@ -0,0 +1,2 @@ +34659 too low +2944566 too high diff --git a/2019/18/src/main.rs b/2019/18/src/main.rs index 70ac452..19a5030 100644 --- a/2019/18/src/main.rs +++ b/2019/18/src/main.rs @@ -140,6 +140,7 @@ fn remove(map: &mut HashMap, k: Key) { } } +/* fn distance_to_door(map: &HashMap, k: &Key) -> usize { let (distances, _, _) = traverse(map, find(map, k.0).unwrap()); let door_pos = find(map, k.0.to_uppercase().next().unwrap()); @@ -154,6 +155,7 @@ fn distance_to_locked_door(map: &HashMap, k: &Key) -> usize { .min() .unwrap_or(&0) } +*/ fn next_key( map: &mut HashMap, @@ -198,6 +200,9 @@ fn next_key( .collect(); //.min_by_key(|k| ) + let nearest = dependencies.iter() + .filter(|(_, (_, d))| d.is_empty()) + .min_by_key(|(k, _)| distances.get(&find(&map, k.0).unwrap())); let dead_end_keys = find_keys_at_dead_ends(&dependencies); let mut next: Vec<_> = dead_end_keys .iter() @@ -215,16 +220,16 @@ fn next_key( //.chain(useful_keys) .collect(); next.sort_by_key(|(k, n)| *n);//*3 + distance_to_door(map, k)); + nearest.map(|(k, _)| next.insert(0, (k, *distances.get(&find(map, k.0).unwrap()).expect("No distance to nearest?")))); useful_keys.sort_by_key(|(k, n)| *n); useful_keys.reverse(); useful_keys.pop().map(|k| next.insert(0, k)); - useful_keys.pop().map(|k| next.push(k)); - useful_keys.pop().map(|k| next.push(k)); + //useful_keys.pop().map(|k| next.push(k)); useful_keys.pop().map(|k| next.push(k)); useful_keys.pop().map(|k| next.push(k)); let len = next.len(); next.par_iter() - .take((len.min(5) - 1).max(1)) + .take((len.min(4) - 1).max(1)) .map(|(k, _)| { let mut map2 = map.clone(); let next_pos = find(&map2, k.0).expect("Could not find key #3"); @@ -255,8 +260,8 @@ fn main() { }) .collect(); let pos = find(&map, '@').unwrap(); - let steps = 0; - let (_, dependencies, _) = traverse(&map, pos.to_owned()); + //let steps = 0; + //let (_, dependencies, _) = traverse(&map, pos.to_owned()); let p1 = next_key(&mut map.clone(), &pos, 0); println!("Part 1: {}", p1); println!("Part 1: {}", BEST.lock().unwrap()); diff --git a/2019/20/Cargo.toml b/2019/20/Cargo.toml new file mode 100644 index 0000000..e4a040a --- /dev/null +++ b/2019/20/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day20" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2019/20/input b/2019/20/input new file mode 100644 index 0000000..62b7260 --- /dev/null +++ b/2019/20/input @@ -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 diff --git a/2019/20/src/main.rs b/2019/20/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/2019/20/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/2019/24/Cargo.toml b/2019/24/Cargo.toml new file mode 100644 index 0000000..0e5c23e --- /dev/null +++ b/2019/24/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day24" +version = "0.1.0" +authors = ["kageru "] +edition = "2018" + +[dependencies] +grid = { path = "../grid" } diff --git a/2019/24/input b/2019/24/input new file mode 100644 index 0000000..d7c9947 --- /dev/null +++ b/2019/24/input @@ -0,0 +1,5 @@ +.#..# +.#.#. +#..## +.#.## +##..# diff --git a/2019/24/src/main.rs b/2019/24/src/main.rs new file mode 100644 index 0000000..de28806 --- /dev/null +++ b/2019/24/src/main.rs @@ -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 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 = 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::>() + }) + .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::>() + }) + .collect(); + } +} diff --git a/2019/grid/src/lib.rs b/2019/grid/src/lib.rs index 4e24cdb..e423631 100644 --- a/2019/grid/src/lib.rs +++ b/2019/grid/src/lib.rs @@ -73,6 +73,19 @@ impl Position2D { (Direction::Left, *self + Direction::Left), ] } + + pub fn moore(&self) -> [Position2D; 8] { + [ + *self + Direction::Up + Direction::Left, + *self + Direction::Up, + *self + Direction::Up + Direction::Right, + *self + Direction::Left, + *self + Direction::Right, + *self + Direction::Down + Direction::Left, + *self + Direction::Down, + *self + Direction::Down + Direction::Right, + ] + } } impl Direction {