-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.zig
158 lines (135 loc) · 4.33 KB
/
day11.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const std = @import("std");
const print = std.debug.print;
const assert = std.debug.assert;
const expectEqual = std.testing.expectEqual;
const mem = std.mem;
const absInt = std.math.absInt;
const indexOfScalar = std.mem.indexOfScalar;
const max = std.math.max;
const min = std.math.min;
const rotate = std.mem.rotate;
const round = std.math.round;
const sort = std.sort.sort;
const BoundedArray = std.BoundedArray;
const Parser = @import("lib/parse3.zig").Parser;
const REAL_INPUT = @embedFile("inputs/day11.txt");
pub fn main() !void {
print("Part 1: {}\n", .{try part1(&Parser.init(REAL_INPUT))});
print("Part 2: {}\n", .{try part2(&Parser.init(REAL_INPUT))});
}
fn part1(input: *Parser) !u64 {
var world_raw = try BoundedArray(u8, 100).init(0);
const width: usize = 10;
for (input.source) |c| {
if (c == '\n') continue;
try world_raw.append(c - '0');
}
var world: [100]u8 = world_raw.buffer;
var flashes: u64 = 0;
var iteration: u64 = 0;
while (iteration < 100) : (iteration += 1) {
for (world) |*w| {
w.* += 1;
}
var last_flash_count: u64 = 99999999;
while (flashes != last_flash_count) {
last_flash_count = flashes;
for (world) |*w, i| {
if (w.* > 9) {
w.* = 0;
flashes += 1;
for (neighbour_indicies(world.len, width, i)) |ni| {
if (ni == null) break;
if (world[ni.?] == 0) continue;
world[ni.?] += 1;
}
}
}
}
}
return flashes;
}
fn part2(input: *Parser) !u64 {
var world_raw = try BoundedArray(u8, 100).init(0);
const width: usize = 10;
for (input.source) |c| {
if (c == '\n') continue;
try world_raw.append(c - '0');
}
var world: [100]u8 = world_raw.buffer;
var flashes: u64 = 0;
var iteration: u64 = 0;
while (flashes < 100) : (iteration += 1) {
flashes = 0;
for (world) |*w| {
w.* += 1;
}
var last_flash_count: u64 = 999999;
while (flashes != last_flash_count) {
last_flash_count = flashes;
for (world) |*w, i| {
if (w.* > 9) {
w.* = 0;
flashes += 1;
for (neighbour_indicies(world.len, width, i)) |ni| {
if (ni == null) break;
if (world[ni.?] == 0) continue;
world[ni.?] += 1;
}
}
}
}
}
return iteration;
}
fn neighbour_indicies(count: usize, width: usize, location: usize) [9]?usize {
var result = BoundedArray(?usize, 9).init(0) catch unreachable;
const top_row = location < width;
const bottom_row = location + width >= count;
const left_col = location % width == 0;
const right_col = location % width == width - 1;
if (!top_row) {
if (!left_col) result.append(location - width - 1) catch unreachable;
result.append(location - width) catch unreachable;
if (!right_col) result.append(location - width + 1) catch unreachable;
}
if (!left_col) result.append(location - 1) catch unreachable;
if (!right_col) result.append(location + 1) catch unreachable;
if (!bottom_row) {
if (!left_col) result.append(location + width - 1) catch unreachable;
result.append(location + width) catch unreachable;
if (!right_col) result.append(location + width + 1) catch unreachable;
}
result.appendNTimes(null, result.buffer.len - result.len) catch unreachable;
return result.buffer;
}
test "Part 1" {
const test_input =
\\5483143223
\\2745854711
\\5264556173
\\6141336146
\\6357385478
\\4167524645
\\2176841721
\\6882881134
\\4846848554
\\5283751526
;
try expectEqual(@as(u64, 1656), try part1(&Parser.init(test_input)));
}
test "Part 2" {
const test_input =
\\5483143223
\\2745854711
\\5264556173
\\6141336146
\\6357385478
\\4167524645
\\2176841721
\\6882881134
\\4846848554
\\5283751526
;
try expectEqual(@as(u64, 195), try part2(&Parser.init(test_input)));
}