diff options
Diffstat (limited to '11')
-rw-r--r-- | 11/a.rb | 51 | ||||
-rw-r--r-- | 11/b.rb | 58 | ||||
-rw-r--r-- | 11/input.txt | 95 |
3 files changed, 204 insertions, 0 deletions
@@ -0,0 +1,51 @@ +input = File.readlines('input.txt').map { |l| ".#{l.chomp}.".split '' } +input.unshift(['.'] * input[0].length) +input.push(['.'] * input[0].length) + +next_map = [] +input.size.times do + row = [] + input[0].size.times do + row.push '.' + end + next_map.push row +end + +changed = true +current_count = 0 +while changed + current_count = 0 + changed = false + (1...input.length-1).each do |i| + (1...input[0].length-1).each do |j| + if input[i][j] != '.' + count = 0 + (-1..1).each do |di| + (-1..1).each do |dj| + if !(di == 0 && dj == 0) + count += input[i+di][j+dj] == '#' ? 1 : 0 + end + end + end + + if count == 0 && input[i][j] == 'L' + next_map[i][j] = '#' + changed = true + elsif count >= 4 && input[i][j] == '#' + next_map[i][j] = 'L' + changed = true + else + next_map[i][j] = input[i][j] + end + + current_count += next_map[i][j] == '#' ? 1 : 0 + end + end + end + + tmp = input + input = next_map + next_map = tmp +end + +puts current_count @@ -0,0 +1,58 @@ +input = File.readlines('input.txt').map { |l| ".#{l.chomp}.".split '' } +input.unshift(['.'] * input[0].length) +input.push(['.'] * input[0].length) + +next_map = [] +input.size.times do + row = [] + input[0].size.times do + row.push '.' + end + next_map.push row +end + +changed = true +current_count = 0 +while changed + current_count = 0 + changed = false + (1...input.length-1).each do |i| + (1...input[0].length-1).each do |j| + if input[i][j] != '.' + count = 0 + (-1..1).each do |di| + (-1..1).each do |dj| + if !(di == 0 && dj == 0) + offset_i = i + di + offset_j = j + dj + while offset_i > 0 && offset_i < input.length - 1 && offset_j > 0 && offset_j < input[0].length - 1 && input[offset_i][offset_j] == '.' + offset_i += di + offset_j += dj + end + + count += input[offset_i][offset_j] == '#' ? 1 : 0 + end + end + end + + if count == 0 && input[i][j] == 'L' + next_map[i][j] = '#' + changed = true + elsif count >= 5 && input[i][j] == '#' + next_map[i][j] = 'L' + changed = true + else + next_map[i][j] = input[i][j] + end + + current_count += next_map[i][j] == '#' ? 1 : 0 + end + end + end + + tmp = input + input = next_map + next_map = tmp +end + +puts current_count diff --git a/11/input.txt b/11/input.txt new file mode 100644 index 0000000..8f2a9e1 --- /dev/null +++ b/11/input.txt @@ -0,0 +1,95 @@ +LLLLLLLL.LLLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLL.L +LLLLLLLLLLLLLLLLLLL.L.LL.LLL.LLLLL.LLLLLLLLLLL.LL.LLLLL.LLLLLLLL.LLLLLLLLL.LLL.LLLLLLLLLLLLLLLLLLL +LLLLLLLLLLLLL.LLLLLLLLLLLLLL.LL.LL.LLLLLLL.LLLLLL.LLLLLLLLL.LLLL.LLLLL.LLLLLLL.LLLLLL.LLLLLLLLLLLL +LLLLLLLL.LLLL.LLLLL..LLLLLLL.L.LLL.LLLLLLL.LLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLL +LLLLLL.L.LLLL.L.LLL.LLLLLLLLLLLLLL.LLL.LLL.LLLLLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLLLLLLLL +LLLLLLLL.LLLL.LLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLL.L.LLLLLLLLLLLLLLLLLLL +L...LLL...L....L......L..L....LL...L.LLL...............L..LL.LLL.LL..L..........L......L.L....L.LL +LLLLLLLL.LLLL.LLLLL.LLLLLLLL.LLLLL.L.LLLL.LLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLL +LLLLLLLL.LLLL.LLLLL.LLLLLL.L.LLLLL.LLL.LLL.LLLLLL.LLLLL.LLLL.LLL.LLLLL.LLLLLLLLLLLL.L.LLLLLLLLL.LL +LLLLLLLL.LLLL.L.LLL.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