News

Hints for Day 04 and 05

Written on 05.12.2022 10:01 by Sven Rahmann

Good morning,

yesterday's problem (day 04) was not too hard (it helps do draw pictures). Remember that you can use split with an argument, like line.split(','), which will split on comma instead of whitespace. Once you have extracted integer start end end position of elf 1 and elf 2, you can just check whether containment holds (or another condition in part 2).

An elegant functional-style solution would be possible here for both parts, but of course is not required.

 

For today's problem (day 05), we should point out that you can use a list as a stack: The bottom is element 0, the top is the last element (-1). To push something on a stack/list S, use S.append(item). To remove (and remember) and item from the top, use item = S.pop().

There is one gotcha "between" parts 1 and 2. Part 2 requires you to re-start from the same configuration as given in the input file. Either you read the input file again (safe), or you make a deep copy (not a normal copy) of your list of stacks before you modify it in part 1. (You do not want to start part 2 with the modified stacks after part 1!). It can be done as follows:

from copy import deepcopy

# Let S0 be the original list of stacks

S = deepcopy(S0)  # create a copy for modification in part 1; S0 remains the original

 

Privacy Policy | Legal Notice
If you encounter technical problems, please contact the administrators.