r/adventofcode • • Dec 04 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 4 Solutions -🎄-

--- Day 4: Repose Record ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 4

Transcript:

Today’s puzzle would have been a lot easier if my language supported ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

38 Upvotes

340 comments sorted by

15

u/gerikson Dec 04 '18

Mods, first off, love your work, you're the best!

Small request, can the time the thread is unlocked be added to the message?

I.e.

edit: Leaderboard capped, thread unlocked at 00:45!

0

u/[deleted] Dec 05 '18

[deleted]

→ More replies (1)

25

u/jonathan_paulson Dec 04 '18 edited Dec 04 '18

Python, #19/10. Glad to see they're getting tougher. Video of me solving at https://www.youtube.com/watch?v=YQjPXSlSelc. Not reading everything up front probably cost me time here. This code solves part 2 (part 1 requires a minor modification).

 from collections import defaultdict
 lines = open('4.in').read().split('\n')
 lines.sort()

 def parseTime(line):
     words = line.split()
     date, time = words[0][1:], words[1][:-1]
     return int(time.split(':')[1])

 C = defaultdict(int)
 CM = defaultdict(int)
 guard = None
 asleep = None
 for line in lines:
     if line:
         time = parseTime(line)
         if 'begins shift' in line:
             guard = int(line.split()[3][1:])
             asleep = None
         elif 'falls asleep' in line:
             asleep = time
         elif 'wakes up' in line:
             for t in range(asleep, time):
                 CM[(guard, t)] += 1
                 C[guard] += 1

 def argmax(d):
     best = None
     for k,v in d.items():
         if best is None or v > d[best]:
             best = k
     return best

 best_guard, best_min = argmax(CM)
 print best_guard, best_min

 print best_guard * best_min

4

u/Dad2us Dec 04 '18

Not reading everything is what cost me this time, too. After 30 minutes I gave up and went to bed. Woke up this morning and completed it to find that I spent the previous night solving for part #2 without even knowing what it was.

Thank god I never delete the unused code.

→ More replies (1)

4

u/paracuerdas Dec 05 '18

argmax

argmax also works with this "implementation": 🙂

def argmax(d):
    k = max(d, key=d.get)
    return key, d[k]

What do you think about this suggestion:

best_guard = max(d, key=d.get)
best_min = d[best_guard]

👍

2

u/jonathan_paulson Dec 05 '18

Nice! Much cleaner

3

u/dylanfromwinnipeg Dec 04 '18

I love the videos! Keep it up!

3

u/mebeim Dec 04 '18

I love how you probably have countless fancy programs and text editors installed but you still use Vim like a real pro! Cool video, and congrats for the result :)

PS: lol @ those 35k unread emails

2

u/pythondevgb Dec 05 '18

Obviously you're a pro and you know better but I have a feeling that if you used re or other parsing library you would get much quicker. Also have "from collections import defaultdict" in a boilerplate file.

→ More replies (2)
→ More replies (11)

20

u/HiramAbiff Dec 04 '18 edited Dec 04 '18

AWK

4.1

sort dat.txt | awk -v FS="[\]:# ]" '/Guard/{g=$7}/falls/{s=$3}/wakes/{for(t=s;t<$3;++t){++zg[g];++zgt[g","t]}}END{for(g in zg)if(zg[g]>zg[og])og=g;for(t=0;t<60;++t)if(zgt[og","t]>zgt[og","ot])ot=t;print og*ot}'

4.2

sort dat.txt | awk -v FS="[\]:# ]" '/Guard/{g=$7}/falls/{s=$3}/wakes/{for(t=s;t<$3;++t)++zgt[g","t]}END{for(gt in zgt)if(zgt[gt]>zgt[ogt])ogt=gt;split(ogt,oa,",");print oa[1]*oa[2]}'
→ More replies (1)

15

u/mstksg Dec 04 '18 edited Dec 04 '18

Taken from my Day 4 Reflections Post:

[Haskell] Day 4 was fun because it's something that, on the surface, sounds like it requires a state machine to run through a stateful log and accumulate a bunch of time sheets.

However, if we think of the log as just a stream of tokens, we can look at at it as parsing this stream of tokens into time sheets -- no state or mutation required.

First, the types at play:

type Minute = Finite 60

type TimeCard = Map Minute Int

data Time = T { _tYear   :: Integer
              , _tMonth  :: Integer
              , _tDay    :: Integer
              , _tHour   :: Finite 24
              , _tMinute :: Minute
              }
  deriving (Eq, Ord)

newtype Guard = G { _gId :: Int }
  deriving (Eq, Ord)

data Action = AShift Guard
            | ASleep
            | AWake

Note that we have a bunch of "integer-like" quantities going on: the year/month/day/hour/minute, the guard ID, and the "frequency" in the TimeCard frequency map. Just to help us accidentally not mix things up (like I personally did many times), we'll make them all different types. A Minute is a Finite 60 (Finite 60, from the finite-typelits library, is a type that is basically the integers limited from 0 to 59). Our hours are Finite 24. Our Guard ID will be a newtype Guard, just so we don't accidentally mix it up with other types.

Now, after parsing our input, we have a Map Time Action: a map of times to actions committed at that time. The fact that we store it in a Map ensures that the log items are ordered and unique.

We now essentially want to parse a stream of (Time, Action) pairs into a Map Guard TimeCard: A map of TimeCards indexed by the guard that has that time card.

To do that, we'll use the parsec library, which lets us parse over streams of arbitrary token type. Our parser type will take a (Time, Action) stream:

import qualified Text.Parsec as P

type Parser = P.Parsec [(Time, Action)] ()

A Parser Blah will be a parser that, given a stream of (Time, Action) pairs, will aggregate them into a value of type Blah.

Turning our stream into a Map Guard TimeCard is now your standard run-of-the-mill parser combinator program.

-- | We define a nap as an `ASleep` action followed by an `AWake` action.  The
-- result is a list of minutes slept.
nap :: Parser [Minute]
nap = do
    (T _ _ _ _ m0, ASleep) <- P.anyToken
    (T _ _ _ _ m1, AWake ) <- P.anyToken
    pure [m0 .. m1 - 1]     -- we can do this because m0 < m1 always in the
                            --   input data.

-- | We define a a guard's shift as a `AShift g` action, followed by
-- "many" naps.  The result is a list of minutes slept along with the ID of the
-- guard that slept them.
guardShift :: Parser (Guard, [Minute])
guardShift = do
    (_, AShift g) <- P.anyToken
    napMinutes    <- concat <$> many (P.try nap)
    pure (g, napMinutes)

-- | A log stream is many guard shifts. The result is the accumulation of all
-- of those shifts into a massive `Map Guard [Minute]` map, but turning all of
-- those [Minutes] into a frequency map instead by using `fmap freqs`.
buildTimeCards :: Parser (Map Guard TimeCard)
buildTimeCards = do
    shifts <- M.fromListWith (++) <$> many guardShift
    pure (fmap freqs shifts)

We re-use the handy freqs :: Ord a => [a] -> Map a Int function, to build a frequency map, from Day 2.

We can run a parser on our [(Time, Action)] stream by using P.parse :: Parser a -> [(Time, Action)] -> SourceName -> Either ParseError a.

The rest of the challenge involves "the X with the biggest Y" situations, which all boil down to "The key-value pair with the biggest some property of value".

We can abstract over this by writing a function that will find the key-value pair with the biggest some property of value:

import qualified Data.List.NonEmpty as NE

maximumValBy
    :: (a -> a -> Ordring)  -- ^ function to compare values
    -> Map k a
    -> Maybe (k, a)         -- ^ biggest key-value pair, using comparator function
maximumValBy c = fmap (maximumBy (c `on` snd)) . NE.nonEmpty . M.toList

-- | Get the key-value pair with highest value
maximumVal :: Ord a => Map k a -> Maybe (k, a)
maximumVal = maximumValBy compare

We use fmap (maximumBy ...) . NE.nonEmpty as basically a "safe maximum", allowing us to return Nothing in the case that the map was empty. This works because NE.nonEmpty will return Nothing if the list was empty, and Just otherwise...meaning that maximumBy is safe since it is never given to a non-empty list.

The rest of the challenge is just querying this Map Guard TimeCard using some rather finicky applications of the predicates specified by the challenge. Luckily we have our safe types to keep us from mixing up different concepts by accident.

eitherToMaybe :: Either e a -> Maybe a
eitherToMaybe = either (const Nothing) Just

day04a :: Map Time Action -> Maybe Int
day04a logs = do
    -- build time cards
    timeCards               <- eitherToMaybe $ P.parse buildTimeCards "" (M.toList logs)
    -- get the worst guard/time card pair, by finding the pair with the
    --   highest total minutes slept
    (worstGuard , timeCard) <- maximumValBy (comparing sum) timeCards
    -- get the minute in the time card with the highest frequency
    (worstMinute, _       ) <- maximumVal timeCard
    -- checksum
    pure $ _gId worstGuard * fromIntegral worstMinute

day04b :: Map Time Action -> Maybe Int
day04b logs = do
    -- build time cards
    timeCards                      <- eitherToMaybe $ P.parse buildTimeCards "" (M.toList logs)
    -- build a map of guards to their most slept minutes
    let worstMinutes :: Map Guard (Minute, Int)
        worstMinutes = M.mapMaybe maximumVal timeCards
    -- find the guard with the highest most-slept-minute
    (worstGuard, (worstMinute, _)) <- maximumValBy (comparing snd) worstMinutes
    -- checksum
    pure $ _gId worstGuard * fromIntegral worstMinute

Like I said, these are just some complicated queries, but they are a direct translation of the problem prompt. The real interesting part is the building of the time cards, I think! And not necessarily the querying part.

Parsing, again, can be done by stripping the lines of spaces and using words and readMaybes. We can use packFinite :: Integer -> Maybe (Finite n) to get our hours and minutes into the Finite type that T expects.

parseLine :: String -> Maybe (Time, Action)
parseLine str = do
    [y,mo,d,h,mi] <- traverse readMaybe timeStamp
    t             <- T y mo d <$> packFinite h <*> packFinite mi
    a             <- case rest of
      "falls":"asleep":_ -> Just ASleep
      "wakes":"up":_     -> Just AWake
      "Guard":n:_        -> AShift . G <$> readMaybe n
      _                  -> Nothing
    pure (t, a)
  where
    (timeStamp, rest) = splitAt 5
                      . words
                      . clearOut (not . isAlphaNum)
                      $ str

8

u/Kaligule Dec 04 '18

Look, you need to write a Blog. This is too good to be forgotten after 24 hours. I always appreciate a good post explaining how someone solved a problem, especially in haskell.

3

u/gerikson Dec 04 '18

Coming from a duck-typing language I could really appreciate types in this problem, if nothing else to not have to do some pre-coding scanning to see if guards slept until after 01:00, whether 2 guards overlapped etc. As it was I eyeballed the data and accounted for some obvious things (like guards who never sleep on one shift) and hacked together something that worked good enough.

→ More replies (1)

15

u/jayfoad Dec 04 '18 edited Dec 05 '18

APL #89/71

Sorting the input is trivial in Dyalog 17.0 thanks to the Total Array Ordering. Parsing the input lines is a little messy, using global assignments from a dfn to update global state. Once we've got an array a telling us how many times each guard is asleep for each minute of the witching hour, it becomes much more elegant:

f←{⊃⍸⍵=⌈/,⍵} ⍝ coordinates of maximum value
{⍵×f ⍵⌷a}f+/a ⍝ part 1
×/f a ⍝ part 2

13

u/morfi717 Dec 04 '18

Are you a human?

2

u/jayfoad Dec 05 '18

Bleep bloop, I mean yes. That's why I like writing short programs. Some of these solutions, even ones that made the leaderboard, take 5 lines of code to find the maximum value (or its location) in a list. I can do it with ⌈/vec (or vec⍳⌈/vec, the "vec-index of the max reduce of vec"). What's not to like? Especially when you're coding against the clock.

If you want to learn, you could check out The APL Orchard.

→ More replies (8)

12

u/throwaway_the_fourth Dec 04 '18

So did anyone else lose time because they had a correct solution but returned just the minute they chose, forgetting to multiply it with the guard ID? I lost so much time catching that error that it made the difference between spot 60ish and 160 on Part 1.

27

u/topaz2078 (AoC creator) Dec 04 '18

I try to make the highlighted part of the last paragraph of each puzzle a summary of that puzzle. Today, it was:

What is the ID of the guard you chose multiplied by the minute you chose?

17

u/throwaway_the_fourth Dec 04 '18

You were totally clear. I even read that sentence. It's just that my dumb ass got excited about finding a solution and forgot what result was requested.

27

u/topaz2078 (AoC creator) Dec 04 '18

Thanks; one of my biggest fears for every puzzle is that I was ambiguous somehow and made the puzzle unsolvable or unfair or something. (Seriously; I have nightmares of this.)

→ More replies (2)
→ More replies (2)

9

u/Portal2Reference Dec 04 '18

My problem was I kept multiplying by the amount of times they slept on the minute, instead of the minute itself. Made that mistake for both parts.

6

u/itsDixieNormis Dec 04 '18

The good news is that this comment made me realize why I wasn't getting the right answer for part 1, so thanks!

3

u/kieuk Dec 04 '18

I'm an idiot

→ More replies (1)

5

u/ka-splam Dec 04 '18

I lost time because I skim read "Strategy 1: Find the guard that has the most minutes asleep" and stopped reading and tried to put the guard ID.

2

u/c17r Dec 04 '18

Yup, that's what I did as well.

2

u/Flueworks Dec 04 '18

I got stumped because I did not see that a guard could fall asleep more than once in a shift. I got the first question right, so I assumed that I parsed the input correctly. So I kept trying out new ways to do part 2, but it always gave the same answer. It was not untill I manually sorted the file that I saw that a guard could fall asleep several times!

9

u/sciyoshi Dec 04 '18 edited Dec 04 '18

Python 3, #2/#2. Reached for dateutil in the heat of the moment although turns out you only need the minute!

guards = collections.defaultdict(list)
times = collections.defaultdict(int)

for line in sorted(inp(4).splitlines()):
    time, action = line.split('] ')

    time = dateutil.parser.parse(time[1:])

    if action.startswith('Guard'):
        guard = int(action.split()[1][1:])
    elif action == 'falls asleep':
        start = time
    elif action == 'wakes up':
        end = time
        guards[guard].append((start.minute, end.minute))
        times[guard] += (end - start).seconds

(guard, time) = max(times.items(), key=lambda i: i[1])
(minute, count) = max([
    (minute, sum(1 for start, end in guards[guard] if start <= minute < end))
for minute in range(60)], key=lambda i: i[1])

print('part 1:', guard * minute)

(guard, minute, count) = max([
    (guard, minute, sum(1 for start, end in guards[guard] if start <= minute < end))
for minute in range(60) for guard in guards], key=lambda i: i[2])

print('part 2:', guard * minute)

4

u/markasoftware Dec 04 '18

haha, the dates set me off as well, didn't realize they were there just for sorting.

6

u/ElecNinja Dec 04 '18

And since they use the ISO standard, you can just sort them as strings and don't need to worry about it haha.

→ More replies (6)

8

u/autid Dec 04 '18 edited Dec 04 '18

FORTRAN

Will edit with cleaned up code later. Got held up a looong time by having copy pasted a line into another loop without then updating which loop index it was using.

While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.

No.

PROGRAM DAY4
  IMPLICIT NONE
  INTEGER :: I,J,K,L,HOUR,MINUTE,DAY
  INTEGER :: IERR
  CHARACTER(LEN=50),ALLOCATABLE :: LINES(:)
  CHARACTER(LEN=50) :: LINE
  INTEGER, ALLOCATABLE :: GUARDS(:)
  LOGICAL, ALLOCATABLE :: ASLEEP(:,:)
  INTEGER, ALLOCATABLE :: DATES(:)
  CHARACTER(LEN=8) :: DATE
  INTEGER :: GUARD,BESTGUARD,SLEEP,BESTSLEEP,SLEEPMINUTE(0:59),BESTMINUTE,BESTMINUTEAMMOUNT
  OPEN(1,FILE='input.txt')
  I=0
  DO
     READ(1,*,IOSTAT=IERR)
     IF(IERR.NE.0)EXIT
     I=I+1
  END DO
  ALLOCATE(LINES(I))
  REWIND(1)
  READ(1,'(A)')LINES
  CLOSE(1)

  J=1
  DO K=2,I
     IF(ANY(LINES(1:K-1)(1:12).EQ.LINES(K)(1:12)))CYCLE
     J=J+1
  END DO

  ALLOCATE(GUARDS(J),ASLEEP(J,0:59),DATES(J))
  DATES=99999999
  ASLEEP=.FALSE.
  GUARDS=0
  WRITE(DATE,'(A)') LINES(1)(2:5)//LINES(1)(7:8)//LINES(1)(10:11)
  READ(DATE,*) DAY
  DATES(1)=DAY
  L=2
  DO K=2,I
     WRITE(DATE,'(A)') LINES(K)(2:5)//LINES(K)(7:8)//LINES(K)(10:11)
     READ(DATE,*) DAY
     IF(ANY(DATES(1:L-1).EQ.DAY))CYCLE
     DATES(L)=DAY
     L=L+1
  END DO

  DO J=1,I
     WRITE(DATE,'(A)') LINES(J)(2:5)//LINES(J)(7:8)//LINES(J)(10:11)
     READ(DATE,*) DAY
     READ(LINES(J)(13:14),*)HOUR
     READ(LINES(J)(16:17),*)MINUTE
     IF(HOUR>0)DAY=MINVAL(DATES,MASK=DATES>DAY)
     SELECT CASE (LINES(J)(20:24))
     CASE ('Guard')
        READ(LINES(J)(SCAN(LINES(J),'#')+1:SCAN(LINES(J),'b')-1),*) GUARDS(MINLOC(DATES,MASK=DATES.EQ.DAY,DIM=1))
     CASE('wakes')
        ASLEEP(MINLOC(DATES,MASK=DATES.EQ.DAY,DIM=1),MINUTE:59)=.NOT.ASLEEP(MINLOC(DATES,MASK=DATES.EQ.DAY,DIM=1),MINUTE:59)
     CASE('falls')
        ASLEEP(MINLOC(DATES,MASK=DATES.EQ.DAY,DIM=1),MINUTE:59)=.NOT.ASLEEP(MINLOC(DATES,MASK=DATES.EQ.DAY,DIM=1),MINUTE:59)
     END SELECT
  END DO

  !Part 1
  BESTGUARD=0
  BESTSLEEP=0
  DO I=1,SIZE(GUARDS,DIM=1)
     SLEEP=0
     GUARD=GUARDS(I)
     DO J=1,SIZE(GUARDS,DIM=1)
        IF (GUARDS(J).EQ.GUARD) SLEEP=SLEEP+COUNT(ASLEEP(J,:))
     END DO
     IF(SLEEP>BESTSLEEP)THEN
        BESTSLEEP=SLEEP
        BESTGUARD=GUARD
     END IF
  END DO
  SLEEPMINUTE=0
  DO I=1,SIZE(GUARDS,DIM=1)
     IF(GUARDS(I).NE.BESTGUARD)CYCLE
     DO J=0,59
        IF(ASLEEP(I,J)) SLEEPMINUTE(J)=SLEEPMINUTE(J)+1
     END DO
  END DO
  WRITE(*,'(A,I0)') 'Part 1: ',(MAXLOC(SLEEPMINUTE,DIM=1)-1)*BESTGUARD

  !PART 2
  BESTMINUTE=0
  BESTMINUTEAMMOUNT=0
  DO I=1,SIZE(GUARDS,DIM=1)
     SLEEPMINUTE=0
     GUARD=GUARDS(I)
     DO J=1,SIZE(GUARDS,DIM=1)
        IF (GUARDS(J).NE.GUARD)CYCLE
        DO K=0,59
           IF(ASLEEP(J,K)) SLEEPMINUTE(K)=SLEEPMINUTE(K)+1
        END DO
     END DO
     IF(MAXVAL(SLEEPMINUTE)>BESTMINUTEAMMOUNT)THEN
        BESTMINUTE=MAXLOC(SLEEPMINUTE,DIM=1)-1
        BESTGUARD=GUARD
        BESTMINUTEAMMOUNT=MAXVAL(SLEEPMINUTE)
     END IF
  END DO
  WRITE(*,'(A,I0)') 'Part 2: ',BESTGUARD*BESTMINUTE

  DEALLOCATE(LINES,DATES,GUARDS,ASLEEP)
END PROGRAM DAY4

7

u/0rac1e Dec 04 '18 edited Aug 30 '21

Perl 6

Looks like I'm posting the first Perl 6 solution of the day.

This is post-cleanup... but more or less what I wrote, except all the functions were inline. My cleanup was just moving them all into (anonymous) functions so the implementation reads better.

# Helper functions
my &guard              = { .substr(26).words.head         }
my &mins               = { .substr(15, 2).Int             }
my &total-sleep-length = { .value.total                   }
my &most-slept-minute  = { .value.max(*.value).value      }
my &magic-value        = { .key × .value.max(*.value).key }

# State variables
my (%sleep, $guard, $falls);

# Data collection
for 'input'.IO.lines.sort {
    when *.contains('Guard') { $guard = .&guard }
    when *.contains('falls') { $falls = .&mins  }
    when *.contains('wakes') { %sleep{$guard} ⊎= $falls ..^ .&mins }
}

# Output solution
for &total-sleep-length, &most-slept-minute -> &func {
    say %sleep.max(&func).&magic-value
}

The ⊎ operator is the Multiset Union operator (ASCII version: (+)). Perl 6 has a Bag type (aka. Multiset). I create a range of the minutes slept and add those to the Bag of values for that guard.

→ More replies (1)

7

u/that_lego_guy Dec 04 '18

Day 4...IN EXCEL??!?!?

 =COUNTBYCELLCOLOR(M4:BT4,$I$1)

https://github.com/thatlegoguy/AoC2018

6

u/vash3r Dec 04 '18

Python 2, just missed the leaderboard for the second star. I completely forgot about 'in' and spent too long indexing the strings, as well as not initially sorting the data by time and sorting the list of guards the wrong way, which I figured out after I tried with the test code. (The code has been edited.)

l = sorted(lines)
guards = defaultdict(lambda:[0 for x in range(60)])
for s in l:
	if s[25]=="#":
		g=s.split()[3]
	elif s[25]=="a":
		st=int(s[15:17])
	else: # wake up
		t=int(s[15:17])
		for x in range(st,t):
			guards[g][x]+=1

# part 1
g1 = sorted(guards.keys(), key=lambda g:-sum(guards[g]))[0]
# part 2
g2 = sorted(guards.keys(), key=lambda g:-max(guards[g]))[0]

for g in [g1,g2]:
	gh = guards[g]
	minute = gh.index(max(gh))
	print int(g[1:])*minute
→ More replies (1)

6

u/u794575248 Dec 04 '18

Python 3 51/63

from collections import Counter, defaultdict
from datetime import datetime

guards = defaultdict(Counter)
for t, m in [l.split('] ') for l in sorted(s.splitlines()) if l]:
    t = datetime.strptime(t, '[%Y-%m-%d %H:%M')
    if '#' in m:     g = int(m.split('#')[1].split()[0])
    if 'falls' in m: start = t
    if 'wakes' in m:
        minutes = int((t - start).total_seconds() // 60)
        guards[g].update(Counter((start.minute+i)%60 for i in range(minutes)))

_, id = max((sum(c.values()), id) for id, c in guards.items())
part1 = id * guards[id].most_common()[0][0]

(_, minute), id = max((c.most_common()[0][::-1], id) for id, c in guards.items())
part2 = id * minute

2

u/EquationTAKEN Dec 04 '18

At which point do you read the input? Looks like the variable s is undefined, so presumably that's the input?

2

u/u794575248 Dec 04 '18

so presumably that's the input

That's right. I just copy and paste it manually to s variable in IPython.

→ More replies (4)

0

u/[deleted] Dec 04 '18

[deleted]

→ More replies (1)

5

u/udoprog Dec 04 '18 edited Dec 04 '18

Today's puzzle would have been a lot easier if my language supported looser types.

I also need to learn how to read the instructions closer to make my life simpler!

Another day, another Rust:

use aoc2018::*;
use chrono::{Timelike, Duration};

fn main() -> Result<(), Error> {
    let mut records = Vec::new();

    for line in BufReader::new(input!("day4.txt")).lines() {
        let line = line?;
        let (date, rest) = line.split_at(18);
        let date = chrono::NaiveDateTime::parse_from_str(date, "[%Y-%m-%d %H:%M]")?;
        records.push((date, rest.trim().to_string()));
    }

    records.sort_by(|a, b| a.0.cmp(&b.0));

    let mut current = 0u32;
    let mut asleep = None;

    let mut minutes_asleep = HashMap::<_, u32>::new();
    let mut guard_asleep_at = HashMap::<_, HashMap<u32, u32>>::new();
    let mut minute_asleep = HashMap::<_, HashMap<u32, u32>>::new();

    for (date, rest) in records {
        if rest.ends_with("begins shift") {
            current = str::parse(&rest.split(" ").nth(1).unwrap()[1..])?;
            asleep = None;
            continue;
        }

        match rest.as_str() {
            "falls asleep" => {
                asleep = Some(date);
            }
            "wakes up" => {
                let asleep = asleep.as_ref().expect("guard did not fall asleep");
                let count = date.signed_duration_since(asleep.clone()).num_minutes() as u32;

                *minutes_asleep.entry(current).or_default() += count;

                let mut m = asleep.minute();

                for i in 0..count {
                    *guard_asleep_at
                        .entry(current)
                        .or_default()
                        .entry(m)
                        .or_default() += 1;

                    *minute_asleep
                        .entry(m)
                        .or_default()
                        .entry(current)
                        .or_default() += 1;

                    m += 1;
                }
            }
            other => {
                panic!("other: {}", other);
            }
        }
    }

    let max = minutes_asleep
        .into_iter()
        .max_by(|a, b| a.1.cmp(&b.1))
        .expect("no max asleep");

    let pair = guard_asleep_at
        .remove(&max.0)
        .unwrap_or_default()
        .into_iter()
        .max_by(|a, b| a.1.cmp(&b.1))
        .expect("no max guard asleep");

    let mut sleep_min = None;

    for (ts, guards) in minute_asleep {
        if let Some((guard, times)) = guards.into_iter().max_by(|a, b| a.1.cmp(&b.1)) {
            sleep_min = match sleep_min {
                Some((ts, guard, max)) if max > times => Some((ts, guard, max)),
                Some(_) | None => Some((ts, guard, times)),
            };
        }
    }

    let sleep_min = sleep_min.expect("no result found");

    assert_eq!(pair.0 * max.0, 19830);
    assert_eq!(sleep_min.0 * sleep_min.1, 43695);
    Ok(())
}
→ More replies (1)

8

u/markasoftware Dec 04 '18 edited Dec 04 '18

Finally got top 100 :) 62/43 with Gawk (used multidimensional arrays so I guess it's not awk anymore)

This is exact code I submitted, no ifs ands or buts.

Run sort -V on the file before inputting to Awk

Part 1:

BEGIN {
#   RS
  FPAT = "#?[0-9]+"
  guard=99999999
}
/#[0-9]+/{
  guard=$6
}
/wake/{
  minute=$5
  for(i=fell_asleep[guard];i<minute;i++) {
    asleep_minutes[guard][i]++
  }
  sleep_time[guard] += (minute - fell_asleep[guard])
}
/falls/ {
  minute=$5
  fell_asleep[guard] = minute
}
END {
  max_sleep_time=0
  max_sleep_guard=999999999
  for(guard in sleep_time) {
    if (sleep_time[guard] > max_sleep_time) {
      max_sleep_guard = guard
      max_sleep_time = sleep_time[guard]
    }
  }
  max_minute=9999999
  max_minute_val=0
  for(minute in asleep_minutes[max_sleep_guard]) {
    if (asleep_minutes[max_sleep_guard][minute] > max_minute_val) {
      max_minute= minute
      max_minute_val=asleep_minutes[max_sleep_guard][minute]
    }
  }
  print max_sleep_guard
  print max_minute
}

Part 2:

BEGIN {
#   RS
  FPAT = "#?[0-9]+"
  guard=99999999
}
/#[0-9]+/{
  guard=$6
}
/wake/{
  minute=$5
  for(i=fell_asleep[guard];i<minute;i++) {
    asleep_minutes[guard][i]++
  }
  sleep_time[guard] += (minute - fell_asleep[guard])
}
/falls/ {
  minute=$5
  fell_asleep[guard] = minute
}
END {
#   max_sleep_time=0
#   max_sleep_guard=999999999
#   for(guard in sleep_time) {
#     if (sleep_time[guard] > max_sleep_time) {
#       max_sleep_guard = guard
#       max_sleep_time = sleep_time[guard]
#     }
#   }
  max_minute=9999999
  max_minute_val=0
  max_guard=99999999999
  for(guard in asleep_minutes) {
    for(minute in asleep_minutes[guard]) {
      if (asleep_minutes[guard][minute] > max_minute_val) {
        max_minute= minute
        max_guard=guard
        max_minute_val=asleep_minutes[guard][minute]
      }
    }
  }
  print max_guard
  print max_minute
}
→ More replies (6)

4

u/PendragonDaGreat Dec 04 '18

Powershell 5.1

[card] Python's sum()

Both halves used this basic layout:

$data = Get-Content $inputPath | Sort-Object

$timer = New-Object System.Diagnostics.Stopwatch
$timer.Start()

$guards = @{}
$curDate = ''
$curGuard = -1
[int]$sleepMinute = -1
foreach ($line in $data) {
    $tokens = $line.Replace('[', '').Replace(']', '').Replace(':', ' ').Replace('#', '').Split(' ')
    if ($tokens[0] -ne $curDate -or [int]$tokens[1] -eq 23) {
        $curDate = $tokens[0]
        if ($tokens[4] -ne 'asleep') {
            [int]$curGuard = $tokens[4]
        }
        if ($null -eq $guards[$curGuard]) {
            $guards[$curGuard] = New-Object int[] 60
        }
    }

    if ($tokens[3] -eq 'falls') {
        [int]$sleepMinute = $tokens[2]
    }
    elseif ($tokens[3] -eq 'wakes') {
        for ($i = $sleepMinute; $i -lt [int]$tokens[2]; $i++) {
            $guards[$curGuard][$i]++
        }
    }

}

Basically enumerate everything a hashtable, with the key as the guard's ID and a 60 int array for the minutes as the value, and the amount of time they were asleep. then hook the appropriate ending on the bottom from below

Part 1

$curBestGuard = -1
$curBestGuardTime = -1
$curBestMinute = -1

foreach ($g in $guards.Keys) {
    $sum = 0
    $guards[$g] | ForEach-Object {$sum += $_}
    if ($sum -gt $curBestGuardTime) {
        $curBestGuard = $g
        $curBestGuardTime = $sum
        $maximum = ($guards[$g] | Measure-Object -Max).Maximum
        [int]$curBestMinute = [Array]::IndexOf($guards[$g], [int]$maximum) 
        
    }
}

$check = [int]$curBestGuard * [int]$curBestMinute
Write-Host $check
$timer.Stop()
Write-Host $timer.Elapsed

Average runtime: 0.058s

Part 2

$curBestGuard = -1
$curBestGuardTime = -1
$curBestMinute = -1

foreach ($g in $guards.Keys) {
    $maximum = ($guards[$g] | Measure-Object -Max).Maximum
    if ($maximum -gt $curBestMinute) {
        [int]$curBestGuardTime = [Array]::IndexOf($guards[$g], [int]$maximum)
        $curBestGuard = $g
        $curBestMinute = $maximum
    }
        
}


$check = [int]$curBestGuard * [int]$curBestGuardTime
Write-Host $check

$timer.Stop()
Write-Host $timer.Elapsed

Average runtime 0.053s

4

u/tehjimmeh Dec 04 '18 edited Dec 05 '18

C++

int main(int argc, char* argv[]) {
    std::ifstream ifs(argv[1]); std::vector<std::string> lines;
    for (std::string l; std::getline(ifs, l);) { lines.push_back(l); }
    std::sort(lines.begin(), lines.end());

    std::map<int, std::array<int, 60>> guards;
    int s = -1, id = -1, *g = nullptr; std::smatch m;
    for (const auto& l : lines)
        if (std::regex_match(l, m, std::regex(R"(.*Guard #(\d+) begins shift)")))
            g = guards[std::stoi(m[1])].data();
        else if (std::regex_match(l, m, std::regex(R"(.*:(\d+)\] wakes up)")))
            std::for_each(g+s, g+std::stoi(m[1]), [](auto& x){x++;});
        else if (std::regex_match(l, m, std::regex(R"(.*:(\d+)\] falls asleep)")))
            s = std::stoi(m[1]);
    
    auto maxMinute = [](const auto& gi) { return[&gi](auto m) -> std::pair<int,int>{
        return {std::distance(gi.begin(), m), *m};}(std::max_element(gi.begin(), gi.end()));};

    const auto& [id1,guardInfo1] = (*std::max_element(guards.begin(), guards.end(),
        [](auto& l, auto& r) { return std::reduce(l.second.begin(), l.second.end()) <
            std::reduce(r.second.begin(), r.second.end()); }));
    const auto& minute1 = maxMinute(guardInfo1).first;

    const auto& [id2,guardInfo2] = *std::max_element(guards.begin(), guards.end(),
        [&](auto& l, auto& r) { return maxMinute(l.second).second < maxMinute(r.second).second; });
    const auto& minute2 = maxMinute(guardInfo2).first;

    std::cout << "1: " << minute1 * id1 << "\n" << "2: " << minute2 * id2 << "\n";
}

2

u/antfarmar Dec 04 '18

I think Part 2 should be the following, since maxMinute returns an index, and not the value there!

const auto &[id2, guardInfo2] =
        *std::max_element(guards.begin(), guards.end(), [&](auto &l, auto &r) {
            return l.second[maxMinute(l.second)] <
                   r.second[maxMinute(r.second)];
        });
→ More replies (2)

2

u/willkill07 Dec 05 '18

Wow, my solution was very similar to yours.

#include <unordered_map>
#include <vector>
#include <range/v3/algorithm.hpp>
#include <range/v3/getlines.hpp>
#include <range/v3/numeric.hpp>
#include <range/v3/view/slice.hpp>

namespace view = ranges::view;

template <>
template <bool part2>
void
Day<4>::solve(std::istream& is, std::ostream& os) {
  std::vector<std::string> events(ranges::getlines(is));
  ranges::sort(events);
  std::unordered_map<int, std::array<int, 60>> guards;
  {
    int id {0}, start {0}, stop {0};
    char c;
    for (std::string const& s : events) {
      if (sscanf(s.c_str() + 15, "%d] %c", &stop, &c) == 2 && c == 'w') {
        for (auto& t : guards[id] | view::slice(start, stop)) {
          ++t;
        }
      } else if (sscanf(s.c_str() + 15, "%d] %c", &start, &c) == 2 && c == 'f') {
      } else if (sscanf(s.c_str() + 19, "Guard #%d", &id) == 1) {
      }
    }
  }
  auto max = [](const auto& l) {
    return ranges::distance(ranges::begin(l), ranges::max_element(l));
  };
  auto map = [&](auto const& pair) {
    if constexpr (auto& [_, l] = pair; part2) {
      return ranges::max(l);
    } else {
      return ranges::accumulate(l, 0);
    }
  };
  const auto& [id, data] = *ranges::max_element(guards, std::less<>{}, map);
  const auto& minute = max(data);
  os << id * minute << std::endl;
}
→ More replies (2)
→ More replies (1)

4

u/binajohny Dec 04 '18

My Kotlin solution (GitHub)

data class Event(val month: Int, val day: Int, val hour: Int, val minute: Int, val guardId: Int, val type: Type) {
    enum class Type { BEGIN_SHIFT, FALL_ASLEEP, WAKE_UP }

    companion object {
        private val REGEX = Regex("\\d+")

        fun fromString(s: String): Event {
            val (m, d, h, min, gid) = REGEX.findAll(s).map { it.value.toInt() }.plus(-1).drop(1).take(5).toList()

            val type = when {
                s.contains("wakes") -> Type.WAKE_UP
                s.contains("falls") -> Type.FALL_ASLEEP
                else -> Type.BEGIN_SHIFT
            }

            return Event(m, d, h, min, gid, type)
        }
    }
}

private fun createSleepStats(input: List<Event>): Map<Int, IntArray> {
    val map = mutableMapOf<Int, IntArray>()

    var currentGuardId = -1
    var beginMinute = -1

    input.forEach {
        when (it.type) {
            Event.Type.BEGIN_SHIFT -> currentGuardId = it.guardId
            Event.Type.FALL_ASLEEP -> beginMinute = it.minute
            Event.Type.WAKE_UP -> {
                val arr = map.getOrPut(currentGuardId) { IntArray(60) }
                (beginMinute until it.minute).forEach { i -> arr[i]++ }
            }
        }
    }

    return map
}

fun part1(input: List<Event>): Int {
    val sleepStats = createSleepStats(input)

    val max = sleepStats.maxBy { it.value.sum() }!!
    val maxMinute = max.value.withIndex().maxBy { it.value }!!.index

    return max.key * maxMinute
}

fun part2(input: List<Event>): Int {
    val sleepStats = createSleepStats(input)

    val max = sleepStats.map {
        it.key to it.value.withIndex().maxBy { x -> x.value }!!
    }.maxBy { it.second.value } ?: return 0

    return max.first * max.second.index
}
→ More replies (2)

4

u/TheMuffinMan616 Dec 04 '18

Haskell. Spent too much time trying to find a date parsing library. Not really happy with how it came out. State monad might be a better option here. shrug

module Day04 where

import Data.Ord
import Data.List
import Data.List.Split
import Data.Map (Map)
import qualified Data.Map as M

import Debug.Trace

data Time = Time
    { month :: Int
    , day :: Int
    , hour :: Int
    , minute :: Int
    } deriving (Show, Eq, Ord)

data Event = Start Int | Sleep | Wake deriving (Show)

isStart :: Event -> Bool
isStart (Start _) = True
isStart _         = False

readInput :: String -> IO [(Time, Event)]
readInput f = toEvents . lines <$> readFile f
    where toEvents = sortBy (comparing fst) . map parse

parse :: String -> (Time, Event)
parse s = (Time (read m) (read d) (read h) (read mm), toEvent rest)
    where (_:m:d:h:mm:rest) = split (dropDelims . dropBlanks $ oneOf " #[]-:") s
          toEvent ["Guard", id, _, _]   = Start (read id)
          toEvent ["falls", _]          = Sleep
          toEvent ["wakes", _]          = Wake

analyzeShift :: [(Time, Event)] -> (Int, [Int])
analyzeShift z@((_, Start id):es) = (id, minutes es)
    where minutes [] = []
          minutes ((ts, Sleep):(ta, Wake):xs) = [minute ts..(minute ta) - 1] ++ minutes xs

aggregate :: [(Time, Event)] -> Map Int (Map Int Int)
aggregate = M.map freq . M.fromListWith (++) . map analyzeShift . toShifts
    where freq xs = M.fromListWith (+) [(x, 1) | x <- xs]
          toShifts = split (keepDelimsL . dropInitBlank $ whenElt (isStart . snd))

