DEV Community

Discussion on: AoC Day 4: Repose Record

Collapse
 
lschultebraucks profile image
Lasse Schultebraucks
from collections import defaultdict


def main():
    guard_sleep_dict = calculate_guard_sleep_dict()
    part_one(guard_sleep_dict)
    part_two(guard_sleep_dict)


def calculate_guard_sleep_dict():
    with open('input.txt', 'r') as data:

        sorted_data = sorted(data)  # This will work too... No extra key required

        guard_sleep_dict = defaultdict(lambda: [0 for x in range(60)])
        current_guard = -1
        start_sleeping = -1
        for line in sorted_data:
            if line[25] == "#":
                current_guard = line.split()[3]
            elif line[25] == "a":
                start_sleeping = int(line[15:17])
            else:  # "wakes up"
                end_sleeping = int(line[15:17])
                for x in range(start_sleeping, end_sleeping):
                    guard_sleep_dict[current_guard][x] += 1

    return guard_sleep_dict


def part_one(guard_sleep_dict):
    guard = sorted(guard_sleep_dict.keys(), key=lambda g: -sum(guard_sleep_dict[g]))[0]

    gh = guard_sleep_dict[guard]
    minute = gh.index(max(gh))
    print int(guard[1:]) * minute


def part_two(guard_sleep_dict):
    guard = sorted(guard_sleep_dict.keys(), key=lambda g: -max(guard_sleep_dict[g]))[0]
    gh = guard_sleep_dict[guard]
    minute = gh.index(max(gh))
    print int(guard[1:]) * minute


if __name__ == '__main__':
    main()

First missunderstood the task, so I wasted my whole time before university for the wrong solution... Fortunately I had some time after the first seminar to fix my solution.

Then found out that I can just call sorted and sort the data and I don't need any extra key for this. This made solving the puzzle much easier.

Collapse
 
deciduously profile image
Ben Lovy

Did that yesterday! Elegantly solved exactly the wrong problem, had to go back and re-read the whole page to figure out what I was doing wrong. Devastating.

Collapse
 
lschultebraucks profile image
Lasse Schultebraucks

Yeah it is just super frustrating... Especially if you notice then that you have to move on to do other things and will loose expensive points on your private leaderboards 🙈
Tomorrow I will read the task at least twice before I start typing.

Thread Thread
 
deciduously profile image
Ben Lovy

Damn life, getting in the way of AoC. The very nerve.

Collapse
 
quoll profile image
Paula Gearon

I used the same approach, but I really like the tools you have for searching the structure. The Clojure ones work, but were much more manual, making the code a little more clunky.