# Advent of Code session Dec 19, 2024
- This page: https://hackmd.io/@coderefinery/advent-of-code
- Dec 19, 9-11 CET/ 10-12 EET
- Zoom: https://cscfi.zoom.us/j/66165768646
- Advent of Code: https://adventofcode.com/
## How we can try to approach this
(Thanks to Marijn van Vliet for suggesting the approach below)
1. Read the puzzle together and walk through the examples together. Point out interesting aspects of the puzzle to each other.
2. Give some time for everybody to come up with a "gut feeling" solution.
3. When we (or most of us) feel like we have an inkling of a way to tackle the problem:
1. everybody gets a turn to explain what kind of approach they were thinking about
2. we try to think through each approach together: could this work? edge cases? difficult/easy to implement?
3. we decide together on an approach to try and actually implement
4. We start writing code:
1. People can write code together on a shared screen
2. People can write their own solution in their chosen programming language if they want
3. Back to step 3.3 if our chosen approach ends up not working at all
5. Back to step 1 for part 2 of the puzzle.
6. We share and discuss our different solutions in different programming languages, see how things play out in Python vs Julia vs Rust vs Java or something.
Marijn's solution to part 1:
```python=
import re
with open("day19.txt") as f:
available_towels = next(f).strip().split(", ")
next(f)
designs = f.read().strip().split("\n")
pattern = re.compile(rf"^({'|'.join(available_towels)})+$")
print("part 1:", sum(1 if pattern.match(design) else 0 for design in designs))
```
or as a tree search:
```python=
def is_possible(design, available_towels):
"""Check if it is possible to make the design out of the available towels.
Parameters
----------
design : str
The design to create.
available_towels : list
The list of available towels.
Returns
-------
possible : bool
True if it is possible to build the design.
"""
# When designing a recursive function, always do the end conditions first.
if len(design) == 0:
return True
# Try all possible towels to see if they would be a good beginning for the design.
for towel in available_towels:
if design.startswith(towel) and is_possible(design[len(towel):], available_towels):
return True
return False
print("part 1:", sum(1 if is_possible(design, available_towels) else 0 for design in designs))
```
Somehow this does not seem different, but it works on Radovan's input
``` python=
from functools import cache
with open("input.txt") as f:
available_towels = next(f).strip().split(", ")
next(f)
designs = f.read().strip().split("\n")
@cache
def is_possible(design):
"""Check if it is possible to make the design out of the available towels.
Parameters
----------
design : str
The design to create.
available_towels : list
The list of available towels.
Returns
-------
possible : bool
True if it is possible to build the design.
"""
# When designing a recursive function, always do the end conditions first.
if len(design) == 0:
return True
# Try all possible towels to see if they would be a good beginning for the design.
for towel in available_towels:
if design.startswith(towel) and is_possible(design.removeprefix(towel)):
return True
return False
print("part 1:", sum(1 if is_possible(design) else 0 for design in designs))
```
Summing up possible lines using a filter:
```python
print("part 1:", len(list(filter(is_possible, designs))))
```
Radovan's input: https://github.com/bast/advent-of-code/blob/main/2024/day-19/input.txt