solve :: ([Int] -> Int) -> Map Int (Map Int Int) -> Int
solve f gs = (fst . maximumBy (comparing snd) . M.toList . snd $ target) * (fst target)
    where target = maximumBy (comparing maxMinutes) . filter (not . null . snd) . M.toList $ gs
          maxMinutes = f . M.elems . snd

part1 :: Map Int (Map Int Int) -> Int
part1 = solve sum

part2 :: Map Int (Map Int Int) -> Int
part2 = solve maximum

main :: IO ()
main = do
    input <- aggregate <$> readInput "input/Day04.txt"
    print . part1 $ input
    print . part2 $ input
→ More replies (1)

4

u/Theguy6758 Dec 04 '18

Pure Bash

Uses the guard ids as the index and stores the frequency of the minutes spent sleeping as a string of numbers separated by spaces. Every time I want to query a guard's sleep frequency, the "midnight" array needs to get rebuilt.

Full Script: https://pastebin.com/0sxWZuKi

Part 1
part1()
{
    [[ -f "${PWD}/input" ]] && {
        mapfile -t file < "${PWD}/input"
        mapfile -t file < <(qsort "${file[@]}")

        i="0"
        while ((i < ${#file[@]})); do
            read -r _ _ _ id _ <<< "${file[$i]}"
            id="${id/'#'}"

            if [[ "${midnight_list[$id]}" ]]; then
                read -ra midnight <<< "${midnight_list[$id]}"
            else
                for j in {0..59}; do
                    midnight[$j]="0"
                done
            fi

            ((i++))
            j="0"
            while [[ ! "${file[$i]}" =~ 'Guard' ]] && ((i < ${#file[@]})); do
                ((i++)); ((j++))
            done

            for ((k = i - j; k < i; k++)); do
                read -r _ sleep_time _ <<< "${file[$((k++))]}"
                read -r _ wake_time _ <<< "${file[$k]}"

                sleep_time="${sleep_time/*:}"
                sleep_time="${sleep_time/']'}"
                sleep_time="$((10#${sleep_time}))"

                wake_time="${wake_time/*:}"
                wake_time="${wake_time/']'}"
                wake_time="$((10#${wake_time}))"

                for ((l = sleep_time; l < wake_time; l++)); do
                    ((midnight[l]++))
                done
            done

            printf -v midnight_list[$id] "%d " "${midnight[@]}"
        done

        for id in "${!midnight_list[@]}"; do
            unset total_time
            unset max

            read -ra midnight <<< "${midnight_list[$id]}"
            for i in "${midnight[@]}"; do
                ((total_time += i))
                ((max = i > ${max:=0} ? i : ${max:=0}))
            done

            printf -v midnight_list[$id] "%d " \
                "${total_time}" \
                "${max}" \
                "${midnight[@]}"
        done

        unset max
        for id in "${!midnight_list[@]}"; do
            read -ra midnight <<< "${midnight_list[$id]}"
            ((max = midnight[0] > ${max:=0} ? midnight[0] : ${max:=0}))
        done

        for id in "${!midnight_list[@]}"; do
            max_min="0"
            read -ra midnight <<< "${midnight_list[$id]}"
            ((max == midnight[0])) && \
                for minute in "${midnight[@]:2}"; do
                    if ((midnight[1] != minute)); then
                        ((max_min++))
                    else
                        break 2
                    fi
                done
        done

        printf "ID: %d\\nMinute: %d\\n" "${id}" "${max_min}"
        printf "Part1: %d\\n\\n" "$((id * max_min))"
    }
}
Part 2
part2()
{
    [[ "${midnight_list[*]}" ]] && {
        unset max
        for id in "${!midnight_list[@]}"; do
            read -ra midnight <<< "${midnight_list[$id]}"
            ((max = midnight[1] > ${max:=0} ? midnight[1] : ${max:=0}))
        done

        for id in "${!midnight_list[@]}"; do
            max_min="0"
            read -ra midnight <<< "${midnight_list[$id]}"
            ((max == midnight[1])) && \
                for minute in "${midnight[@]:2}"; do
                    if ((midnight[1] != minute)); then
                        ((max_min++))
                    else
                        break 2
                    fi
                done
        done

        printf "ID: %d\\nMinute: %d\\n" "${id}" "${max_min}"
        printf "Part2: %d\\n" "$((id * max_min))"
    }
}
Quick sort implementation
_qsort()
{
    (($# == 0)) && {
        unset qreturn
        return
    }

    local pivot="$1"; shift
    local i
    local -a higher
    local -a lower

    for i in "$@"; do
        if [[ "$i" < "${pivot}" ]]; then
            lower+=("$i")
        else
            higher+=("$i")
        fi
    done

    _qsort "${higher[@]}"
    higher=("${qreturn[@]}")

    _qsort "${lower[@]}"
    qreturn+=("${pivot}" "${higher[@]}")
}

qsort()
(
    qreturn=()
    _qsort "$@"
    printf "%s\\n" "${qreturn[@]}"
)

4

u/[deleted] Dec 04 '18

TCL, probably spent too much time on part 1 wondering about whether the watch-starts before midnight would be significant in part 2... plus looking up man pages for "clock scan/format" was completely uneccessary :-))

proc scantime {month day hour minute} {
    return [clock scan "1518 $month $day $hour $minute" -format {%Y %m %d %H %M}]
}

proc minutes {end start} {
    return [expr {($end-$start)/60.0}]
}

set guard ??
set currguard $guard
array set times {}
while {[gets stdin line] >= 0} {

    # [1518-11-01 00:00] Guard #10 begins shift
    if {[scan $line {[1518-%d-%d %d:%d] Guard #%d begins shift} month day hour minute guard] == 5} {
	if {[llength $currguard] > 1} {
	    lappend times([lindex $currguard 0]) [lrange $currguard 1 end]
	}
	set start [scantime $month $day $hour $minute]
	set currguard [list $guard $start]
    } elseif {[scan $line {[1518-%d-%d %d:%d] falls %s} month day hour minute what] == 5} {
	set asleep [scantime $month $day $hour $minute]
	if {[llength $currguard] %2 != 0} {
	    error "$currguard odd length at sleep $line"
	}
	lappend currguard $asleep
    } elseif {[scan $line {[1518-%d-%d %d:%d] wakes %s} month day hour minute what] == 5} {
	set wakeup [scantime $month $day $hour $minute]
	if {[llength $currguard] %2 == 0} {
	    error "$currguard even length on wakeup $line"
	}
	lappend currguard $wakeup
    } else {
	error "cant parse line {$line}"
    }
}
# dont miss the last entry
if {[llength $currguard] > 1} {
    lappend times([lindex $currguard 0]) [lrange $currguard 1 end]
}

set guard_maxsleep ""
set maxsleep_secs 0
set part2_solution {"" 0 0}
set maxmin {0 0}
foreach {g ts} [array get times] {
    set asleep 0
    array unset minute_sleeping
    foreach elt $ts {
	# watch start might be before midnight, does that matter? not for this puzzle...
	foreach {start end} [lrange $elt 1 end] {
	    set mstart [scan [clock format $start -format %M] %d]
	    set mend [scan [clock format $end -format %M] %d ]
	    for {set m $mstart} {$m < $mend} {incr m} {
		incr asleep
		incr minute_sleeping($m)
	    }
	}
    }
    # part 2
    foreach {m numtimes} [array get minute_sleeping] {
	if {$numtimes > [lindex $part2_solution 2]} {
	    set part2_solution [list $g $m $numtimes]
	}
    }    

    # part 1
    if {$asleep > $maxsleep_secs} {
	set maxsleep_secs $asleep
	set guard_maxsleep [list $g ]
	foreach {m numtimes} [array get minute_sleeping] {
	    if {$numtimes > [lindex $maxmin 2]} {
		set maxmin [list $g $m $numtimes]
	    }
	}
    }
}

puts "Part 1: guard [lindex $maxmin 0] sleeps $maxsleep_secs secs, $maxmin => [expr {[lindex $maxmin 0]*[lindex $maxmin 1]}]"
puts "Part 2: $part2_solution => [expr {[lindex $part2_solution 0]*[lindex $part2_solution 1]}]"

4

u/azatol Dec 04 '18

My F# solution. After struggling and learning from Day 3, this one wasn't as hard. I did learn about using folds.

https://gist.github.com/apeterson-BFI/8f96ed4573f3aadfc50c9a3634ff3ff8

3

u/chunes Dec 04 '18

I'm glad to see an F# solution. It's one of my favorite languages to read.

2

u/azatol Dec 05 '18

I've been learning a lot more about the language by working through these. I do use it a bit at work, but usually not with the kind of algorithms the Advent of Code problems need.

→ More replies (1)

4

u/phil_g Dec 04 '18 edited Dec 04 '18

Here's my Common Lisp solution.

I got around to writing a magic input function. It's defined in my library package. The input function pulls the year and day out of the current package name, downloads and caches the data, and presents things in a reasonable-useful format (either a single string or a list of strings, depending on the original format).

This also demonstrates one of many areas where iterate is more concise than loop. loop doesn't have anything like iterate's (finding *x* maximizing *y*) clause.

In retrospect I overengineered some of the logic. I was prepared for tricky things like guards falling asleep before midnight or guards only being woken up implicitly by their replacements arriving, but the data was a lot more straightforward than that. Once you've sorted the data, you only needed to look at the minute values for the sleep/wake up lines.

Edit: Visualization of my data.

1

u/dlamblin Dec 04 '18 edited Dec 04 '18

[Card]

Today's puzzle would have been a lot easier if my language supported the Elvis operator.

Perl (5.28) … I copied all the input out of the browser for pbpaste.

$ pbpaste|sort|perl -e '
use warnings;use strict;use feature ":5.28";use List::Util "reduce";
my($g,$f,$w,$x,$Gx,$GMx,$Gy,$GMy,%G,%M)=((0)x8,()x2); foreach((<>)){
if (m/#(\d+) /)     {$g=int($1)}
if (m/(\d{2})\] f/) {$f=int($1)}
if (m/(\d{2})\] w/) {$w=int($1); $G{$g}+=$w-$f; for(my $m=$f; $m<$w; $m++){
┆ my $mx=$M{$g}{$m}++; if ($mx>$x) {$x=$mx; $Gy=$g; $GMy=$m;} } } }
print "Minutes    "; foreach (0..59) {printf " %02d", $_;}
foreach my $u (sort {$a<=>$b} keys %M) {printf "\nG%4d T%4d", $u, $G{$u};
┆ foreach (0..59) {printf " %2s", exists $M{$u}{$_} ? $M{$u}{$_} : "";} } say"";
$Gx  = reduce {     $G{$a} > $G{$b}      ? $a:$b} keys %G;
$GMx = reduce {$M{$Gx}{$a} > $M{$Gx}{$b} ? $a:$b} keys %{$M{$Gx}};
say "Part 1: ".($Gx*$GMx)." Guard #$Gx Awake $G{$Gx} mins. At min $GMx was awake $M{$Gx}{$GMx} times";
say "Part 2: ".($Gy*$GMy)." Guard #$Gy at min $GMy was awake $M{$Gy}{$GMy} times";'

Minutes     00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
G  73 T 234     1  1  1  1  1  1  2  2  2  2  3  3  4  4  5  4  4  5  5  6  5  4  5  5  6  6  6  5  6  6  6  6  6  5  5  5  5  5  5  4  4  4  4  4  5  5  5  5  5  4  5  5  4  4  4  2  1  1
G 151 T 310  1  1  1  2  2  2  2  4  4  4  4  5  6  6  6  7  6  6  6  7  7  7  7  7  6  6  5  5  3  4  4  4  3  3  3  3  3  5  5  7  8  8 10 11 12 11 10  9  8  6  6  5  3  4  4  4  5  4  3
G 389 T 280     1  2  3  4  4  4  4  4  4  5  5  4  4  4  4  4  4  3  5  5  5  6  6  6  7  8  7  6  5  5  5  5  6  6  6  6  6  6  6  6  4  4  5  6  6  5  4  4  5  6  7  6  5  5  4  3  3  2
G 463 T 426     1  1  1  1  1  1  1  1  1  1  2  3  3  3  2  3  6  6  6  5  6  7  7  9  8  9  8  8  8  9  9  8  9  8  9  9  9 10 10  9 10 11 12 13 14 14 17 18 20 18 14 11 10  8  7  7  3  1
G 523 T 511  2  3  3  4  5  6  6  6  7  7  7  7  7  6  6  6  7  6  6  6  9  9  9 10 10 10 11 12 12 12 12 13 12 12 12 12 13 13 14 12 12 12 12 12 12 12 11 11 10 10  9  6  7  8  8  7  6  4
G 653 T 188              1  1  1  1  1  1  1  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3  4  4  4  5  5  5  5  6  6  6  6  6  6  6  5  5  5  5  5  5  4  5  5  4  4  4  4  3  3  2
G 829 T 372     1  1  1  1  1  1  2  2  3  3  3  3  3  3  3  4  4  6  6  7  7  7  7  7  6  7  7  7  8  9  9  9  9  8  9  9  9  9  8 10 10 10 10  9 11 11 13 13 12 11 10  8  6  6  4  4  4  1
G1069 T 319  1  1  3  4  4  4  4  5  5  5  5  4  4  4  4  3  3  3  3  4  4  5  5  4  6  6  6  7  7  7  8  8  9  9 10  9 11 11 10  9  9  8  8  7  7  7  6  6  6  4  3  3  4  5  5  3  2  1  1
G1123 T 304  1  1  1  1  1  2  3  3  3  2  3  3  4  5  5  5  5  7  7  9  9  9  9  9  9  9  9  9  9 10  9  9  8  8  8  6  6  6  6  6  4  5  5  6  5  6  3  4  4  4  4  2  3  3  4  3  3  1  1
G1399 T 444     1  1  3  3  3  3  5  5  6  7  8  8  8  9  9  8  9  9  9  9 10  9  9  9 10 10  9  8  8  8  9 10 10 11 11 11 10 11 11 12 12 10 11 11 11 10  9  7  6  6  6  5  5  4  4  5  3
G1597 T 236     2  2  2  2  2  2  2  3  3  3  4  4  4  4  3  3  3  3  3  3  3  3  4  4  5  5  5  7  7  7  6  7  6  5  4  4  3  3  3  3  3  4  4  5  4  4  4  4  5  5  5  6  6  6  6  6  5  3
G1699 T 370  1  1  1  1  1  2  3  3  3  2  2  2  3  3  4  4  4  5  5  5  5  7  7  7  9  9  9  9 11 10 10 10 11 10 11 12 13 13 13 12 12 11 10  9  9  8  7  6  4  4  4  8  8  7  4  2  1  3
G1759 T 338           1  2  2  2  2  2  2  3  3  4  4  4  3  3  4  4  5  5  5  5  5  4  4  4  4  5  5  4  4  4  5  5  7  8  8  9 10 12 12 12 12 12 12 12 13 12 12 12  9  7  7  7  5  5  3  2
G1861 T 287              1  1  2  2  3  3  4  4  4  4  4  5  5  5  6  7  7  6  6  6  6  6  6  5  6  6  6  6  6  7  7  6  6  7  8  6  8  8  7  6  6  6  4  4  4  5  5  6  6  6  6  5  4  5  1
G1913 T 120                                         1  1  3  3  4  4  3  3  3  3  3  3  4  4  4  4  2  3  2  2  2  2  2  2  2  1  1  2  2  2  2  3  3  3  3  2  2  3  3  2  3  4  4  3  2  1
G2333 T 226           1  1  1  1  1  1  1  1  1  1  2  1  2  2  4  4  5  4  5  4  4  4  4  4  4  4  3  4  5  7  7  7  7  7  7  7  7  5  5  4  7  6  6  5  7  7  7  7  6  5  4  3  2  2  2  1
G2557 T 332           1  1  1  1  2  2  2  3  3  3  3  4  4  4  7  7  8  8  9  9  9 10 10  9  9  7  8  8  8  9  9 10  8  8  8  7  8  7  8  8  9  9  9  8  8  7  7  7  6  5  4  1  1  1
G3253 T 227  2  2  3  3  4  4  5  5  5  5  5  6  6  6  5  5  5  5  5  4  4  4  4  4  4  4  5  4  4  4  4  4  4  5  5  5  5  5  3  4  4  4  6  4  4  2  3  3  4  4  3  3  3  1     1  2  1
G3463 T 461           1  1  2  3  4  5  6  9 11 12 12 12 11 11 12 12 12 12 12 13 14 13 13 11 11 11 10  9  9  9 11 10 10 10 10 12 11  8  8  8  9  8  7  6  4  4  4  5  5  5  4  5  5  4  3  2
G3469 T 311     1  2  2  2  3  3  4  4  5  5  5  7  7  6  6  6  6  5  5  5  4  4  4  4  4  4  4  5  5  5  5  5  5  5  7  5  6  6  8  8  7  7  8  9  8  8  8  8  7  6  6  6  6  5  4  7  6  3
Part 1: 19874 Guard #523 Awake 511 mins. At min 38 was awake 14 times
Part 2: 22687 Guard #463 at min 49 was awake 20 times

1

u/daggerdragon Dec 05 '18

the Elvis operator

Welp, TIL.

→ More replies (5)

3

u/Bogdanp Dec 04 '18

Racket:

part 1:

#lang racket

(require gregor
         gregor/period
         gregor/time)

(struct log (timestamp message)
  #:transparent)

(define LOG-REGEXP #px"\\[(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2})\\] (.+)")
(define (string->log s)
  (match-define (list _ year month day hour minute message) (regexp-match LOG-REGEXP s))
  (log (datetime (string->number year)
                 (string->number month)
                 (string->number day)
                 (string->number hour)
                 (string->number minute))
       message))

(define GUARD-ID-REGEXP #px"Guard #(\\d+) begins shift")
(define (string->guard-id s)
  (match-define (list _ id) (regexp-match GUARD-ID-REGEXP s))
  id)

(define logs (map string->log (sort (file->lines "day-04-1.txt") string<=?)))
(define frequencies
  (for/fold ([frequencies (hash)]
             [guard-id #f]
             [sleep-timestamp #f]
             #:result frequencies)
            ([l logs])
    (define message (log-message l))
    (cond
      [(string-prefix? message "Guard #")
       (values frequencies (string->guard-id message) #f)]

      [(string=? message "falls asleep")
       (values frequencies guard-id (log-timestamp l))]

      [(string=? message "wakes up")
       (match-define (period [minutes mdelta]) (period-between sleep-timestamp (log-timestamp l)))
       (define frequencies*
         (for/fold ([frequencies frequencies])
                   ([i (in-range mdelta)])
           (define k (->time (+period sleep-timestamp (period [minutes i]))))
           (hash-update frequencies k (curry cons guard-id) (list))))
       (values frequencies* guard-id #f)])))

(define appearances-by-id
  (for*/fold ([appearances (hash)])
             ([(minute ids) (in-hash frequencies)]
              [id ids])
    (hash-update appearances id (curry cons minute) null)))

(define most-sleepy-id
  (car
   (first (sort (hash->list appearances-by-id) (lambda (a b)
                                                 (> (length (cdr a))
                                                    (length (cdr b))))))))

(define appearances-by-minute
  (for*/fold ([appearances (hash)])
             ([minute (hash-ref appearances-by-id most-sleepy-id)])
    (hash-update appearances minute add1 0)))

(define most-sleepy-minute
  (first (sort (hash->list appearances-by-minute) (lambda (a b)
                                                    (> (cdr a) (cdr b))))))

(*
 (string->number most-sleepy-id)
 (->minutes (car most-sleepy-minute)))

part 2:

#lang racket

(require gregor
         gregor/period
         gregor/time)

(struct log (timestamp message)
  #:transparent)

(define LOG-REGEXP #px"\\[(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2})\\] (.+)")
(define (string->log s)
  (match-define (list _ year month day hour minute message) (regexp-match LOG-REGEXP s))
  (log (datetime (string->number year)
                 (string->number month)
                 (string->number day)
                 (string->number hour)
                 (string->number minute))
       message))

(define GUARD-ID-REGEXP #px"Guard #(\\d+) begins shift")
(define (string->guard-id s)
  (match-define (list _ id) (regexp-match GUARD-ID-REGEXP s))
  id)

(define logs (map string->log (sort (file->lines "day-04-2.txt") string<=?)))
(define frequencies
  (for/fold ([frequencies (hash)]
             [guard-id #f]
             [sleep-timestamp #f]
             #:result frequencies)
            ([l logs])
    (define message (log-message l))
    (cond
      [(string-prefix? message "Guard #")
       (values frequencies (string->guard-id message) #f)]

      [(string=? message "falls asleep")
       (values frequencies guard-id (log-timestamp l))]

      [(string=? message "wakes up")
       (match-define (period [minutes mdelta]) (period-between sleep-timestamp (log-timestamp l)))
       (define frequencies*
         (for/fold ([frequencies frequencies])
                   ([i (in-range mdelta)])
           (define k (->time (+period sleep-timestamp (period [minutes i]))))
           (hash-update frequencies k (curry cons guard-id) (list))))
       (values frequencies* guard-id #f)])))

(define (most-common xs)
  (define counts
    (for/fold ([counts (hash)])
              ([x xs])
      (hash-update counts x add1 0)))

  (first (sort (hash->list counts) (lambda (a b)
                                     (> (cdr a) (cdr b))))))

(define most-frequent-by-minute
  (for/fold ([appearances (hash)])
            ([(minute ids) (in-hash frequencies)])
    (hash-set appearances minute (most-common ids))))

(define most-frequently-sleepy-at-minute
  (first (sort (hash->list most-frequent-by-minute) (lambda (a b)
                                                      (> (cddr a)
                                                         (cddr b))))))

(* (->minutes (car most-frequently-sleepy-at-minute))
   (string->number (cadr most-frequently-sleepy-at-minute)))

I finished in about 15 minutes this time around (wasn't streaming), but I overslept sadly.

→ More replies (1)

3

u/[deleted] Dec 04 '18

Mathematica

input = Import[NotebookDirectory[] <> "day4.txt", "List"];
log = Join@@StringCases[input, "[" ~~ ts : __ ~~ "] " ~~ evt : __ :> {DateObject[ts], evt}];
slog = Sort[log];

sleeprec = <||>;
Module[{crec, guard, sleep, t, msg},
  Do[
   {t, msg} = event;
   Switch[msg,
    "falls asleep",
    (sleep = t),
    "wakes up",
    (crec[[DateValue[sleep, "Minute"] + 1 ;; DateValue[t, "Minute"]]] += 1;
     sleeprec[guard] = crec),
    _,
    (guard = ToExpression@First@StringCases[msg, NumberString];
     crec = Lookup[sleeprec, guard, ConstantArray[0, 60]])],
   {event, slog}]];

Part 1

guardMostSleep = Last@Keys@Sort[Total /@ sleeprec]

guardMostSleep*(Ordering[sleeprec[guardMostSleep], -1] - 1)

Part 2

guardMostFreqSleep = Last@Keys@Sort[Max /@ sleeprec]

guardMostFreqSleep*(Ordering[sleeprec[guardMostFreqSleep], -1] - 1)

3

u/hpzr24w Dec 04 '18

C++

Plugged away. Not pretty but it works. 9 ms on my i5-8600.

Header

// Advent of Code 2018
// Day 04 - Repose Record

#include "aoc.hpp"
using namespace std;

struct is_delim : std::unary_function<const char&, bool> {
    bool operator()(const char& c) const { return strchr(" :[]#-",int(c))!=nullptr; }
};

const static bool compare(const pair<int,int>& a, const pair<int,int>& b) {
    return (a.second<b.second);
}

main

int main(int argc, char* argv[])
{
    vector<string> input = read_input("day_04.txt");
    sort(begin(input),end(input));

    // build a map of guards to total sleep time and by minute asleep
    auto guard{0};
    auto wakeat{0};
    auto sleepat{59};
    auto sleeping = map<int,int>();
    auto minutes = map<int,array<int,60>>();
    for (auto l : input) {
        auto tokens = split(begin(l),end(l),is_delim());
        if (l.find("Guard")!=string::npos)  guard   = stoi(tokens[7]);
        if (l.find("asleep")!=string::npos) sleepat = stoi(tokens[5]);
        if (l.find("wake")) {
            wakeat = stoi(tokens[5]);
            sleeping[guard]+=wakeat-sleepat;
            for (int i=sleepat;i<wakeat;++i) minutes[guard][i]++;
        }
    }

Part 1

    // Part 1 - Find the sleepiest guard * the minute he spent most asleep
    auto g = max_element(begin(sleeping),end(sleeping),compare);
    cout << "Sleepiest guard: " << (*g).first << " slept " << (*g).second << " mins.\n";
    guard = (*g).first;
    auto m = max_element(begin(minutes[guard]),end(minutes[guard]));
    auto minute = m-minutes[guard].data();
    cout << "Part 1: Guard * Minutes: " << guard*minute << "\n";

Part 2

    // Part 2 - Find the Guard# * Most Slept in Minute
    auto maxminutes = 0;
    auto mostminute = 0;
    auto mostguard = 0;
    for( auto g : minutes ) {
        // find the minute for this guard
        auto m = max_element(begin(minutes[g.first]),end(minutes[g.first]));
        auto minute = m-minutes[g.first].data();

        // track guard and minute
        if (*m>maxminutes) {
            maxminutes = *m;
            mostminute = minute;
            mostguard = g.first;
        }
    }
    cout << "Part 2: Most frequently asleep guard * minute: " << mostguard << " m " << mostminute << " -> " << mostguard*mostminute << "\n";
}

2

u/Chrinkus Dec 04 '18

I like the is_delim method of splitting up the inputs. Your solution is waaaaayy shorter than mine.

3

u/raevnos Dec 04 '18 edited Dec 04 '18

While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.

Organizing and analyzing data points? Sounds like a good job for a database!

Perl script that massages the input into SQL statements that populate a SQLite table and queries that solve both parts (I cheated a bit by making sort the first step in the ETL pipeline instead of doing it all in the perl script):

#!/usr/bin/perl
use warnings;
use strict;
use feature qw/say/;

# Usage:
# sort -k1,2 day04.txt | perl day04etl.pl | sqlite3

my %guards;
my $guard;

print <<EOQ;
CREATE TABLE observations(guard INTEGER, minute INTEGER, state INTEGER);
BEGIN TRANSACTION;
EOQ

while (<>) {
    if (/\[(\d\d\d\d-\d\d-\d\d) \d\d:(\d\d)\] Guard #(\d+) begins shift/) {
        $guard = $3;
        $guards{$guard}->{$1}->{$2} = 0;
    } elsif (/\[(\d\d\d\d-\d\d-\d\d) \d\d:(\d\d)\] falls asleep/) {
        $guards{$guard}->{$1}->{$2} = 1;
    } elsif (/\[(\d\d\d\d-\d\d-\d\d) \d\d:(\d\d)\] wakes up/) {
        $guards{$guard}->{$1}->{$2} = 0;
    }
}

while (my ($g, $days) = each %guards) {
    for my $minutes (values %$days) {
        my $state = undef;
        for my $min ("00" .. "59") {
            $state = $minutes->{$min} if exists $minutes->{$min};
            say "INSERT INTO observations VALUES($g, $min, $state);"
                if defined $state;
        }
    }
}

    
print <<EOQ;
CREATE INDEX obs_idx ON observations(guard, minute, state);
COMMIT;

CREATE VIEW byminute(guard, minute, sleep_time) AS
 SELECT guard, minute, sum(state) FROM observations GROUP BY guard, minute;

.header on
.mode column
.timer on
    
SELECT guard, minute, guard * minute AS "Part 1"
FROM byminute
WHERE guard = (SELECT guard FROM observations
               WHERE state = 1 GROUP BY guard ORDER BY count(*) DESC LIMIT 1)
ORDER BY sleep_time DESC
LIMIT 1;

SELECT guard, minute, guard * minute AS "Part 2"
FROM byminute
ORDER BY sleep_time DESC
LIMIT 1;

EOQ
→ More replies (3)

3

u/drakmaniso Dec 04 '18

Go (golang)

Part 1:

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strings"
	"time"
)

type entry struct {
	date   time.Time
	action action
	guard  int
}

type action int

const (
	beginShift action = iota
	fallAsleep
	wakeUp
)

func main() {
	entries := read()

	var sleepyguard int
	asleep := map[int]int{}
	var guard, from int
	for _, e := range entries {
		switch e.action {
		case beginShift:
			guard = e.guard
		case fallAsleep:
			from = e.date.Minute()
		case wakeUp:
			t := e.date.Minute() - from
			asleep[guard] += t
			if asleep[guard] > asleep[sleepyguard] {
				sleepyguard = guard
			}
		}
	}

	minutes := [60]int{}
	guard = -1
	var sleepyminute int
	for _, e := range entries {
		if e.action == beginShift {
			guard = e.guard
			continue
		}
		if guard != sleepyguard {
			continue
		}
		switch e.action {
		case fallAsleep:
			from = e.date.Minute()
		case wakeUp:
			to := e.date.Minute()
			for i := from; i < to; i++ {
				minutes[i]++
				if minutes[i] > minutes[sleepyminute] {
					sleepyminute = i
				}
			}
		}
	}

	fmt.Printf("Answer: guard %d * minute %d = %d\n",
		sleepyguard, sleepyminute, sleepyguard*sleepyminute)
}

func read() []entry {
	entries := []entry{}
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		txt := s.Text()
		e := entry{guard: -1}

		var y, m, d, hr, mn int
		n, err := fmt.Sscanf(txt, "[%d-%d-%d %d:%d]", &y, &m, &d, &hr, &mn)
		if n < 5 || err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
			continue
		}
		e.date = time.Date(y, time.Month(m), d, hr, mn, 0, 0, time.UTC)

		i := strings.Index(txt, "] ")
		if i == -1 {
			fmt.Fprintf(os.Stderr, "ERROR: unable to parse message\n")
			continue
		}
		txt = txt[i+2:]
		n, err = fmt.Sscanf(txt, "Guard #%d begins shift", &e.guard)
		switch {
		case n == 1:
			e.action = beginShift
		case txt == "falls asleep":
			e.action = fallAsleep
		case txt == "wakes up":
			e.action = wakeUp
		default:
			fmt.Fprintf(os.Stderr, "ERROR: unknown action\n")
			continue
		}
		entries = append(entries, e)
	}

	sort.Slice(entries, func(i, j int) bool {
		return entries[i].date.Before(entries[j].date)
	})
	return entries
}

Part 2:

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strings"
	"time"
)

type entry struct {
	date   time.Time
	action action
	guard  int
}

type action int

const (
	beginShift action = iota
	fallAsleep
	wakeUp
)

func main() {
	entries := read()

	var sleepyguard, sleepyminute int
	minutes := map[int]*[60]int{}
	var guard, from int
	for _, e := range entries {
		switch e.action {
		case beginShift:
			guard = e.guard
			if minutes[guard] == nil {
				minutes[guard] = &[60]int{}
			}
			if minutes[sleepyguard] == nil {
				sleepyguard = guard
			}

		case fallAsleep:
			from = e.date.Minute()
		case wakeUp:
			to := e.date.Minute()
			for i := from; i < to; i++ {
				minutes[guard][i]++
				if minutes[guard][i] > minutes[sleepyguard][sleepyminute] {
					sleepyguard = guard
					sleepyminute = i
				}
			}
		}
	}

	fmt.Printf("Answer: guard %d * minute %d = %d\n",
		sleepyguard, sleepyminute, sleepyguard*sleepyminute)
}

func read() []entry {
	entries := []entry{}
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		txt := s.Text()
		e := entry{guard: -1}

		var y, m, d, hr, mn int
		n, err := fmt.Sscanf(txt, "[%d-%d-%d %d:%d]", &y, &m, &d, &hr, &mn)
		if n < 5 || err != nil {
			fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
			continue
		}
		e.date = time.Date(y, time.Month(m), d, hr, mn, 0, 0, time.UTC)

		i := strings.Index(txt, "] ")
		if i == -1 {
			fmt.Fprintf(os.Stderr, "ERROR: unable to parse message\n")
			continue
		}
		txt = txt[i+2:]
		n, err = fmt.Sscanf(txt, "Guard #%d begins shift", &e.guard)
		switch {
		case n == 1:
			e.action = beginShift
		case txt == "falls asleep":
			e.action = fallAsleep
		case txt == "wakes up":
			e.action = wakeUp
		default:
			fmt.Fprintf(os.Stderr, "ERROR: unknown action\n")
			continue
		}
		entries = append(entries, e)
	}

	sort.Slice(entries, func(i, j int) bool {
		return entries[i].date.Before(entries[j].date)
	})
	return entries
}

3

u/knotdjb Dec 05 '18

I did a solution in Go as well. I notice you parsed the date/time into a time struct. Since the date/time is in big-endian form, it naturally lends itself to string sorting.

For example, once you have all the lines in a string slice, you can simply do the following:

sort.Slice(lines, func(i, j int) bool {
    l := len("[1518-07-14 00:02]")
    return lines[i][0:l] < lines[j][0:l]
})
→ More replies (1)

2

u/[deleted] Dec 04 '18

Nice! I think i'm going to start using Sscanf.

Here is mine for both parts:

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"
)

func main() {
	start := time.Now()
	file, err := os.Open("input.txt")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	// see: https://regex101.com/r/VRbjzk/1
	var re = regexp.MustCompile(`.(.*)(]\s)(.[a-zA-Z]*)(..)(.*)`)
	lines := []string{}
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	sort.Strings(lines)
	currentGuard := 0
	guardSleepTimes := make([][]int, 4000) //should calculate highest guard id instead of hardcoding 4000
	for i := 0; i < 4000; i++ {
		guardSleepTimes[i] = make([]int, 61) // minute counters, 61 is summ
	}

	sleepTimer := 0 // time guard fell asleep
	for _, line := range lines {
		eventLog := re.FindAllStringSubmatch(line, -1)
		minuteSplit := strings.Split(eventLog[0][1], ":")
		minutes, _ := strconv.Atoi(minuteSplit[1])
		logType := eventLog[0][3] // falls,wakes,Guard
		if logType == "Guard" {
			s := strings.Split(eventLog[0][5], " ")
			currentGuard, err = strconv.Atoi(s[0])
			if err != nil {
				fmt.Println(err)
			}
		}
		if logType == "falls" {
			sleepTimer = minutes
		}
		// when guard wakes up calculate time sleep
		if logType == "wakes" {
			for i := sleepTimer; i < minutes; i++ {
				guardSleepTimes[currentGuard][i]++  // incrememnt current slept count
				guardSleepTimes[currentGuard][60]++ // total minutes slept buffer
			}
			sleepTimer = 0

		}

	}
	// Find sleepiest Guard
	sleepiestGuard := 0
	sleepiestMinute := 0
	sleepBuf := 0
	sleepiestGuardMin := 0
	for guard, sleepTime := range guardSleepTimes {
		// only get guards that have slept
		if sleepTime[60] == 0 {
			continue
		}
		if sleepTime[60] >= guardSleepTimes[sleepiestGuard][60] {
			sleepiestGuard = guard

		}
		for minute, sleepMin := range sleepTime {
			if sleepMin > sleepBuf && minute != 60 {
				sleepiestMinute = minute
				sleepBuf = sleepMin
				sleepiestGuardMin = guard
			}
		}
	}
	// Find sleepiest hour for sleepiest guard
	sleepiestHour := 0
	sleepBuf = 0
	for hour, sleepTime := range guardSleepTimes[sleepiestGuard] {
		if sleepTime > sleepBuf && hour != 60 {
			sleepiestHour = hour
			sleepBuf = sleepTime
		}
	}
	fmt.Println("Sleepiest Guard:", sleepiestGuard)
	fmt.Println("Sleepiest hour:", sleepiestHour)
	fmt.Println("Part 1 Code:", sleepiestGuard*sleepiestHour)
	fmt.Println("Part 2 Code:", sleepiestGuardMin*sleepiestMinute)
	fmt.Println(time.Since(start))
}
→ More replies (1)

3

u/donaldihunter Dec 04 '18 edited Dec 04 '18

Perl 6

#!/usr/bin/env perl6
use v6;

class Guard {
    has $.id;
    has int @.sleep[60];
    has $.fall;

    method fall(Int $t) { $!fall = $t; }
    method wake(Int $t) {  @!sleep[$_] += 1 for $!fall..^$t; }
    method totes { [+] @!sleep; }
    method max { @!sleep.max; }
    method gist { "Guard #{$!id} – { [+] @!sleep } – {@!sleep}"; }

    method show {
        my $max = @!sleep.maxpairs[0];
        say '';
        say "Guard #{$!id} had {$max.value} sleeps in minute {$max.key}";
        say "Giving {$!id * $max.key}";
    }
}

my %guards;

for '4a-input.txt'.IO.lines.sort -> $event {
    my ($mm, $act) = $event.match(/ ':' (\d+) ']' \s (.*) /).map: ~*;

    state $current;
    given $act {
        when /'Guard #' (\d+)/ {
            my $id = ~$/[0];
            $current = %guards{$id} //= Guard.new(:$id);
        }
        when /'falls asleep'/ {
            $current.fall(+$mm);
        }
        when /'wakes up'/ {
            $current.wake(+$mm);
        }
    }
}

# Part One
%guards.values.max(*.totes).show;

# Part Two
%guards.values.max(*.max).show;

3

u/p_tseng Dec 04 '18 edited Dec 04 '18

Ruby

in which I cut pretty much all the corners. I am sorry.

input = (ARGV.empty? ? DATA : ARGF).each_line.map(&:chomp).map(&:freeze).freeze

guards = Hash.new { |h, k| h[k] = Hash.new(0) }

guard = nil
started_sleeping = nil

input.sort.each { |l|
  last_number = l.scan(/\d+/).last.to_i
  if l.end_with?('begins shift')
    guard = last_number
  elsif l.end_with?('falls asleep')
    started_sleeping = last_number
  elsif l.end_with?('wakes up')
    woke_up = last_number
    (started_sleeping...woke_up).each { |min| guards[guard][min] += 1 }
  end
}

%i(sum max).each { |f|
  id, minutes = guards.max_by { |_, v| v.values.public_send(f) }
  puts id * minutes.keys.max_by(&minutes)
}
→ More replies (1)

3

u/vypxl Dec 04 '18 edited Dec 04 '18

I REALLY hate Java.. This is horrible.

import java.io.*;
import java.nio.file.Files;
import java.util.*;

public class Day4 {
    public static void main(String[] args) throws Exception {
        Map<Integer, int[]> data = new HashMap<Integer, int[]>();
        List<String> input = Files.readAllLines(new File("./4.in").toPath());
        Collections.sort(input);
        int current = 0;
        int start = 0;
        for (String l : input) {
            int min = Integer.parseInt(l.substring(15, 17));
            boolean shift = l.contains("shift");
            boolean wake = l.contains("wake");
            boolean sleep = l.contains("sleep");

            int id;
            if(shift) {
                id = Integer.parseInt(l.split(" ")[3].substring(1));
                current = id;
                if(!data.containsKey(id)) {
                data.put(current, new int[60]);
                Arrays.fill(data.get(current), 0);
                }
            }
            if(sleep) {
                start = min;
            }
            if(wake) {
                for (int i = start; i < min; i++) {
                    data.get(current)[i] += 1;
                }
            }    
        }

        List<Integer> keys = new ArrayList(data.keySet());
        int best = keys.get(0);
        int bestsum = 0;
        for (int id : keys) {
            int sum = Arrays.stream(data.get(id)).filter(x -> x >= 1).sum();
            if(sum > bestsum) {
                bestsum = sum;
                best = id;
            }
        }
        int bestMinute = 0;
        int bestTime = 0;
        for(int i = 0; i < 60; i++) {
            if(data.get(best)[i] > bestMinute) {
                bestMinute = data.get(best)[i];
                bestTime = i;
            }
        }
        System.out.println("Guard id: " + best);
        System.out.println("Minute: " + bestTime);
        System.out.println("Solution to part 1: " + best * bestTime);

        best = keys.get(0);
        bestMinute = 0;
        for (int id : keys) {
            int minute = Arrays.stream(data.get(id)).max().orElse(-1);
            if(minute > bestMinute) {
                bestMinute = minute;
                best = id;
            }
        }
        
        bestTime = 0;
        for(int i = 0; i < 60; i++) {
            if(data.get(best)[i] == bestMinute) {
                bestTime = i;
            }
        }
        System.out.println("Guard id: " + best);
        System.out.println("Minute: " + bestTime);
        System.out.println("Solution to part 2: " + best * bestTime);
    }
}

I hate myself for this.

Edit: Card: Today’s puzzle would have been a lot easier if my language supported any non-cumbersome methods of doing anything..

2

u/[deleted] Dec 04 '18

Give Kotlin a try?

→ More replies (1)
→ More replies (1)

3

u/__Abigail__ Dec 04 '18 edited Dec 19 '18

Perl

Simple parsing, a few sorts (using a Schwartzian Transform) and we're done.

#!/opt/perl/bin/perl

use 5.028;
        
use strict;
use warnings;
no  warnings 'syntax';   
         
use experimental 'signatures';
use experimental 'lexical_subs';     
         
my $input = "input";
open my $fh, "<", $input or die "Failed to open $input: $!";
chomp (my @lines = sort <$fh>);

my $current_guard = -1;
my $start_sleep   = -1;
my $wake_up       = -1;

my %total_sleep;
my %sleep;

foreach (@lines) {
    /^\[ (?<year>[0-9]{4}) - (?<month>[0-9]+) - (?<day>[0-9]+) \s+
         (?<hour>[0-9]{2}) : (?<minute>[0-9]+) \] \s+/gx
         or die "Failed to parse $_";
    my $minute = $+ {minute};
    if (/\G Guard \s+ \#([0-9]+) \s+ begins \s+ shift/x) {
        $current_guard = $1;
        next;
    }
    if (/\G falls \s+ asleep/x) {
        $start_sleep = $minute;
        next;
    }
    if (/\G wakes \s+ up/x) {
        die "Unexpected order of lines" if $current_guard < 0 ||
                                           $start_sleep   < 0;
        $wake_up = $minute;

        #
        # Mark any minute the guard is asleep. 
        #
        foreach my $tick ($start_sleep .. $wake_up - 1) {
            $sleep {$current_guard} {$tick} ++;
        }

        # 
        # Add the number of minutes the guard slept this nap.
        #  
        $total_sleep {$current_guard} += $wake_up - $start_sleep;

        #
        # Not really needed to reset it, but it helps to catch
        # problems with the input.   
        #
        $start_sleep = -1;
        next;
    }

    die "Failed to parse $_";

}

#
# Find the guard who sleeps the longest. We flatten the %total_sleep
# structure to a list [guard, total time slept], and reserve sort
# on total time slept. The sleepiest guard will be first.
#
my ($sleepy_guard)     = map  {$$_ [0]}
                         sort {$$b [1] <=> $$a [1]}
                         map  {[$_ => $total_sleep {$_}]}
                         keys %total_sleep;
        
#
# Find the minute the guard sleeps the most often. We flatten the
# %{$sleep {$sleepy_guard}} structure to a list
# [minute, times slep that minute], and reverse sort on times slept
# that minute. The minute the guard slepts the most often will be first.
#
my ($sleepy_minute)    = map  {$$_ [0]}
                         sort {$$b [1] <=> $$a [1]}
                         map  {[$_ => $sleep {$sleepy_guard} {$_}]}
                         keys %{$sleep {$sleepy_guard}};

         
say "Part 1: ", $sleepy_guard * $sleepy_minute;
         
        
#
# Find the minute in which a guard sleeps the most. We flatten the %sleep
# structure to a list of the form [guard, minute, times slept], then
# reverse sort on times slept. The first element of the list will have
# the guard and the minute; we just have to multiply them to get the answer.
#
my ($sleepiest_minute) = sort {$$b [2] <=> $$a [2]}
                         map  {my $guard = $_;
                               map {[$guard, $_, $sleep {$guard} {$_}]}
                               keys %{$sleep {$guard}}}
                         keys %sleep;
         
say "Part 2: ", $$sleepiest_minute [0] * $$sleepiest_minute [1];
        
__END__
→ More replies (1)

5

u/ka-splam Dec 04 '18 edited Dec 04 '18

I thought I was wayyy out of the running on that one, but 19 minutes and first time into leaderboard part 2! Thanks PowerShell date handling! #37 / #61

[Card] today's puzzle would have been a lot easier if my language supported: Python's enumerate().

PowerShell

Code:

# hashtable of Guard ID -> (0,0,0,0 ... array of 60 minutes for that guard.
$guards = @{}

Get-Content .\data.txt  | sort-object {

    [void]($_ -match '\[(.*)\] (.*)')
    Get-Date $matches[1]

} | foreach { 

    [void]($_ -match '\[(.*)\] (.*)')
    $date = get-date $matches[1]
    $message = $matches[2]

    switch -regex ($message)    # switch state machine
    {
        # For a Guard identifier line, get the ID, set them up with 60 blank minutes.
        'Guard #(\d+)' { 
            $script:guard = $matches[1]
            if (-not $guards.ContainsKey($script:guard)){ $guards[$script:guard] = @(0) * 60 }
        }

        # If they fell asleep, store the date for use when they wake.
        'sleep' { $script:sleep = $date }
    
        # If they wake, loop over the minutes from sleep..wake and increment their array
        'wakes' {
            $script:sleep.Minute..($date.Minute-1)| foreach-object {
                $guards[$script:guard][$_]++
            }
        }
    }
}

# Part 1, most minutes asleep, which minute is highest
$mostSleepy = $guards.GetEnumerator() | sort-object { $_.Value | measure -sum | % sum } | select -Last 1
$minute = $mostSleepy.Value.IndexOf(($mostSleepy.Value | sort)[-1])
"Part 1: Guard $($mostSleepy.Name), minute $minute"
$minute * $mostSleepy.Name

# Part 2, guard with most same-minute asleep
$mostSame = $guards.GetEnumerator() | sort-object { ($_.Value | sort)[-1] } | select -Last 1
$minute = $mostSame.Value.IndexOf(($mostSame.Value | sort)[-1])
"Part 2: Guard $($mostSame.Name), minute: $minute"
 $minute * $mostSame.Name

(I actually made the hashtable and eyeballed the largest value and counted the minutes by hand, because it was quicker than writing code to do it, but I've added that code here)

2

u/Nathan340 Dec 04 '18

No date handling for me, the input format meant a simple alphabetic sort put them in the correct chronological order.

Same sort of process for checking which line type we're on, set the current guard and start minute on the respective line types, and add to tables during wake up lines.

Then just like you I did the relevant sorting to find the sleepiest guard, and found his sleepy minute.

I'm quite pleased with my part 2 solution. I stored the current Guard and current Minute concatenated as a string as the key to a hash table. Each time that Guard/Minute was hit, we only need to do a simple increment. At first pass I was going to parse this string out at the end, but instead I switched my 'delimiter' between Guard and Minute to be *. The multiplication to get the answer is then an Invoke-Expression on the key of the highest value in the hash table.

$in = gc .\04-input.txt | sort

$gHash = @{}
$mHash = @{}

for($i = 0;$i -lt $in.count;$i++){
	$m = @(([regex]"\d+").matches($in[$i]).value)
	
	
	if($m[5]){
		$curGuard = +$m[5]
	}elseif($in[$i] -like "*falls asleep*"){
		$sleepStart = +$m[4]
	}else{
		$sleepEnd = +$m[4]-1
		
		$sleepStart..$sleepEnd | % {
			$gHash[$curGuard]+=@($_)
			$mHash["$curGuard * $_"]++
		}
	}
	
}

$sleepyGuard = ($gHash.getEnumerator() | sort {$_.value.count} -descending)[0].name
$sleepyMinute = ($gHash[$sleepyGuard] | group | sort count -descending)[0].name

$sleepyGuard*$sleepyMinute

($mHash.getEnumerator() | sort value -descending)[0].name | iex  

2

u/craigontour Dec 08 '18

[void]($_ -match '\[(.*)\] (.*)')

Hi ka-splam, been folliowing (4 days behind at the moment!) your PS solutions.

What does the above do/mean please?

→ More replies (1)
→ More replies (3)

5

u/Unihedron Dec 04 '18 edited Dec 04 '18

Yeah!!! I have no idea what I'm doing anymore!!! Ruby

[Card] Image

h={}
ll=0
sl={}
p (a=$<.sort).map{|x|m = x.chomp
m =~ /^\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)$/
j,k,o,q,s=[$1, $2, $3, $4, $5]
j,k,o,q=b=[j,k,o,q].map(&:to_i)
#p b
s =~ /^(?:Guard #(\d+)|(falls)|(wakes))/
if $1
ll=$1.to_i
elsif $2
sl[ll]=b
elsif $3
u,i,l,n=sl[ll]
kk=h[ll] ? h[ll] : h[ll]=0
kk +=  (o*60+q)- (l*60+n)
h[ll] = kk
end
}
guard = h.max_by{|x,y|y}.first
p guard

# part 1

sl=0
qqq=Hash.new 0
a.map{|x|m = x.chomp
m =~ /^\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)$/
j,k,o,q,s=[$1, $2, $3, $4, $5]
j,k,o,q=b=[j,k,o,q].map(&:to_i)
#p b
s =~ /^(?:Guard #(\d+)|(falls)|(wakes))/
if $1
ll=$1.to_i
elsif $2
(sl=q) if ll == guard
elsif $3
(n=sl
(qqq[n]+=1

n+=1
n=0 if n==60)while n!=q) if ll == guard
end
}
p qqq.max_by{|x,y|y}.first*guard

# part 2
qqqt=Hash.new{Hash.new 0}
qsl={}
a.map{|x|m = x.chomp
m =~ /^\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)$/
j,k,o,q,s=[$1, $2, $3, $4, $5]
j,k,o,q=b=[j,k,o,q].map(&:to_i)
#p b
s =~ /^(?:Guard #(\d+)|(falls)|(wakes))/
if $1
ll=$1.to_i
elsif $2
(qsl[ll]=q) #if ll == guard
elsif $3
(n=qsl[ll]
qqq=qqqt.has_key?(ll) ? qqqt[ll] : (qqqt[ll]=qqqt[ll])
(qqq[n]+=1

n+=1
n=0 if n==60)while n!=q) #if ll == guard
end
}
p qqqt
a,b= qqqt.max_by{|x,y|y.max_by{|x,y|y}.last}
p a*b.max_by{|x,y|y}.first
p b.max_by{|x,y|y}.last

Edit: Yes, just like all the previous days, I didn't read the question...

Edit: Changed the layout of the code so you can run it without any hassle and it will just work. If you're reading this, bless you

2

u/Zarel Dec 04 '18 edited Dec 04 '18

JavaScript, #28/#130 ;_;

I was inspired by mserrano's "extract numbers" regex from his Day 3 solution, so I implemented it in JavaScript.

I misinterpreted Part Two at first, which apparently knocked me off the leaderboard.

I sorted my input using my text editor before running this - most text editors have a "Sort Lines" ability; I used VS Code's.

function numbers(str) {
  return (str.match(/-?[0-9]+/g) || []).map(Number);
}
function maxBy(array, criterion) {
  return array.reduce((a, b) => (criterion(a) > criterion(b) ? a : b));
}
function maxEntry(obj) {
  return maxBy(Object.entries(obj), entry => entry[1]);
}

// most minutes asleep

let guardTimes = {};

let sleepStart = 0;
let curGuard = 0;

for (const line of input) {
  const [a, b] = line.slice(1).split('] ');
  let time = new Date(a).getTime();
  if (b.startsWith('Guard')) {
    [curGuard] = numbers(b);
  } else if (b === 'wakes up') {
    guardTimes[curGuard] = (guardTimes[curGuard] || 0) + (time - sleepStart);
  } else if (b === 'falls asleep') {
    sleepStart = time;
  } else {
    throw new Error("bug");
  }
}

console.log(maxEntry(guardTimes));

// most-asleep minute

let minutes = {};
let startMin = 0;

for (const line of input) {
  const [a, b] = line.slice(1).split('] ');
  let min = numbers(a)[4];
  if (b.startsWith('Guard')) {
    [curGuard] = numbers(b);
  } else if (b === 'wakes up') {
    if (curGuard !== GUARD_FROM_PREVIOUS_RESULT) continue;
    for (i = startMin; i !== min; i = (i + 1) % 60) {
      minutes[i] = (minutes[i] || 0) + 1;
    }
  } else if (b === 'falls asleep') {
    startMin = min;
  } else {
    throw new Error("bug");
  }
}

console.log(maxEntry(minutes));

// most popular minute

let guardMinutes = {};
let curTable = {};
let startMin = 0;

for (const line of input) {
  const [a, b] = line.slice(1).split('] ');
  let min = numbers(a)[4];
  if (b.startsWith('Guard')) {
    [curGuard] = numbers(b);
    curTable = (guardMinutes[curGuard] || {});
    guardMinutes[curGuard] = curTable;
  } else if (b === 'wakes up') {
    for (i = startMin; i !== min; i = (i + 1) % 60) {
      curTable[i] = (curTable[i] || 0) + 1;
    }
  } else if (b === 'falls asleep') {
    startMin = min;
  } else {
    throw new Error("bug");
  }
}

let table2 = Object.entries(guardMinutes).map(([k, v]) => {
  if (!Object.entries(v).length) return [k, 0, 0];
  return [k].concat(maxEntry(v));
});

console.log(maxBy(table2, a => a[2]));

2

u/ElecNinja Dec 04 '18

I was inspired by mserrano's "extract numbers" regex

Haha decided to learn some regex to handle the inputs for today's challenge from that post

2

u/ka-splam Dec 04 '18

I wasted a whole couple of minutes screwing around with regex this time trying not to clash with [ and ], which felt really stupid. I even saw /u/mserrano 's regex.

fwiw in PowerShell it's as easy as

$a, $b, $c, $d   =   $line -split '\D+' -ne '' -as [int[]]

for how many pieces there are; split on anything that isn't a number, drop the blanks, cast to an array of ints. No need for invoking big library call, grouping, extracting values, making each one int individually..

It would have saved me so much time if I'd just used it!

→ More replies (1)
→ More replies (2)

2

u/Euphoric_Classic Dec 04 '18 edited Dec 04 '18

SuperCollider solution:

(
s = File.readAllString("~/aoc/4/input".standardizePath).split($\n).sort;
t = s.collect({|l| l.findRegexp("\\d+|f|w").collect({|x| x[1][0].isDecDigit.if {x[1].asInteger} {x[1]}})});
t = t[1..].collect(_[..5]);
d = []!10000;
t.do {|r|
	var yr, mon, day, hr, min, x;
	#yr, mon, day, hr, min, x = r;
	x.isInteger.if {
		g = x
	} {
		(x == "f").if {
			s = min
		} {
			d[g] = d[g].add([s, min])
		}
	}
};

m = d.collect({|a,i| [i, a.collect({|b| b[1] - b[0]}).sum]}).maxItem(_[1]);
b = d.collect({|a,i| [i, a.collect({|b| Array.iota(b[1]-b[0]) + b[0]}).flat.asBag]});
c = b[m[0]];
e = c[1].contents.asPairs.clump(2).maxItem(_[1]);
(m[0] * e[0]).postln;
f = b.collect({|c| [c[0], c[1].contents.asPairs.clump(2).maxItem(_[1])]}).select({|a|a[1].notNil}).maxItem({|a|a[1][1]});
(f[0] * f[1][0]).postln;
)

2

u/NeuroXc Dec 04 '18

Rust

Solutions are getting longer, so I'm posting a link for today's: https://git.onewebdev.info/soichiro/aoc2018/blob/master/src/day4.rs

Still blazing fast:

Day 4 - Part 1 : 74743

generator: 3.652771ms,

runner: 49.367µs

Day 4 - Part 2 : 132484

generator: 1.853941ms,

runner: 41.043µs

[Card] Today’s puzzle would have been a lot easier if my language supported input via telepathy.

→ More replies (1)

2

u/TellowKrinkle Dec 04 '18

I really need to actually read these directions instead of just skimming through and looking at the goal at the end. [Card] Today’s puzzle would have been a lot easier if my language supported telling me that I wasn't actually following the directions.

  • I didn't notice that while the example data was sorted, the actual data wasn't. Got very confused when I noticed the input contained multiple wakes up / falls asleep in a row. Solution? Obviously if someone's asleep and they fall asleep again they're still asleep. Better support that. Finally realized I needed to sort after like 3 tries

  • I missed the text saying that only the minute mattered for time asleep. To figure out how many parts I would have to support, I quickly looked through the demo text, saw the change from 23:58 (guard begins shift) to 00:40 (falls asleep) and assumed those would happen in the fall asleep/wakeup time periods too.

Ended up taking 30 minutes for the whole thing

Swift version that supports a bunch of extra useless stuff:

func aocD4(_ input: [((year: Int, month: Int, day: Int, hour: Int, minute: Int), Int?, Bool)]) {
	var guards: [Int: Int] = [:]
	var sleeping: [Int: [Int: Int]] = [:]
	var current = 0
	var startedSleeping: (hour: Int, minute: Int)? = nil
	for event in input {
		if let id = event.1 {
			current = id
		}
		else if event.2 {
			if startedSleeping == nil {
				startedSleeping = (event.0.hour, event.0.minute)
			}
		}
		else {
			if let startedSleeping = startedSleeping {
				var count = 0
				for i in sequence(first: startedSleeping, next: {
					let next = (($0.hour + ($0.minute + 1) / 60) % 24, ($0.minute + 1) % 60)
					return next.0 == event.0.hour && next.1 == event.0.minute ? nil : next
				}) {
					sleeping[current, default: [:]][i.minute, default: 0] += 1
					count += 1
				}
				guards[current, default: 0] += count
			}
			startedSleeping = nil
		}
	}
	/* Part A */
	let sleepiest = guards.max(by: { $0.value < $1.value })!.key
	print(sleeping[sleepiest]!.max(by: { $0.value < $1.value })!.key * sleepiest)

	/* Part B */
	let mostSleepyMinutes = sleeping.mapValues({ $0.max(by: { $0.value < $1.value })! })
	let mostSleep = mostSleepyMinutes.max(by: { $0.value.value < $1.value.value })!
	print(mostSleep)
	print(mostSleep.key * mostSleep.value.key)
}

import Foundation
let str = try! String(contentsOfFile: CommandLine.arguments[1])

import Foundation
let input = str.split(separator: "\n").map({ line -> ((year: Int, month: Int, day: Int, hour: Int, minute: Int), Int?, Bool) in
	let numbers = line.split(whereSeparator: { !"0123456789".contains($0) }).map { Int($0)! }
	return ((numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]), numbers.count > 5 ? numbers[5] : nil, line.contains("falls"))
}).sorted(by: { $0.0 < $1.0 })

aocD4(input)

2

u/tharko Dec 04 '18

Clojure. Assumes the input file is already sorted

(require 'clojure.string)

(defn get-lines []
  (with-open [r (clojure.java.io/reader
                  (clojure.java.io/resource "input"))]
    (doall (line-seq r))))

(defn loop-through-times []
  (loop [lines (get-lines) id nil times [] last-time nil]
    (if (nil? (first lines))
      times
      (let [line    (first lines)
            minutes (Integer/parseInt (second (re-find #"\:(\d+)" line)))]
        (if (clojure.string/ends-with? line "begins shift")
          (recur (rest lines) (first (re-seq #"\#\d+" line)) times nil)
          (if (nil? last-time)
            (recur (rest lines) id times minutes)
            (recur (rest lines) id (conj times [id minutes last-time]) nil)))))))

(defn time-range-maps [[id wake-time sleep-time]]
  {id (- wake-time sleep-time)})

(defn get-sleepiest-guard [time-ranges]
  (first (apply max-key second (apply merge-with + time-ranges))))

(def sleepiest-guard (get-sleepiest-guard (map time-range-maps (loop-through-times))))

(defn all-minutes [[id wake-time sleep-time]]
  (map (fn [x] [id x]) (range sleep-time wake-time)))

(defn most-common-element [coll]
  (first (apply max-key second (frequencies coll))))

(defn most-common-minute [times]
  (most-common-element
    (map second
      (filter #(= (first %) sleepiest-guard) times))))

(def all-slept-minutes (mapcat all-minutes (loop-through-times)))

(println (most-common-minute all-slept-minutes))

(println (most-common-element all-slept-minutes))

2

u/will_bui Dec 04 '18 edited Dec 04 '18

This one seemed a good candidate for qsql

/ input
parsedt:{p:(0 4 5 7 8 10 11 13 14 _ x);((24*60*`long$get"."sv p 0 2 4)+`long$get":"sv p 6 8)}
parseevent:{("au#"?x 6;$[count g:x where x in .Q.n;get g;0N])}
events:flip`dt`event`id!flip{raze {(parsedt[x];parseevent[y])} . 1_' "]" vs x} each read0`p4
events:update id:fills id from `dt xasc events
events:delete from events where event=2

getCounts:{count each group raze(({x+til y-x}.)each 2 cut x)mod 1440}

/ part 1
most:select {sum(-/)each 2 cut reverse x} dt by id from events
guard:first exec id from most where dt=max dt
minutes:first exec dt by id from events where id=guard
minute:first where counts=max counts:getCounts minutes
guard*minute

/ part 2
most:select {m:getCounts x;(n;where m=n:max m)} dt by id from events
first exec id*minute from first `cnt xdesc select id, cnt:first each dt, minute:last each dt from most

2

u/Smylers Dec 04 '18 edited Dec 04 '18

Perl for both parts:

use v5.20; use warnings; use experimental qw<signatures>; use List::AllUtils qw<max_by>;

my ($guard, $fell_asleep, %total_asleep, %min_asleep);
foreach (sort <>) {
  if (/ 00:(\d+)\] falls asleep/) {
    $fell_asleep = $1;
  }
  else {
    if (defined $fell_asleep) {
      my $awoke = / 00:(\d\d)\] wakes up/ ? $1 : 60;
      $total_asleep{$guard} += $awoke - $fell_asleep;
      $min_asleep{$guard}[$_]++ for $fell_asleep .. $awoke - 1;
      $fell_asleep = undef;
    }
    $guard = $1 if /#(\d+) begins shift/;
  }
}

$guard = max_by { $total_asleep{$_} } keys %total_asleep;
say 'Part 1: ', $guard * sleepiest_min($min_asleep{$guard});

$guard = max_by { $min_asleep{$_}[sleepiest_min($min_asleep{$_})] } keys %min_asleep;
say 'Part 2: ', $guard * sleepiest_min($min_asleep{$guard});

sub sleepiest_min($tally) { max_by { $tally->[$_] // 0 } 0 .. 59 }

max_by turned out to be handy here; I hadn't used it before, but was pleased to find it in List::AllUtils. The solution allows for a guard not waking up until after the hour ends, though that turned out not be necessary.

[Card] Today’s puzzle would have been a lot easier if my language supported mind-reading.

→ More replies (1)

2

u/Hashbrown777 Dec 04 '18

Javascript

2ms

function day4(input) {
    let parse = (serialised) => {
        serialised = serialised.match(parse.regex);
//if ((serialised[4] == 'wakes' || serialised[4] == 'falls') && serialised[1].match(/ (\d+):/)[1] != '00') throw serialised;
        return {
            time : serialised[1],
            minute : serialised[2],
            type : serialised[4]
        };
    };
    parse.regex = /^ *\[ *(\d+-\d+-\d+ *\d+:(\d+)) *\] *(Guard #|)(\d+|wakes|falls)/;

    let events = [];
    for (let log of input.split('\n'))
        events.push(parse(log));
    events = events.sort((a, b) => (a.time < b.time) ? -1 : 1);

    let current = null;
    day4.sleep = {};
    day4.output = {total:0};
    for (let event of events) {
        switch (event.type) {
        case 'wakes':
//if (!current.fell) throw 'not asleep!';
            for (let minute = current.fell; minute < event.minute; ++minute) {
                ++current.total;
                current[minute] = (current[minute] || 0) + 1;
                if (current[minute] > current[current.best])
                    current.best = minute;
            }
            if (current.total > day4.output.total)
                day4.output = current;
//current.fell = null;
            break;

        case 'falls':
//if (current.fell) throw 'already sleeping!';
            current.fell = event.minute;
            break;

        default:
//if (current && current.fell) throw 'still sleeping!';
            current = day4.sleep[event.type] || (day4.sleep[event.type] = {id:event.type, total:0, best:'none', none:0});
        };
    }

    day4.output = parseInt(day4.output.id) * parseInt(day4.output.best);
}

Part 2, 2ms

function day4_1(input) {
    let parse = (serialised) => {
        serialised = serialised.match(parse.regex);
        return {
            time : serialised[1],
            minute : serialised[2],
            type : serialised[4]
        };
    };
    parse.regex = /^ *\[ *(\d+-\d+-\d+ *\d+:(\d+)) *\] *(Guard #|)(\d+|wakes|falls)/;

    let events = [];
    for (let log of input.split('\n'))
        events.push(parse(log));
    events = events.sort((a, b) => (a.time < b.time) ? -1 : 1);

    let current = null;
    day4_1.sleep = {};
    day4_1.output = {best:'none', none:0};
    for (let event of events) {
        switch (event.type) {
        case 'wakes':
            for (let minute = current.fell; minute < event.minute; ++minute) {
                current[minute] = (current[minute] || 0) + 1;
                if (current[minute] > day4_1.output[day4_1.output.best]) {
                    current.best = minute;
                    day4_1.output = current;
                }
            }
            break;

        case 'falls':
            current.fell = event.minute;
            break;

        default:
            current = day4_1.sleep[event.type] || (day4_1.sleep[event.type] = {id:event.type, total:0, best:'none', none:0});
        };
    }

    day4_1.output = parseInt(day4_1.output.id) * parseInt(day4_1.output.best);
}
→ More replies (1)

2

u/Dr_Donut Dec 04 '18

Python 3 Object oriented solution.

I suck at programming / math so I just simulated each minute. It somehow all worked, eventually.

https://pastebin.com/FjEhgSZY # Second part solution. Part 1 quite similar

2

u/scul86 Dec 04 '18

Python 3.

I spent WAY too much time not realizing the input was not sorted.
-It works on the test input, why not the actual data?!?!

d'oh

from utils.decorators import time_it
from collections import defaultdict, Counter

with open('input') as f:
    puzzle_input = f.readlines()


@time_it
def part1(n):
    guards = defaultdict(list)
    times = defaultdict(int)
    for line in sorted(n):
        time, event = line.split('] ')
        minute = int(time[-2:])

        if 'Guard' in event:
            id = int(event.split('#')[1].split(' ')[0])
        elif 'falls' in event:
            start = minute
        elif 'wakes' in event:
            end = minute
            for x in range(start, end):
                guards[id].append(x)
            times[id] += end - start

    guard, time = max(times.items(), key=lambda i: i[1])
    minute, count = Counter(guards[guard]).most_common(1)[0]

    count_max = -1
    guard2 = -1
    for g in guards:
        minute2, count = Counter(guards[g]).most_common(1)[0]
        if count > count_max:
            count_max = count
            minute_max = minute2
            guard2 = g

    return guard * minute, guard2 * minute_max


@time_it
def part2(guards, times):
    pass


test_one = [
    '[1518-11-01 00:00] Guard #10 begins shift',
    '[1518-11-01 00:05] falls asleep',
    '[1518-11-01 00:25] wakes up',
    '[1518-11-01 00:30] falls asleep',
    '[1518-11-01 00:55] wakes up',
    '[1518-11-01 23:58] Guard #99 begins shift',
    '[1518-11-02 00:40] falls asleep',
    '[1518-11-02 00:50] wakes up',
    '[1518-11-03 00:05] Guard #10 begins shift',
    '[1518-11-03 00:24] falls asleep',
    '[1518-11-03 00:29] wakes up',
    '[1518-11-04 00:02] Guard #99 begins shift',
    '[1518-11-04 00:36] falls asleep',
    '[1518-11-04 00:46] wakes up',
    '[1518-11-05 00:03] Guard #99 begins shift',
    '[1518-11-05 00:45] falls asleep',
    '[1518-11-05 00:55] wakes up'
]
p1, p2 = part1(test_one)
assert p1 == 240
assert p2 == 4455

print(f'Part 1, 2: {part1(puzzle_input)}')
→ More replies (1)

2

u/Frizkie Dec 04 '18

Ruby

require 'time'

data = File.read('data.txt').chomp.split("\n")
           .map! { |d| d.match(/\[([\d\-: ]+)\] ([ \w#]+)/).to_a }
           .sort_by! { |d| Time.parse(d[1]) }

guards = {}
recent_guard = nil

data.each do |d|
  m = d[2].match(/#(\d+)/)
  recent_guard = m[1]&.to_i if m
  guards[recent_guard] ||= {}

  date = DateTime.parse(d[1])
  guards[recent_guard][date.strftime('%m-%d')] ||= '.' * 60

  c = '#' if d[2] == 'falls asleep'
  c = '.' if d[2] == 'wakes up'
  next unless c

  guards[recent_guard][date.strftime('%m-%d')][date.minute..59] = c * (60 - date.minute)
end

Part 1

sleepiest = guards.map { |id, s| [id, s.values.join.count('#')] }.max_by { |id, total| total }[0]
mins = (0..59).map { |m| guards[sleepiest].values.map { |s| s[m] }.join.count('#') }
puts sleepiest * mins.each_with_index.max[1]

Part 2

soln = guards.map do |id, s|
  t = (0..59).map { |m| s.values.map { |s| s[m] }.join.count('#') }
  [id, t.each_with_index.max[1], t.max]
end

soln = soln.max_by { |s| s[2] }
p soln[0] * soln[1]

2

u/blowjobtransistor Dec 04 '18 edited Dec 04 '18

PostgreSQL, again!

create table sleep_events (
  session_id bigint,
  dt timestamp,
  guard_id bigint,
  label text
);

insert into sleep_events
with raw_sleep_events as (
  select
    to_timestamp((regexp_matches(log, '\[[0-9\- :]+\]'))[1], '[YYYY-MM-DD HH24:MI]') dt,
    ltrim((regexp_matches(log, '#[0-9]+'))[1], '#') guard_id,
    (regexp_matches(log, 'falls asleep|wakes up|begins shift'))[1] as label
  from input
  order by log
),
sleep_sessionized as (
  select
    dt,
    guard_id,
    sum(case when guard_id isnull then 0 else 1 end) over (order by dt) as session_num,
    label
  from raw_sleep_events
)
select
  session_num,
  dt,
  first_value(cast(guard_id as bigint)) over (partition by session_num order by dt) as guard_id,
  label
from sleep_sessionized;

create table sleeps as
select
  session_id,
  guard_id,
  dt,
  case
    when label = 'wakes up'
      then int4range(
        extract(epoch from age(lag(dt) over (partition by session_id order by dt), date_trunc('day', dt)))::integer / 60,
        extract(epoch from age(dt, date_trunc('day', dt)))::integer / 60
      )
    else null
  end as sleep_range,
  case when label = 'wakes up' then dt - lag(dt) over (partition by session_id order by dt) else null end as time_slept
from sleep_events;

create view part_1_solution as
with sleepiest_guard as (
  select
    guard_id,
    sum(time_slept) filter (where time_slept notnull) total_sleep
  from sleeps
  group by guard_id
  order by total_sleep desc nulls last
  limit 1
),
sleepiest_guard_sleepiest_hour as (
  select
    guard_id,
    minute,
    count(*)
  from generate_series(-30, 100) minute join sleeps
    on sleeps.sleep_range @> minute
  join sleepiest_guard using (guard_id)
  group by guard_id, minute
  order by count desc
  limit 1
)
select 1 as part, guard_id * minute as answer from sleepiest_guard_sleepiest_hour;

create view part_2_solution as
with sleepiest_guard_mintues as (
  select guard_id, minute, count(*)
  from sleeps join generate_series(-30, 100) minute
    on sleeps.sleep_range @> minute
  group by guard_id, minute
  order by count desc
  limit 1
)
select 2 as part, guard_id * minute from sleepiest_guard_mintues;

select * from part_1_solution
union all
select * from part_2_solution;

Range types are great, except when you get off-by-1 errors with them :P

→ More replies (1)

2

u/tvtas Dec 04 '18

Day 4 in MATLAB

G = zeros(5000,60);
x = sort(importdata('input.txt'));
for i=1:size(x,1)
    idx = strfind(x{i},'#');
    if idx
        awake = true;
        ID = sscanf(x{i}(idx+1:end),'%f');
        continue
    end
    t = str2double(x{i}(16:17));
    G(ID,t+1:end) = G(ID,t+1:end)-1+2*awake;
    awake = ~awake;
end
[~,a] = max(sum(G,2));
[~,b] = max(G(a,:));
[~,c] = max(max(G,[],2));
[~,d] = max(G(c,:));
disp((b-1)*a) % Part 1
disp((d-1)*c) % Part 2

2

u/nutrecht Dec 04 '18

Day 4 in Kotlin

private val regex = "\\[([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})] (Guard #([0-9]+) begins shift|falls asleep|wakes up)".toRegex()
private val instructions = resourceLines(2018, 4).mapNotNull { regex.matchEntire(it)?.groupValues?.drop(1) }
        .map {
            val time = LocalDateTime.of(it[0].toInt(), it[1].toInt(), it[2].toInt(), it[3].toInt(), it[4].toInt())
            val guard = if(it[5].startsWith("Guard")) { it[6].toInt() } else null

            Event(time, guard, it[5])
        }
        .sortedBy { it.time }

private val map: Map<Int, Map<Int,Int>> by lazy {
    val map = mutableMapOf<Int, MutableMap<Int, Int>>()

    var guard = 0
    var asleep: LocalDateTime? = null

    instructions.forEach {
        when {
            it.guard != null -> guard = it.guard
            it.inst.startsWith("falls") -> asleep = it.time
            it.inst.startsWith("wakes") -> {
                val minutes = (asleep!!.minute until it.time.minute).map { m -> asleep!!.withMinute(m) }

                minutes.forEach { m ->
                    map.computeIfAbsent(guard) { mutableMapOf() }.compute(m.minute) { _, v -> v?.plus(1) ?: 1}
                }
            }
        }
    }
    map
}

override fun part1() : String {
    val guard = map.maxBy { it.value.values.sum() }!!
    val minute = guard.value.entries.maxBy { it.value }!!.key

    return (guard.key * minute).toString()
}

override fun part2() : String {
    val max = map.map {
        it.key to it.value.maxBy { m -> m.value }!!
    }.maxBy { it.second.value } ?: throw IllegalArgumentException()

    return (max.first * max.second.key).toString()
}

data class Event(val time: LocalDateTime, val guard: Int?, val inst: String)

Got stuck hard on both part 1 and 2 due to not interpreting the instructions but ending up with code that did work on the test input :(

2

u/usbpc102 Dec 04 '18

Today your solution looks quite a bit diffrent from mine but I also did today way more relaxed.... mostly cause I overslept the puzzle release. So I tried to write nice code instead of as fast as possible.

And for your regex you can use\d for all digits instead of your own defined group [0-9] ;)

2

u/nutrecht Dec 04 '18

Yeah I know, it's just personal preference :)

I got up at 6am local time but got nowhere close to making the leaderboard. Think I spent an hour on it or so :D

2

u/Graatz Dec 04 '18

C#

Could be better, but it works.

class Day4 : Task
{
    public List<GuardAction> AllActions { get; set; }
    public Dictionary<int, Guard> Guards { get; set; }

    public Day4(string file) : base(file)
    {
        AllActions = SetActions();
        Guards = SetGuards();
    }

    public int Part1()
    {
        KeyValuePair<int, Guard> sleepyGuard = Guards
            .OrderByDescending(g => g.Value.MinutesAsleep)
            .First();

        var mostAsleepMinute = sleepyGuard
            .Value
            .SleepyMinutesMap
            .OrderByDescending(m => m.Value)
            .First().Key;

        var sleepyGuardId = sleepyGuard.Key;

        return mostAsleepMinute * sleepyGuardId;
    }

    public int Part2()
    {
        int maxMinute = 0;
        int mostAsleepMinute = 0;
        int guardId = 0;

        foreach (var guard in Guards)
        {
            foreach (var minute in guard.Value.SleepyMinutesMap)
            {
                if (minute.Value > maxMinute)
                {
                    maxMinute = minute.Value;
                    mostAsleepMinute = minute.Key;
                    guardId = guard.Key;
                }
            }
        }

        return mostAsleepMinute * guardId;
    }

    public Dictionary<int, Guard> SetGuards()
    {
        Dictionary<int, Guard> guards = new Dictionary<int, Guard>();
        int currentGuardId = 0;
        int fallsAsleepMinute = 0;

        foreach (var action in AllActions)
        {
            if (action.Value.Contains("Guard"))
            {
                string[] parsedValue = action.Value.Split
                (
                    new char[] { ' ', '#' },
                    StringSplitOptions.RemoveEmptyEntries
                );

                currentGuardId = int.Parse(parsedValue[1]);

                if (!guards.ContainsKey(currentGuardId))
                    guards.Add(currentGuardId, new Guard());
            }
            else if (action.Value.Equals("falls asleep"))
            {
                fallsAsleepMinute = action.Date.Minute;
            }
            else if (action.Value.Equals("wakes up"))
            {
                for (int i = fallsAsleepMinute; i < action.Date.Minute; i++)
                {
                    if (!guards[currentGuardId].SleepyMinutesMap.ContainsKey(i))
                        guards[currentGuardId].SleepyMinutesMap.Add(i, 0);

                    guards[currentGuardId].SleepyMinutesMap[i] += 1;
                    guards[currentGuardId].MinutesAsleep += 1;
                }
            }
        }

        return guards;
    }

    public List<GuardAction> SetActions()
    {
        List<GuardAction> guardActions = new List<GuardAction>();

        char[] separators = new char[] { '[', ']' };
        foreach (var line in Input)
        {
            var parsedLine = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            GuardAction guardAction = new GuardAction()
            {
                Date = DateTime.Parse(parsedLine[0]),
                Value = parsedLine[1].Remove(0, 1)
            };

            guardActions.Add(guardAction);
        }

        return guardActions.OrderBy(a => a.Date).ToList();
    }
}

class GuardAction
{
    public DateTime Date { get; set; }
    public string Value { get; set; }
}

class Guard
{
    public Guard()
    {
        Actions = new List<GuardAction>();
        SleepyMinutesMap = new Dictionary<int, int>();
    }

    public List<GuardAction> Actions { get; set; }
    public Dictionary<int, int> SleepyMinutesMap { get; set; }
    public int MinutesAsleep { get; set; }
}

2

u/zqvt Dec 04 '18 edited Dec 04 '18

Python, input pre-sorted with sort -V

import re
from collections import Counter


with open("input3.txt", "r") as f:
    guards = {}
    start, stop, current_guard = 0, 0, 0
    lines = [x.strip() for x in f.readlines()]

    for line in lines:
        values = re.findall("\d+", line)
        if "Guard" in line:
            current_guard = int(values[-1])
        elif "falls asleep" in line:
            start = int(values[-1])
        elif "wakes up" in line:
            stop = int(values[-1])
            for i in range(start, stop):
                guards.setdefault(current_guard, []).append(i)
    # part 1
    id1 = max(guards, key=lambda x: len(guards[x]))
    c = Counter(guards[id1])
    minute = c.most_common()[0][0]
    print(id1 * minute)
    # part2
    id2 = max(guards, key=lambda x: Counter(guards[x]).most_common()[0][1])
    print(id2 * Counter(guards[id2]).most_common()[0][0])

2

u/zirtec Dec 04 '18

We have surprisingly similar solutions, although yours is more elegant. I missed the fact max could be used the way your used it. Thanks. I was doing this:

count_per_guard = {k: len(v) for (k, v) in guards.items()}
sleeper_id = max(count_per_guard, key=count_per_guard.get)
→ More replies (2)

2

u/ephemient Dec 04 '18 edited Apr 24 '24

This space intentionally left blank.

→ More replies (1)

2

u/jonay20002 Dec 04 '18

i'm trying to do every day as a python oneliner. this is today's oneliner (though i am allowing the input reading to be on a different line):

(challenge 2)

import re
from functools import reduce
with open("input.txt") as f: 
    inputs = f.readlines()
print((lambda entries:((lambda res:(lambda newentries:[(lambda guard:[guard*{key:(lambda lst:lst.index(max(lst)))(value) for key,value in res.items()}[guard]][0])(max(newentries,key=newentries.get))][0])({key:max(value)for key,value in res.items()}))({key:[len(i) for i in item]for key,item in (lambda res:[[(lambda i,j: res[i[2]][j].add(len(res[i[2]][j])))(i,j) for i in entries for j in range(i[0],i[1])],res][1])({i[2]:[set() for i in range(60)] for i in entries}).items()})))(list(map(lambda i:[i[0]%60,i[1]%60,i[2]],[[i[0],j[0],i[2]] for i,j in (lambda i:zip(i[::2],i[1::2]))(list(filter(lambda i: i[1] != "start",reduce(lambda acc,val:[val.append(acc[0]) if val[1] != "start" else 0,[val[2] if val[1] == "start" else acc[0], acc[1] + [val]]][1],list(map(lambda i: [i[0],"start",i[1]] if type(i[1]) == int else i, map(lambda i: [i[0],int(re.split(r"#(\d+)",i[1])[1])] if "#" in i[1] else i,sorted(list(map(lambda i:[(lambda year,month,day,hour,min: int(min) + (int(hour) + (int(day) + sum([31,28,31,30,31,30,31,31,30,31,30,31][0:int(month)]) + 365*int(year))*24)*265 )(i[:-1]),i[-1]], [re.split(r"[(\d)+-(\d+)-(\d+) (\d+):(\d+)] (.)\n",i)[1:-1]for i in inputs])), key = lambda i:i[0])))),[None,[]])[1])))]))))

for the other days: https://github.com/jonay2000/aoc2018

2

u/wzkx Dec 04 '18

J

r=: 'Guard';'0';'#';'';' begins shift';'';'falls asleep';'1 0';'wakes up';'2 0'
d=: (".&(rplc&('[1518-';'';']';'';'-';' ';':';' ';r))&>)"0 cutLF CR-.~fread '04.dat'

v=: 0 60$0 NB. data matrix for minutes 0..59, empty at start
w=: 0$0    NB. corresponding guards, empty at start
g=: 0      NB. current guard
j=: 0      NB. current guard index
s=: 0      NB. started sleeping
t=: 60$0   NB. timesheet for currend guard

f=: 3 : 0  NB. changes state (v w g j s t) for each input row
  select. 4{y  NB. y = mon day hr min action guard
  case.0 do. j=:w i.g=:{:y if. j=#w do. v=:v,0 [ w=:w,g end. t=:j{v
  case.1 do. s=:3{y
  case.2 do. v=:(t=:t+1(s+i.(3{y)-s)}60$0)j}v
  end.
)

f"1 /:~ d

echo ((i.>./)i{v)*w{~i=.(i.>./)+/"1 v
echo n*w{~i i.n=.<./i=.v i."1 0>./>./v

exit 0

2

u/wzkx Dec 04 '18 edited Dec 04 '18

Let's pack it a bit to fit in one screen here.

r=:'Guard';'0';'#';'';' begins shift';'';'falls asleep';'1 0';'wakes up';'2 0'
d=:(".&(rplc&('[1518-';'';']';'';'-';' ';':';' ';r))&>)"0 cutLF CR-.~fread'04.dat'
v=:0 60$0[w=:0$s=:j=:g=:0[t=:60$0
a=:3 :'j=:w i.g=:{:y if. j=#w do.v=:v,0 [ w=:w,g end. t=:j{v'
b=:3 :'s=:3{y'
c=:3 :'v=:(t=:t+1(s+i.s-~3{y)}60$0)j}v'
(3 :'a`b`c@.(4{y)y')"1 /:~ d
echo ((i.>./)i{v)*w{~i=.(i.>./)+/"1 v
echo n*w{~i i.n=.<./i=.v i."1 0>./>./v
exit 0
→ More replies (2)
→ More replies (1)

2

u/dpeckett Dec 04 '18

Continuing with my quest to solve all this years challenges using nothing other than AWK:

Solution To Both Challenges:

{
  gsub(/[\[\]#]/,"",$0);t[NR]=$0
} END {
  n=asort(t);
  for(i=1;i<=n;i++)
    if(split(t[i],v,/[ \-\:]/)>7)g=v[7];
    else if(index(v[6],"f"))p=v[5];
    else for(j=p;j<v[5];j++){z[g","j]++;if(++zt[g]>zt[x])x=g}
  for(i=0;i<60;i++)if(z[x","i]>z[x","y])y=i;
  print x*y;
  for(i in z)if(z[i]>z[w])w=i;
  split(w,v,",");print v[1]*v[2];
}

Runtime

real 0m0.027s
user 0m0.014s
sys  0m0.005s

2

u/irongeek73 Dec 04 '18

Was it cheating that I did

sort input > input.sort

Before I started?

2

u/21_cribbage Dec 04 '18 edited Dec 04 '18

Python 3, both solutions in one script. Found out about collections.Counter which is pretty cool!

import sys
import re
from collections import defaultdict, Counter

def parse_line(regex, line):
    m = regex.match(line)
    if m:
        return int(m.group(1))


# Init regex for line parsing
guard_id_re = re.compile(r'^\[.*\] Guard #(\d+) begins shift$')
mins_re = re.compile(r'^\[\d+-\d+-\d+ \d+:(\d+)\].*$')

# Store the events and sort them chronologically
events = [line.strip() for line in sys.stdin]
events.sort()

# Parse the events and increment the counts for minutes each guard was asleep
guards = defaultdict(Counter)
for event in iter(events):
    if 'begins shift' in event:
        current_id = parse_line(guard_id_re, event)
    elif 'falls asleep' in event:
        start = parse_line(mins_re, event)
    elif 'wakes up' in event:
        end = parse_line(mins_re, event)
        for i in range(start, end):
            guards[current_id][i] += 1

# Init dummy values to hold the max result
max_strat_1 = (-1, -1, -1, -1)
max_strat_2 = (-1, -1, -1, -1)

# Complete both strategies in one pass
for id, mins in guards.items():
    total = sum(mins.values())
    minute, count = mins.most_common(1)[0] # Counter.most_common returns: [(key, count)]

    # Strategy 1
    if total > max_strat_1[1]:
        max_strat_1 = (id, total, minute, id*minute)

    # Strategy 2
    if count > max_strat_2[2]:
        max_strat_2 = (id, minute, count, id*minute)

# Output
print("Strategy 1: Guard {} was sleepiest with a total of {} mins. Most common min: {}. Result (id x min): {}".format(*max_strat_1))
print("Strategy 2: Guard {} was most commonly asleep on the same minute: {} times on minute {}. Result (id x min): {}".format(*max_strat_2))

2

u/alexmeli Dec 04 '18

My Clojure solution:

(ns clojure-solution.core
  (:require [clojure.string :as str])
  (:gen-class))

(defn read-file [path] 
  (with-open [rdr (clojure.java.io/reader path)] 
    (doall (sort (line-seq rdr)))))

(defn get-num [patt line] 
  (->> 
    (re-find patt line)
    second 
    Integer/parseInt))

(defn merge-data [g start end data]
  (let [mins (zipmap (range start end) (repeat 1))]
    (->
      (update-in data [g :total] (fnil + 0) (- end start))
      (update-in [g :mins] #(merge-with + mins %)))))

(defn process [lines] 
  (loop [[x & xs :as l] lines g nil start nil data {}] 
    (cond 
      (empty? l) data
      (str/ends-with? x "shift") 
        (recur xs (get-num #"#(\d+)" x) start data)
      (str/ends-with? x "asleep")
        (recur xs g (get-num #":(\d+)" x) data)
      (str/ends-with? x "up") 
        (recur xs g start (merge-data g start (get-num #":(\d+)" x) data)))))

(defn max-val [pred d] 
  (key (apply max-key pred d)))

;; part 1 and 2
(defn solve [data] 
  (let [max-guard1 (max-val #(:total (val %)) data)
        max-guard2 (max-val #(val (apply max-key val (:mins (val %)))) data)
        max-mins1 (max-val val (get-in data [max-guard1 :mins]))
        max-mins2 (max-val val (get-in data [max-guard2 :mins]))] 
    [(* max-guard1 max-mins1) (* max-guard2 max-mins2)]))

(defn -main
  [& args]
  (println (solve (process (read-file "resources/input.txt")))))

2

u/JakDrako Dec 04 '18

Vb.Net

Cleaned up version. First version didn't have regexes and extracted the answers with a couple of plain "for loops" over the dictionary...

Sub main

	Dim dic = New Dictionary(Of Integer, Integer()) ' id, array for 60 minutes

	Dim id = 0, dt As DateTime
	For Each line In GetDay(4).orderby(Function(x) x)
		Dim dat = CDate(regex.match(line, "\[(.+?)\]").groups(1).value)
		Select Case True
			Case line.contains("begins")
				id = Cint(regex.match(line, "(?<=#)\d+").value)
				If Not dic.containskey(id) Then dic.add(id, New Integer(59) {})
			Case line.contains("wakes")
				enumerable.range(dt.minute, datediff(dateinterval.minute, dt, dat)) _
				          .foreach(Sub(x) dic(id)(x) += 1)
		End Select
		dt = dat
	Next

	Dim parts = {Function(x) Enumerable.Sum(x), Function(x) Enumerable.Max(x)}
	For Each fn In parts
		Dim guard = dic.aggregate(Function(l, r) If(fn(l.value) > fn(r.value), l, r))
		console.writeline($"part {Array.IndexOf(parts, fn) + 1}: {guard.key * array.indexof(guard.value, guard.value.max)}")
	Next

End Sub
→ More replies (1)

2

u/cluk Dec 04 '18

I am learning Python this year. I really wanted to use built-in max, tuples for the win! I would appreciate any tips.

import re

with open('input_04.txt') as f:
	lines = f.read().splitlines()

guards = {}
id = 0
falls = 0
for line in sorted(lines):	
	if 'Guard' in line:
		id = int(re.findall('#(\d+)', line)[0])
		if id not in guards:
			guards[id] = [0] * 59
		continue
	minute = int(re.findall(':(\d+)\]', line)[0])
	if 'falls asleep' in line:
		falls = minute
	else:
		for m in range(falls, minute):
			guards[id][m] += 1

def star1(guards):
	_, guard_id = max((sum(minutes), guard) for (guard, minutes) in guards.items())
	_, chosen_minute = max((m, idx) for (idx, m) in enumerate(guards[guard_id]))
	return guard_id * chosen_minute

def star2(guards):	
	_, chosen_minute, guard_id = max((minute, idx, guard) for (guard, minutes) in guards.items() for (idx, minute) in enumerate(minutes))
	return guard_id * chosen_minute

print("Star 1:", star1(guards))
print("Star 2:", star2(guards))

2

u/streetster_ Dec 04 '18

Day 04 in Q/KDB+

/ read input
r:{ (where like[;"*Guard*"] each x) cut x} asc read0 `:input/04.txt
/ build table from input
t:{ `g`m`s!(enlist"J"$1_@[;3]" "vs x 0),$[1=count x;(0;0);(::;sum)@'raze each flip { (x+til y-x;y-x) }.'2 cut "J"$.[;(::;1;3 4)]" "vs'1_x] } each r
/ group by guard
t:exec { `mx`m`s!(max[c];first where c=max c:count each group raze x;sum y) }[m;s] by g:g from t
/ part 1
first exec g*m from t where s=exec max s from t
/ part 2
first exec g*m from t where mx=exec max mx from t

2

u/qwertyuiop924 Dec 04 '18

Today’s puzzle would have been a lot easier if my language supported smacking me upside the head for missing something so obvious.

Part one and two in terrible AWK: https://pastebin.com/9Utr1QAA https://pastebin.com/3sv2BepN

2

u/SuyashD95 Dec 04 '18

Usually, this is the time that I post my solution on this thread... But, due to some hectic work, I forgot to check today's question.... I have just seen the question half an hour back...

And, till now, I have just arranged the input in chronological order but I am just too tired to continue.... I, very well know, that I am going to miss today's deadline...

For me, AoC is all about having some fun with solving some cool programming puzzles, engaging in wonderful discussion with all of you (about possible ways to solve a problem) and learn some new things...

So, don't worry, I am not here to cheat or look at your solutions... I want to give it my best shot before coming back here (to hopefully post my solution)...

But, since, this is the first time, I am participating in AoC... So, I have few queries that I hope that you could answer for me:

  1. If I unable to submit my solution on time, would I need to complete today's puzzles before unlocking tomorrow's puzzles.. (I don't think that tomorrow's puzzles will be locked but I am not 100% sure... ). Hence, this question...

  2. And, more importantly, will I be able to gain stars even if I complete the solution later (but before AoC 2018 ends)? Or, for in order to gain stars, I need to finish the puzzles in the given 24 hr time period?

Again, it is a not a deal breaker if I am not able to gain stars... It's just feels so exciting when I press answer, and I get the message, "You have earned 1 or 2 gold stars'...

Finally, a BIG, HEARTY CONGRATULATIONS to all my fellow coders for completing today's set of puzzles...

When I joined this challenge, I never thought that I will enjoy it so much... And, you guys (and gals) made this experience so much more wonderful.... And, I can't wait for joining in further discussions with all of you....

→ More replies (4)

3

u/sebranly Dec 04 '18 edited Dec 04 '18

[Edit: I was wrong, please don't upvote]

Argh... I just encountered an annoying issue to be honest. I completed part 1, then coded part 2 in a few seconds/minutes. I ran it on the small example, everything was working perfectly. I decided to run it on my input file, surprisingly I got the same answer as part 1. But it was wrong. After 10 minutes, I figured that the reason was because my set contained two guards that were ex-aequo (who slept the most for a specific minute). And I was checking for the highest score this way:

if (count > highscore) highscore = count;

I had to change it to >= to make it work. I'm a bit disappointed because this didn't appear in the problem statement.

Happy to share my input once I clean up my code.

3

u/I-AM-PIRATE Dec 04 '18

Ahoy sebranly! Nay bad but me wasn't convinced. Give this a sail:

Argh... me just encountered a annoying issue t' be honest. me completed part 1, then coded part 2 in a few ticks o' tha clock/minutes. me ran it on thar puny example, everything be working perfectly. me decided t' run it on me input file, surprisingly me got thar same answer as part 1. But it be wrong. After 10 minutes, me figured that thar reason be because me set contained two guards that were ex-aequo (who slept thar most fer a specific minutes). N' me be checking fer thar highest score dis way:

if (count > highscore) highscore = count;

me had t' change it t' >= t' make it duty. I be a bit disappointed because dis didn't appear in thar problem statement.

Grog-filled t' share me input once me clean up me code.

2

u/kindermoumoute Dec 04 '18

Well, this is kinda unfair as I did the same mistake as you but didn't end up with a bad input.

→ More replies (5)

1

u/ValdasTheUnique Dec 04 '18

C#. A boilerplaty solution which took too much time to implement. And the lack of argmax function is annoying. Part1:

public static void Part1()
{
    var lines = Input.Split("\n")
        .Select(x =>
        {
            var idStart = x.IndexOf('#');
            var id = idStart > 0 ? x.Substring(idStart+1, x.IndexOf(' ', idStart) - idStart) : null;
            return new
            {
                date = DateTime.Parse(x.Substring(1, 16)),
                id = id != null ? int.Parse(id) : (int?)null,
                isAsleep = x.Contains("asleep")
            };
        })
        .OrderBy(x => x.date)
        .ToList();
    var sleepTimes = new Dictionary<int, int[]>();
    var currentId = lines[0].id.Value;
    for (var i = 0; i < lines.Count-1; i++)
    {
        if (lines[i + 1].id != null)
        {
            currentId = lines[i + 1].id.Value;
            continue;
        }
        if (!sleepTimes.ContainsKey(currentId))
        {
            sleepTimes.Add(currentId, new int[60]);
        }

        if (lines[i].isAsleep)
        {
            var minutes = (lines[i + 1].date - lines[i].date).Minutes;
            for (int j = 0; j < minutes; j++)
            {
                var minuteStart = lines[i].date.Minute;
                sleepTimes[currentId][(minuteStart + j)%60]++;
            }
        }
    }

    var result = sleepTimes
        .Select(x =>
        {
            var max = x.Value.Max();
            var index = -1;
            var minutesAsleep = 0;
            for (int i = 0; i < x.Value.Length; i++)
            {
                if (x.Value[i] == max)
                {
                    index = i;
                }

                minutesAsleep += x.Value[i];
            }

            return new
            {
                id = x.Key,
                minute = index,
                minutesAsleep
            };
        })
        .OrderByDescending(x => x.minutesAsleep)
        .First();
    Console.WriteLine(result.id * result.minute);

Part2 was very easy with the code I had, this was the only change:

var result = sleepTimes
    .Select(x =>
    {
        var max = x.Value.Max();
        var index = -1;
        for (int i = 0; i < x.Value.Length; i++)
        {
            if (x.Value[i] == max)
            {
                index = i;
            }
        }

        return new
        {
            id = x.Key,
            minute = index,
            max
        };
    })
    .OrderByDescending(x => x.max)
    .First();
Console.WriteLine(result.id * result.minute);

1

u/throwaway_the_fourth Dec 04 '18

python3 (#160/#148):

import re
from collections import defaultdict

from aoc_input import AOCInput

TEXT = AOCInput(4).value.split('\n')
TEXT.sort()  # since each line is in YYYY-MM-DD HH:MM, it is easy to sort by string ordering

# parse out the relevant ints
# this splits each line on the time colon since we only care about the minute and the guard ID.
ROWS = [['falls' in l, 'wakes' in l, *map(int, re.findall(r'\d+', l.partition(':')[2]))] for l in TEXT if l]


# returns a dict -> dict -> int that maps ID -> minute -> times asleep.
def parse_input():
    guards = defaultdict(lambda: defaultdict(int))

    for line in ROWS:
        fell_asleep, woke_up, minute = line[:3]
        name = line[3] if len(line) == 4 else None  # len 4 when ID in the line

        if name is not None:
            current_guard = name

        if fell_asleep:
            time_fell = minute

        if woke_up:
            for minute in range(time_fell, minute):
                guards[current_guard][minute] += 1
    return guards


def part_a(guards):
    sleepiest_guard = max(guards.keys(), key=lambda g: sum(guards[g].values()))
    sleepiest_minute = max(guards[sleepiest_guard].keys(), key=lambda min: guards[sleepiest_guard][min])
    return sleepiest_guard * sleepiest_minute


def part_b(guards):
    sleepiest_minute = {guard: max(guards[guard].keys(), key=lambda m: guards[guard][m]) for guard in guards.keys()}
    sleepiest_guard = max(guards.keys(), key=lambda g: guards[g][sleepiest_minute[g]])
    return sleepiest_guard * sleepiest_minute[sleepiest_guard]


if __name__ == '__main__':
    parsed_guards = parse_input()
    print(part_a(parsed_guards))
    print(part_b(parsed_guards))

This is not what I wrote in the moment — that code was full of copy-paste, bad variable names, and overly verbose loops. This is the cleaned-up version which still uses the same algorithm/technique.

1

u/Frodolas Dec 04 '18 edited Dec 04 '18

The perils of trying to learn a new statically typed language on Advent of Code - the strict typing can really slow you down :'(

#1326, #1232 - Crystal:

Common:

guard_id = -1
guard_sleep_events = File.read_lines("#{__DIR__}/input.txt").sort.map do |line|
    nums = line.scan(/-?\d+/).map(&.[0].to_i)
    if line.includes? "begins shift"
        guard_id = nums[5]
        next
    end
    starts_sleep = line.includes? "falls asleep"
    {nums[4], guard_id, starts_sleep}
end

guard_sleep_times = guard_sleep_events.compact.group_by(&.[1]).transform_values do |sleep_events|
    abort("something wrong") if sleep_events.size.odd? # should fall asleep then wake up
    sleep_events.in_groups_of(2, {60, -1, false}).flat_map do |(first, second)|
        (first[0]...second[0]).to_a
    end
end

Part 1:

most_asleep_guard = guard_sleep_times.max_by(&.[1].size)[0]
most_asleep_time = guard_sleep_times[most_asleep_guard].group_by(&.itself).max_by(&.[1].size)[0]

puts most_asleep_guard * most_asleep_time

Part 2:

most_common_sleep = guard_sleep_times.flat_map { |id, times| times.map { |time| {time, id} } }
                                     .group_by(&.itself)
                                     .max_by(&.[1].size)
                                     .first

puts most_common_sleep[0] * most_common_sleep[1]

1

u/pythondevgb Dec 04 '18 edited Dec 04 '18

I got obliterated this time, just finished. I'm not even bothered to clean up or optimize my code any more. Anyway here is my answer.

(If anyone wanna join a private reddit/python leaderboard PM for the code, the only requirement is to use python and to not have made it onto the overall leaderboard thus far [although I can't enforce no. 1, that I'll take on faith])

import numpy as np
from collections import *
from itertools import *
import re
import datetime

with open('day4_input.txt') as f:    
    theinput = f.read().splitlines()

##Part 1

datepattern = re.compile(r"\[(\d+)\-(\d+)\-(\d+) (\d+):(\d+)\]")
records = []
for record in theinput:
    date = map(int,datepattern.search(record).groups())
    d = datetime.datetime(*date)
    records.append((d,record[19:]))
records = sorted(records)

count = Counter()
perminute = defaultdict(lambda: defaultdict(int))
for d, record in records:
    shift = re.findall(r'\d+', record)
    if shift:
        sleep = False
        guard = int(shift[0])
    sleep = 'sleep' in record
    awake = 'wakes' in record
    if sleep:
        sleepstart = d
    
    if awake:                
        for minute in range(sleepstart.minute, d.minute):            
            count[guard]+=1
            perminute[guard][minute]+=1

guard_most_sleep = max(count.items(),key=lambda c: c[1])[0]
most_slept_minute = max(perminute[guard_most_sleep].items(), key=lambda c: c[1])[0]

print(guard_most_sleep * most_slept_minute)

#Part 2
times = 0
for guard in perminute:
    minutes = perminute[guard]
    most_slept_minute = max(minutes.items(), key = lambda t: t[1])

    times_for_guard = most_slept_minute[1]
    whichminute = most_slept_minute[0]
    
    if times_for_guard >= times:
        times = times_for_guard
        theminute = whichminute
        theguard = guard


print(theminute*theguard)

2

u/RadioactiveHop Dec 04 '18

more numpy / re / datetime

import re
from datetime import datetime
import numpy as np

input_file = [line.rstrip() for line in open('input.txt')]
input_file.sort() # sort alphabeticaly is enough
sample_size = len(input_file)
guards = {}
current_guard = None
sleeping_from = None

log_re = re.compile("\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2})\] (Guard #(\d+) begins shift|falls asleep|wakes up)")

for l in input_file:
    log_entry = log_re.match(l).groups()
    timestamp = datetime.fromisoformat(log_entry[0])
    if log_entry[2] is not None and log_entry[2].isdigit():
        current_guard = int(log_entry[2])
        if current_guard not in guards:
            guards[current_guard] = np.zeros(60)
    elif "falls asleep" in log_entry[1]:
        sleeping_from = timestamp
    elif "wakes up" in log_entry[1]:
        for m in range(sleeping_from.minute, timestamp.minute):
            guards[current_guard][m] += 1
        sleeping_from = None
    else:
        pass # we shouldn't be here

#part 1
biggest_sleeper = max(guards.items(), key=lambda k: sum(k[1]))
print(biggest_sleeper[0] * biggest_sleeper[1].argmax())

#part 2
frequent_sleeper = max(guards.items(), key=lambda k: max(k[1]))
print(frequent_sleeper[0] * frequent_sleeper[1].argmax())

1

u/ZZRJo Dec 04 '18

PowerShell

I did not edit for readability, I did not really take a very structured approach. I apologize in advance for any bleeding eyeballs out there. I feel like the idea behind nesting the hashtables was a good one, but the execution was a 1/10.

$logs = Get-Content 'C:\Users\Admin\Documents\Advent Of Code 2018\Day4.txt'
$logs = $logs -replace '[[]', ""
$logs = $logs -split '] '
$Dates = @{}
$Guards = @{}
$GuardSums = @{}
$HighGuard = 0
$HighGuardName = $null
$HighGuardMinute = $null

Function Add-GuardTime($LocalTable, $Guard, $Minute){
    if(!$LocalTable.ContainsKey($Guard)){
        $LocalTable.$Guard = @{}
    }
    if(!$LocalTable.$Guard.ContainsKey($Minute)){
        $LocalTable.$Guard.$Minute = 0
    }
    $LocalTable.$Guard.$Minute++
}

For($i=0; $i -lt $logs.count; $i=$i+2){
    [datetime]$LogDate = [datetime]$logs[$i]
    $Dates.$Logdate = $logs[[int]$i+1]
}

$Dates = $Dates.GetEnumerator() | Sort-Object -Property name

For($i=0; $i -lt $Dates.count; $i++){
    if($Dates[$i].value -match 'Guard'){
        If($asleep -eq $true){
            For($i2=0; $i2 -lt ([datetime]$Dates[$i].Key-$startSleep).Minutes; $i2++){
                Add-GuardTime -LocalTable $Guards -Guard $currentGuard -Minute $($startSleep.Minute+$i2)
            }
        }
        $currentGuard = $Dates[$i].value -replace "[^0-9]"
        $asleep = $false
    }
    If($Dates[$i].value -match 'asleep'){
        $asleep = $true
        [datetime]$startSleep = [datetime]$Dates[$i].Key
    }
    If(($asleep -eq $true) -and ($Dates[$i].value -match 'wakes')){
        For($i2=0; $i2 -lt ([datetime]$Dates[$i].Key-$startSleep).Minutes; $i2++){
            Add-GuardTime -LocalTable $Guards -Guard $currentGuard -Minute $($startSleep.Minute+$i2)
        }
        $asleep = $false
    }
}

ForEach($Guard in $Guards.Keys){
    $GuardSums.$Guard = ($Guards.$Guard.Values | Measure-Object -Sum).sum
    if(($Guards.$Guard.GetEnumerator() | Sort-Object Value)[$Guards.$Guard.Count-1].Value -gt $HighGuard){
        $HighGuard = ($Guards.$Guard.GetEnumerator() | Sort-Object Value)[$Guards.$Guard.Count-1].Value
        $HighGuardName = $Guard
        $HighGuardMinute = ($Guards.$Guard.GetEnumerator() | Sort-Object Value)[$Guards.$Guard.Count-1].Key
    }
}

[int]$Part1GuardNum = [int](($GuardSums.GetEnumerator() | Sort-Object Value)[$GuardSums.Count-1].Key)
Write-Host "The answer to part 1 is" ([int]$Part1GuardNum * [int](($Guards.$([string]$Part1GuardNum).GetEnumerator() | Sort-Object Value)[$Guards.$([string]$Part1GuardNum).Count-1].Key))
Write-Host "The answer to part 2 is" ([int]$HighGuardName * [int]$HighGuardMinute)

1

u/CaptainCa Dec 04 '18

JS, no leaderboard. Took a bit too long trial / error'ing part two. Took the time to cleanup the code to post

const sleepMin = (a, b, prev) => {
	let o = a.getMinutes();
	prev = prev || {};
	for(let i = 0; i < new Date(b-a).getMinutes(); i++){
		prev[o+i] = prev[o+i] ?  prev[o+i]+1 : 1;
    }
	return prev;
}

const sortByIndex = (array, i, asc) => {
	return array.slice().sort((a,b)=>{
			if(a[i]<b[i])
				return -1*asc; 
			if(a[i]>b[i])
				return asc; 
		return 0;
	});
}

const input = sortByIndex(document.body.innerText.split('\n').filter(c=>c).map(c=>{let m = c.match(/\[([^\]]*)\] (.+)/); return [new Date(m[1]), m[2]]}), 0, 1);
const g = {};
let currentGuard = 0;
let sleepsAt = null;
for(let i = 0; i < input.length; i++){
	let time = input[i][0];
	let s = input[i][1];

	var n = s.match(/Guard #([0-9]+)/);
	if(n != null){
		currentGuard = n[1];	
	}
	else if(s == "wakes up"){
		g[currentGuard] = sleepMin(sleepsAt, time, g[currentGuard])
	}
	else {
		sleepsAt = new Date(time.getTime());
	}	
}

const totals = Object.entries(g)
	.map(c => 
		[
		+c[0], //id
		...[...sortByIndex(Object.entries(c[1]).sort((a,b) => a-b), 1, -1)[0]].map(Number), //which minute was max, and with what value
		Object.values(c[1]).reduce((a,c)=>a+c), //total minutes
	]);
	
const part1 = sortByIndex(totals, 3, -1);	
const part2 = sortByIndex(totals, 2, -1);
console.log("Part 1", part1[0][0] * part1[0][1]); 
console.log("Part 2", part2[0][0] * part2[0][1]);

1

u/Axsuul Dec 04 '18

TypeScript / JavaScript

Critiques welcome, I'm still very new to TypeScript and feel like I'm not using all the features I could be :)

Repo

Part A + B

import { readInput } from '../helpers'

const input: string[] = readInput('04', 'input')

interface Guard {
  minutesAsleep: number,
  minutes: any,
}

interface Guards {
  [key: string]: Guard,
}

// Organize chronologically
const lines = input
  .map((line: string): [string, number] => {
    const date: string = RegExp(/\[(.+)\]/).exec(line)![1]

    return [line, +new Date(date)]
  })
  .sort((a: [string, number], b: [string, number]) => a[1] - b[1])
  .map(([line]: any[]) => line as string)

const guards: Guards = {}
let currentGuardId
let sleptAt

for (const line of lines) {
  const match = RegExp(/.+ \d+\:(\d+)\] (.+)/).exec(line)

  if (match) {
    const minute = Number(match[1])
    const action = match[2]

    const guardMatch = RegExp(/Guard \#(\d+)/).exec(line)

    if (guardMatch) {
      currentGuardId = Number(guardMatch[1])

      if (!guards.hasOwnProperty(currentGuardId)) {
        guards[currentGuardId] = {
          minutes: {},
          minutesAsleep: 0,
        }
      }
    } else if (currentGuardId) {
      if (RegExp(/asleep/).test(action)) {
        sleptAt = minute
      } else if (sleptAt && RegExp(/wakes/).test(action)) {
        const duration = minute - sleptAt

        guards[currentGuardId].minutesAsleep += duration

        for (let m = sleptAt; m < minute; m++) {
          if (!guards[currentGuardId].minutes.hasOwnProperty(m)) {
            guards[currentGuardId].minutes[m] = 0
          }

          guards[currentGuardId].minutes[m] += 1
        }
      }
    }
  }
}

// Find guard who slept the most
const [winningGuardId]: number[] = Object
  .keys(guards)
  .map(Number)
  .reduce((winner: number[], guardId: number) => {
    const minutesAsleep: number = guards[guardId].minutesAsleep

    return minutesAsleep > winner[1] ? [guardId, minutesAsleep] : winner
  }, [0, 0])

const findMostGuardMinute = (guardId: number): number[] => {
  const minutes = guards[guardId].minutes

  return Object
    .keys(minutes)
    .map(Number)
    .reduce((winner: number[], minute: number) => {
      const count: number = minutes[minute]

      return count > winner[1] ? [minute, count] : winner
    }, [0, 0])
}

console.log(`Part 1: ${winningGuardId * findMostGuardMinute(winningGuardId)[0]}`)

const mostAsleep = Object.entries(guards).map((value: [string, Guard]) => {
  const guardId = +value[0]

  return [guardId].concat(findMostGuardMinute(guardId))
}).reduce((winner: number[], current: number[]) => {
  return current[2] > winner[2] ? current : winner
})

console.log(`Part 2: ${mostAsleep[0] * mostAsleep[1]}`)

1

u/valtism Dec 04 '18

Node / JavaScript (JS)

Part 1: 11.921ms

Part 2: 16.650ms

Things got a bit ugly around part 2 because of the transition between Maps and Arrays. Kinda wishing at this point I had decided to go with TypeScript just so I could keep a handle on these data structures.

function part1(data) {
    const guardLog = generateGuardLogs(data);

    const totalSleepTimes = getTotalSleepTimes(guardLog);
    const longestSleeper = getEntryWithLargestValue(totalSleepTimes);

    const sleepyGuardsLog = guardLog.get(longestSleeper[0]);
    const longestMinute = getEntryWithLargestValue(sleepyGuardsLog);

    return Number(longestSleeper[0]) * longestMinute[0];
}

function generateGuardLogs(data) {
    const observations = data.split("\n").map(parseInput);
    observations.sort(sortByTimestamp);
    const guardLog = initGuardLog(observations);
    populateMinutes(observations, guardLog);
    return guardLog;
}

function parseInput(observation) {
    const timestampRe = /(?<=\[).*?(?=\])/;
    const beginsRe = /begins/;
    const sleepRe = /sleep/;
    const idRe = /(?<=#)\d+/;
    const isBeginShift = beginsRe.test(observation);
    return {
        timestamp: new Date(timestampRe.exec(observation)[0]),
        isBeginShift: isBeginShift,
        isSleep: sleepRe.test(observation),
        guardId: isBeginShift ? idRe.exec(observation)[0] : null
    };
}

function sortByTimestamp(a, b) {
    if (a.timestamp < b.timestamp) return -1;
    if (a.timestamp > b.timestamp) return 1;
    return 0;
}

function initGuardLog(observations) {
    const guardIds = observations
        .filter(o => o.guardId)
        .map(o => [o.guardId, new Map()]);
    const guardLog = new Map(guardIds);
    return guardLog;
}

function populateMinutes(observations, guardLog) {
    let guard = null;
    let sleepTime = null;
    observations.forEach(observation => {
        if (observation.isBeginShift) {
            guard = guardLog.get(observation.guardId);
            return;
        }
        if (observation.isSleep) {
            sleepTime = observation.timestamp;
            return;
        }
        if (!observation.isSleep) {
            const sleepMin = (observation.timestamp - sleepTime) / 1000 / 60;
            for (
                let i = sleepTime.getMinutes();
                i < sleepTime.getMinutes() + sleepMin;
                i++
            ) {
                let minute = i % 60;
                if (!guard.has(minute)) {
                    guard.set(minute, 1);
                } else {
                    let count = guard.get(minute);
                    guard.set(minute, ++count);
                }
            }
        }
    });
}

function getTotalSleepTimes(guardLog) {
    const totalSleepTimes = new Map();
    for (const [id, minutes] of guardLog) {
        const vals = Array.from(minutes.values());
        const min = vals.reduce((acc, curr) => acc + curr, 0);
        totalSleepTimes.set(id, min);
    }
    return totalSleepTimes;
}

function getEntryWithLargestValue(map) {
    const entries = Array.from(map.entries());
    return entries.reduce(
        (longest, curr) => (longest[1] > curr[1] ? longest : curr),
        [0, 0]
    );
}

function part2(data) {
    const guardLog = generateGuardLogs(data);
    const mostSleptMinuteLog = getMostSleptMinuteLog(guardLog);
    const mostSleptMinute = getMostSleptMinute(mostSleptMinuteLog);
    return Number(mostSleptMinute[0]) * mostSleptMinute[1][0];
}

function getMostSleptMinuteLog(guardLog) {
    let mostSleptMinuteLog = new Map();
    for (const [id, log] of guardLog) {
        mostSleptMinuteLog.set(id, getEntryWithLargestValue(log));
    }
    return mostSleptMinuteLog;
}

function getMostSleptMinute(mostSleptMinuteLog) {
    const mostSleptMinuteArr = Array.from(mostSleptMinuteLog.entries());
    const mostSleptMinute = mostSleptMinuteArr.reduce((acc, curr) =>
        acc[1][1] > curr[1][1] ? acc : curr
    );
    return mostSleptMinute;
}

module.exports = {
    part1: part1,
    part2: part2
};

1

u/wlandry Dec 04 '18

C++ (3183/3059) Runs in about 10ms

I started late and took forever. It was pretty straightforward. I presorted the input using Emacs. Everything worked the first time it compiled, so I was obviously being too careful ;) I feel like there is a cleaner, more elegant solution hiding within.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>

enum class State {begin, sleep, awaken};
struct Action
{
  State state;
  int64_t guard_id;
};

struct Event
{
  int64_t month, day, hour, minute;
  Action action;
};


std::istream &operator>>(std::istream &is, Action &action)
{
  std::string element;
  is >> element;
  if(element=="Guard")
    {
      action.state=State::begin;
      char c;
      is >> c >> action.guard_id;
    }
  else if(element=="falls")
    {
      action.state=State::sleep;
    }
  else if(element=="wakes")
    {
      action.state=State::awaken;
    }
  else
    {
      throw std::runtime_error("Invalid input");
    }
  std::getline(is,element);
  return is;
}

std::istream &operator>>(std::istream &is, Event &event)
{
  char c;
  int64_t year;
  is >> c;
  if(is.good())
    {
      is >> year >> c >> event.month >> c >> event.day
         >> event.hour >> c >> event.minute >> c
         >> event.action;

      if(event.hour==23)
        {
          event.minute=0;
        }
    }
  return is;
}



std::ostream &operator<<(std::ostream &os, const Action &action)
{
  switch(action.state)
    {
    case State::begin:
      os << "Guard #" << action.guard_id << " begins shift";
      break;
    case State::sleep:
      os << "falls asleep";
      break;
    case State::awaken:
      os << "wakes up";
      break;
    default:
      os << "What?";
      break;
    }
  return os;
}

std::ostream &operator<<(std::ostream &os, const Event &event)
{
  os << "[1518-"
     << std::setw(2) << std::setfill('0') << event.month << "-"
     << std::setw(2) << std::setfill('0') << event.day << " "
     << std::setw(2) << std::setfill('0') << event.hour << ":"
     << std::setw(2) << std::setfill('0') << event.minute << "] "
     << event.action;
  return os;
}

std::pair<int64_t,int64_t> max_incident(const std::vector<std::pair<int64_t,
                                        int64_t>> &incidents)
{
  std::vector<int64_t> sleep_incidents(60,0);
  for(auto &times: incidents)
    {
      for(size_t time=times.first; time<times.second; ++time)
        { ++(sleep_incidents[time]); }
    }

  int64_t max_incidents(0), max_minute(0);
  for(size_t time=0; time<sleep_incidents.size(); ++time)
    {
      if(max_incidents<sleep_incidents[time])
        {
          max_incidents=sleep_incidents[time];
          max_minute=time;
        }
    }
  return std::make_pair(max_incidents,max_minute);
}


int main(int argc, char *argv[])
{
  std::ifstream infile(argv[1]);
  std::vector<Event> events(std::istream_iterator<Event>(infile), {});

  std::map<int,std::vector<std::pair<int64_t,int64_t>>> guards;

  int64_t guard_id;
  int64_t start(-1);
  for(auto &event: events)
    {
      switch(event.action.state)
        {
        case State::begin:
          guard_id=event.action.guard_id;
          break;
        case State::sleep:
          start=event.minute;
          break;
        case State::awaken:
          guards[guard_id].emplace_back(start,event.minute);
          break;
        }
    }

  int64_t max_time_guard, max_time(0);
  for(auto &guard: guards)
    {
      int64_t total_time(0);
      for(auto &time: guard.second)
        {
          total_time+=time.second-time.first;
        }
      if(total_time>max_time)
        {
          max_time_guard=guard.first;
          max_time=total_time;
        }
    }

  auto [max_incidents,max_minute]=max_incident(guards[max_time_guard]);
  std::cout << "Part 1: " << (max_minute*max_time_guard) << "\n";

  std::vector<int64_t> num_incidents(guards.size());
  int64_t max_guard_incidents(0), max_guard_minute(0), max_guard_id(-1);
  for(auto &guard: guards)
    {
      auto [incidents,minute]=max_incident(guard.second);
      if(incidents>max_guard_incidents)
        {
          max_guard_incidents=incidents;
          max_guard_minute=minute;
          max_guard_id=guard.first;
        }
    }
  std::cout << "Part 2: " << (max_guard_id * max_guard_minute) << "\n";
}

1

u/DrinkinBird Dec 04 '18

Ahhh totally forgot the time today and then rushed it after already being 30 mins late...

Anyways, here is my solution in nim:

import re, rdstdin, strutils, sequtils, algorithm, tables

func present(s: string): bool = len(s) > 0
let lines = stdin.readAll().splitLines().filter(present).sorted(cmp)

type
  DayData = object of RootObj
    guardId: int
    asleepMinutes: seq[int]

func parseGuardId(line: string): int =
  parseInt(line.findAll(re(r"\d+"), 18)[0])

func parseMin(line: string): int =
  line.findAll(re(r"\d+"))[4].parseInt

func parseHour(line: string): int =
  line.findAll(re(r"\d+"))[3].parseInt

func sleepMins(startMin: int, endMin: int): seq[int] =
  toSeq(startMin..<endMin)

var days = newSeq[DayData]()
var startSleep: int

for line in lines:
  if line.contains("begins shift"):
    days.add(DayData(asleepMinutes: newSeq[int](), guardId: parseGuardId(line)))
  if line.contains("falls asleep"):
    if line.parseHour == 00:
      startSleep = line.parseMin
    else:
      startSleep = 0
  if line.contains("wakes up"):
    if line.parseHour == 00:
      days[days.high].asleepMinutes.insert(sleepMins(startSleep, line.parseMin))
    else:
      days[days.high].asleepMinutes.insert(sleepMins(startSleep, 59))

func guardIds(): seq[int] =
  days.map(func(day: DayData): int = day.guardId).deduplicate

proc task1(): int =
  let guardDataMax = guardIds()
    .map(func(guard: int): (int, int, int) =
      let totalMinutes =
        days
          .filter(func(day: DayData): bool = day.guardId == guard)
          .map(func(day: DayData): seq[int] = day.asleepMinutes)
          .foldl(a.concat(b))

      if totalMinutes.len == 0: return

      let highestMinute = totalMinutes
        .deduplicate
        .map(func(min: int): (int, int) = (-totalMinutes.count(min), min))
        .sorted(cmp)[0][1]

      (-totalMinutes.len, guard, highestMinute)
    ).sorted(cmp)[0]

  guardDataMax[1] * guardDataMax[2]

proc task2(): int =
  var minTable = newCountTable[(int, int)]()

  for day in days:
    for minute in day.asleepMinutes:
      minTable.inc((day.guardId, minute))

  minTable.largest[0][0] * minTable.largest[0][1]

echo task1()
echo task2()

1

u/hnra Dec 04 '18

Haskell (part 1). Its long but quite readable IMO, most of it is just the datatypes and parsing.

{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified Data.ByteString as B
import Data.Attoparsec.ByteString.Char8
import Data.List
import qualified Data.HashMap.Lazy as HM

data Action =
    BeginShift Int
  | FallAsleep
  | WakeUp
  deriving (Eq, Show)

data Record = Record {
  _date :: Int,
  _time :: (Int, Int),
  _action :: Action
} deriving Eq

data Guard = Guard {
  _id :: Int,
  _ms :: [Int]
} deriving (Eq, Show)

instance Show Record where
  show (Record date (h,m) action) =
    (show date) ++ " " ++ (show h) ++ ":" ++ (show m) ++ " - " ++ (show action) ++ "\n"

instance Ord Record where
  compare (Record date (h,m) _) (Record date' (h', m') _)
    | date == date' && h == h' = compare m m'
    | date == date' = compare h h'
    | otherwise = compare date date'

parseAction :: Parser Action
parseAction = do
  firstChar <- anyChar
  case firstChar of
    'f' -> return FallAsleep
    'w' -> return WakeUp
    _   -> do
      string "uard #"
      id <- decimal
      return $ BeginShift id

parseRecord :: Parser Record
parseRecord = do
  char '['
  year <- many' digit
  char '-'
  month <- many' digit
  char '-'
  day <- many' digit
  space
  hour <- decimal
  char ':'
  minute <- decimal
  char ']'
  space
  action <- parseAction
  manyTill anyChar endOfLine
  return $ Record (read $ year ++ month ++ day) (hour, minute) action

parseRecords :: Parser [Record]
parseRecords = do
  records <- many' parseRecord
  return records

hmToGuards :: HM.HashMap Int [Int] -> [Guard]
hmToGuards hm =
  HM.foldrWithKey (\k v acc -> (Guard k v):acc) [] hm

getGuards :: [Record] -> [Guard]
getGuards records = go records HM.empty 0 0
  where
    go [] hm _ _ = hmToGuards hm
    go (r:rs) hm gid ptime =
      case _action r of
        BeginShift newGid -> go rs hm newGid 0
        FallAsleep -> go rs hm gid (snd $ _time r)
        WakeUp -> go rs (HM.insertWith (++) gid [ptime..(snd $ _time r)] hm) gid 0

findMinute :: Guard -> (Int, Int)
findMinute (Guard _ mins) =
  let minmap = foldr (\m acc -> HM.insertWith (+) m 1 acc) HM.empty mins
  in HM.foldrWithKey (\k v (m, count) -> if v >= count then (k, v) else (m, count)) (0,0) minmap

main :: IO ()
main = do
  file <- B.readFile "day4_input"
  case parseOnly parseRecords file of
    Left _ -> putStrLn "Failed to parse"
    Right recs' -> do
      let recs = sort recs'
          guards = getGuards recs
          guard = foldr (\g g' -> if (length $ _ms g) > (length $ _ms g') then g else g') (Guard (-1) []) guards
          (m, _) = findMinute guard
      putStrLn $ "Part 1: Guard #" ++ (show $ _id guard) ++ " Minute: " ++ (show m) ++ " Result: " ++ (show $ m * (_id guard)) 

1

u/IWearATinFoilHat Dec 04 '18

PHP

Part 1:

<?php

$time_start = microtime(true);

$input = file(__DIR__ . '/input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$formattedArray = [];
$guardData = [];
$sleepStart = 0;
$currentGuard = 0;

foreach ($input as $line) {
	preg_match('/^\[(?<date>\d+\-\d+-\d+\s\d+:\d+)]\s(?<status>.*)$/', $line, $matches);

	$formattedArray[$matches['date']] = $matches['status'];
}

ksort($formattedArray);

foreach ($formattedArray as $key => $value) {
	if (0 === strpos($value, 'Guard')) {
		$currentGuard = preg_replace('/[^0-9]/', '', $value);
	} elseif ('wakes up' === $value) {
		$minute = (int) substr($key, -2);
		$guardData[$currentGuard]['totalSleepTime'] = $guardData[$currentGuard]['totalSleepTime'] + ($minute - $sleepStart);

		for ($i = $sleepStart; $i < $minute; ++$i) {
			++$guardData[$currentGuard]['sleepMinute'][$i];
		}
	} elseif ('falls asleep' === $value) {
		$sleepStart = (int) substr($key, -2);
		$guardData[$currentGuard]['id'] = $currentGuard;
	}
}

$max = array_reduce($guardData, function ($carry, $item) {
	if ($carry['totalSleepTime'] > $item['totalSleepTime']) {
		return $carry;
	}

	return $item;
});

$maxMostSleptMinute = max($max['sleepMinute']);
$mostSleptMinute = array_search($maxMostSleptMinute, $max['sleepMinute']);

echo $max['id'] * $mostSleptMinute . "\n";
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start) . "\n";

Average run time: 0.0045

Part 2:

<?php

$time_start = microtime(true);

$input = file(__DIR__ . '/input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$formattedArray = [];
$guardData = [];
$sleepStart = 0;
$currentGuard = 0;

foreach ($input as $line) {
	preg_match('/^\[(?<date>\d+\-\d+-\d+\s\d+:\d+)]\s(?<status>.*)$/', $line, $matches);

	$formattedArray[$matches['date']] = $matches['status'];
}

ksort($formattedArray);

foreach ($formattedArray as $key => $value) {
	if (0 === strpos($value, 'Guard')) {
		$currentGuard = preg_replace('/[^0-9]/', '', $value);
	} elseif ('wakes up' === $value) {
		$minute = (int) substr($key, -2);
		$guardData[$currentGuard]['totalSleepTime'] = $guardData[$currentGuard]['totalSleepTime'] + ($minute - $sleepStart);

		for ($i = $sleepStart; $i < $minute; ++$i) {
			++$guardData[$currentGuard]['sleepMinute'][$i];
		}
	} elseif ('falls asleep' === $value) {
		$sleepStart = (int) substr($key, -2);
		$guardData[$currentGuard]['id'] = $currentGuard;
	}
}

$max = array_reduce($guardData, function ($carry, $item) {
	$carryMaxSleptMinute = max($carry['sleepMinute']);
	$itemMaxSleptMinute = max($item['sleepMinute']);

	if ($carryMaxSleptMinute > $itemMaxSleptMinute) {
		return $carry;
	}

	return $item;
});

$maxMostSleptMinute = max($max['sleepMinute']);
$mostSleptMinute = array_search($maxMostSleptMinute, $max['sleepMinute']);

echo $max['id'] * $mostSleptMinute . "\n";
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start) . "\n";

Average run time: 0.0046

1

u/ramrunner0xff Dec 04 '18 edited Dec 04 '18

scheme

fun problem... here it is with Object Oriented LISP (or just data dispatch on lambdas mwehehe)

AOC2018 repo

(use extras vector-lib srfi-13 srfi-1)

;;stores events, loads and sort them.
(define (events)
  (let* ((ostore '())
         (ordstore '()))
    (define (rd fname)
      (set! ostore (with-input-from-file fname read-lines)))
    (define (totsecs lst) (+ (cadddr lst) (* 60 (caddr lst)) (* 1440 (cadr lst)) (* 44640 (car lst))))
    (define (sortstore) (let ((store (map (lambda (s)
                                          (cons (totsecs (map string->number (string-split (substring s 6 17) "-: ")))
                                          s)) ostore)))
                   (set! ordstore (sort store (lambda (i j) (< (car i) (car j)))))))
    (define (print)
      (format #t "store:~A~%" ordstore))
    (define (dispatch m)
      (cond ((eq? m 'read) rd)
            ((eq? m 'sort) sortstore)
            ((eq? m 'getstore) ordstore)
            ((eq? m 'print) print)))
    dispatch))

;;stores a guard association. either with name, sl/wakeup sequence lists, or with 60-minute vectors.
(define (guards)
  (let* ((gstore '())
         (slstore '())
         (minstore '()))
    (define (act num time)
      (set! gstore (alist-update num (append (alist-ref num gstore eqv? '()) (list time)) gstore)))
    (define (getstore) gstore)
    (define (getsleeps) (sort (map (lambda (e) (cons (caar e) (fold-right + 0 (cdar e)))) slstore)
                              (lambda (a b) (< (cdr a) (cdr b)))))
    (define (csleeps) (set! slstore (map (lambda (e) (alist-cons (car e) (calcdiffs (cdr e)) slstore)) gstore)))
    (define (calcsleeps) csleeps)
    (define (cminutes) (set! minstore (map (lambda (e) (alist-cons (car e) (vecmins (cdr e)) minstore)) gstore)))
    (define (dispatch m)
      (cond ((eq? m 'getstore) getstore)
            ((eq? m 'getsleeps) slstore)
            ((eq? m 'getminutes) minstore)
            ((eq? m 'calcsleeps) csleeps)
            ((eq? m 'calcminutes) cminutes)
            ((eq? m 'act) act)))
  dispatch))

(define (vector-inc! v i hm)
  (vector-set! v  i (+ hm (vector-ref v i))))

(define (incvec! v m1 m2)
  (letrec ((loop (lambda (m1 m2)
                   (if (< m1 m2)
                       (begin
                         (vector-inc! v m1 1)
                         (loop (+ m1 1) m2))))))
    (loop m1 m2)))

;;generates a vector for a guard based on his events.
(define (vecmins lst)
  (letrec* ((v (vector-unfold (lambda (x) 0) 60))
            (loop (lambda (lst) (if (not (null? lst)) (begin (incvec! v (car lst) (cadr lst)) (loop (drop lst 2)))))))
    (loop lst)
    v))
;;generates an asleep minute list for a guard based on his events
(define (calcdiffs lst)
  (if (null? lst)
      '()
      (cons (- (cadr lst) (car lst))
            (calcdiffs (drop lst 2)))))

(define (with-guards/minutes g)
  (with-guards g get-minute))

(define (with-guards/total g)
  (with-guards g get-totmin))

;;parsers from the event struct
(define (get-totmin e)
  (car e))

(define (get-minute e)
  (string->number (substring (cdr e) 15 17)))

(define (with-guards g exfunc)
  (let ((cg 0))
   (lambda (e f)
     (if (string-prefix? "Guard" (substring (cdr e) 19))
         (begin
           (set! cg (string->number (string-trim-both (substring (cdr e) 26 30))))
           (format #t "guard is ~A from ~A ~%" cg (substring (cdr e) 26 30)))
         ((g 'act) cg (exfunc e))))))

(define ev (events))
((ev 'read) "d4input")
((ev 'sort))
(define sortedev (ev 'getstore))
;;part1
(define mg0 (guards))
(fold (with-guards/total mg0) '() sortedev))
((mg0 'calcsleeps))
(define tots (mg0 'getsleeps))
;;gets the sleepiest
(map (lambda (e) (cons (caar e) (fold-right (lambda (a acc) (+ a acc)) 0 (cdar e)))) tots)
;;part1 end and p2
(define mg1 (guards))
(fold (with-guards/minutes mg1) '() sortedev)
(define p1e (mg1 'getstore))
;;assoc to the sleepiest guard and his minute
((mg1 'calcminutes))
(define mins (mg1 'getminutes))
;;gives the guard and the max same minute sleep.
(map (lambda (e) (cons (caar e) (vector-fold (lambda (a b c) (max b c)) 0 (cdar e)))) mins)

1

u/sebastiannielsen Dec 04 '18

This was my solution to Day4. Works pretty well.

#!/usr/bin/perl

open(INPUT, "aoc_4_A_input.txt");
@input = <INPUT>;
close(INPUT);

%activeguardid = ();
%guardsleep = ();
%guardsleepcount = ();

@maxdays = (0,31,28,31,30,31,30,31,31,30,31,30,31);

foreach $guardlogline (@input) {

$guardlogline =~ s/falls asleep/A-1/;
$guardlogline =~ s/wakes up/B-1/;
$guardlogline =~ s/Guard \#(\d+) begins shift/C-$1/;

$guardlogline =~ s/^\[1518-(\d\d)-(\d\d) (\d\d):(\d\d)\] (A|B|C)-(\d+)(\D*)/$1$2$3$4_$5_$6/;

if (substr($guardlogline,4,2) eq "23") {
$newdate = int(substr($guardlogline,2,2))+1;
$newmonth = int(substr($guardlogline,0,2));

if (int($newdate) > $maxdays[$newmonth]) {
$newmonth++;
$newdate = 1;
}

if (length($newdate) == 1) {
$newdate = "0".$newdate;
}
if (length($newmonth) == 1) {
$newmonth = "0".$newmonth;
}

$guardlogline = $newmonth.$newdate."0000_".substr($guardlogline,9,length($guardlogline)-9);


}


push(@niceguardlog, $guardlogline);
}

@niceguardlog = sort(@niceguardlog);
foreach $item (@niceguardlog) {
($dateandtime, $action, $guardid) = split("_", $item);

if ($action eq "C") {
$activeguardid{substr($dateandtime,0,4)} = $guardid;
}
if ($action eq "A") {
if (length($guardsleep{substr($dateandtime,0,4)}) == 0) {
$guardsleep{substr($dateandtime,0,4)} = "A"x60;
}
$minuteportion = substr($dateandtime,6,2);
$guardsleep{substr($dateandtime,0,4)} = substr($guardsleep{substr($dateandtime,0,4)},0,$minuteportion) . "B"x(60 - $minuteportion);
}
if ($action eq "B") {
if (length($guardsleep{substr($dateandtime,0,4)}) == 0) {
$guardsleep{substr($dateandtime,0,4)} = "A"x60;
}
$minuteportion = substr($dateandtime,6,2);
$guardsleep{substr($dateandtime,0,4)} = substr($guardsleep{substr($dateandtime,0,4)},0,$minuteportion) . "A"x(60 - $minuteportion);
}

}

%sleeprecords = ();
foreach $datetime (keys %activeguardid) {
$sched = $guardsleep{$datetime};
$sched =~ s/A//g;
$sleeprecords{$activeguardid{$datetime}} = $sleeprecords{$activeguardid{$datetime}} + length($sched);
}

$mostsleepyguardid = 0;
$longestsleep = 0;
foreach $guard (keys %sleeprecords) {
if ($sleeprecords{$guard} > $longestsleep) {
$longestsleep = $sleeprecords{$guard};
$mostsleepyguardid = $guard;
}
}

@sleepcount = ();
print "Most sleepy guard: $mostsleepyguardid\n";

foreach $datetime (keys %activeguardid) {
if ($activeguardid{$datetime} eq $mostsleepyguardid) {

@sched = split("", $guardsleep{$datetime});

for ($i = 0; $i < 60; $i++) {
if ($sched[$i] eq "B") {
$sleepcount[$i]++;
}
}

}
}

$mostsleepyminute = 0;
$maxsleepcounts = 0;

for ($i = 0; $i < 60; $i++) {
if ($sleepcount[$i] > $maxsleepcounts) {
$mostsleepyminute = $i;
$maxsleepcounts = $sleepcount[$i];
}
}

print "Most sleepy minute: $mostsleepyminute\n";

%sleeprecords = ();
foreach $datetime (keys %activeguardid) {
if (length($guardsleep{$datetime}) == 60) {
@sleepsched = split("",$guardsleep{$datetime});

for ($i = 0; $i < 60; $i++) {
if ($sleepsched[$i] eq "B") {
$sleeprecords{$activeguardid{$datetime}."-".$i} = int($sleeprecords{$activeguardid{$datetime}."-".$i}) + 1;
}
}

}
}

foreach $guard (keys %sleeprecords) {

$rnumber = $sleeprecords{$guard};
if (length($rnumber) < 2) {
$rnumber = "0".$rnumber;
}

push(@resultlist, $rnumber."-".$guard);

}
@resultlist = sort(@resultlist);
@resultlist = reverse(@resultlist);

($numberofsleeps, $guardid, $mostsleepyminute) = split("-",$resultlist[0]);

print "PART2: GuardID: ".$guardid."\n";
print "PART2: MostSleepyMinute: ".$mostsleepyminute."\n";

1

u/wzkx Dec 04 '18

Rust, SweetRust

use std::io::{BufRead,BufReader}; // lines() is in BufRead
use std::collections::HashMap;

fn main():
  let reader = BufReader::new( std::fs::File::open( "04.dat" ).unwrap() );
  let mut lines: Vec<String> = reader.lines().filter_map( |x| x.ok() ).collect();
  lines.sort();
  let mut m: HashMap<i32,[i32; 60]> = HashMap::new();
  let mut g = 0; // current guard
  let mut s = 0; // started sleeping
  let mut t: [i32; 60] = [0; 60];
  for l in lines:
    let l = l.replace("[1518-","").replace("-"," ").replace(":"," ").replace("]","").replace("#","");
    let dt: Vec<i32> = l[..11].split_whitespace().filter_map( |x| x.parse().ok() ).collect();
    if &l[12..17]=="Guard":
      g = l.replace(" begins shift","")[18..].parse::<i32>().unwrap();
      t = match m.get(&g) { Some(x) => *x, None => [0; 60] };
    else if &l[12..17]=="falls":
      s = dt[3];
    else:
      for i in s..dt[3]:
        t[i as usize] += 1;
      m.insert(g,t);
  // analysis
  let (mut mxg, mut mxs, mut mxn) = (0,0,0);
  for (g,v) in &m:
    let (mut s, mut maxmin, mut maxval) = (0,0,0);
    for i in 0..60:
      if v[i]>maxval:
        maxmin = i; maxval = v[i];
      s += v[i];
    if s>mxs:
      mxs = s; mxg = *g; mxn = maxmin;
  println!( "{}", mxg*(mxn as i32) ); // 8950
  let (mut mxg, mut mxv, mut mxn) = (0,0,0);
  for i in 0..60:
    let (mut maxval, mut maxgrd) = (0,0);
    for (g,v) in &m:
      if v[i]>maxval:
        maxgrd = *g; maxval = v[i];
    if maxval>mxv:
      mxv = maxval; mxg = maxgrd; mxn = i;
  println!( "{}", mxg*(mxn as i32) ); // 78452

Definitely it'll be much shorter in J. Interesting, will it be any better in R?

→ More replies (2)

1

u/mainhaxor Dec 04 '18

Haskell. I am pretty sure part1 could be done much better with groupBy and sort, but otherwise I am pretty happy with my solution.

import Control.Arrow
import Data.Char
import Data.Function
import Data.List
import qualified Data.Map as M
import Data.Maybe

parseNums :: String -> [Int]
parseNums = groupBy ((==) `on` isDigit) >>> filter (head >>> isDigit) >>> map read

orderRecords :: [String] -> [String]
orderRecords = sortBy (compare `on` (parseNums >>> take 5))

-- Map from (guard, minute) into count of times guard has been asleep at that minute
buildSleepMap :: [String] -> (Int, Int) -> M.Map (Int, Int) Int
buildSleepMap [] _ = M.empty
buildSleepMap (rec:recs) (guard, asleepSince)
  | isInfixOf "Guard" rec = buildSleepMap recs ((parseNums rec) !! 5, 0)
  | isInfixOf "asleep" rec = buildSleepMap recs (guard, (parseNums rec) !! 4)
  | isInfixOf "wakes" rec =
  let wakesUpAt = (parseNums rec) !! 4
      m = buildSleepMap recs (guard, 0)
   in foldl' (\m e -> M.alter (\c -> Just ((fromMaybe 0 c) + 1)) (guard, e) m) m [asleepSince..wakesUpAt-1]

part1 :: M.Map (Int, Int) Int -> Int
part1 m =
    let guards = map fst >>> nub $ M.keys m
        totalAsleep g = map (\min -> M.lookup (g, min) m) >>> mapMaybe id >>> sum $ [0..59]
        bestGuard = maximumBy (compare `on` totalAsleep) guards
        bestGuardSleptPerMinute = map (\min -> (min, M.lookup (bestGuard, min) m)) [0..59]
        (bestMinute, _) = maximumBy (compare `on` snd) bestGuardSleptPerMinute
     in bestGuard * bestMinute

part2 :: M.Map (Int, Int) Int -> Int
part2 m =
    let ((bestGuard, bestMinute), _) = maximumBy (compare `on` snd) $ M.toList m
     in bestGuard * bestMinute

main = do
    contents <- readFile "day4.txt"
    let records = orderRecords $ lines contents
    let sleepMap = buildSleepMap records (0, 0)
    print $ part1 sleepMap
    print $ part2 sleepMap

1

u/ephemient Dec 04 '18 edited Apr 24 '24

This space intentionally left blank.

1

u/kucao Dec 04 '18

My code works for the example input but not for my input?! I have used python 3 and the only difference is that I used .sort on it!

1

u/eshansingh Dec 04 '18 edited Dec 04 '18

Python Part 1, way more complex than really necessary but I wanted to see how nice and readable I could make it (kind of failed at that too :\) No real rules except to not use third-party libraries. I still use the standard lib, which is kind of cheating, but eh.

https://hastebin.com/makowubeva.py

2

u/zirtec Dec 04 '18

Eeeer... Yes it's all designed in functions and all but overall it's so complex readability suffer (tl;dr syndrom included). If I may suggest: drop the classes, list and dict and tuples are your friends, you write something very readable with them.

→ More replies (4)

1

u/banteg Dec 04 '18

Python 3

import re
import aoc
import pendulum
from collections import Counter

example = '''[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up'''


def parse_guards(data):
    guards = {int(x) for x in re.findall(r'#(\d+)', data)}
    slept = Counter()
    minutes = {guard: Counter() for guard in guards}
    for line in sorted(data.splitlines()):
        if 'begins shift' in line:
            ts, guard = re.search(r'\[(.*)\].*?(\d+)', line).groups()
            ts = pendulum.parse(ts)
            guard = int(guard)
        if 'falls asleep' in line:
            ts = pendulum.parse(re.search(r'\[(.*)\]', line).group(1))
        if 'wakes up' in line:
            wake_ts = pendulum.parse(re.search(r'\[(.*)\]', line).group(1))
            slept[guard] += (wake_ts - ts).total_minutes()
            for t in (wake_ts - ts).range('minutes'):
                if t == wake_ts:
                    continue
                minutes[guard][t.minute] += 1
    return guards, slept, minutes


@aoc.test({example: 240})
def part_1(data: aoc.Data):
    guards, slept, minutes = parse_guards(data)
    guard = slept.most_common()[0][0]
    minute = minutes[guard].most_common()[0][0]
    return guard * minute


@aoc.test({example: 4455})
def part_2(data: aoc.Data):
    guards, slept, minutes = parse_guards(data)
    freqs = {g: minutes[g].most_common()[0] for g in guards if minutes[g]}
    guard = max(freqs, key=lambda x: freqs[x][1])
    return guard * freqs[guard][0]

1

u/chunes Dec 04 '18

Factor

It got a bit spaghetti today.

https://pastebin.com/XFYujCmE

1

u/TheMondanaoan Dec 04 '18

Is there a possibility of getting two minutes having the most times the guard sleeps? I seem to have a guard sleeping the same number of times for minute 25 and 26.

1

u/TheVarmari Dec 04 '18

Javascript runnable in dev console (#5304/#4975)
Very very late and not the prettiest, but it runs directly in console.

const input = $('pre').textContent.split('\n');
const dateRegex = /[0-9]{4}.[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}/;
const stateRegex = /\[(\d+)-(\d+)-(\d+) (\d+):(\d+)\] (Guard #|)(\d+|wakes|falls)/;

// Sort the input
input.sort((a, b) => {
    return new Date(a.match(dateRegex)) - new Date(b.match(dateRegex));
});

// Base variables
var guards = [];
var guard;
var fell;

// Let's loop the input!!
for (let line of input) {
    if (!line) continue;
    [/*match*/, /*year*/, /*month*/, /*day*/, /*hour*/, minute, /*grouping junk*/, state] = stateRegex.exec(line);

    if (state === 'wakes') { // Wake up, iterate to increase minute counts
        for (let i = fell; i <= parseInt(minute); i++) {
            guards[guard][i]++;
        }
    } else if (state === 'falls') { // Fall asleep, store when
        fell = parseInt(minute);
    } else { // Guard change, switch over and create array if none
        guard = state;
        if (!guards[guard]) guards[guard] = new Array(60).fill(0);
    }
}

function maxMinute(arr) {
    let minute = Math.max(...arr);
    let index = arr.findIndex(v => {return v == minute; });
    return [minute, index];
}

// Most minutes slept
var most = [-1, -1];
guards.forEach((arr, id) => {
    let minutes = arr.reduce((a, b) => { return a + b; }, 0);
    [,index] = maxMinute(arr);
    if (minutes > most[1]) most = [id, minutes, index];
});

console.log(`Guard #${most[0]} slept most with a whopping ${most[1]} minutes of sleep (@ 00:${most[2]})!`);
console.log(`Part 1 answer: ${most[0]*most[2]}`);

// Bigliest minute slept
most = [-1, -1];
guards.forEach((arr, id) => {
    [minute, index] = maxMinute(arr);
    if (minute > most[1]) most = [id, minute, index];
});

console.log(`Guard #${most[0]} slept most consistently, ${most[1]} times on a singular minute (@ 00:${most[2]}) across all logged minutes.`);
console.log(`Part 2 answer: ${most[0]*most[2]}`);

1

u/mschaap Dec 04 '18

Perl 6 solution with a grammar with actions.

#!/usr/bin/env perl6
use v6.c;

#use Grammar::Tracer;

$*OUT.out-buffer = False;   # Autoflush

grammar ReposeRecord
{
    rule TOP { <entry>+ }
    rule entry { '['<yyyymmdd> <hh>':'<mm>']' [ <shift> | <sleep> | <wake> ] }

    rule shift { 'Guard #' <id> 'begins shift' }
    rule sleep { 'falls asleep' }
    rule wake { 'wakes up' }

    token yyyymmdd { \d ** 4 '-' \d ** 2 '-' \d ** 2 }
    token hh { \d ** 2 }
    token mm { \d ** 2 }
    token id { \d+ }
}

class Schedule
{
    has $.on-duty;
    has $.sleep-start;
    has %.asleep{Int};

    has Bool $.verbose = False;

    # Actions for ReposeRecord parsing
    method entry($/)
    {
        # Determine the starting minute of the timestamp.  If the time is 00:mm, this is
        # simply mm, but if it's hh:mm with hh ≥ 1, we use 0, since we only care about the
        # Midnight hour.
        my $minute = $<hh> == 0 ?? +$<mm> !! 0;

        # Shift start
        with $<shift> {
            say "$<yyyymmdd>T$<hh>:$<mm>: Shift starts for guard #$_<id>." if $!verbose;

            $!on-duty = +$_<id>;
            %!asleep{$!on-duty} //= [0 xx 60];
        }

        # Falling asleep
        with $<sleep> {
            say "$<yyyymmdd>T$<hh>:$<mm>: Guard #$!on-duty falls asleep." if $!verbose;

            $!sleep-start = $minute;
        }

        # Waking up
        with $<wake> {
            say "$<yyyymmdd>T$<hh>:$<mm>: Guard #$!on-duty wakes up." if $!verbose;

            %!asleep{$!on-duty}[$!sleep-start ..^ $minute]»++;
        }
    }

    method sleepiest-guard
    {
        %!asleep.sort(*.value.sum).tail.key;
    }

    method sleepiest-minutes
    {
        %!asleep.map({ $_.key => $_.value.maxpairs.head }).Hash;
    }
}

#| Process repose record input
multi sub MAIN(*@input, Bool :v(:$verbose) = False)
{
    my $schedule = Schedule.new(:$verbose);
    my $rr = ReposeRecord.parse(@input.sort.join("\n"), :actions($schedule)) or die "Invalid repose record input";

    my $sleepiest-minutes = $schedule.sleepiest-minutes;
    my $guard1 = $schedule.sleepiest-guard;
    my $minute1 = $sleepiest-minutes{$guard1}.key;
    say "Part 1: guard $guard1 × minute $minute1 = { $guard1 × $minute1 }";

    my $guard2 = $sleepiest-minutes.sort(*.value.value).tail.key;
    my $minute2 = $sleepiest-minutes{$guard2}.key;
    say "Part 2: guard $guard2 × minute $minute2 = { $guard2 × $minute2 }";
}

#| Process repose record input from a file
multi sub MAIN(IO() $inputfile where *.f, Bool :v(:$verbose) = False)
{
    MAIN($inputfile.lines, :$verbose);
}

#| Process default repose record file (aoc4.input)
multi sub MAIN(Bool :v(:$verbose) = False)
{
    MAIN($*PROGRAM.sibling('aoc4.input'), :$verbose);
}

1

u/sim642 Dec 04 '18

My Scala solution.

Spent some time semi-nicely organizing the record data. The annoying part was parsing the log into a format, which actually knows corresponding time ranges. Having sinned with some mutable data structures in previous days, I really wanted to do it purely.

When it comes to the two strategies, I was frustrated again because now third day in a row I feel like I want multisets, which Scala collections doesn't have. Can't say I'm happy with the strategy code. Maybe I should go through all the trouble just to implement my own multisets with all of their useful operations...

→ More replies (1)

1

u/manniL Dec 04 '18 edited Dec 04 '18

Today’s puzzle would have been a lot easier if my language supported pattern matching, currying and more functional parts.

Node.js (functional with Ramda)

REPO

const R = require('ramda')
const {readFile} = require('../utils/fs.js')
const path = require('path')

const dateFns = require('date-fns')
const splitWith = require('../utils/splitWith')

const run = async () => {
  const input = await readFile(path.join(__dirname, './input.txt'), 'utf-8')
  const formattedInput = formatInput(input)
  console.log(splitWith(a => a === 1, [1, 2, 1, 1, 1, 3]))
  console.log('Part 1:', partOne(formattedInput))
}

run()

const dateComparator = (a, b) => dateFns.compareAsc(a.date, b.date)
const transformToObject = str => {
  const [, dateString, action] = R.match(/\[(.*)] (.*)/, str)
  const date = dateFns.parse(dateString)
  return {
    date,
    action
  }
}

const partOne = input => {
  // Split by "begin shift" records
  // Result is the interval for that specific guard
  const intervals = splitWith(({action}) => isShiftAction(action), input)
  const shifts = R.pipe(
    R.pluck('action'),
    R.aperture(2),
    R.reduce(onlyShiftsWithSleep, []),
    R.map(R.match(/(\d+)/g)),
    R.unnest
  )(input)
  // Merge intervals by creating pairs and resolve them
  const sleepTimes = R.map(
    R.pipe(
      R.splitEvery(2),
      R.map(([asleep, wakeup]) => R.range(dateFns.getMinutes(asleep.date), dateFns.getMinutes(wakeup.date))),
      R.unnest
    )
  )(intervals)
  // 1:1 mapping -> zip
  const zippedPairs = R.zip(shifts, sleepTimes)
  // Merge duplicates ;)
  const dedupedPairs = R.reduce((idMap, [id, minutes]) => {
    const newValue = idMap.has(id) ? idMap.get(id).concat(minutes) : minutes
    idMap.set(id, newValue)
    return idMap
  }, new Map)(zippedPairs)

  const [id, minutesAsleep] = mostMinutesAsleep(Array.from(dedupedPairs))

  const minute = minuteMostOftenAsleep(minutesAsleep)
  return {id, minute, result: id * minute}
}

const isShiftAction = R.test(/shift/)

const mostMinutesAsleep = R.reduce(R.maxBy(([, minutes]) => minutes.length), [0, []])
const formatInput = R.pipe(R.split('\n'), R.dropLast(1), R.map(transformToObject), R.sort(dateComparator))

const minuteMostOftenAsleep = R.pipe(R.countBy(Number), R.toPairs, R.reduce(R.maxBy(([k, v]) => Number(v)), [0, 0]), R.head)

const onlyShiftsWithSleep = (shifts, [actionOne, actionTwo]) => {
  if (isShiftAction(actionOne) && !isShiftAction(actionTwo)) {
    shifts.push(actionOne)
  }
  return shifts
}

1

u/BalbinHS Dec 04 '18 edited Dec 04 '18

My solution today is so disgusting (not in a good way), I almost don't want to post it. But there are no Elixir solutions posted so far so here we go:

https://pastebin.com/raw/qE39GZmF

I'm going to tidy it up at some point. The stack_and_map function in particular: I am certain that these lists will get too big at some point, since once a guard goes on shift, they are permanently added to one of the 2 stacks and won't ever be removed, when they go on shift again another one gets added.

→ More replies (1)

1

u/RainVector Dec 04 '18

python3

import re
from operator import add
file = open("day4.txt","r")

record = dict()
for line in file:
    (time,content) = [t(s) for t,s in zip((str,str), re.search('^(\[.+\] )(.+)$',str(line)).groups())]
    if(time in record):
        print("It's not right list")
    else:
        record[time] = content
file.close()

# 第一步:获取排序之后的记录
sortedRecord = list()
outFile = open("test.txt","w")
for key in sorted(record):
    sortedRecord.append((key,record[key]))
    outFile.write(key + record[key] + '\n')
    
outFile.close()
guards = dict()
guardID = str(" ")

for rec in sortedRecord:
    time = rec[0]
    state = rec[1]
    if(state[0] is 'G'):
        guardID = re.search('^Guard.#([\d.]+) begins shift',str(state)).groups()
        if(guardID not in guards):
            onehourList = [0]*60
            guards[guardID]  = onehourList
    elif(state[0] is 'f'):
        timeBeginSleep = int(re.search('^\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\].$',str(time)).groups()[0])
        for i in range(timeBeginSleep,60):
            guards[guardID][i]  += 1
    elif(state[0] is 'w'):
        timeBeginAwake= int(re.search('^\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\].$',str(time)).groups()[0])
        for i in range(timeBeginAwake,60):
            guards[guardID][i]  -= 1

# part 1
mostlazyGruadID = -1        
mostSleepTime = 0
mostSuitHour = -1
for guard in guards:
    sleeptime = sum(guards[guard])
    if mostSleepTime < sleeptime:
        mostSleepTime  = sleeptime
        # 获取最懒guard的ID
        mostlazyGruadID = int(guard[0])
        # 获取那个重合最多
        mostSuitHour = guards[guard].index(max(guards[guard]))
        
print(int(mostlazyGruadID) * int(mostSuitHour))




# part 2
"""
找到频率最高的那个ID和次数
"""
mostmost = 0
output = 0
for guard in guards:
    index = guards[guard].index(max(guards[guard]))
    lazyguard = int(guard[0])
    result = index * lazyguard
    if mostmost < guards[guard][index]:
        output = result
        mostmost = guards[guard][index]
        
        
print(output)

1

u/morfi717 Dec 04 '18

Ruby

F = File.read("#{__dir__}/data/day4.txt")
          .scan(/.+?#(\d+)(.+?)(?:G|\Z)/m).map{ |o| o[-1] = o.last.scan(/\:(\d+)\] f.+?\:(\d+)\] w/m); [*o]}
          .group_by(&:first).map{ |k, v|
               [k.to_i, v.map{ |i| i.last }.flatten(1).map(&->a { a.map(&:to_i) }).map(&->t { (t.first..t.last-1).to_a }).flatten]
          }.reject{ |v| v.last.empty? }

puts "Ans 1: %s" % F.max_by{|o| o.last.size}.yield_self{ |k, v| k * v.group_by(&:itself).values.max_by(&:size).first }
puts "Ans 2: %s" % F.max_by{|o| o.last.group_by(&:itself).values.max_by(&:size).size }.yield_self{ |k, v| k * v.group_by(&:itself).values.max_by(&:size).first }

1

u/panda_yo Dec 04 '18

This took me longer than I'd like to admit.

Also, I found part 2 confusing, as I shall take the sum of minutes asleep over the days instead of the mean.

Anyway, here is my solution:

def extractInformation(line):
    year = int(line[1:5])
    month = int(line[6:8])
    day = int(line[9:11])
    hour = int(line[12:14])
    minute = int(line[15:17])
    information = line[19:-1].strip()
    if information[0] == "G":
        ID = information[7:-13]
        information = "shift"
    else:
        ID = None
        if information[0] == "f":
            information = "sleep"
        else:
            information = "awake"
    return({"year":year, "month":month, "day":day,
            "hour":hour, "minute":minute,
            "information":information, "ID":ID})

import pandas as pd
import numpy as np
from operator import itemgetter

file = open("input.txt", "r")
lines = file.readlines()

values = [extractInformation(line) for line in lines]
sorted_values = sorted(values,
                       key=itemgetter("year","month",
                                      "day","hour",
                                      "minute"))
for i in range(1,len(sorted_values)):
    if sorted_values[i]["ID"] is None:
        sorted_values[i]["ID"] = sorted_values[i-1]["ID"]

editedValues = []

for line in sorted_values:
    if not line["information"] == "shift":
        dictVal = {"ID":line["ID"],
                   "month":line["month"],
                   "day":line["day"]}
        if line["information"] == "sleep":
            value = 1
        else:
            value = -1
        for i in range(60):
            if i < line["minute"]:
                dictVal["{0:02d}".format(i)]=0
            else:
                dictVal["{0:02d}".format(i)]=value
        editedValues.append(dictVal)

df = pd.DataFrame(editedValues)
dfSummed = df.groupby(["ID","day","month"]).aggregate(sum)
dfIDSum = dfSummed.\
     groupby(["ID"]).aggregate(sum)
#print(dfIDSum.head(23))
dfComplete = dfIDSum.aggregate(sum, axis=1).idxmax()
print("--------------------")
print("-     Part One     -")
print("--------------------")
print(dfComplete)
print("--------------------")
print(dfIDSum.loc[dfComplete].idxmax())
print("--------------------")
print(int(dfIDSum.loc[dfComplete].idxmax())*int(dfComplete))
dfIDMean = dfSummed.\
     groupby(["ID"]).aggregate('sum')
print("--------------------")
print("-     Part Two     -")
print("--------------------")
dfIDMeanMax = dfIDMean.aggregate('max', axis=1)
print(dfIDMeanMax.idxmax())
print("--------------------")
print(dfIDMean.loc[dfIDMeanMax.idxmax()].idxmax())
print("--------------------")
print(int(dfIDMean.loc[dfIDMeanMax.idxmax()].
          idxmax())*
      int(dfIDMeanMax.idxmax()))~~~~

1

u/alexgand Dec 04 '18

Usual pandas solution:

import numpy as np
import pandas as pd
from pandas import DataFrame, Series

file = 'C:/code/advent_of_code_2018/day04data.txt'

data = pd.read_csv(file,header=None,sep='] ',names=['date','text'])
data['date'] = data['date'].apply(lambda x: x.replace('[','').replace('1518','2018'))
data['date'] = pd.to_datetime(data['date'])
data = data.sort_values(by='date')

results = DataFrame(columns=['start','end'])

for i in range(len(data.index)):
    if '#' in data['text'].iloc[i]:
        new_guard = int(data['text'].iloc[i].replace('Guard #','').replace(' begins shift',''))
    elif 'falls asleep' in data['text'].iloc[i]:
        results = results.append( DataFrame({'start':data['date'].iloc[i].minute,'end':data['date'].iloc[i+1].minute-1},index=[new_guard]) )

results['diff'] = results['end'] - results['start']
most_sleepy = (results.groupby(results.index).sum().sort_values('diff',ascending=False)).index[0]

new_results = results.loc[most_sleepy]

minutes = []

for i in range(len(new_results)):
    for minute in range(60):
        if minute >= new_results.iloc[i]['start'] and minute <= new_results.iloc[i]['end']:
            minutes.append(minute)

minutes = Series(minutes).value_counts().sort_values(ascending=False)

print('part #1 answer:',minutes.index[0] * most_sleepy)

#part 2:

most_minute = Series()

for i in range(len(results)):
    for minute in range(60):
        if minute >= results.iloc[i]['start'] and minute <= results.iloc[i]['end']:
            most_minute = most_minute.append( Series([minute],index=[results.index[i]] ) )

top = most_minute.groupby(most_minute.index).value_counts().sort_values(ascending=False)

print('part #2 answer:',top.index[0][0] * top.index[0][1])

1

u/wjholden Dec 04 '18

This was really hard (for me, at least) in Mathematica. This would have been bread-and-butter in Java or Python.

Read each line of text from input. Split the string into the date and then rest of it. We can do this the easy way, with absolute string indices, since the strings have a well-defined format. DateObject can recognize the ISO-8601 format directly. We parse the second half of the string as a 1 if the guard wakes up and 0 if the guard goes to sleep. For shift changes we match "_" (a pattern matching any expression) and get the guard number. StringCases returns an array so get the first, and only, element from this list. Finally, sort the entire thing.

day4 = Sort[
  Map[
   {DateObject[StringTake[#, {2, 17}]],
     Switch[StringTake[#, {20, Length[#] - 1}],
      "wakes up", 1,
      "falls asleep", 0,
      _, ToExpression[
       First[StringCases[StringTake[#, {20, Length[#] - 1}], 
         RegularExpression["Guard #(\\d+) begins shift"] -> "$1"]]]
      ]
     } &,
   Import["day4.txt", "List"]]]

Start with an association that will hold an array for each guard. The contents of the array represent each minute a guard might be asleep. Next iterate through the calendar, taking care not to double-count the last entry. If the second element in the calendar is greater than one then we are changing the guard and initializing the state to 1 (awake). If this guard has not been added to the sleep tracker then initialize it. If the state transitions to zero (asleep) then find the time the guard went to sleep and the time of the next element. An unstated invariant in the input is that guards always wake up before 1am. Increment the sleep counter for all minutes between the sleeping and waking times on the closed/open interval [start,stop). Sort the data structure by the total number of minutes spent sleeping. (This actually isn't a perfect statistical metric for finding the sleepiest guard; what if the guard simply worked more shifts?) Print the position (minutes 1, because Mathematica is one-indexed) of the sleepiest minutes of the sleepiest guard.

minutes = Association[];
guard = -1;
state = 0;
For[i = 1, i < Length[day4], i++,
  If[day4[[i]][[2]] > 1,
   guard = day4[[i]][[2]];
   state = 1,
   state = day4[[i]][[2]]
   ];
  If[Not[KeyExistsQ[minutes, guard]],
   minutes[guard] = ConstantArray[0, {60}]];
  If[state == 0,
   m = minutes[guard];
   start = DateValue[day4[[i]][[1]], "Minute"] + 1;
   stop = DateValue[day4[[i + 1]][[1]], "Minute"];
   m[[start ;; stop]]++;
   minutes[guard] = m
   ]
  ];
minutes = SortBy[minutes, Total]
Position[Last[minutes], Max[Last[minutes]]] - 1

I didn't figure out a good way to get the key of the last element in a sorted association so at this point I read the output and multiply manually. Finding the second part is easy now.

sleepiestMinute = Max[Flatten[Values[minutes]]]
Select[minutes, MemberQ[#, sleepiestMinute] &]
(Position[minutes[269], sleepiestMinute] - 1) 269

1

u/Tarmen Dec 04 '18 edited Dec 04 '18

Haskell

Took ages on this one. Not because I was confused about what to do but because I stumbled about unnecessary problems. At first I used foldMap for byMinute because I assumed it'd lift the semigroup instance instead of being left biased, for instance.

{-# OPTIONS_GHC -fdefer-type-errors #-}
{-# LANGUAGE RecordWildCards #-}
{-# Language TupleSections #-}
module Foo where
import qualified Data.Map as M
import Text.Megaparsec as P
import Data.Ord
import Text.Megaparsec.Char
import Data.List
import Data.Void
import Data.Char
import Data.Monoid

main :: IO ()
main = do
    content <- readFile "4.txt"
    let e = parseAll content
    let g = fmap byMinute $ groupByGuard e
    let (guard, mins) = bestest (sum . M.elems) g
    let (min, amount) = bestest id mins
    print (guard * min)
    let bestMins = fmap (bestest id) g
        (guard, (min, amount)) = bestest snd bestMins
    print (guard * min)

groupByGuard = M.fromListWith (++) . map (\e -> (guard e, [e]))
byMinute :: [Event] -> M.Map Int (Sum Int)
byMinute  = M.fromListWith (+) . concatMap (\Event{..}-> map (,Sum 1) [begin..end-1])

bestest :: (Ord o) => (b -> o) -> M.Map a b -> (a,b)
bestest f = maximumBy (comparing (f . snd)) . M.toList

parseEvent :: Parser [Event]
parseEvent = do
    parseTimestamp
    word "Guard #"
    guard <- parseInt
    word "begins shift"
    many $ try $ do
        begin <- parseTimestamp
        word "falls asleep"
        end <- parseTimestamp
        word "wakes up"
        return $ Event{..}
parseAll :: String -> [Event]
parseAll ls = case runParser (many parseEvent) "" ls  of
    Right x -> concat x
    Left err -> error (parseErrorPretty err)
parseInt :: Parser Int
parseInt = (read <$> takeWhile1P Nothing isDigit) <* many (satisfy isSpace)
type Parser = Parsec Void String
parseTimestamp :: Parser Int
parseTimestamp = do
    char' '['
    parseInt
    char' '-'
    parseInt
    char' '-'
    parseInt
    parseInt
    char' ':'
    r <- parseInt
    char' ']'
    skip
    return r
data Event = Event {guard :: Int, begin :: Int, end :: Int}
  deriving Show
word ls = string ls <* skip
skip = many (satisfy isSpace)

1

u/orgodemir Dec 04 '18 edited Dec 04 '18

Python 3

from functools import partial

def Input(day):
    return open('input{}.txt'.format(day))
inp = sorted(Input(4).read()[:-1].split('\n'))

def parse(line):
    return re.match(r"\[(\d\d\d\d-\d\d-\d\d) (\d\d):(\d\d)] (Guard #(\d+))?(falls asleep)?(wakes up)?", line).groups()

midnight_array = partial(np.zeros, shape=(60))
guards = defaultdict(midnight_array)
for line in inp:
    dt,hr,mn,g,gid,sleeps,wakes = parse(line)
    if g: cur_gid=int(gid)
    if hr=='00' and wakes:  guards[cur_gid][int(mn):] -= 1
    if hr=='00' and sleeps: guards[cur_gid][int(mn):] += 1

# part 1
gid = sorted(guards, key=lambda x: guards[x].sum(), reverse=True)[0]
print(gid * guards[gid].argmax())

# part 2
gid = sorted(guards, key=lambda x: max(guards[x]), reverse=True)[0]
print(gid * guards[gid].argmax())  

1

u/ttapu Dec 04 '18

input parsing was a challenge for me

import re
from collections import Counter
with open("4.input", "r") as allomany:
    inp=sorted(allomany.read().splitlines())
    clear=dict()
    for x in inp:
        if 'Guard' in x:
            y=re.findall(r'#\d*', x)[0]
            if y not in clear:
                clear[y]=[]
        else:
            _,z,sleep = re.split(r':|]', x)
            clear[y].append(int(z))

total_time=dict()
minutes=dict()
#collecting values into these 2 dicts
for k,v in clear.items():
    t=0
    minutes[k]=[]
    for i,e in enumerate(v):
        if i%2:
            t+=e-v[i-1]
            mins=(m for m in range(e-1,v[i-1]-1,-1))
            minutes[k].extend(mins)

    total_time[k] =t

Sleepy=max(total_time, key=total_time.get)
a=int(Sleepy[1:])
b=Counter(minutes[Sleepy]).most_common(1)[0][0]
print(a,'*',b,'=', a*b)

#part2
part2=(0,0,0)
for k,v in minutes.items():
    if len(v):
        mc=Counter(v).most_common(1)
        if mc[0][1]>part2[2]:
            part2=(k, mc[0][0], mc[0][1])
print(part2, '=>',int(part2[0][1:])*part2[1])

1

u/andrew_4ever Dec 04 '18 edited Dec 04 '18

Python 3

How to sort dates fast:

import datetime
data = [x.strip() for x in open('./puzzleInput.txt', 'r').readlines()]
dates = [datetime.datetime.strptime(ts[1:17], '%Y-%m-%d %H:%M') for ts in data]
dates.sort()

5

u/c17r Dec 04 '18

You didn't need to do that. By nature of the ISO 8601 datetime format, it will sort naturally just as strings: data = open('./puzzleInput.txt').read().splitlines().sort()

1

u/meepys Dec 04 '18

My Kotlin Day4: Bitbucket

Still learning, comments welcome!

class Day4(rawInput: List<String>) : Day(rawInput) {

    val guards = mutableMapOf<Int, IntArray>()

    val parseID = """Guard #(\d+) """.toRegex()
    val parseSleeps = """00:(\d\d)] falls""".toRegex()
    val parseWakes = """00:(\d\d)] wakes""".toRegex()

    override fun part1() {
        var currentID = -1
        var sleepTime = -1

        for (line in rawInput.sorted()) {
            val foundID = parseID.find(line)
            if (foundID != null) {
                currentID = foundID.groupValues[1].toInt()
                if (guards[currentID] == null)
                    guards[currentID] = IntArray(60)
            }

            val foundSleeps = parseSleeps.find(line)
            if (foundSleeps != null) {
                sleepTime = foundSleeps.groupValues[1].toInt()
            }

            val foundWakes = parseWakes.find(line)
            if (foundWakes != null) {
                val wakeTime = foundWakes.groupValues[1].toInt()
                val guardArray = guards[currentID]!!
                for (i in sleepTime..wakeTime) {
                    guardArray[i] += 1
                }
            }
        }

        val (maxID, maxTimes) = guards.maxBy { (id, times) ->
            times.sum()
        }!!

        val maxMinute = maxTimes.max()!!

        for (i in 0 until maxTimes.size) { // NOTE: multiple minutes match
            if (maxTimes[i] == maxMinute)
                println("part1: $maxID * $i= ${maxID * i}")
        }
    }

    override fun part2() {
        val (bestGuard, bestTime) = guards.map { (id, times) ->
            id to times.max()!!
        }.maxBy { (id, maxTime) ->
            maxTime
        }!!

        val bestIndex = guards[bestGuard]!!.indexOf(bestTime)
        println("part2: $bestGuard * $bestIndex = ${bestGuard * bestIndex}")
    }
}

1

u/AbdussamiT Dec 04 '18

I’m not a coding juggernaut so instead of writing pythonic code to sort the times chronologically I put them in an Google Sheet, sorted them and then put them back into my input.txt file, heh.

2

u/21_cribbage Dec 04 '18

You could read them into a list and then just call list.sort() - this date structure sorts chronologically :)

→ More replies (1)

1

u/adrian17 Dec 04 '18

Rust, both parts. Would appreciate tips on simplifying the code.

use std::fs;
use std::collections::HashMap;
use regex::Regex;

fn read_input() -> Vec<String> {
    let contents = fs::read_to_string("input.txt").expect("File read error");
    contents.lines().map(|x| x.to_string()).collect()
}

fn main() {
    let mut lines = read_input();
    lines.sort();

    let pattern = Regex::new(r"\[.+:(\d+)\] (.+)").unwrap();

    let mut person = -1;
    let mut start_minute = -1;
    let mut people: HashMap<i32, Vec<_>> = HashMap::new();

    for line in lines {
        let captures = pattern.captures(&line).unwrap();
        let minute: i32 = captures[1].parse().unwrap();
        let text = captures[2].to_string();

        match text.as_ref() {
            "falls asleep" => start_minute = minute,
            "wakes up" => people.entry(person).or_default().push((start_minute, minute)),
            _ => {
                let parts: Vec<_> = text.split_whitespace().collect();
                person = parts[1].trim_start_matches('#').parse().unwrap();
            }
        }
    }

    let (best_person_id, best_person_periods) = people.iter().max_by_key(|(_, periods)| -> i32 {
        periods.iter().map(|(start, end)| end-start).sum()
    }).unwrap();

    let mut counts: HashMap<i32, i32> = HashMap::new();
    for &(start, end) in best_person_periods {
        for minute in start..end {
            *counts.entry(minute).or_default() += 1;
        }
    }
    let (best_minute, _) = counts.iter().max_by_key(|&(_, value)| value).unwrap();
    println!("{}", best_person_id * best_minute);

    let mut counts: HashMap<(i32, i32), i32> = HashMap::new();
    for (person, periods) in people {
        for (start, end) in periods {
            for minute in start..end {
                *counts.entry((person, minute)).or_default() += 1;
            }
        }
    }
    let ((best_person, best_minute), _) = counts.iter().max_by_key(|&(_, value)| value).unwrap();
    println!("{}", best_person * best_minute);
}

1

u/[deleted] Dec 04 '18

Go/Golang

My second year doing AoC, I am doing it in go again this year.

The rest of my solutions: Github

If anyone has any suggestions for ways I can improve it is much appreciated.

Part 1

// part1 calculates the most common minute asleep * guard ID with the most time slept
func part1(logs []actionLog) int {
	// Calcluate the numer of minutes slept for each guard
	noOfMinutesSlept := make(map[int]int)
	for i := 0; i < len(logs); i++ {
		if _, ok := noOfMinutesSlept[logs[i].id]; ok {
			if logs[i].activity == 2 {
				// time awake - time asleep
				noOfMinutesSlept[logs[i].id] += int(logs[i].timeStamp.Sub(logs[i-1].timeStamp).Minutes())
			}
		} else {
			noOfMinutesSlept[logs[i].id] = 0
		}
	}

	// Find the guard Id with the most minuites slept
	var hSleepDuration, hDurationGuardID int
	for currentID, currentMinutesSlept := range noOfMinutesSlept {
		if currentMinutesSlept > hSleepDuration {
			hDurationGuardID = currentID
			hSleepDuration = currentMinutesSlept
		}
	}

	// Fill out sleepSchedule with the frequency that
	// that minute the guard is asleep in
	var sleepSchedule [59]int
	var lastAsleep int
	for i := 0; i < len(logs); i++ {
		// Filter for corrrect guard
		if logs[i].id == hDurationGuardID {
			// Check if activity is wake up or fell asleep
			if logs[i].activity == 1 {
				lastAsleep = logs[i].timeStamp.Minute()
			} else if logs[i].activity == 2 {
				for j := lastAsleep; j < logs[i].timeStamp.Minute(); j++ {
					sleepSchedule[j]++
				}
			}
		}
	}

	// Find most frequent minute
	var mostFrequentMin, hFrequency int
	for i := 0; i < len(sleepSchedule); i++ {
		if sleepSchedule[i] > hFrequency {
			hFrequency = sleepSchedule[i]
			mostFrequentMin = i
		}
	}

	return hDurationGuardID * mostFrequentMin
}

Part 2

// part2 calculates the most common minute asleep * guard ID
func part2(logs []actionLog) int {
	// Init noOfMinutesSlept with keys as guard ids and empty sleep schedules
	noOfMinutesSlept := make(map[int][]int)
	for i := 0; i < len(logs); i++ {
		if _, ok := noOfMinutesSlept[logs[i].id]; !ok {
			noOfMinutesSlept[logs[i].id] = make([]int, 59)
		}
	}

	// Fill in all guards sleeping schedule
	for currentGuardID, currentSleepSchedule := range noOfMinutesSlept {
		var lastAsleep int
		for i := 0; i < len(logs); i++ {
			// Filter for correct guard
			if logs[i].id == currentGuardID {
				// Check if activity is wake up or fell asleep
				if logs[i].activity == 1 {
					lastAsleep = logs[i].timeStamp.Minute()
				} else if logs[i].activity == 2 {
					for j := lastAsleep; j < logs[i].timeStamp.Minute(); j++ {
						currentSleepSchedule[j]++
					}
				}
			}
		}
	}

	// Find most frequent minute for any guard
	var mostFrequentMin, hFrequency, hGuardID int
	for currentGuardID, currentSleepSchedule := range noOfMinutesSlept {
		for i := 0; i < len(currentSleepSchedule); i++ {
			if currentSleepSchedule[i] > hFrequency {
				hFrequency = currentSleepSchedule[i]
				mostFrequentMin = i
				hGuardID = currentGuardID
			}
		}
	}

	return hGuardID * mostFrequentMin
}

Other Stuff

func parseInput(input string) (logs []actionLog) {
	lines := strings.Split(input, "\n")

	for i := 0; i < len(lines)-1; i++ {
		var y, m, d, h, min int
		var desc string
		var currentLog actionLog

		// [1518-11-03 00:05] Guard #10 begins shift
		// [1518-11-01 00:05] falls asleep
		// [1518-11-01 00:25] wakes up

		if !strings.Contains(lines[i], "#") {
			_, err := fmt.Sscanf(lines[i], "[%d-%d-%d %d:%d] %s",
				&y, &m, &d, &h, &min,
				&desc)
			checkError(err)
			currentLog.id = -1
		} else {
			_, err := fmt.Sscanf(lines[i], "[%d-%d-%d %d:%d] %s #%d",
				&y, &m, &d, &h, &min,
				&desc, &currentLog.id)
			checkError(err)
		}

		currentLog.timeStamp = time.Date(y, time.Month(m), d, h, min, 0, 0, time.UTC)

		if desc == "Guard" {
			currentLog.activity = 0
		} else if desc == "wakes" {
			currentLog.activity = 2
		} else {
			currentLog.activity = 1
		}
		logs = append(logs, currentLog)
	}

	sort.Slice(logs, func(i, j int) bool { return logs[i].timeStamp.Before(logs[j].timeStamp) })

	for i := 0; i < len(logs); i++ {
		if logs[i].id == -1 {
			logs[i].id = logs[i-1].id
		}
		// fmt.Printf("%v : %v %v\n", logs[i].timeStamp, logs[i].command, logs[i].id)
	}
	return
}

1

u/chicagocode Dec 04 '18

Kotlin - [Blog/Commentary] | [GitHub Repo]

This one was interesting. I had a pretty ugly solution before stopping and re-thinking my assumptions!

class Day04(rawInput: List<String>) {

    private val sleepMinutesPerGuard: Map<Int, List<Int>> = parseInput(rawInput)

    fun solvePart1(): Int =
        sleepMinutesPerGuard
            .maxBy { it.value.size }!!
            .run { key * value.mostFrequent()!! }

    fun solvePart2(): Int =
        sleepMinutesPerGuard.flatMap { entry ->
            entry.value.map { minute ->
                entry.key to minute // Guard to Minute
            }
        }
            .mostFrequent()!! // Which guard slept the most in a minute?
            .run { first * second }

    private fun parseInput(input: List<String>): Map<Int, List<Int>> {
        val sleeps = mutableMapOf<Int, List<Int>>()
        var guard = 0
        var sleepStart = 0

        input.sorted().forEach { row ->
            when {
                row.contains("Guard") -> guard = guardPattern.single(row).toInt()
                row.contains("asleep") -> sleepStart = timePattern.single(row).toInt()
                else -> {
                    val sleepMins = (sleepStart until timePattern.single(row).toInt()).toList()
                    sleeps.merge(guard, sleepMins) { a, b -> a + b }
                }
            }
        }
        return sleeps
    }

    companion object {
        private val guardPattern = """^.+ #(\d+) .+$""".toRegex()
        private val timePattern = """^\[.+:(\d\d)] .+$""".toRegex()
    }

    private fun Regex.single(from: String): String =
        this.find(from)!!.destructured.component1()
}

fun <T> Iterable<T>.mostFrequent(): T? =
    this.groupBy { it }.maxBy { it.value.size }?.key

1

u/[deleted] Dec 04 '18

Python3 - Github

Won't post here because the solution ended up being way too long. Probably spent too long figuring out the parser like many others. Probably could have just sorted the lines as they were, instead of splitting them into object with a timestamp and event.

1

u/rolandtritsch Dec 04 '18

ETA (Haskell on the JVM)

https://github.com/rolandtritsch/haskell-aoc-2018

Yep. Today was the most tricky day yet. Spend some time on finding a good way to process the input.

And then some time on finding the key aggregations (number of minutes per minute per guard).

If you think about it long enough it becomes easier :).

1

u/UncoolJohn Dec 04 '18

c#. I always feel like my code is so messy.

      static string Day4Part1()
        {
            var filename = inputDir + "4-1.txt";
            var reader = ReadAsLines(filename);

            var data = new DataTable();

            data.Columns.Add("Timestamp");
            data.Columns.Add("guard");
            data.Columns.Add("action");

            foreach (var record in reader)
                data.Rows.Add(record.Split('\t'));

            DataView dv = data.DefaultView;
            dv.Sort = "Timestamp ASC";
            DataTable sortedDT = dv.ToTable();

            Dictionary<string, List<int>> sleepdata = new Dictionary<string, List<int>>();

            string currentguard = string.Empty;
            int sleepminute = -1;
            int wakeminute = -1;
            foreach (DataRow r in sortedDT.Rows)
            {
                if (r[1].ToString() != string.Empty)
                {
                    currentguard = r[1].ToString();
                    if (!sleepdata.Keys.Contains(currentguard))
                    {
                        List<int> minutes = new List<int>();
                        for (int i = 0; i < 60; i++)
                        {
                            minutes.Add(0);
                        }
                        sleepdata.Add(currentguard, minutes);
                    }
                    continue;
                }
                else
                {
                    if (r[2].ToString() == "falls asleep")
                    {
                        sleepminute = Convert.ToInt32(r[0].ToString().Substring(15, 2));
                    }
                    else
                    {
                        wakeminute = Convert.ToInt32(r[0].ToString().Substring(15, 2));
                    }
                }

                if (sleepminute > -1 && wakeminute > -1)
                {
                    for (int i = sleepminute; i < wakeminute; i++)
                    {
                        sleepdata[currentguard][i]++;
                    }
                    sleepminute = -1; wakeminute = -1;
                }
            }

            Dictionary<string, int> GuardSleeps = new Dictionary<string, int>();
            foreach (string s in sleepdata.Keys)
                GuardSleeps.Add(s, sleepdata[s].Sum());

            string guard = GuardSleeps.OrderByDescending(s => s.Value).ToList()[0].Key;

            List<int> L = sleepdata[guard];
            int max = L.OrderByDescending(s => s).ToList()[0];
            int index = L.FindIndex(a => a == max);

            return guard + " - minute " + index.ToString();
        }

in the second part I just changed the last section:

            Dictionary<string, int> GuardSleeps = new Dictionary<string, int>();
            foreach (string s in sleepdata.Keys)
            {
                List<int> L = sleepdata[s];
                int max = L.OrderByDescending(a => a).ToList()[0];
                GuardSleeps.Add(s, max);
            }

            string guard = GuardSleeps.OrderByDescending(s => s.Value).ToList()[0].Key;
            int index = sleepdata[guard].FindIndex(a => a == GuardSleeps[guard]);
            return guard + " - minute " + index.ToString();

1

u/Chrinkus Dec 04 '18

C++

Mine is too long to post here. GitHub. Today's new things were juggling std::pair's and the std::max_element algorithm.

1

u/ElliPirelly Dec 04 '18

I used Python and a 2D numpy array to solve it:

Maybe a bit complicated but I'm still proud because I'm fairly new to Python and numpy :)

import numpy as np
import parse

guard_id = '''[{year:d}-{month:d}-{day:d} {hour:d}:{minute:d}] {word1} {id} {rest}\n'''
guard_other = '''[{year:d}-{month:d}-{day:d} {hour:d}:{minute:d}] {word1} {word2}\n'''
input =  open('Input.txt')
input = input.readlines()
input.sort()
file = open('Sorted.txt', 'w')
file.writelines(input)
all_id = []
for i in range(len(input)):
    r = parse.parse(guard_id, input[i])
    if(r is None):
        r = parse.parse(guard_other, input[i])
    if(r['word1'] == "Guard"):
        if(r['id'] not in all_id):
            all_id.append(r['id'])
all_id.sort()

a = np.zeros(shape=(60, len(all_id)))
for i in range(len(input)):
    r = parse.parse(guard_id, input[i])
    if(r is None):
        r = parse.parse(guard_other, input[i])
    if(r['word1']=="Guard"):
        id = r['id']
    if(r['word1']=="falls"):
        t_start = int(r['minute'])
    if(r['word1']=="wakes"):
       a[t_start:r['minute'], all_id.index(id)] += 1

time_per_guard = np.sum(a, axis=0)
most_asleep_id = all_id[np.argmax(time_per_guard)]
minute_most_asleep = np.argmax(a[:, np.argmax(time_per_guard)])
print(most_asleep_id, minute_most_asleep)
#------Part2-------
minute_most_asleep, index_id = np.unravel_index(a.argmax(), a.shape)
print(all_id[index_id], minute_most_asleep)

1

u/Ryuujinx Dec 04 '18

I am not proud of this, but hey it's day4 of me even using Go so I guess I'm glad I got the solution in the end.

Even if it's a bunch of spaghetti to get there.

Part1:

package main

import (
	"fmt"
	"os"
	"sort"
	"bufio"
	"strings"
	"time"
	"regexp"
	"strconv"
)

func main() {


	f, err := os.Open("input")
	if err != nil {
		fmt.Print(err)
	}
	data := make([]string,0)
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		data = append(data, scanner.Text())
	}

	sort.Strings(data)

	guard_key := make(map[string][]string,0)

	var t1 string
	var t2 string
	var current_gid string
	tr,_ := regexp.Compile("\\[.*\\]")
	cg,_ := regexp.Compile("Guard #.+ begins")
	woker,_ := regexp.Compile("wakes up")
	for _,l := range data {
		t2 = t1
		t1 = tr.FindString(l)
		t1 = t1[1:len(t1)-1]
		gid := cg.FindString(l)
		gid2 := strings.Split(gid, " ")
		if  len(gid2) > 1  {
			if len(guard_key[gid2[1][1:]]) <1 {
				guard_key[gid2[1][1:]] = append(guard_key[gid2[1][1:]] ,"")
			}
			current_gid = gid2[1][1:]
			continue
		}


		if woker.MatchString(l) {
			guard_key[current_gid] = append(guard_key[current_gid],t2)
			guard_key[current_gid] = append(guard_key[current_gid],t1)
		}


	}

	var t1parsed time.Time
	var t2parsed time.Time
	var zero_dur time.Duration
	layout := "2006-01-02 15:04"
	guard_sleep := make(map[string]time.Duration)
	for gid,guard := range guard_key {
		guard_sleep[gid] = zero_dur
		for k,timestamp := range guard {
			if int(k) % 2 == 0 && int(k) > 0{
				t2parsed,_ = time.Parse(layout, timestamp)
				guard_sleep[gid] = guard_sleep[gid] + t2parsed.Sub(t1parsed)
			} else {
				t1parsed,_ = time.Parse(layout, timestamp)
			}
		}
	}

	var sleepyguard string
	for gid,_ := range guard_sleep {
		if sleepyguard == "" {
			sleepyguard = gid
		} else {
			if guard_sleep[gid] > guard_sleep[sleepyguard] {
				sleepyguard = gid
			}
		}
	}


	minutes := make([]int,60)
	for i := 0; i < 60; i++ {
		for k, _ := range guard_key[sleepyguard] {
			if k > 0 && k % 2 == 0 {
				st1,_ := strconv.Atoi(guard_key[sleepyguard][k-1][len(guard_key[sleepyguard][k-1])-2:])
				st2,_ := strconv.Atoi(guard_key[sleepyguard][k][len(guard_key[sleepyguard][k])-2:])
				if i >= st1 && i < st2 {
					minutes[i] = minutes[i] + 1
				}
			}
		}
	}

	var mostslept int
	for k,m := range minutes {
		if k == 0 {
			mostslept = k
		} else {
			if m > minutes[mostslept] {
				mostslept = k
			}
		}
	}

	sleepyguardi,_ := strconv.Atoi(sleepyguard)
	fmt.Println("Asshole sleepy guard ID is:", sleepyguardi)
	fmt.Println("He slept the most on minute:", mostslept)
	fmt.Println("The solution is:", sleepyguardi * mostslept)
}

part2:

package main

import (
	"fmt"
	"os"
	"sort"
	"bufio"
	"strings"
	"regexp"
	"strconv"
)

func main() {


	f, err := os.Open("input")
	if err != nil {
		fmt.Print(err)
	}
	data := make([]string,0)
	scanner := bufio.NewScanner(f)
	for scanner.Scan() {
		data = append(data, scanner.Text())
	}

	sort.Strings(data)

	guard_key := make(map[string][]string,0)

	var t1 string
	var t2 string
	var current_gid string
	tr,_ := regexp.Compile("\\[.*\\]")
	cg,_ := regexp.Compile("Guard #.+ begins")
	woker,_ := regexp.Compile("wakes up")
	for _,l := range data {
		t2 = t1
		t1 = tr.FindString(l)
		t1 = t1[1:len(t1)-1]
		gid := cg.FindString(l)
		gid2 := strings.Split(gid, " ")
		if  len(gid2) > 1  {
			if len(guard_key[gid2[1][1:]]) <1 {
				guard_key[gid2[1][1:]] = append(guard_key[gid2[1][1:]] ,"")
			}
			current_gid = gid2[1][1:]
			continue
		}
		if woker.MatchString(l) {
			guard_key[current_gid] = append(guard_key[current_gid],t2)
			guard_key[current_gid] = append(guard_key[current_gid],t1)
		}
	}

	minutes := make(map[string][]int,60)
	for gid,_ := range guard_key {
		for init := 0; init < 60; init++ {
			minutes[gid] = append(minutes[gid],0)
		}
	}
	for i := 0; i < 60; i++ {
		for gid,_ := range guard_key {
			for k,_ := range guard_key[gid] {
				if k > 0 && k % 2 == 0 {
					st1,_ := strconv.Atoi(guard_key[gid][k-1][len(guard_key[gid][k-1])-2:])
					st2,_ := strconv.Atoi(guard_key[gid][k][len(guard_key[gid][k])-2:])
					if i >= st1 && i < st2 {
						minutes[gid][i] = minutes[gid][i] + 1
					}
				}
			}
		}
	}


	var mostslept string
	var minuteslept int
	var sleepcounter int
	for i,_ := range minutes {
		for k,m := range minutes[i] {
			if m > sleepcounter {
				minuteslept = k
				mostslept = i
				sleepcounter = m
			}
		}
	}

	sleepyguardi,_ := strconv.Atoi(mostslept)
	fmt.Println("Asshole sleepy guard ID is:", sleepyguardi)
	fmt.Println("He slept the most of everyone on minute:", minuteslept)
	fmt.Println("The solution is:", sleepyguardi * minuteslept)
}

1

u/Markavian Dec 04 '18 edited Dec 04 '18

Over engineered node.js solution for Day 4 - I got badly tripped up by not realising that guards could fall asleep and wake up multiple times in a night.

https://github.com/johnbeech/advent-of-code-2018/blob/master/solutions/day4/solution.js

However, regex parsing for the win. I've become much more confident from previous years at quickly mapping the input to JSON structures, which for me personally makes life much easier.

Also it means I can link to some of the intermediary data structures used in my solution, such as:- Guards: https://github.com/johnbeech/advent-of-code-2018/blob/master/solutions/day4/guards.json- Sleeping guards: https://github.com/johnbeech/advent-of-code-2018/blob/master/solutions/day4/sleeping-guards.json- Sleepiest minute guards: https://github.com/johnbeech/advent-of-code-2018/blob/master/solutions/day4/sleepiest-minute-guards.json

If I get round to visualisation, these JSON files will make it super easy to render using HTML/CSS/JS.

Look at this trooper; would not want to break in on #1381's shift:

{
  "guardId": 1381,
  "days": {
    "1518-06-03": {
      "events": [],
      "minutesAsleep": 0,
      "beganShift": "00:00"
    },
    "1518-07-28": {
      "events": [],
      "minutesAsleep": 0,
      "beganShift": "00:00"
    },
    "1518-11-10": {
      "events": [],
      "minutesAsleep": 0,
      "beganShift": "23:58"
    }
  }
}

1

u/purplemonkeymad Dec 04 '18

Powershell

Since there was a 'small' data set I was not too worried about having a lot of iterations over the data. I wasn't trying for speed so I went with making each part simple. Parse Data, Sort Data, Fill data, Process Data.

I used a switch when I was still thinking of it as a full FSM and I had more to do, but they could now be a couple of ifs.

[CmdletBinding()]
Param(
    [parameter(ValueFromPipeline)]
    $Scribbles
)

begin {

    if (-not $Scribbles) {
        $Scribbles = gc .\input.txt
    }
    if (-not $Scribbles) {
        Write-Error "No input"
        return
    }
    $Scribbles = $Scribbles -split "(,|`n)"
    $AllScribbles = [System.Collections.Generic.List[object]]@()
}
process {
    $Scribbles | %{
        [void]( $_ -match '\[(?<date>.*?)\]\s*(Guard #(?<guardnumber>\d+)\s*|)(?<Action>(falls|wakes|begin))');
        [void]$AllScribbles.Add( 
            [PSCustomObject]@{
                Date = Get-date $matches.date
                GuardNumber = $matches.GuardNumber
                Action = $matches.Action
            }
        )
    }    
}
end {
    $AllScribbles = $AllScribbles | sort Date
    $current = -1
    $AllScribbles | %{ 
        $item = $_
        switch ($item.Action) {
            "begin" { 
                $current = $item.GuardNumber
            }
            Default {
                $item.GuardNumber = $current
            }
        }
    }
    ## build time table
    $guardtimes = @{}
    $guardtotal = @{}
    # fsm
    $state = 'awake'
    $lasttime = get-date 0
    $AllScribbles | %{
        $item = $_
        switch ($state) {
            'awake' { 
                switch ($item.Action) {
                    "falls" { 
                        $lasttime = $item.date
                        $state = 'asleep'
                    }
                }
            }
            'asleep' { 
                switch ($item.Action) {
                    "wakes" { 
                        for ($time= $lasttime; $time -lt $item.date; $time = $time.AddMinutes(1) ) {
                            if ($time.Hour -eq 00){
                                $guardtimes[$item.guardnumber+(',{0}' -f $time.Minute)]++
                                $guardtotal[$item.guardnumber]++
                            }
                        }
                        $state = 'awake'
                    }
                }
            }
        }
    }
    $top = $guardtotal.GetEnumerator() | sort -Descending -Property Value | select -First 1
    Write-Host -ForegroundColor Green ('Top Sleep Guard {0} for {1} minutes.' -f $top.name,$top.Value)
    $toptime = $guardtimes.GetEnumerator() | ? name -like "$($top.name),*" | sort -Descending -Property Value | select -First 1
    Write-Host -ForegroundColor Green ('Top Sleep time: {0} for {1} times.' -f $toptime.name,$toptime.Value)
    $toptime.name -split ',' -join '*' | iex
    $bestminute = $guardtimes.GetEnumerator() | sort -Descending -Property value | select -first 1
    Write-Host -ForegroundColor Green ('Best minute: {0} for {1} times.' -f $bestminute.name,$bestminute.Value)
    $bestminute.name -split ',' -join '*' | iex
}

1

u/paveldurmanov Dec 04 '18

Here is mine solution Github

1

u/jkmonger Dec 04 '18

Javascript

The first one I've found challenging this year, this took me around 20 minutes.

const InstructionType = {
    NEW_GUARD: 1,
    FALL_ASLEEP: 2,
    WAKE_UP: 3
};

const mapLineToInstruction = (line) => {
    const extracted = /\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] (.+)/.exec(line);
    const minute = parseInt(extracted[1]);
    const message = extracted[2];

    if (message === "falls asleep") {
        return {
            type: InstructionType.FALL_ASLEEP,
            minute: minute
        };
    }

    if (message === "wakes up") {
        return {
            type: InstructionType.WAKE_UP,
            minute: minute
        };
    }

    const guardInfo = /Guard #(\d+) begins shift/.exec(message);

    return {
        type: InstructionType.NEW_GUARD,
        minute: minute,
        guardId: parseInt(guardInfo[1])
    };
};

const getInstructions = (input) => {
    const lines = input.split("\n");
    lines.sort();

    return lines.map(mapLineToInstruction);
};

const parseTimesheets = (input) => {
    let currentGuard = null;
    let sleepTime = null;
    const timesheets = {};

    for (const instruction of getInstructions(input)) {
        if (instruction.type === InstructionType.NEW_GUARD) {
            currentGuard = instruction.guardId;
            sleepTime = null;

            if (timesheets[currentGuard] === undefined) {
                timesheets[currentGuard] = {
                    guardId: currentGuard,
                    total: 0,
                    log: []
                };
            }

            continue;
        }

        if (instruction.type === InstructionType.FALL_ASLEEP) {
            sleepTime = instruction.minute;
            continue;
        }

        const guardTimesheet = timesheets[currentGuard];

        for (let i = sleepTime; i < instruction.minute; i++) {
            guardTimesheet.log[i] = (guardTimesheet.log[i] || 0) + 1;
            guardTimesheet.total++;
        }
    }

    return Object.values(timesheets);
}

const getSleepiestMinute = (log) => {
    // remove all empty minutes
    const sleptMinutes = log.filter(x => x !== undefined);
    const highestIncidence = Math.max(...sleptMinutes);

    return log.indexOf(highestIncidence);
};

const partOne = (input) => {
    const sleepiestGuard = parseTimesheets(input).sort((a, b) => b.total - a.total)[0];

    return sleepiestGuard.guardId * getSleepiestMinute(sleepiestGuard.log);
};

const partTwo = (input) => {
    const timesheets = parseTimesheets(input);

    let highestAmount = 0;
    let highestGuard = null;
    let highestMinute = null;

    for (const timesheet of timesheets) {
        const sleepiest = getSleepiestMinute(timesheet.log);
        const value = timesheet.log[sleepiest];

        if (value > highestAmount) {
            highestAmount = value;
            highestGuard = timesheet.guardId;
            highestMinute = sleepiest;
        }
    }

    return highestGuard * highestMinute;
}

module.exports = {
    partOne: partOne,
    partTwo: partTwo
};

1

u/jonathrg Dec 04 '18

Python with numpy and parse

from datetime import datetime

import numpy as np
import parse

# Parse data
times, events = [], []
with open("input4.txt") as infile:
    for line in infile.readlines():
        time, event = parse.parse("[{}] {}", line)
        dt = datetime.strptime(time, '%Y-%m-%d %H:%M')
        times.append(dt)
        events.append(event)

# Sort data
times, events = map(list, zip(*sorted(zip(times, events))))

# Calculate number of times each guard is asleep each minute
guard_sleeps = {}
current_guard_id = None
current_asleep_time = None
for time, event in zip(times, events):
    guard_id = parse.parse("Guard #{} begins shift", event)
    if guard_id is not None:
        current_guard_id, = guard_id
        current_guard_id = int(current_guard_id)
    elif event == "falls asleep":
        current_asleep_time = time
    elif event == "wakes up":
        wakeup_minute = time.minute
        asleep_minute = current_asleep_time.minute
        guard_sleep = np.array(60)
        guard_sleep[asleep_minute:wakeup_minute] = 1
        if current_guard_id in guard_sleeps:
            guard_sleeps[current_guard_id] += guard_sleep
        else:
            guard_sleeps[current_guard_id] = guard_sleep

# Strategy 1
max_sleep_sum = 0
max_sleep = None
sleepy_guard_id = None
for guard_id, sleeps in guard_sleeps.items():
    if sum(sleeps) > max_sleep_sum:
        max_sleep_sum = sum(sleeps)
        max_sleep = sleeps
        sleepy_guard_id = guard_id
print(sleepy_guard_id * np.argmax(max_sleep))

# Strategy 2
max_sleep_for_minute = 0
sleepy_minute = None
sleepy_guard_id = None
for guard_id, sleeps in guard_sleeps.items():
    minute = np.argmax(sleeps)
    sleep_for_minute = sleeps[minute]
    if sleep_for_minute > max_sleep_for_minute:
        max_sleep_for_minute = sleep_for_minute
        sleepy_minute = minute
        sleepy_guard_id = guard_id
print(sleepy_guard_id * sleepy_minute)

1

u/[deleted] Dec 04 '18

If anyone is able to read my C++ solution have fun... I just wrote it and in a few hours will probably forget what I did.
Oh well

https://pastebin.com/Pm8Kgrjg

1

u/skarlso Dec 04 '18 edited Dec 05 '18

I actually managed to get a pretty decent run time with some math and some thinking. :)

Solution in Go (golang):

❯ time ./part1/main input.txt
95199
./part1/main input.txt  0.01s user 0.00s system 87% cpu 0.010 total
package main

import (
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"strings"
)

func main() {
	filename := os.Args[1]
	content, _ := ioutil.ReadFile(filename)
	lines := strings.Split(string(content), "\n")
	guards := make(map[int][]int, 0)
	mostAsleep := make(map[int]int)
	var year, month, day, hour, minute, id int
	for _, l := range lines {
		if strings.Contains(l, "begins shift") {
			fmt.Sscanf(l, "[%d-%d-%d %d:%d] Guard #%d begins shift", &year, &month, &day, &hour, &minute, &id)
			if _, ok := guards[id]; !ok {
				guards[id] = make([]int, 59)
			}
		}
		if strings.Contains(l, "falls asleep") {
			fmt.Sscanf(l, "[%d-%d-%d %d:%d] falls asleep", &year, &month, &day, &hour, &minute)
		}
		if strings.Contains(l, "wakes up") {
			sleptFrom := minute
			fmt.Sscanf(l, "[%d-%d-%d %d:%d] wakes up", &year, &month, &day, &hour, &minute)
			diff := int(math.Abs(float64(sleptFrom) - float64(minute)))
			for i := 0; i < diff; i++ {
				index := (sleptFrom + i) % 60
				guards[id][index]++
			}
			mostAsleep[id] += diff
		}
	}

	max := 0
	maxID := 0
	for k, v := range mostAsleep {
		if v > max {
			max = v
			maxID = k
		}
	}

	mostDays := 0
	mostDaysIndex := 0
	for i, v := range guards[maxID] {
		if v > mostDays {
			mostDays = v
			mostDaysIndex = i
		}
	}

	fmt.Println(maxID * mostDaysIndex)
}

This is based on the fact that after a sleep there is always a wakeup. I don't really like the max finding at the end but I'm tired and can't bother to refactor it. :)

1

u/[deleted] Dec 04 '18

Dlang:

import std.experimental.all;

void main() {
    auto input = slurp!(string, string, string, string)("day04-input.txt", "[%s] %s %s %s")
        .sort!"a[0] < b[0]"
        .map!(t => Tuple!(string, "key", int, "value")
              (t[1], t[1] == "Guard" ? t[2][1..$].to!int : t[0][$-2..$].to!int));

    int[][int] asleep;
    int time_from, curr_guard;
    foreach(t; input) {
        if (t.key == "Guard") {
            curr_guard = t.value;
            if (t.value !in asleep) asleep[t.value] = new int[60];
        } else if (t.key == "falls") {
            time_from = t.value;
        } else {
            asleep[curr_guard][time_from .. t.value] += 1;
        }
    }

    auto p1 = asleep.byPair.maxElement!"a.value.sum";
    writeln("Result 4a: ", p1.key * p1.value.maxIndex);

    auto p2 = asleep.byPair.maxElement!"a.value.maxElement";
    writeln("Result 4b: ", p2.key * p2.value.maxIndex);
}

1

u/nibarius Dec 04 '18

Kotlin

[CARD] Today’s puzzle would have been a lot easier if my language supported finding the snooziest minute.

I'm not very fond of using regexp, so I'm usually chaining a couple of substring calls when parsing input to get what I want. To me this is often more readable to me. With my approach part 2 ended up being very easy to solve.

class Day4(input: List<String>) { private val guards = parseInput(input)

    private fun parseInput(input: List<String>): MutableMap<Int, IntArray> {
        val sorted = input.sorted()
        var currentGuard = -1
        var i = 0
    
        // Map of all guards, value is an array where each entry specifies how many days the guard was
        // sleeping during that minute
        val guards = mutableMapOf<Int, IntArray>()
        while (i < sorted.size) {
            val line = sorted[i++]
            // Example lines to parse (falls asleep is always followed by wakes up):
            // [1518-11-01 00:00] Guard #10 begins shift
            // [1518-11-01 00:05] falls asleep
            // [1518-11-01 00:25] wakes up
            if (line.endsWith("begins shift")) {
                currentGuard = line.substringAfter("#").substringBefore(" ").toInt()
                guards.putIfAbsent(currentGuard, IntArray(60))
                continue
            }
    
            val fallAsleep = line.substringAfter(":").take(2).toInt()
            val wakeup = sorted[i++].substringAfter(":").take(2).toInt()
            (fallAsleep until wakeup).forEach { guards.getValue(currentGuard)[it]++ }
        }
        return guards
    }
    
    private fun findGuardMinuteCombination(strategy: (Map.Entry<Int, IntArray>) -> Int): Int {
        val snooziestGuard = guards.maxBy { strategy(it) }!!
        val snooziestMinute = snooziestGuard.value.withIndex().maxBy { it.value }!!.index
        return snooziestGuard.key * snooziestMinute
    }
    
    fun solvePart1(): Int {
        // Find the guard with most total sleep time
        return findGuardMinuteCombination { guards -> guards.value.sum() }
    }
    
    fun solvePart2(): Int {
        // Find the guard with the maximum sleep time for a given minute
        return findGuardMinuteCombination { guards -> guards.value.max()!! }
    }
}

Special thanks to /u/bdjnk for the great variable name suggestion.

1

u/NabrenX Dec 04 '18

Still kind of new at Python, so any feedback is appreciated:

import re

with open("advent4_input.txt") as input_file:
  input = input_file.read()

compiled_re = re.compile(r'\[([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+)\] (.*)')
guard_re = re.compile(r'Guard #([0-9]+) begins shift')
parsed_lines = sorted([(int(year), int(month), int(day), int(hour), int(minute), action) for (year, month, day, hour, minute, action) in compiled_re.findall(input)])

guard_data = {}

for (year, month, day, hour, minute, action) in parsed_lines:
  if action == 'falls asleep':
    asleep_time = minute
  elif action == 'wakes up':
    awake_time = minute
    for i in xrange(asleep_time, awake_time):
      current_guard[i] += 1
  else:
    guard_id = int(guard_re.match(action).group(1))
    current_guard = guard_data.get(guard_id)

    if current_guard == None:
      current_guard = [0] * 60
      guard_data[guard_id] = current_guard

best_guard = max(guard_data.iteritems(), key=lambda guard: sum(guard[1]))
best_hour = max(xrange(60), key=best_guard[1].__getitem__)
print 'Part one result: Guard {0} for hour {1} = {2} '.format(best_guard[0], best_hour, best_guard[0] * best_hour)

best_guard = max(guard_data.iteritems(), key=lambda guard: max(guard[1]))
best_hour = max(xrange(60), key=best_guard[1].__getitem__)
print 'Part two result: Guard {0} for hour {1} = {2} '.format(best_guard[0], best_hour, best_guard[0] * best_hour)

1

u/misterboya Dec 04 '18

Partial Haskell solution that uses parser to read logs:

{-# LANGUAGE OverloadedStrings #-}

import Data.Attoparsec.ByteString.Char8 hiding (take, count)
import Data.ByteString.Char8 hiding (maximum, length, pack, head, count, take, foldl, putStrLn, map)
import Prelude hiding (getContents, count, lines)

import Data.Char
import Data.List (sort, sortBy, maximumBy)
import Control.Applicative
import qualified Data.Map.Strict as Map

data Event = ShiftStarts Integer | FallsAsleep | WakesUp deriving (Show)
data Log = Log Integer Integer Integer Integer Integer Event deriving (Show)

parseShiftStarts :: Parser Event
parseShiftStarts = do
  string "Guard"
  space
  char '#'
  guard <- decimal
  space
  string "begins"
  space
  string "shift"
  return $ ShiftStarts guard

parseWakesUp :: Parser Event
parseWakesUp = do
  string "wakes"
  space
  string "up"
  return $ WakesUp

parseFallsAsleep :: Parser Event
parseFallsAsleep = do
  string "falls"
  space
  string "asleep"
  return $ FallsAsleep

parseEvent :: Parser Event
parseEvent = parseFallsAsleep <|> parseWakesUp <|> parseShiftStarts

parseLog :: Parser Log
parseLog = do
  char '['
  year <- decimal
  char '-'
  month <- decimal
  char '-'
  day <- decimal
  space
  hour <- decimal
  char ':'
  minute <- decimal
  char ']'
  space
  event <- parseEvent
  return $ Log year month day hour minute event

parser input = parser' result'
  where
    parser' (Right a) = a
    result' = parseOnly parseLog input

solution :: [Log] -> Integer
solution cs = guard * minute
  where
    ...

main :: IO ()
main = do
   content <- getContents
   let
     toTuple (Log year month day hour minute _) = (year,month,day,hour,minute)
   putStrLn $ show $ solution $ sortBy (\a b -> compare (toTuple a) (toTuple b)) $ map (parser) $ lines content

1

u/Gnidleif Dec 04 '18

Another day, another language. Today I went with Java. I had quite some issues solving the one today. This is not getting easier. Because it's Java it's also verbose , so here's the link to my repo.

1

u/Sentreen Dec 05 '18

Elixir

I decided to create a map which maps guard ids to a collection of how many times they were awake each minute. The reduction to create that map got pretty.... messy. However, once I had that map, it was fairly easy to get the answer to both parts.

Note that the aoc 2018, 4 is a simply macro that generates a module with some predefined functions (specifically the input, which loads the input file into a stream of strings).

aoc 2018, 4 do
  def get do
    input()
    |> Enum.sort()
    |> Stream.map(&Regex.scan(~r/\[1518-(\d\d)-(\d\d) (\d\d):(\d\d)\] (.*)/, &1))
    |> Stream.map(&tl(hd(&1)))
    |> Stream.map(&parse_line/1)
    |> Enum.reduce({-1, -1, %{}}, fn
      %{entry: {:guard, id}}, {_, _, m} -> {id, -1, m}
      %{entry: :sleep, minute: min}, {id, _, m} -> {id, min, m}
      %{entry: :wake, minute: min}, {id, start, m} ->
        m = Enum.reduce(
          start .. min - 1,
          m,
          &Map.update(&2, id, %{}, fn times -> Map.update(times, &1, 1, fn cur -> cur + 1 end) end))
        {id, -1, m}
    end)
    |> elem(2)
  end

  def parse_line([month, day, hour, minute, entry]) do
    %{
      entry: parse_entry(entry),
      month: String.to_integer(month),
      day: String.to_integer(day),
      hour: String.to_integer(hour),
      minute: String.to_integer(minute)
    }
  end

  def parse_entry("falls asleep"), do: :sleep
  def parse_entry("wakes up"), do: :wake

  def parse_entry(entry) do
    [[_, id]] = Regex.scan(~r/Guard #(\d+) begins shift/, entry)
    {:guard, String.to_integer(id)}
  end

  def p1 do
    id =
      get()
      |> Enum.map(fn {id, map} -> {id, Enum.sum(Map.values(map))} end)
      |> Enum.max_by(&elem(&1, 1))
      |> elem(0)
    min =
      get()[id]
      |> Enum.max_by(&elem(&1, 1))
      |> elem(0)
    id * min
  end

  def p2 do
    id =
      get()
      |> Enum.map(fn {id, map} -> {id, Enum.max_by(map, &elem(&1, 1))} end)
      |> Enum.max_by(fn {_, {_, x}} -> x end)
      |> elem(0)
    min =
      get()[id]
      |> Enum.max_by(&elem(&1, 1))
      |> elem(0)
    id * min
  end
end
→ More replies (3)

1

u/Sampsc Dec 05 '18

Can someone help me with my solution in python? I'm kind of a newbie and today i was at work all day so i couldn't put much effort into it. Not the most elegant solution but i can't figure out what's wrong

for i in lines:
    month=int(i[6:8])
    day = int(i[9:11])
    hour= int(i[12:14])
    minute[cnt] = int(i[15:17])
    time[cnt]=month*30*24*60 + day*24*60 + hour *60 + minute[cnt]
    if "begins shift" in i:
        guard=i.split("#")[1]
        guard=guard.split(" ")[0]
        guard_id[cnt]=int(guard)
        #if asleep[cnt]==1:
         #   asleep[cnt:]-=1
    if  "asleep" in i:
        asleep[cnt]+=1
    if "wakes" in i:
        awake[cnt]+=1
    cnt += 1

guard_id=guard_id[time.argsort()]
asleep=asleep[time.argsort()]
awake=awake[time.argsort()]
time.sort()

guard=0
sleeptime=np.zeros(int(max(guard_id)+1))
timeasleep=0
seenmins=np.zeros([60, int(max(guard_id)+1)])
lastasleep=0


for i in range(len(lines)):
    if guard_id[i] != 0:
        if lastasleep==1:
            timeasleep += time[i]- 1 - sleepstart
            minend=int(minute[i]-1)
            seenmins[minstart:minend, guardname] += 1
        guardname=int(guard_id[i])
        timeasleep=0
    if awake[i]==1:
        sleeptime[guardname] +=time[i]-1-sleepstart
        minend=int(minute[i]-1)
        seenmins[minstart:minend, guardname] += 1
        lastasleep=0
    if asleep[i]==1:
        sleepstart=time[i]
        minstart=int(minute[i])
        lastasleep=1


print(sleeptime.argmax())
print(seenmins[:,sleeptime.argmax()].argmax())

1

u/sotsoguk Dec 05 '18 edited Dec 05 '18

Not a very elegant solution, but due to my timezone (CET), work and being sick i was late to the game and lost time to a nasty bug. I set the end of the sleeping intervals 1 minute early. The solution to part 1 was nevertheless correct, which almost drove me crazy ...

import re
import numpy as np
import operator
with open('input.txt') as f:
    lines = f.readlines()

lines = [line.strip() for line in lines]
lines.sort()
guards = {}

activeGuard = 0
startSet = False
startTime = 0
endTime = 0
for l in lines:
    nums = list(map(int,re.findall(r'\d+',l)))
    if len(nums) == 6:
        gnum = nums[5]
        activeGuard = gnum
        if gnum not in guards.keys():
            guards[gnum] = np.zeros(60)
    elif not startSet:
        startTime = nums[4]
        startSet = True
    else:
        startSet = False
        endTime = nums[4]
        guards[activeGuard][startTime:endTime]+=1
        

guardsTime={}
guardsMax = {}
guardsMaxMin = {}
for k,v in guards.items():
    guardsTime[k] = np.sum(v)
for k,v in guards.items():
    guardsMax[k] = max(guards[k])
    guardsMaxMin[k] = np.argmax(guards[k])

maxGuard =  max(guardsMax.items(), key=operator.itemgetter(1))[0]

# Part1
sleepyGuard = max(guardsTime.items(), key=operator.itemgetter(1))[0]
minute=np.argmax(guards[sleepyGuard])
print("Part1:\t" + str(sleepyGuard * minute))

# Part2
print("Part2:\t"+ str(maxGuard*guardsMaxMin[maxGuard]))

Edit: Finally correct code block formatting

1

u/tinyhurricanes Dec 05 '18

Modern Fortran 2018 (complete code)

Extra difficult with no sort functionality.

program main
use syslog_mod
use fclap_mod
use file_tools_mod
use string_tools_mod
use guard_record_mod
implicit none

!-- Counters
integer :: i, j

!-- Input file unit
integer :: input_unit

!-- Number of lines in input file
integer :: num_lines

!-- Parameters
integer,parameter :: DAYS_IN_YEAR         =  365
integer,parameter :: TRACKED_MINS_PER_DAY =   60
integer,parameter :: MAX_NUM_GUARDS       = 9999

!-- Main variables

! Tracks currently propogating guard
integer :: current_guard = 1        

! Stores all records, even incomplete ones
type(guard_record),allocatable :: records_all(:)       

! Stores complete records sorted by (day,minute)
type(guard_record) :: records_sorted(DAYS_IN_YEAR,0:TRACKED_MINS_PER_DAY-1)

! Tracks total sleep time per (guard,minute)
integer :: sleepytime(MAX_NUM_GUARDS,0:TRACKED_MINS_PER_DAY-1) = 0

! Tracks whether guard is "currently" asleep
integer :: is_sleeping(MAX_NUM_GUARDS)

! Tracks the "sleepiest" guard
integer :: sleepiest_guard = 1

! The minute during which the most sleep happens
integer :: sleepiest_minute = 0

! Input file reading properties
integer,parameter            :: max_line_len = 100
character(len=max_line_len)  :: line
character(len=:),allocatable :: input_file

!-- Initialize System Log
call init_syslog

!-- Process Command Line Arguments
call configure_fclap
call parse_command_line_arguments

!-- Get input file name from command line
input_file = get_value_for_arg('input_file')

!-- Count lines in input file
num_lines = lines_in_file(input_file)

!-- Allocate data appropriately
allocate(records_all(num_lines))

!-- Open file and read into memory
open (                    & 
    newunit = input_unit, & 
    file    = input_file, &
    action  = 'read',     &
    status  = 'old',      &
    form    = 'formatted' &
)
write (syslog%unit,*) 'Raw Records:'
write (syslog%unit,*) 'Guard Act   Hr  Min  Mth  Day Year  DoY'
do i = 1, num_lines
    read (input_unit,'(a)') line
    records_all(i) = Guard_Record(line)
    write (syslog%unit,'(8i5)')    &
        records_all(i) % guard_id, &
        records_all(i) % action,   &
        records_all(i) % hour,     &
        records_all(i) % minute,   &
        records_all(i) % month,    &
        records_all(i) % day,      &
        records_all(i) % year,     &
        records_all(i) % doy
end do
close (input_unit)

!-- Start timer
call syslog % start_timer

!-- Sort records according to day,time
do i = 1, num_lines
    records_sorted(records_all(i)%doy, records_all(i)%minute) = records_all(i)
end do

!-- Propogate values
write (syslog%unit,*) ' Records:'
write (syslog%unit,*) 'Guard Act  Min  Mth  Day  DoY'
DAYS_LOOP: do i = 1, DAYS_IN_YEAR

    MINS_LOOP: do j = 0, TRACKED_MINS_PER_DAY - 1
    
        if (records_sorted(i,j) % action == ActionUnspecified) cycle MINS_LOOP

        select case (records_sorted(i,j) % action)
        case (ActionBeginShift)
            current_guard = records_sorted(i,j) % guard_id
        case (ActionWakeUp,ActionFallAsleep)
            records_sorted(i,j) % guard_id = current_guard
        case default
            write (syslog%unit,*) 'Error: unknown record action'
            call bomb
        end select

        write (syslog%unit,'(6i5)')         &
            records_sorted(i,j) % guard_id, &
            records_sorted(i,j) % action,   &
            records_sorted(i,j) % minute,   &
            records_sorted(i,j) % month,    &
            records_sorted(i,j) % day,      &
            records_sorted(i,j) % doy

    end do MINS_LOOP

end do DAYS_LOOP

!-- Calculate answers
DAYS_LOOP_2: do i = 1, DAYS_IN_YEAR

    is_sleeping(:) = 0 ! re-initialize

    MINS_LOOP_2: do j = 0, TRACKED_MINS_PER_DAY - 1

        if (records_sorted(i,j) % action == ActionFallAsleep) then
            is_sleeping(records_sorted(i,j) % guard_id) = 1
        end if

        if (records_sorted(i,j) % action == ActionWakeUp) then
            is_sleeping(records_sorted(i,j) % guard_id) = 0
        end if

        if (records_sorted(i,j) % action == ActionBeginShift) then
            is_sleeping(records_sorted(i,j) % guard_id) = 0
        end if

        sleepytime(:,j) = sleepytime(:,j) + is_sleeping(:)

    end do MINS_LOOP_2

end do DAYS_LOOP_2

!-- Guard totals
write (syslog%unit,*) 'Guard   ID MINS'
GUARDS_LOOP: do i = 1, MAX_NUM_GUARDS
    
    ! Find sleepiest guard
    if (sum(sleepytime(i,:)) > 0) then
        if (sum(sleepytime(i,:)) > sum(sleepytime(sleepiest_guard,:))) then
           sleepiest_guard = i
        end if
        write (syslog%unit,'(ai5,a,i0)') ' Guard ',i, ' ', sum(sleepytime(i,:))
    end if
end do GUARDS_LOOP

!-- Answers
write (syslog%unit,'(a,i0,a,i0,a)') &
    'Guard ',sleepiest_guard,' slept ', sum(sleepytime(sleepiest_guard,:)), ' mins'
write (syslog%unit,'(a,i0,a,i0)') &
    'Guard ',sleepiest_guard,' slept the most during minute ',maxloc(sleepytime(sleepiest_guard,:))-1

!-- Diagnostic
write (syslog%unit,*) 'MIN  #OCCUR'
do i = 0, TRACKED_MINS_PER_DAY - 1
    write (syslog%unit,'(2i5)') i, sleepytime(sleepiest_guard,i)
end do

! maxloc apparently gets the location of the max with respect to a 1-indexed array
write (syslog%unit,'(a,i0)') &
    'Part 1 answer: ',       & !50558
    (maxloc(sleepytime(sleepiest_guard,:))-1) * sleepiest_guard
write (syslog%unit,'(a,i0,a,i0,a)') &
    'Guard ',sleepiest_guard,' slept ',sum(sleepytime(sleepiest_guard,:)), ' mins.'

sleepiest_guard = 1

GUARDS_LOOP_2: do i = 1, MAX_NUM_GUARDS
    ! Find sleepiest minute
    MINS_LOOP_3: do j = 0, TRACKED_MINS_PER_DAY - 1
        if (sleepytime(i,j) > sleepytime(sleepiest_guard,sleepiest_minute)) then
            sleepiest_guard  = i
            sleepiest_minute = j
        end if
    end do MINS_LOOP_3
end do GUARDS_LOOP_2
write (syslog%unit,'(3(a,i0))')                   &
    'Guard ',sleepiest_guard,' slept ',           &
    sleepytime(sleepiest_guard,sleepiest_minute), &
    ' times during minute ', sleepiest_minute

! 28198
write (syslog%unit,'(a,i0)') 'Part 2 answer: ',sleepiest_guard * sleepiest_minute

!-- End timer
call syslog % end_timer

call syslog%log(__FILE__,'Done.')

end program

Guard_Record object:

module guard_record_mod
use syslog_mod
implicit none

! Total number of days from previous months
integer,parameter :: doy_per_month(12) = &
    [000, 031, 059, 090, 120, 151, 181, 212, 243, 273, 304, 334]
    !Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
    ! 1    2    3    4    5    6    7    8    9    10   11   12

enum, bind(c)
    enumerator :: ActionUnspecified = -1
    enumerator :: ActionBeginShift
    enumerator :: ActionFallAsleep
    enumerator :: ActionWakeUp
end enum

type :: guard_record
    ! Properties
    integer :: guard_id = -1
    integer :: action = ActionUnspecified
    ! Date Properties
    integer :: hour
    integer :: minute
    integer :: month
    integer :: day
    integer :: year
    integer :: doy ! (day of year)
end type
interface guard_record
    module procedure init_record
end interface

contains

type(guard_record) function init_record(string) result(r)
    implicit none
    character(len=*) :: string
    character(len=:),allocatable :: datestring
    character(len=:),allocatable :: actionstring
    integer :: i
    character(len=6) :: begins
    character(len=5) :: shift

    ! Remove special characters from string
    do i = 1, len(string)
        if (string(i:i) == '[') string(i:i) = ' '
        if (string(i:i) == ']') string(i:i) = ' '
        if (string(i:i) == '-') string(i:i) = ' '
        if (string(i:i) == ':') string(i:i) = ' '
        if (string(i:i) == '#') string(i:i) = ' '
    end do

    ! Split strings
    datestring   = trim(adjustl(string(1:20)))
    actionstring = trim(adjustl(string(20:)))

    ! Read variables into data
    read (datestring,*) r % year, r % month, r % day, r % hour, r % minute

    ! Get day of year
    r % doy = doy_per_month(r % month) + r % day

    ! Assign action
    i = index(actionstring, ' ')
    select case (actionstring(1:i))
    case ('Guard')
        r % action = ActionBeginShift
        read (actionstring(i:),*) r % guard_id, begins, shift
    case ('wakes')
        r % action = ActionWakeUp
    case ('falls')
        r % action = ActionFallAsleep
    case default
        write (syslog%unit,*) 'Error: unexpected input string: ',string
        call bomb
    end select
end function
end module

1

u/Philboyd_Studge Dec 05 '18

This one I thought the description was confusing and made me spend about 2 hours over-engineering it, then scrapping it completely and redoing it.

Java

https://pastebin.com/mcrTrcyM

1

u/[deleted] Dec 05 '18

So, I am able to solve this after waking up this morning. Wasn't able to get correct result the last night. Seems like I am that guard. :D

1

u/[deleted] Dec 05 '18 edited Dec 05 '18

Extremely over-engineered Haskell solution:

module Main where

import Data.Maybe (mapMaybe)
import Text.ParserCombinators.ReadP (
  ReadP, readP_to_S, char, string, munch1
  )
import Data.Char (isDigit)
import Data.List (sort)
import Control.Applicative ((<|>))
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as M
import Control.Monad.Trans.State.Strict
import Data.Functor (($>))
import Control.Arrow (first, second, (&&&))
import Control.Monad (guard)

data Action = Begin Int | Sleep | Wake
  deriving (Show, Eq, Ord)

data Entry = Entry {
  year :: Int, month :: Int, day :: Int,
  hour :: Int, minute :: Int,
  action :: Action
  } deriving (Show, Eq, Ord)

number :: ReadP Int
number = read <$> munch1 isDigit

parseAction :: ReadP Action
parseAction =
  fmap Begin (string "Guard #" *> number <* string " begins shift") <|>
  (string "falls asleep" $> Sleep) <|>
  (string "wakes up" $> Wake)

entry :: ReadP Entry
entry = do
  year   <- char '[' *> number
  month  <- char '-' *> number
  day    <- char '-' *> number
  hour   <- char ' ' *> number
  minute <- char ':' *> number
  action <- string "] " *> parseAction
  return $ Entry year month day hour minute action

parse :: String -> Maybe Entry
parse s = case readP_to_S entry s of
  [(res, [])] -> Just res
  _           -> Nothing

processEntries :: [String] -> [Entry]
processEntries = sort . mapMaybe parse

--                 guard   minute freq
type EventState = (IntMap (IntMap Word), [Entry])

nextEvent :: State EventState ()
nextEvent = do
  (m, events) <- get
  case events of
    (x:xs) -> put (m, xs)
    []     -> return ()

begin :: State EventState Bool
begin = do
  events <- gets snd
  nextEvent
  case action <$> events of
    (Begin gId:Sleep:_)   -> sleep gId    -- guard falls asleep
    (Begin _:Begin _:_)   -> begin        -- guard didn't sleep at all
    _                     -> return False -- invalid

sleep :: Int -> State EventState Bool
sleep gId = do
  events <- gets snd
  nextEvent
  case (action &&& minute) <$> events of
    ((Sleep, start):_) -> wake gId start -- guard wakes up
    _                  -> return False   -- invalid

wake :: Int -> Int -> State EventState Bool
wake gId start = do
  events <- gets snd
  nextEvent
  case action <$> events of
    (Wake:es) -> do
        let end     = minute $ head events
            minutes = M.fromDistinctAscList [(n, 1) | n <- [start..end - 1]]
        modify $ first $ M.insertWith (M.unionWith (+)) gId minutes
        case es of
          []          -> return True  -- success, last event in list was wake
          (Begin _:_) -> begin        -- new guard starts
          (Sleep:_)   -> sleep gId    -- guard goes back to sleep
          _           -> return False -- invalid
    _         -> return False         -- invalid

run :: [String] -> (Bool, IntMap (IntMap Word))
run xs = second fst $ runState begin (M.empty, sort $ mapMaybe parse xs)

part1 :: [String] -> Maybe Int
part1 xs = do
  let (success, coverage) = run xs
  guard success
  let ((g, _, m, _), _) = M.mapAccumWithKey argMax (0, 0, 0, 0) coverage
  return $ g * m
  where 
    argMax c@(bestGuard, bestTotal, bestMinute, bestCount) gId m =
      let (total, largest, minute') = compact m
      in  if total > bestTotal
          then ((gId, total, minute', largest), ())
          else (c, ())
    compact = M.foldlWithKey' f (0, 0, 0)
    f (total, largest, m) minute' count =
      if count > largest
      then (count + total, count, minute')
      else (count + total, largest, m)

part2 :: [String] -> Maybe Int
part2 xs = do
  let (success, coverage) = run xs
  guard success
  let ((g, m, _), _) = M.mapAccumWithKey argMax (0, 0, 0) coverage
  return $ g * m
  where
    argMax c@(bestGuard, bestMinute, bestCount) gId m =
      let (largest, minute') = compact m
      in  if largest > bestCount
          then ((gId, minute', largest), ())
          else (c, ())
    compact = M.foldlWithKey' f (0, 0)
    f (largest, m) minute' count =
      if count > largest
      then (count, minute')
      else (largest, m)

main :: IO ()
main = do
  input <- lines <$> readFile "input4.txt"
  print $ part1 input
  print $ part2 input

1

u/Pyrobolser Dec 05 '18

C# solution for today! If anybody is interested to read or comment, I put my solutions on GitHub too. Any remark or idea appreciated. Have fun folks!

public class Guard
{
    public int Id { get; private set; }
    public Dictionary<int, int> Sleep { get; private set; }

    public Guard(int id)
    {
        Id = id;
        Sleep = Enumerable.Range(0, 60).ToDictionary(d => d, d => 0);
    }
}

public static class Day4Part1
{
    private static string[] InputLines => File.ReadAllLines(@"Inputs\Day4.txt").OrderBy(s => s).ToArray();

    public static void Solve()
    {
        HashSet<Guard> guards = new HashSet<Guard>();
        Guard guard = null;
        int start = 0, end = 0;
        foreach (string line in InputLines)
        {
            Match match = Regex.Match(line, @":(\d+)\] (Guard|falls|wakes) #?(\d+)?");
            switch (match.Groups[2].Value)
            {
                case "Guard":
                    guard = guards.FirstOrDefault(g => g.Id == int.Parse(match.Groups[3].Value));
                    if (guard == null)
                    {
                        guard = new Guard(int.Parse(match.Groups[3].Value));
                        guards.Add(guard);
                    }
                    break;
                case "falls":
                    start = int.Parse(match.Groups[1].Value);
                    break;
                case "wakes":
                    end = int.Parse(match.Groups[1].Value);
                    for (int i = start; i < end; i++)
                        guard.Sleep[i]++;
                    break;
            }
        }

        var sleepy = guards.OrderByDescending(g => g.Sleep.Sum(s => s.Value)).First();
        var most = sleepy.Sleep.OrderByDescending(g => g.Value).Select(s => s.Key).First();

        Console.WriteLine($"The ID of the guard multiplied by the minute is { sleepy.Id * most }.");
    }
}

public static class Day4Part2
{
    private static string[] InputLines => File.ReadAllLines(@"Inputs\Day4.txt").OrderBy(s => s).ToArray();

    public static void Solve()
    {
        HashSet<Guard> guards = new HashSet<Guard>();
        Guard guard = null;
        int start = 0, end = 0;
        foreach (string line in InputLines)
        {
            Match match = Regex.Match(line, @":(\d+)\] (Guard|falls|wakes) #?(\d+)?");
            switch (match.Groups[2].Value)
            {
                case "Guard":
                    guard = guards.FirstOrDefault(g => g.Id == int.Parse(match.Groups[3].Value));
                    if (guard == null)
                    {
                        guard = new Guard(int.Parse(match.Groups[3].Value));
                        guards.Add(guard);
                    }
                    break;
                case "falls":
                    start = int.Parse(match.Groups[1].Value);
                    break;
                case "wakes":
                    end = int.Parse(match.Groups[1].Value);
                    for (int i = start; i < end; i++)
                        guard.Sleep[i]++;
                    break;
            }
        }

        var sleepy = guards.OrderByDescending(g => g.Sleep.Max(s => s.Value)).First();
        var most = sleepy.Sleep.OrderByDescending(g => g.Value).Select(s => s.Key).First();

        Console.WriteLine($"The ID of the guard multiplied by the minute is { sleepy.Id * most }.");
    }
}

1

u/hayden592 Dec 05 '18

Javascript runs in the browser. Having a little trouble with part 2

input = document.querySelector('pre').textContent.split('\n');
//remove last empty string
input.pop();

input.sort((a,b) => new Date(a.match(/(\d+.*\:\d+)/g)[0]) - new Date(b.match(/(\d+.*\:\d+)/g)[0]))

parseLog = (log) => (log.match(/(\d+.*\:\d+)|(\d+|falls|wakes)/g));
isGuard = (action) => (action.match(/\d+/g));
getMins = (nap) => (nap.split(':')[1]);

currentGuard;
sleepiest;
sleepStudy = input.reduce((a, c)=> {
  parsed = parseLog(c);
  if (isGuard(parsed[1])) { 
    currentGuard = parsed[1];
    if(!a[currentGuard]) {
      a[currentGuard] = { totalSleep: 0, timeSleptAtMinute: new Array(60).fill(0), naps: [] };
    }
  } else {
    if (a[currentGuard].naps.length === 1) {
      end = getMins(parsed[0]);
      start = getMins(a[currentGuard].naps.pop());
      a[currentGuard].totalSleep += end-start;
      // i think there is a problem in here preventing me from solving part2
      for(i=start; i<end; i++){
        a[currentGuard].timeSleptAtMinute[i]++;
      }
      if (!a[sleepiest]) {
        sleepiest = currentGuard;
      }
      sleepiest = ( a[currentGuard].totalSleep >= a[sleepiest].totalSleep) ? currentGuard : sleepiest;
    } else {
      a[currentGuard].naps.push(parsed[0]);
    }
  }
  return a;
}, {});


console.log('part1', sleepiest* sleepStudy[sleepiest].timeSleptAtMinute.indexOf(Math.max(...sleepStudy[sleepiest].timeSleptAtMinute)));

// this should work
part2 = Object.keys(sleepStudy).reduce((a, c) => {
  guard = c;
  minutes = Math.max(...sleepStudy[c].timeSleptAtMinute);
  index = sleepStudy[c].timeSleptAtMinute.indexOf(minutes);
  return (minutes >= a.minutes) ? {guard, minutes, index } : a;
}, {guard: '', minutes: 0, index: 0})
console.log('part2', part2.guard * part2.index)


1

u/alex4743 Dec 05 '18

My problem with all of these coding challenge type of problems is just understanding what is even being asked. You have no idea how long it took me to actually understand where he got 24 from in his example. Poor gcc, I redid this one 5 times thinking I understood what I was trying to find lol

1

u/jorosp Dec 05 '18

Haskell

I originally had a fancy parsing function and records and such but once I realized I only needed the minutes I tossed that all in the trash.

{-# LANGUAGE ViewPatterns #-}
import Data.Char
import Data.Maybe
import Data.List
import Data.List.Split
import Data.Function

type Minute  = Int
type GuardID = Int
data Event = Begin GuardID | Sleep Minute deriving (Eq, Ord, Show)

reduce :: (a -> a -> a) -> [a] -> a
reduce f (x:xs) = foldl f x xs

parseLine :: String -> Maybe Event
parseLine = parseLine' . filter (isDigit . head) . groupBy ((==) `on` isDigit)
  where
    parseLine' [_, _, _, _, read -> minute]     = Just (Sleep minute)
    parseLine' [_, _, _, _, _, read -> guardID] = Just (Begin guardID)

isSleep :: Event -> Bool
isSleep (Sleep _) = True
isSleep _         = False

glob :: (Ord a, Ord b) => ((a, b) -> (a, b) -> (a, b)) -> [(a, b)] -> [(a, b)]
glob f = map (reduce f) . groupBy ((==) `on` fst) . sort

process :: [Event] -> [(Event, [(Minute, Int)])]
process events = glob f [(last b, process' s) | [b, s] <- chunksOf 2 . groupBy ((==) `on` isSleep) $ events]
  where    
    process' sleeps = [(m, 1) | [Sleep a, Sleep b] <- chunksOf 2 sleeps, m <- [a..b-1]]
    f (b1, s1) (_, s2) = (b1, s1 ++ s2)

modeMinute :: [(Minute, Int)] -> (Minute, Int)
modeMinute = maximumBy (compare `on` snd) . glob f 
  where
    f (a, b) (_, c) = (a, b + c)

solve1 :: [(Event, [(Minute, Int)])] -> Int
solve1 shifts = 
  let (Begin sleepiest, minutes) = maximumBy (compare `on` length . snd) shifts
      (minute, _) = modeMinute minutes
  in  sleepiest * minute

solve2 :: [(Event, [(Minute, Int)])] -> Int
solve2 shifts = 
  let ((_, minute), Begin sleepiest) = maximum [((f, m), b) | (b, modeMinute -> (m, f)) <- shifts]
  in sleepiest * minute

main :: IO ()
main = do
  input <- sort . lines <$> readFile "04.txt"
  let shifts = process . mapMaybe parseLine $ input
  print $ solve1 shifts
  print $ solve2 shifts

1

u/RockyAstro Dec 05 '18

Icon Part1 and Part2

record guard(id,elist,totalsleep,maxmin,minc)
record night(id,ts,events)
record TS(yy,MM,dd,hh,mm)

procedure main()

    input := []
    guards := table()
    while put(input,trim(read()))

    input := sort(input)

    sleepestguard := &null
    mostmin := &null

    every n := genEvents(input) do {

        /guards[n.id] := guard(n.id,[],0,0,0)
        /sleepestguard := n.id
        /mostmin := n.id

        put(guards[n.id].elist,n)

        mmin := 0
        every t := 1 to 60 do {
            if n.events[t] == "#" then guards[n.id].totalsleep +:= 1
            msleep := 0
            every m := 1 to *guards[n.id].elist do {
                if guards[n.id].elist[m].events[t] == "#" then msleep +:= 1
            }
            if mmin < msleep then {
                guards[n.id].maxmin := t-1
                guards[n.id].minc := msleep
                mmin := msleep
            }
        }

        if guards[sleepestguard].totalsleep < guards[n.id].totalsleep then
            sleepestguard := n.id
        if guards[mostmin].minc < guards[n.id].minc then
            mostmin := n.id
    }

    #write("sleepest guard: #",guards[sleepestguard].id," ",guards[sleepestguard].totalsleep," ",guards[sleepestguard].maxmin)
    write("part1 :",guards[sleepestguard].id*guards[sleepestguard].maxmin)

    #write("most min guard: #",guards[mostmin].id," ",guards[mostmin].totalsleep," ",
    #    guards[mostmin].maxmin," ",guards[mostmin].minc)
    write("part2 :",guards[mostmin].id*guards[mostmin].maxmin)

    #every g := key(guards) do
    #    dumpg(guards[g])
end

procedure dumpg(g)
    write("\n-----")
    write("                000000000011111111112222222222333333333344444444445555555555")
    write("                012345678901234567890123456789012345678901234567890123456789")
    every n := !g.elist do {
        write(right(n.ts.MM,2),"-",right(n.ts.dd,2),"  #",right(n.id,7),
              " ",n.events)
    }
    write(g.totalsleep," ",g.maxmin," ",g.minc)
end
procedure genEvents(input)
    current := &null
    every l := !input do {
        l ? {
            ="["
            ts := parseTS()
            if ts.hh = 23 then {
                ts.hh := 0
                ts.mm := 0
                ts.dd +:= 1
            }
            tab(many(' '))
            case tab(upto(' ')) of {
                "Guard": {
                    suspend \current
                    tab(upto('#'))
                    ="#"
                    current := night(tab(upto(' ')),ts,repl(".",60))
                }
                "falls":
                    current.events[ts.mm+1:0] := repl('#',60-ts.mm)
                "wakes":
                    current.events[ts.mm+1:0] := repl(".",60-ts.mm)
            }
        }
    }
    suspend \current
end

procedure parseTS()
    suspend TS(supto('-'),
             supto('-'),
             supto(' '),
             supto(':'),
             supto(']'))
end
procedure supto(delim)
    suspend 1(tab(upto(delim)),tab(match(delim)))
end

1

u/NeilNjae Dec 05 '18

Good grief, this was a bit of a nightmare, caused mainly by trying to wrestle with Haskell's Time library. Once I managed to get it converting from string to timestamp to minutes, things were relatively smooth (many thanks to u/ephemient for posting their very helpful solution). The activity is stored as a Set of minutes asleep by each guard, and a SleepFrequency Map that goes from the minute in question to the number of times the guard was asleep then. Source on Github

{-# LANGUAGE OverloadedStrings #-}

import Data.List

import Data.Text (Text)
import qualified Data.Text.IO as TIO

import Data.Void (Void)

import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
import qualified Control.Applicative as CA

import Data.Time

import qualified Data.Set as S
import qualified Data.Map.Strict as M

type GuardId = Integer

data LogEvent = Arrives GuardId | Sleeps | Wakes deriving (Eq, Show, Ord)
data LogEntry = LogEntry { _logTime :: UTCTime , _logEvent :: LogEvent } deriving (Eq, Show, Ord)

data GuardState = Asleep UTCTime | Awake
data LogTracker = LogTracker {_currentGuard :: GuardId, _currentState :: GuardState }

type GuardActivity = S.Set (UTCTime, GuardId)
type Guards = S.Set GuardId
-- type Minutes = S.Set Int
type GuardSleepDuration = M.Map GuardId Int
type SleepFrequency = M.Map Int Int -- key = minute, value = times spent asleep
type GuardSleepFrequency = M.Map (GuardId, Int) Int -- key = (guardID, minute), value = times spent asleep


main :: IO ()
main = do 
        text <- TIO.readFile "data/advent04.txt"
        let guardLog = sort $ successfulParse text
        let activity = buildActivity guardLog
        print $ part1 activity
        print $ part2 activity

part1 :: GuardActivity -> Int
part1 activity = (fromIntegral sg) * mostAsleep
    where sd = sleepDurations activity
          sg = sleepiestGuard sd
          sga = guardActivity sg activity
          sgf = sleepFrequency sga
          mostAsleep = keyOfMaxValue sgf

part2 :: GuardActivity -> Int
part2 activity = (fromIntegral g) * m
    where gids = guardsOf activity
          sleepTimes = M.fromSet (\gid -> guardSleepFrequency gid activity) gids
          gsfs = M.foldrWithKey' rekeySleep M.empty sleepTimes
          (g, m) = keyOfMaxValue gsfs

rekeySleep :: GuardId -> SleepFrequency -> GuardSleepFrequency -> GuardSleepFrequency
rekeySleep gid sleepFreq guardSleepFreq = M.foldrWithKey' (\m f gsf -> M.insert (gid, m) f gsf) guardSleepFreq sleepFreq



buildActivity :: [LogEntry] -> GuardActivity
buildActivity guardLog = snd $ foldl' processLogEntry' (initialTracker, S.empty) guardLog
    where initialTracker = LogTracker {_currentGuard = 0, _currentState = Awake}
          processLogEntry' (tracker, activity) entry = processLogEntry (_logEvent entry) (_logTime entry) tracker activity

processLogEntry :: LogEvent -> UTCTime -> LogTracker -> GuardActivity -> (LogTracker, GuardActivity)
processLogEntry (Arrives gid) _    _       activity = (LogTracker {_currentGuard = gid, _currentState = Awake}, activity)
processLogEntry Sleeps        time tracker activity = (tracker {_currentState = Asleep time}, activity)
processLogEntry Wakes         time tracker activity = (tracker {_currentState = Awake}, activity')
    where Asleep sleepTime = _currentState tracker
          guardId = _currentGuard tracker
          sleepMinutes = unfoldr unfoldF sleepTime 
          unfoldF now = if now >= time then Nothing
                        else Just ((now, guardId), addUTCTime 60 now)
          activity' = S.union activity $ S.fromList sleepMinutes

guardsOf :: GuardActivity -> Guards
guardsOf = S.map snd

-- minutesOf :: GuardActivity -> Minutes
-- minutesOf activity = S.map (toMinutes . fst) activity

toMinutes :: UTCTime -> Int
toMinutes = todMin . timeToTimeOfDay . utctDayTime

totalSleepDuration :: GuardId -> GuardActivity -> Int
totalSleepDuration gid activity = S.size $ guardActivity gid activity

-- all activity of one guard
guardActivity :: GuardId -> GuardActivity -> GuardActivity
guardActivity gid activity = S.filter (\(_, g) -> g == gid) activity

sleepDurations :: GuardActivity -> GuardSleepDuration
sleepDurations activity = M.fromSet guardSleepDuration gids 
    where gids = guardsOf activity
          guardSleepDuration gid = totalSleepDuration gid activity


sleepiestGuard :: GuardSleepDuration -> GuardId
sleepiestGuard = keyOfMaxValue


keyOfMaxValue :: Ord b => M.Map a b -> a
keyOfMaxValue m = fst $ M.foldrWithKey mergeKV (M.findMin m) m
    where mergeKV k v (bestK, bestV) = 
            if v > bestV then (k, v) else (bestK, bestV)


sleepFrequency :: GuardActivity -> SleepFrequency
sleepFrequency activity = S.foldl' updateSF M.empty activity
    where updateSF m (t, _) = M.insert (toMinutes t) ((M.findWithDefault 0 (toMinutes t) m) + 1) m

guardSleepFrequency :: GuardId -> GuardActivity -> SleepFrequency
guardSleepFrequency gid activity = sleepFrequency $ guardActivity gid activity


-- Parse the input file

type Parser = Parsec Void Text

sc :: Parser ()
sc = L.space (skipSome spaceChar) CA.empty CA.empty
-- sc = L.space (skipSome (char ' ')) CA.empty CA.empty

lexeme  = L.lexeme sc
integer = lexeme L.decimal
symb = L.symbol sc

openP = symb "["
closeP = symb "]"
dashP = symb "-"
colonP = symb ":"


logFileP = many logEntryP
logEntryP = logify <$> timeStampP <*> eventP
    where logify t e = LogEntry {_logTime = t, _logEvent = e}


eventP = arrivesP <|> sleepsP <|> wakesP
arrivesP = Arrives <$> ((symb "Guard #") *> integer <* (symb "begins shift"))
sleepsP = Sleeps <$ (symb "falls asleep")
wakesP = Wakes <$ (symb "wakes up")

-- [1518-10-25 00:48]
timeStampP = between openP closeP timeStampInnerP
timeStampInnerP = dtify <$> integer <* dashP <*> integer <* dashP <*> integer <*> integer <* colonP <*> integer
    where dtify y mo d h mi = UTCTime (fromGregorian y (fromIntegral mo) (fromIntegral d)) (tify h mi)
          tify h mi = secondsToDiffTime ((h * 60) + mi) * 60

successfulParse :: Text -> [LogEntry]
successfulParse input = 
        case parse logFileP "input" input of
                Left  _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
                Right guardLog -> guardLog

1

u/rolandtritsch Dec 05 '18

ETA (Haskell for the JVM)

https://github.com/rolandtritsch/haskell-aoc-2018

Heavily refactored. Much easier to read/understand.

1

u/itsjeremeh Dec 05 '18

Javascript (Node) - Not the most efficient but it worked :)

const $input = myInput.split('\n');
const logRegex = /\[([\d|\:|\-\s]+)\] (.+)/
let currentGuardId = false;

const logs = $input.map(input => {
	let matches = logRegex.exec(input);
	
	if (!matches) {
	  return null;
	}
	
	return {
		date: new Date(matches[1]),
		event: matches[2],
	}
})
.filter(log => log !== null) // clear empties
.sort((a, b) => {
	if (a.date > b.date) {
	  return 1;
	}
	
	if (a.date < b.date) {
		return -1;
	}
	
	return 0;
})
.reduce((carry, log) => {
	if (log.event.indexOf('Guard') !== -1) {
		let [ match, guardId ] = /.+?\#(\d+).+?/.exec(log.event);
		currentGuardId = guardId;
	 
		if (!carry[guardId]) {
			carry[guardId] = {
				id: guardId,
				events: [],
			}
		}    
	  
		carry[guardId].events[carry[guardId].events.length] = [];
		return carry;
	}
	
	carry[currentGuardId].events[carry[currentGuardId].events.length - 1].push(log);
	
	return carry;
}, {});

const minsTotal = Object
.values(logs)
.map(log => {
	let timeline = {};
	let totalAsleep = 0;
	for (let i = 0; i < 60; i++) {
		timeline[i] = 0;
	}

	log.events.forEach((events) => {
		let fellAsleepAt = false;
		events.forEach(event => {
			if (event.event === 'falls asleep') {
				fellAsleepAt = event.date;
				return;
			}
			
			totalAsleep += event.date.getMinutes() - fellAsleepAt.getMinutes();
			for (let i = fellAsleepAt.getMinutes(); i < event.date.getMinutes(); i++) {
				timeline[i] += 1;
			}

		});
	})
	
	let popularMins = Object.keys(timeline).sort((a, b) => {
		if (timeline[a] < timeline[b]) {
			return 1;
		}
		
		if (timeline[b] < timeline[a]) {
	    	return -1;
		}
		
		return 0;
	}).shift();
	
	return {
	  id: log.id,
		timeline,
		totalAsleep,
		popularMins,
	}
})


const partOne = minsTotal.sort((a, b) => {
	if (a.totalAsleep < b.totalAsleep) {
		return 1;
	}
	
	if (a.totalAsleep > b.totalAsleep) {
		return -1;
	}
	
	return 0;
}).shift();

console.log('p1', partOne.id * partOne.popularMins);

const partTwo = minsTotal.map(guard => {
	return {
		id: guard.id,
		total: guard.timeline[guard.popularMins],
		minute: guard.popularMins
	}
}).sort((a, b) => {
	if (a.total < b.total) {
		return 1;
	}
	
	if (a.total > b.total) {
		return -1;
	}
	
	return 0;
}).shift();

console.log(partTwo.id * partTwo.minute)

1

u/siwica Dec 05 '18

My Python 3 solution for both parts:

import re
from collections import defaultdict
from datetime import datetime, timedelta


def date(line):
    return datetime.strptime(line[6:17], '%m-%d %H:%M')


with open("input.txt") as f:
    lines = sorted(f.readlines(), key=date)

sleep_times = defaultdict(lambda: [0]*60)
for line in lines:
    if 'Guard' in line:
        current_guard = int(re.search(r'#(\d+)', line).group(1))
    elif 'falls asleep' in line:
        sleep_start = date(line)
    elif 'wakes up' in line:
        sleep_end = date(line)
        while sleep_start < sleep_end:
            sleep_times[current_guard][sleep_start.minute] += 1
            sleep_start += timedelta(minutes=1)

for part, f in enumerate([sum, max], 1):
    guard_nr, minute = max(sleep_times.items(), key=lambda x: f(x[1]))
    minute_nr = minute.index(max(minute))
    print('Part {}: {}'.format(part, guard_nr * minute_nr))

1

u/[deleted] Dec 05 '18

Ha- HAHAA!!! I made it finally (in Rust). I'm not pasting it here because it sucks, but it's available online.

These are getting tough! I'm trying to be kind to myself about how bad my code is. It's still solving the problems!

1

u/fire1299 Dec 06 '18

Haskell

{-# LANGUAGE OverloadedStrings, TupleSections #-}

module Aoc18.Day4 where

import           Control.Applicative            ( (<|>) )
import qualified Data.Attoparsec.Text          as P
import qualified Data.IntMap.Strict            as M
import           Data.List                      ( foldl'
                                                , maximumBy
                                                , sortOn
                                                )
import           Data.Ord                       ( comparing )
import qualified Data.Text.IO                  as T

data Time = Time
  { year   :: !Int
  , month  :: !Int
  , day    :: !Int
  , hour   :: !Int
  , minute :: !Int
  } deriving (Eq, Ord, Show)

data Event
  = Begin !Int
  | Sleep
  | Wake
  deriving (Show)

main :: (M.IntMap (M.IntMap Int) -> a) -> IO a
main f =
  either error (f . toMap . sortOn fst) . P.parseOnly parseFile <$> T.readFile
    "day4.txt"
 where
  parseFile = P.sepBy' parseLine P.endOfLine
  parseLine = (,) <$> parseTime <* " " <*> parseEvent
  parseTime =
    Time
      <$> ("[" *> P.decimal <* "-")
      <*> P.decimal
      <*  "-"
      <*> P.decimal
      <*  " "
      <*> P.decimal
      <*  ":"
      <*> P.decimal
      <*  "]"
  parseEvent =
    Begin
      <$> ("Guard #" *> P.decimal <* " begins shift")
      <|> Sleep
      <$  "falls asleep"
      <|> Wake
      <$  "wakes up"

toMap :: [(Time, Event)] -> M.IntMap (M.IntMap Int)
toMap = fst . foldl' go (M.empty, (undefined, undefined))
 where
  go (m, (g, s)) (t, e) = case e of
    Begin h -> (m, (h, undefined))
    Sleep   -> (m, (g, minute t))
    Wake    -> (M.insertWith (M.unionWith (+)) g m' m, (g, undefined))
    where m' = M.fromDistinctAscList $ (, 1) <$> [s .. minute t - 1]

part1 :: M.IntMap (M.IntMap Int) -> Int
part1 =
  uncurry (*)
    . fmap (fst . maximumBy (comparing snd) . M.toList . fst)
    . maximumBy (comparing $ snd . snd)
    . M.toList
    . fmap ((,) <*> sum)

part2 :: M.IntMap (M.IntMap Int) -> Int
part2 =
  uncurry (*) . fmap fst . maximumBy (comparing $ snd . snd) . M.toList . fmap
    (maximumBy (comparing snd) . M.toList)

1

u/ShDragon Dec 06 '18

Only just running through this now and got totally confused by part 2.. Totally my own fault. I made my code look for the guard that was asleep the highest percentage of the time at that minute, not simply the most number of times, because that's clearly the way to find what's actually safest, so surely that's what the puzzle is asking for...

I discovered that guard #233 was asleep at minute 51 on every single one of his shifts. (And, interestingly, that is the ONLY guard/minute combination that is 100% guaranteed!) But that's not the right answer because some less consistent guard was assigned more shifts and reading comprehension is hard.

1

u/rjsberry Dec 08 '18

Slipped behind this week, work's been super busy!

RUST

Part 01:

use ::aoc18::day04::*;

const INPUT: &str = include_str!("input/04.txt");

fn main() {
    let sleep_times = batch_sleep_times(INPUT);
    let guard = longest_asleep(&sleep_times);
    let (minute, _) = part01::most_frequent_minute(&sleep_times[&guard]);

    println!("{}", guard * minute);
}

Part 02:

use ::aoc18::day04::*;

const INPUT: &str = include_str!("input/04.txt");

fn main() {
    let sleep_times = batch_sleep_times(INPUT);
    let (guard, minute) = part02::most_frequent_minute(&sleep_times);

    println!("{}", guard * minute);
}

Using the shared module ::aoc18::day04:

use std::{collections::HashMap, iter::once, str::FromStr};

use ::itertools::Itertools;
use ::lazy_static::lazy_static;
use ::regex::Regex;

pub type GuardId = u32;
pub type Minute = u32;

pub mod part01 {
    use super::*;

    /// Finds the most frequent minute asleep for a slice of periods spent asleep.
    /// Returns the minute and the number of times it was encountered in the slice.
    #[inline]
    pub fn most_frequent_minute(minutes: &[(Minute, Minute)]) -> (Minute, usize) {
        minutes
            .iter()
            .fold(HashMap::new(), |mut map, &(min, max)| {
                for minute in min..max {
                    map.entry(minute).and_modify(|m| *m += 1).or_insert(1);
                }
                map
            })
            .into_iter()
            .sorted_by(|a, b| b.1.cmp(&a.1))[0]
    }
}

pub mod part02 {
    use super::*;

    /// Finds the guard who spends a single minute asleep the most from a set of records.
    /// Returns the guard and the most frequent minute spent asleep.
    pub fn most_frequent_minute(
        map: &HashMap<GuardId, Vec<(Minute, Minute)>>,
    ) -> (GuardId, Minute) {
        let result = map
            .iter()
            .map(|(id, times)| (id, part01::most_frequent_minute(&times)))
            .sorted_by(|a, b| (b.1).1.cmp(&(a.1).1))[0];
        (*result.0, (result.1).0)
    }
}

/// Batch a raw set of records into a map of guard ID's and the times that guard spent asleep.
pub fn batch_sleep_times(input: &str) -> HashMap<GuardId, Vec<(Minute, Minute)>> {
    input
        .lines()
        // YYYY-MM-DD HH:MM will sort itself chronologically with no extra work
        .sorted()
        .into_iter()
        // Chunk the sorted input into the following format:
        //
        // ```
        // let batched = &[
        //     vec!["guard_0", "asleep_0", "awake_0", "asleep_1", "awake_1", ... "awake_n"],
        //     vec!["guard_1", "asleep_0", "awake_0", "asleep_1", "awake_1", ... "awake_n"],
        //     ...
        //     vec!["guard_n", "asleep_0", "awake_0", "asleep_1", "awake_1", ... "awake_n"],
        // ];
        // ```
        .batching(|it| {
            if let Some(line) = it.next() {
                if guard_id(line).is_some() {
                    return Some(
                        once(line)
                            .chain(it.take_while_ref(|l| guard_id(l).is_none()))
                            .collect::<Vec<_>>(),
                    );
                }
            }
            None
        })
        // Parse guard ids and minutes from raw records. If the records cannot be parsed they
        // are filtered out, but input guarantees this is never actually the case. The result
        // is predicated on the value from the previous batch containing more than simply a guard
        // ID as this contains no actual useful info.
        .filter_map(|g| extract_sleep_times(&g).filter(|_| g.len() > 1))
        .fold(HashMap::new(), |mut map, (id, sleep_times)| {
            map.entry(id)
                .and_modify(|v| v.extend(&sleep_times))
                .or_insert(sleep_times);
            map
        })
}

#[inline]
fn capture_first_match<F: FromStr>(re: &Regex, text: &str) -> Option<F> {
    re.captures(text)
        .and_then(|captures| captures.get(1).and_then(|c| c.as_str().parse().ok()))
}

#[inline]
fn minute(entry: &str) -> Option<Minute> {
    lazy_static! {
        static ref RE: Regex = Regex::new(r"\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\]").unwrap();
    }
    capture_first_match(&RE, entry)
}

#[inline]
fn guard_id(entry: &str) -> Option<GuardId> {
    lazy_static! {
        static ref RE: Regex = Regex::new(r"Guard #(\d+)").unwrap();
    }
    capture_first_match(&RE, entry)
}

#[inline]
pub fn longest_asleep(map: &HashMap<GuardId, Vec<(Minute, Minute)>>) -> GuardId {
    *map.iter()
        .map(|(id, times)| (id, times.iter().fold(0, |acc, pair| acc + pair.1 - pair.0)))
        .sorted_by(|a, b| b.1.cmp(&a.1))[0]
        .0
}

fn extract_sleep_times(entries: &[&str]) -> Option<(GuardId, Vec<(Minute, Minute)>)> {
    if let Some(id) = guard_id(entries[0]) {
        let mut sleep_times = vec![];
        for (start, stop) in entries[1..].iter().tuples() {
            if let (Some(a), Some(b)) = (minute(start), minute(stop)) {
                sleep_times.push((a, b));
            }
        }
        Some((id, sleep_times))
    } else {
        None
    }
}

1

u/vini_2003 Dec 10 '18 edited Dec 12 '18

C++ (part I & II):

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <fstream>
#include <regex>
 
using namespace std;
 
auto regexID = regex("#([0-9]+)");
auto regexFW = regex(":([0-9]+)");
smatch matches;
 
string input_path = "C:\\Users\\vini2003\\Documents\\Advent of Code\\Day 4\\input";
 
class Guard {
    public:
        void SetEmpty() {
            ID = NULL;
            sleep.clear();
            awake.clear();
        }
        void SetID(int tmpID) {
            ID = tmpID;
        }
        void SetTime(vector<int> tmpsleep, vector<int> tmpAwake) {
            sleep = tmpsleep;
            awake = tmpAwake;
        }
        int MaxSleep() {
            int tmpsleep = 0;
            for (int i = 0; i < sleep.size(); i++) {
                tmpsleep += (awake[i] - sleep[i]);
            }
            return tmpsleep;
        }
        int MaxMinute() {
            int buffer[60] = { 0 };
            for (int i = 0; i < sleep.size(); i++) {
                for (int k = sleep[i]; k <= awake[i]; k++) {
                    buffer[k]++;
                }
            }
            auto MaxMinute = max_element(begin(buffer), end(buffer));;
            amount = *max_element(begin(buffer), end(buffer));
            return MaxMinute - begin(buffer);
        }
        int MaxAmount() {
            MaxMinute();
            return amount;
        }
 
    private:
        int ID;
        int amount;
        vector<int> sleep;
        vector<int> awake;
};
 
void read() {
    Guard guard;
    set<int>IDList;
    map<int, pair<vector<int>, vector<int>>> guardData;
    vector<string> storage;
    string line;
    ifstream input(input_path);
    while (getline(input, line)) {
        storage.push_back(line);
    }
    sort(storage.begin(), storage.end());
    int ID = 0;
    for (string str : storage) {
        if (str.find("#") != string::npos) {
            regex_search(str, matches, regexID);
            IDList.insert(stoi(matches[1]));
            ID = stoi(matches[1]);
        }
        else {
            regex_search(str, matches, regexFW);
            if (str.find("l") != string::npos) {
                guardData[ID].first.push_back(stoi(matches[1]));
            }
            if (str.find("w") != string::npos) {
                guardData[ID].second.push_back(stoi(matches[1]));
            }
        }
    }
    int minute = 0, sleep = 0, tmpID = 0;
    map<int, int> GuardSleep;
    for (int myID : IDList) {
        guard.SetID(myID);
        guard.SetTime(guardData[myID].first, guardData[myID].second);
        GuardSleep[myID] = guard.MaxMinute();
        if (guard.MaxSleep() > sleep) {
            sleep = guard.MaxSleep();
            minute = guard.MaxMinute();
            tmpID = myID;
        }
        guard.SetEmpty();
    }
    cout << "Part I: " << minute * tmpID << endl;
    minute = 0, sleep = 0, tmpID = 0;
    map<int, pair<int, int>> myMap;
    for (int myID : IDList) {
        guard.SetID(myID);
        guard.SetTime(guardData[myID].first, guardData[myID].second);
        myMap[myID] = {guard.MaxMinute(), guard.MaxAmount()};
        if (myMap[myID].second > minute) {
            minute = myMap[myID].second;
            sleep = myMap[myID].first;
            tmpID = myID;
        }
        guard.SetEmpty();
    }
    cout << "Part II: " << sleep * tmpID << endl;
}
 
int main() {
    read();
}

1

u/lib20 Dec 11 '18

In Red, part 1, Execution time: 0:00:00.345739

Red []

time-start: now/time/precise
input-txt: sort read/lines %input.txt		 
input-txt: form input-txt
input-txt: load input-txt

actions: [begins falls wakes]

shifts: parse input-txt [
    collect some collect [
        into [keep pick to end]
        opt [skip set ID issue!] keep (load form ID)
        set action skip keep (index? find actions action) skip
    ]
]


shifts: new-line/all shifts on

range: func [mi [integer!]
			ma [integer!]
			/local bl [block!]
		][
	bl: copy []
	while [mi < ma] [
		append bl mi
		mi: mi + 1
	]	
	bl
]

day: guard: 0
sleep-start: 00:00

guards-sleeping-profile: #()

foreach shift shifts [
	if shift/4 = 1 [
		guard: shift/3
		unless find guards-sleeping-profile guard [
			; create minute profile for that guard
			minute-profile: copy #()
			repeat mi 60 [put minute-profile mi - 1 0]
			put guards-sleeping-profile guard minute-profile
		]
	]
	if shift/4 = 2 [sleep-start: shift/2 sleep-start: sleep-start/minute]
	if shift/4 = 3 [
		sleep-end: shift/2
		sleep-end: sleep-end/minute
		minute-profile: select guards-sleeping-profile guard	

		while [sleep-start < sleep-end] [

			put minute-profile sleep-start (select minute-profile sleep-start) + 1
			sleep-start: sleep-start + 1
		]
		put guards-sleeping-profile guard minute-profile
	]
]

guard-sleeps-most: 0
max-minutes-slept: 0
foreach [guard minute-profile] body-of guards-sleeping-profile [
	guard-slept: 0
	foreach [minute slept] body-of minute-profile [
		guard-slept: guard-slept + slept	
	]	
	if guard-slept > max-minutes-slept [
		guard-sleeps-most: guard
		max-minutes-slept: guard-slept
	]
]


guard-profile: select guards-sleeping-profile guard-sleeps-most
minute-most-slept: 0
max-minutes: 0
foreach [minute slept] body-of guard-profile [
	if slept > max-minutes [	
		max-minutes: slept
		minute-most-slept: minute
	]
]

print rejoin ["Execution time: " now/time/precise - time-start]
probe rejoin ["guard sleeps most: " guard-sleeps-most "    sleeps most at minute: " minute-most-slept "    for " max-minutes " times"]
print guard-sleeps-most * minute-most-slept

1

u/lib20 Dec 11 '18

In Red, part 2, Execution time: 0:00:00.33641

Red []

time-start: now/time/precise
input-txt: sort read/lines %input.txt	 
input-txt: form input-txt
input-txt: load input-txt

actions: [begins falls wakes]

shifts: parse input-txt [
    collect some collect [
        into [keep pick to end]
        opt [skip set ID issue!] keep (load form ID)
        set action skip keep (index? find actions action) skip
    ]
]


shifts: new-line/all shifts on

range: func [mi [integer!]
			ma [integer!]
			/local bl [block!]
		][
	bl: copy []
	while [mi < ma] [
		append bl mi
		mi: mi + 1
	]	
	bl
]

day: guard: 0
sleep-start: 00:00

guards-sleeping-profile: #()

foreach shift shifts [
	if shift/4 = 1 [
		guard: shift/3
		unless find guards-sleeping-profile guard [
			; create minute profile for that guard
			minute-profile: copy #()
			repeat mi 60 [put minute-profile mi - 1 0]
			put guards-sleeping-profile guard minute-profile
		]
	]
	if shift/4 = 2 [sleep-start: shift/2 sleep-start: sleep-start/minute]
	if shift/4 = 3 [
		sleep-end: shift/2
		sleep-end: sleep-end/minute
		minute-profile: select guards-sleeping-profile guard	

		while [sleep-start < sleep-end] [

			put minute-profile sleep-start (select minute-profile sleep-start) + 1
			sleep-start: sleep-start + 1
		]
		put guards-sleeping-profile guard minute-profile
	]
]

guard-sleeps-most: 0
max-minutes-slept: 0
minute-most-slept: -1
foreach [guard minute-profile] body-of guards-sleeping-profile [
	foreach [minute slept] body-of minute-profile [
		if slept > max-minutes-slept [
			max-minutes-slept: slept
		    guard-sleeps-most: guard
			minute-most-slept: minute
		]
	]	
]

print rejoin ["Execution time: " now/time/precise - time-start]
print rejoin ["part 02: " guard-sleeps-most * minute-most-slept]

1

u/[deleted] Dec 13 '18

Common lisp:

Too verbose for my taste, might overhaul it with external libs.

(defun read-input ()
  (let ((sorted (with-open-file (in "04.input")
                  (loop for line = (read-line in nil)
                        while line collect line into lines
                        finally (return (sort lines #'string<))))))
    (loop for line in sorted
          for char = (char line 25)
          for id = (if (char= char #\#)
                       (parse-integer line :start 26 :end (position #\space line :start 26))
                       id)
          for prev-tp = curr-tp
          for curr-tp = (parse-integer line :start 15 :end 17)
          when (char= char #\u) collect (list id prev-tp curr-tp))))

(defun strategy-1 (asleep)
  (destructuring-bind (id minutes sum)
      (reduce (lambda (a b) (if (> (third a) (third b)) a b))
              (loop for k being the hash-keys of asleep using (hash-value v)
                    collect (list k v (reduce #'+ v))))
    (* id (position (reduce #'max minutes) minutes))))

(defun strategy-2 (asleep)
  (destructuring-bind (id minutes max)
      (reduce (lambda (a b) (if (> (third a) (third b)) a b))
              (loop for k being the hash-keys of asleep using (hash-value v)
                    collect (list k v (reduce #'max v))))
    (* id (position max minutes))))

(defun main ()
  (let ((intervals (read-input))
        (asleep (make-hash-table)))
    (loop for int in intervals
          for (arr exists) = (multiple-value-list (gethash (first int) asleep (make-array 60)))
          do (loop for i from (second int) below (third int)
                   do (incf (aref arr i)))
             (unless exists
               (setf (gethash (first int) asleep) arr)))
    (format t "Result 4a: ~d~%" (strategy-1 asleep))
    (format t "Result 4b: ~d~%" (strategy-2 asleep))))

;; CL-USER> (time (main))
;; Result 4a: 77941
;; Result 4b: 35289
;; Evaluation took:
;;   0.005 seconds of real time
;;   0.005114 seconds of total run time (0.005114 user, 0.000000 system)
;;   100.00% CPU
;;   11,035,934 processor cycles
;;   425,168 bytes consed

1

u/cerrosafe Dec 21 '18

Lua

I am very behind, but I'm still participating. Trying to catch up. I tried to re-invent the wheel with my sorting of the entries, only to realize that the Lua table sort can do it just fine.

input = "inputs/day4.txt"

function get_guard_id_or_nil(message)
   local tok = string.gmatch(message, "#%d+")
   local guard_id = tok()
   if guard_id ~= nil then
      guard_id = string.gsub(guard_id, "#", "")
      guard_id = tonumber(guard_id)
   end
   return guard_id
end

-- Given something like "12:34" return "34"
function convert_time_to_minutes(time)
   local time_tok = string.gmatch(time, "[^:]+")
   time_tok()
   local minutes = tonumber(time_tok())
   return minutes
end

function parse_line_to_record(line)
   record = {}
   record_tokenizer = string.gmatch(line, "[^%]]+")
   date_part = record_tokenizer()
   cleaned = string.gsub(date_part, "%[", "")

   record["time"] = cleaned
   record["message"] = record_tokenizer()
   -- Convenient for finding intervals
   record["minutes"] = convert_time_to_minutes(record["time"])
   return record
end

function collect_sleeping(records)
   local guard_id = nil
   for index, value in ipairs(records) do
      local new_guard_id = get_guard_id_or_nil(value["message"])
      if new_guard_id ~= nil then
         guard_id = new_guard_id
      end
      if string.find(value["message"], "asleep") ~= nil then
         local asleep_at = value["minutes"]
         local woke_at = records[index+1]["minutes"]

         -- Increment all the minutes that the guard was asleep for
         for i=asleep_at, woke_at-1 do
            if guards[guard_id]["minutes"][i] == nil then
               guards[guard_id]["minutes"][i] = 0
            end
            guards[guard_id]["minutes"][i] = guards[guard_id]["minutes"][i] + 1
         end

         -- Add to the total minutes the guard was sleeping: [start, end)
         guards[guard_id]["total"] = guards[guard_id]["total"] + (woke_at - asleep_at)
      end
   end
end

function max_minute(minutes)
   local max, max_index = 0, nil
   for index, value in pairs(minutes) do
      if value > max then max, max_index = value, index end
   end
   return max_index, max
end

function max_sleeping_guard(guards)
   local max_gid, minutes_slept = nil, 0
   for gid, values in pairs(guards) do
      if values["total"] > minutes_slept then max_gid, minutes_slept = gid, values["total"] end
   end
   return max_gid
end

function sort(left, right)
   return left["time"] < right["time"]
end

-- Actual script follows here

records = {}
guards = {}

for line in io.lines(input) do
   local new_record = parse_line_to_record(line)
   table.insert(records, new_record)
   local gid = get_guard_id_or_nil(new_record["message"])
   if gid ~= nil then
      guards[gid] = { minutes = {}; total = 0 }
   end
end

table.sort(records, sort)
collect_sleeping(records)

sleepy_gid = max_sleeping_guard(guards)
adjusted_max_minute = max_minute(guards[sleepy_gid]["minutes"])
print("Part 1 answer: " .. sleepy_gid * adjusted_max_minute)

-- Chew up the results to solve part 2
local m_minute, max_gid, max_count = 0, 0, 0
for gid, value in pairs(guards) do
   local minute, count = max_minute(value["minutes"])
   if count > max_count then
      m_minute = minute
      max_gid = gid
      max_count = count
   end
end
print("Part 2 answer: " .. max_gid * m_minute)