1const std = @import("std");
2
3pub fn pt1(input: []const u8) u64 {
4 var pointer: i64 = 50;
5 var counter: u64 = 0;
6
7 var lines = std.mem.splitScalar(u8, input, '\n');
8
9 while (lines.next()) |l| {
10 if (l.len == 0) {
11 // this happens at the end so we are safe to return here.
12 return counter;
13 }
14
15 const op = l[0];
16 const rotation = std.fmt.parseInt(i64, l[1..], 10) catch undefined;
17
18 if (op == 'R') {
19 pointer = @mod(pointer + rotation, 100);
20 } else {
21 pointer = @mod(pointer - rotation, 100);
22 }
23
24 if (pointer == 0) {
25 counter += 1;
26 }
27 }
28
29 return counter;
30}
31
32const operator = enum { left, right };
33pub fn pt2(input: []const u8) u64 {
34 var pointer: i64 = 50;
35 var counter: u64 = 0;
36
37 var lines = std.mem.splitScalar(u8, input, '\n');
38
39 while (lines.next()) |l| {
40 if (l.len == 0) {
41 // this happens at the end so we are safe to return here.
42 return counter;
43 }
44
45 const op = l[0];
46 const rotation = std.fmt.parseInt(i64, l[1..], 10) catch undefined;
47
48 if (op == 'R') {
49 for (0..@intCast(rotation)) |_| {
50 pointer = step(pointer, .right);
51 if (pointer == 0) counter += 1;
52 }
53 } else {
54 for (0..@intCast(rotation)) |_| {
55 pointer = step(pointer, .left);
56 if (pointer == 0) counter += 1;
57 }
58 }
59 }
60
61 return counter;
62}
63
64fn step(p: i64, comptime o: operator) i64 {
65 switch (o) {
66 operator.left => {
67 if (p == 99) return 0;
68 return p + 1;
69 },
70 operator.right => {
71 if (p == 0) return 99;
72 return p - 1;
73 },
74 }
75
76 return p;
77}
78
79test "Day 1 part 1" {
80 const res = pt1(@embedFile("./input/day1"));
81 try std.testing.expect(res == 1165);
82}
83
84test "Day 1 part 2" {
85 const res = pt2(@embedFile("./input/day1"));
86 try std.testing.expect(res == 6496);
87}