-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_01.zig
More file actions
164 lines (135 loc) · 3.82 KB
/
Copy pathday_01.zig
File metadata and controls
164 lines (135 loc) · 3.82 KB
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
159
160
161
162
163
164
const std = @import("std");
const print = std.debug.print;
const Allocator = std.mem.Allocator;
fn parse(alloc: Allocator, lines: [][]const u8) ![]i32 {
var spins = try alloc.alloc(i32, lines.len);
for (lines, 0..) |line, i| {
if (line.len == 0) continue;
const raw_amount = try std.fmt.parseInt(i32, line[1..], 10);
const amount = switch (line[0]) {
'L' => -raw_amount,
'R' => raw_amount,
else => return error.InvalidDirection,
};
spins[i] = amount;
}
return spins;
}
fn readFile(alloc: Allocator, filename: []const u8) ![]u8 {
return try std.fs.cwd().readFileAlloc(alloc, filename, std.math.maxInt(usize));
}
fn splitLines(alloc: Allocator, bytes: []const u8) ![][]const u8 {
var list = std.array_list.Managed([]const u8).init(alloc);
var iter = std.mem.splitScalar(u8, bytes, '\n');
while (iter.next()) |line| {
try list.append(line);
}
return try list.toOwnedSlice();
}
fn solvePart1(spins: []const i32) u32 {
var result: u32 = 0;
var dial: i32 = 50;
for (spins) |amount| {
dial += @rem(amount, 100);
dial = @mod(dial, 100);
if (dial == 0) {
result += 1;
}
}
return result;
}
fn solvePart2(spins: []const i32) u32 {
var result: u32 = 0;
var dial: i32 = 50;
for (spins) |amount| {
const dial_start = dial;
dial += @rem(amount, 100);
result += @abs(amount) / 100;
// If we started at 0, then the next normalization step (wrapping) does not truly pass zero, thus we don't count it.
if (dial_start != 0 and (dial < 0 or dial > 100)) {
result += 1;
}
dial = @mod(dial, 100);
if (dial == 0) {
result += 1;
}
}
return result;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
defer arena.deinit();
const alloc = arena.allocator();
const lines = try splitLines(alloc, try readFile(alloc, "input.txt"));
const spins = try parse(alloc, lines);
print("Part 1: {}\n", .{solvePart1(spins)});
print("Part 2: {}\n", .{solvePart2(spins)});
}
test "solvePart1" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const cases = [_]struct {
name: []const u8,
input: []const u8,
expected: u32,
}{
.{
.name = "real world",
.input =
\\L68
\\L30
\\R48
\\L5
\\R60
\\L55
\\L1
\\L99
\\R14
\\L82
,
.expected = 3,
},
};
for (cases) |tt| {
const lines = try splitLines(alloc, tt.input);
const spins = try parse(alloc, lines);
const result = solvePart1(spins);
try std.testing.expectEqual(tt.expected, result);
}
}
test "solvePart2" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const cases = [_]struct {
name: []const u8,
input: []const u8,
expected: u32,
}{
.{
.name = "real world",
.input =
\\L68
\\L30
\\R48
\\L5
\\R60
\\L55
\\L1
\\L99
\\R14
\\L82
,
.expected = 6,
},
};
for (cases) |tt| {
const lines = try splitLines(alloc, tt.input);
const spins = try parse(alloc, lines);
const result = solvePart2(spins);
try std.testing.expectEqual(tt.expected, result);
}
}