diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-05 22:42:46 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-05 22:42:46 -0800 |
commit | 659d106cc6e92fc8ab89044b3ca260a26a9647ca (patch) | |
tree | 46865d322bb4ab5888aee8eb66c08d02e67e05b6 | |
parent | b8e6840a1780b4358bbd7c483720a14c87d93788 (diff) | |
download | AdventOfCode2021-659d106cc6e92fc8ab89044b3ca260a26a9647ca.tar.gz |
solution for day 6
-rw-r--r-- | day6.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/day6.py b/day6.py new file mode 100644 index 0000000..c964cd9 --- /dev/null +++ b/day6.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +with open("day6.txt") as data: + population = [num for num in map(int, next(data).strip().split(','))] + + +memo = {} +def count_children(age, time): + if (age, time) not in memo: + result = 0 + if time == 0: + result = 1 + elif age == 0: + result = count_children(6, time - 1) + count_children(8, time - 1) + else: + result = count_children(age - 1, time - 1) + memo[(age, time)] = result + return memo[(age, time)] + +# part 1 +print(sum(map(lambda x: count_children(x, 80), population))) + +# part 2 +print(sum(map(lambda x: count_children(x, 256), population))) |