Skip to content

Commit 3183553

Browse files
committed
Solution to puzzle from Day 11 added.
1 parent 930af83 commit 3183553

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

11/input.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
4764745784
2+
4643457176
3+
8322628477
4+
7617152546
5+
6137518165
6+
1556723176
7+
2187861886
8+
2553422625
9+
4817584638
10+
3754285662

11/puzzle.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Dec 11 06:05:15 2021
4+
@author: Marysia
5+
Solution to puzzle from https://adventofcode.com/2021/day/11
6+
"""
7+
import numpy as np
8+
9+
def open_data(in_name):
10+
'''
11+
Opens the data for the puzzle.
12+
'''
13+
with open(in_name, 'r') as file:
14+
out_data = file.readlines()
15+
return [x.strip() for x in out_data]
16+
17+
def input_to_array(in_list):
18+
'''
19+
Transforms input to array.
20+
'''
21+
list_out = []
22+
for row in in_list:
23+
list_out.append([int(x) for x in row])
24+
return list_out
25+
26+
def increase_surroundings(in_list, row_ind, column_ind):
27+
'''
28+
Increases the values of octopuses surrounding the exploded one.
29+
'''
30+
add_row = [-1, 0, 0, 1, -1, -1, 1, 1]
31+
add_column = [0, -1, 1, 0, -1, 1, 1, -1]
32+
for r_ind, c_ind in zip(add_row, add_column):
33+
if all([row_ind+r_ind >= 0, column_ind+c_ind >= 0, row_ind+r_ind < len(in_list[0]),
34+
column_ind+c_ind < len(in_list)]):
35+
if in_list[row_ind+r_ind, column_ind+c_ind] != 0:
36+
in_list[row_ind+r_ind, column_ind+c_ind] += 1
37+
return in_list
38+
39+
def solve_part_one(in_list, steps):
40+
'''
41+
Solves part one.
42+
'''
43+
in_list = input_to_array(in_list)
44+
increment_table = np.ones([len(in_list), len(in_list[0])])
45+
total_flashes = 0
46+
for _ in range(steps):
47+
in_list = in_list + increment_table
48+
change_made = 1
49+
while change_made == 1:
50+
change_made = 0
51+
for row_ind, row in enumerate(in_list):
52+
for column_ind, item in enumerate(row):
53+
if item > 9:
54+
in_list[row_ind, column_ind] = 0
55+
change_made = 1
56+
in_list = increase_surroundings(in_list, row_ind, column_ind)
57+
for item in np.nditer(in_list):
58+
if item == 0:
59+
total_flashes += 1
60+
return total_flashes
61+
62+
def solve_part_two(in_list):
63+
'''
64+
Solves part two.
65+
'''
66+
in_list = np.array(input_to_array(in_list))
67+
increment_table = np.ones([len(in_list), len(in_list[0])])
68+
step = 1
69+
while True:
70+
total_flashes = 0
71+
in_list = in_list + increment_table
72+
change_made = 1
73+
while change_made == 1:
74+
change_made = 0
75+
for row_ind, row in enumerate(in_list):
76+
for column_ind, item in enumerate(row):
77+
if item > 9:
78+
in_list[row_ind, column_ind] = 0
79+
change_made = 1
80+
in_list = increase_surroundings(in_list, row_ind, column_ind)
81+
for item in np.nditer(in_list):
82+
if item == 0:
83+
total_flashes += 1
84+
if total_flashes == len(in_list)*len(in_list[0]):
85+
return step
86+
step += 1
87+
return -1
88+
89+
assert solve_part_one(open_data('test.txt'), 100) == 1656
90+
assert solve_part_two(open_data('test.txt')) == 195
91+
92+
print(solve_part_one(open_data('input.txt'), 100))
93+
print(solve_part_two(open_data('input.txt')))

11/test.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
5483143223
2+
2745854711
3+
5264556173
4+
6141336146
5+
6357385478
6+
4167524645
7+
2176841721
8+
6882881134
9+
4846848554
10+
5283751526

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ This repository contains my solutions to [Advent of Code 2021](https://adventofc
1111
07 [--- Day 7: The Treachery of Whales ---](https://adventofcode.com/2021/day/7)\
1212
08 [--- Day 8: Seven Segment Search ---](https://adventofcode.com/2021/day/8)\
1313
09 [--- Day 9: Smoke Basin ---](https://adventofcode.com/2021/day/9)\
14-
10 [--- Day 10: Syntax Scoring ---](https://adventofcode.com/2021/day/10)
15-
14+
10 [--- Day 10: Syntax Scoring ---](https://adventofcode.com/2021/day/10)\
15+
11 [--- Day 11: Dumbo Octopus ---](https://adventofcode.com/2021/day/11)

0 commit comments

Comments
 (0)