Add days 3, 4 and 5

This commit is contained in:
Sijmen 2018-12-05 14:14:47 +01:00
parent c2974f8fd2
commit e9b068912a
6 changed files with 169 additions and 0 deletions

15
Day3/Day3.py Normal file
View File

@ -0,0 +1,15 @@
import numpy
import re
RE = re.compile(r"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)")
with open("Day3.in", "r") as f:
ROWS = [[int(i) for i in RE.match(line).groups()] for line in f]
FABRIC = numpy.zeros((1000, 1000), dtype=numpy.uint8)
for _, x, y, w, h in ROWS:
FABRIC[x:x+w, y:y+h] += 1
for i, x, y, w, h in ROWS:
if (FABRIC[x:x+w, y:y+h] == 1).all():
print(i)
break

10
Day3/Day3B.hs Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
-- WORK IN PROGRESS
module Day3B where
import Text.Regex.TDFA
main :: IO ()
main = print $ ("#1 @ 662,777: 18x27" =~ "#(\\d+) @ (\\d+),(\\d+): (\\d+)x(\\d+)" :: (String, String, String, String, String))

51
Day4/Day4.py Normal file
View File

@ -0,0 +1,51 @@
from pprint import pprint
from datetime import timedelta
import numpy
import dateutil.parser
import re
from collections import defaultdict
guards_asleep = set()
guard_sleep_duration = defaultdict(int)
guard_sleep_times = defaultdict(lambda: numpy.zeros(24 * 60, dtype=numpy.int))
with open("Day4.in", "r") as f:
lines = sorted(f.readlines())
RE_TIME = re.compile(r"\[(.*)\]")
RE_SHIFT = re.compile(r".*#(\d+).*")
current_guard = None
last_minute = None
for line in lines:
time_match = RE_TIME.match(line)
time = dateutil.parser.parse(time_match.group(1))
minute = time.time().minute
if last_minute is None:
last_minute = minute
delta = minute - last_minute
if current_guard in guards_asleep:
guard_sleep_duration[current_guard] += delta
for i in range(last_minute, minute):
guard_sleep_times[current_guard][i] += 1
last_minute = minute
match = RE_SHIFT.match(line)
if match:
current_guard = int(match.group(1))
if "wakes up" in line:
guards_asleep.discard(current_guard)
if "falls asleep" in line:
guards_asleep.add(current_guard)
a = numpy.array([numpy.insert(value, 0, key) for (key, value) in guard_sleep_times.items()])
x, y = numpy.unravel_index(a[:, 1:].argmax(), a[:, 1:].shape)
guard = a[x, 0]
print(guard, y, guard * y)

49
Day4/Day4A.hs Normal file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
-- WORK IN PROGRESS
module Day4A where
import Data.List (sort)
import Data.Maybe (listToMaybe, mapMaybe, fromJust)
import Data.Time.Clock
import Data.Time.Format
import Debug.Trace
import Text.Regex.TDFA
import qualified Data.IntMap as IM
import qualified Data.Vector as V
handleGuardSwitch :: String -> Maybe Int
handleGuardSwitch x = unwrap <$> matches
where matches = listToMaybe (x =~ "#([0-9]+)")
unwrap (_:id:_) = read id
parseGuardWake :: String -> Maybe Int
parseGuardWake x = unwrap <$> listToMaybe (x =~ "(\\d{2})\\] wakes up")
where unwrap (_:y:_) = read y
parseGuardSleep :: String -> Maybe Int
parseGuardSleep x = unwrap <$> listToMaybe (x =~ "(\\d{2})\\] falls asleep")
where unwrap (_:y:_) = read y
handle' :: [String] -> Maybe Int -> IM.IntMap V.Vector -> Int -> Bool -> IM.IntMap V.Vector
handle' [] _ minutes _ _ = minutes
handle' (x:xs) guard minutes minute asleep
| Just id <- handleGuardSwitch x = handle' xs (Just id) minutes minute asleep
| Just nextMinute <- parseGuardSleep x = handle' xs guard minutes nextMinute True
| Just nextMinute <- parseGuardWake x =
case (nextMinute < minute) of
True -> handle' (x:xs) guard minutes (minute + 1) True
False -> handle' xs guard minutes minute False
| otherwise =
handle' xs guard minutes (minute + 1) asleep
handle :: [String] -> IM.IntMap V.Vector
handle lines = handle' lines Nothing IM.empty 0 False
main :: IO ()
main = interact $ show . handle . sort . lines

23
Day5/Day5A.hs Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
module Day5A where
import Data.Char
import qualified Data.Foldable as F
invertCase :: Char -> Char
invertCase c
| isUpper c = toLower c
| otherwise = toUpper c
calcLength :: String -> String -> Int
calcLength ys (x:y:xs)
| x == invertCase y = case length ys of
0 -> calcLength ys xs
_ -> calcLength (tail ys) (head ys:xs)
| otherwise = calcLength (x:ys) (y:xs)
calcLength ys [x] = calcLength (x:ys) []
calcLength ys [] = length ys
main :: IO ()
main = interact $ show . calcLength "" . filter (/= '\n')

21
Day5/Day5B.hs Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
module Day5B where
import Day5A (calcLength)
import Data.Char
alphabet :: String
alphabet = "abcdefghijklmnopqrstuvwxyz"
caseNEq :: Char -> Char -> Bool
caseNEq x y = toLower x /= toLower y
mapper :: String -> Char -> Int
mapper s x = calcLength "" $ filter (caseNEq x) s
findShortest :: String -> Int
findShortest s = minimum $ map (mapper s) alphabet
main :: IO ()
main = interact $ show . findShortest . filter (/='\n')