Skip to content

Commit cc327f3

Browse files
author
farbfetzen
committed
Solve 2021 day 6
1 parent aa0135d commit cc327f3

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
| Year | Stars |
55
|------|------:|
6-
| 2021 | 10|
6+
| 2021 | 12|
77
| 2020 | 50 ⭐ |
88
| 2019 | 44 ⭐ |
99
| 2018 | 8 ⭐ |

input/2021-06-sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,4,3,1,2

python/2021/day06.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# https://adventofcode.com/2021/day/6
2+
3+
4+
def get_data(filename):
5+
with open(filename) as file:
6+
return [int(x) for x in file.read().split(",")]
7+
8+
9+
def simulate(fish_ages):
10+
count = [fish_ages.count(i) for i in range(9)]
11+
result_at_day_80 = 0
12+
for day in range(1, 256+1):
13+
zeros = count[0]
14+
count[:-1] = count[1:]
15+
count[6] += zeros
16+
count[8] = zeros
17+
if day == 80:
18+
result_at_day_80 = sum(count)
19+
return result_at_day_80, sum(count)
20+
21+
22+
sample_data = get_data("../../input/2021-06-sample.txt")
23+
challenge_data = get_data("../../input/2021-06-input.txt")
24+
25+
if __name__ == "__main__":
26+
sample_part_1, sample_part_2 = simulate(sample_data)
27+
assert sample_part_1 == 5934
28+
assert sample_part_2 == 26984457539
29+
30+
challenge_part_1, challenge_part_2 = simulate(challenge_data)
31+
print(challenge_part_1) # 372300
32+
print(challenge_part_2) # 1675781200288

python/2021/test2021.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import day03
66
import day04
77
import day05
8+
import day06
89

910

1011
class Test2021(unittest.TestCase):
@@ -47,6 +48,15 @@ def test_05(self):
4748

4849
self.assertEqual(day05.part_1(day05.challenge_data), 6267)
4950
self.assertEqual(day05.part_2(day05.challenge_data), 20196)
51+
52+
def test_06(self):
53+
sample_part_1, sample_part_2 = day06.simulate(day06.sample_data)
54+
self.assertEqual(sample_part_1, 5934)
55+
self.assertEqual(sample_part_2, 26984457539)
56+
57+
challenge_part_1, challenge_part_2 = day06.simulate(day06.challenge_data)
58+
self.assertEqual(challenge_part_1, 372300)
59+
self.assertEqual(challenge_part_2, 1675781200288)
5060

5161

5262
if __name__ == "__main__":

0 commit comments

Comments
 (0)