Skip to content

Commit ca59c15

Browse files
authored
Merge branch 'main' into day8
2 parents 49b6860 + d06ce38 commit ca59c15

File tree

7 files changed

+165
-4
lines changed

7 files changed

+165
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Welcome to this community project, where we collaboratively solve the 2020 editi
1313
| deno.ts | **7** | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | | ||||||||||||||||
1414
| c | **6** | 1 | 1 | 1 | 1 | 1 | 1 | | | | ||||||||||||||||
1515
| node.js | **4** | 1 | | | 1 | | 1 | 1 | | | ||||||||||||||||
16+
| zig | **3** | 1 | 1 | 1 | | | | | | | ||||||||||||||||
1617
| c++ | **2** | 1 | 1 | | | | | | | | ||||||||||||||||
1718
| ruby | **2** | 1 | | | 1 | | | | | | ||||||||||||||||
18-
| zig | **2** | 1 | 1 | | | | | | | | ||||||||||||||||
1919
| rust | **1** | 1 | | | | | | | | | ||||||||||||||||
2020
| bash | **0** | | | | | | | | | | ||||||||||||||||
2121
| java | **0** | | | | | | | | | | ||||||||||||||||

days/day-02/solutions/day02.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ const RequirementPassword = struct {
6565
return occurence >= self.min;
6666
}
6767

68-
pub inline fn newValid(self: RequirementPassword) bool {
68+
pub inline fn newValid(self: RequirementPassword) bool {
6969
var min_occurence: usize = @boolToInt(self.password[self.min - 1] == self.char);
7070
var max_occurence: usize = @boolToInt(self.password[self.max - 1] == self.char);
7171
return @as(usize, min_occurence) + @as(usize, max_occurence) == 1;
7272
}
7373
};
7474

75-
fn bytesToRPPArrayList(allocator: *Allocator, bytes: []u8) !ArrayList(RequirementPassword) {
75+
fn bytesToParsedArrayList(allocator: *Allocator, bytes: []u8) !ArrayList(RequirementPassword) {
7676
var list = try ArrayList(RequirementPassword).initCapacity(allocator, 2000);
7777

7878
var it = mem.split(bytes, "\n");
@@ -110,7 +110,7 @@ pub fn main() anyerror!void {
110110

111111
const length = try in.readAll(&buf);
112112

113-
var parsed_input = try bytesToRPPArrayList(allocator, buf[0..length]);
113+
var parsed_input = try bytesToParsedArrayList(allocator, buf[0..length]);
114114

115115
var old_valid: u32 = 0;
116116
var new_valid: u32 = 0;

days/day-03/solutions/day03.zig

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const std = @import("std");
2+
const mem = std.mem;
3+
const io = std.io;
4+
const ArrayList = std.ArrayList;
5+
6+
const MAX_FILE_SIZE = 20_000;
7+
8+
const Map = struct {
9+
bytes: []u8,
10+
line_width: usize,
11+
map_height: usize,
12+
13+
pub inline fn init(bytes: []u8) Map {
14+
var line_width: usize = 0;
15+
while (line_width < bytes.len) : (line_width += 1) {
16+
switch(bytes[line_width]) {
17+
'\n', '\r' => break,
18+
else => continue
19+
}
20+
}
21+
22+
line_width += 1;
23+
24+
return Map {
25+
.bytes = bytes,
26+
.line_width = line_width, // TODO: account for arbitrary line ending format
27+
.map_height = @divFloor(bytes.len, line_width)
28+
};
29+
}
30+
31+
pub inline fn inspectPath(self: Map, rigth: usize, down: usize) usize {
32+
var x_pos: usize = 0;
33+
var y_pos: usize = 0;
34+
var closed: u32 = 0;
35+
36+
while (y_pos < self.map_height) {
37+
var n_skipped = @divFloor(x_pos, self.line_width - 1);
38+
var real_x = (x_pos + n_skipped) % self.line_width;
39+
var real_y = y_pos * self.line_width;
40+
41+
closed += @boolToInt(self.bytes[real_x + real_y] == '#');
42+
43+
x_pos += rigth;
44+
y_pos += down;
45+
}
46+
47+
return closed;
48+
}
49+
};
50+
51+
52+
53+
pub fn main() anyerror!void {
54+
const in = std.io.getStdIn().reader();
55+
const stdout = std.io.getStdOut().writer();
56+
57+
var buf: [MAX_FILE_SIZE]u8 = undefined;
58+
const length = try in.readAll(&buf);
59+
60+
var map = Map.init(buf[0..length]);
61+
62+
var path_3_1 = map.inspectPath(3, 1);
63+
try stdout.print("{}\n", .{path_3_1});
64+
65+
try stdout.print("{}\n", .{map.inspectPath(1, 1) * path_3_1 * map.inspectPath(5, 1) * map.inspectPath(7, 1) * map.inspectPath(1, 2)});
66+
}

days/day-03/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ D=$(dirname $(realpath $0))
66
echo ""
77
echo "--- Day 3: Counting trees ---"
88
$D/../../languages/c.sh $D/input.txt $D/output.txt $D/solutions/day03.c
9+
$D/../../languages/zig.sh $D/input.txt $D/output.txt $D/solutions/day03.zig
910
$D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/day03.stektpotet.go
1011
$D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/day03.tholok97.go
1112
$D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day03.sml

days/day-08/solutions/.keep

Whitespace-only changes.

days/day-08/solutions/day08.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
type Instruction = {
2+
op: "nop" | "acc" | "jmp";
3+
arg: number;
4+
};
5+
6+
const input = (await Deno.readTextFile("/dev/stdin"))
7+
.split("\n")
8+
.slice(0, -1);
9+
10+
const instructions = input
11+
.map((line) => line.split(" "))
12+
.map(([op, arg]) => ({ op, arg: parseInt(arg) })) as Instruction[];
13+
14+
/** --- Part 1 --- */
15+
{
16+
let ipTracker = new Set<number>([]);
17+
let accumulator = 0;
18+
let instructionPointer = 0;
19+
20+
while (!ipTracker.has(instructionPointer)) {
21+
ipTracker.add(instructionPointer);
22+
23+
const { op, arg } = instructions[instructionPointer];
24+
25+
switch (op) {
26+
case "nop":
27+
instructionPointer += 1;
28+
break;
29+
case "acc":
30+
instructionPointer += 1;
31+
accumulator += arg;
32+
break;
33+
case "jmp":
34+
instructionPointer += arg;
35+
break;
36+
}
37+
}
38+
console.log(`${accumulator}`);
39+
}
40+
41+
/** --- Part 2 --- */
42+
{
43+
let ipTracker = new Set<number>([]);
44+
let accumulator = 0;
45+
let instructionPointer = 0;
46+
let modIndex = 0;
47+
48+
for (
49+
let modStep = instructions.findIndex(({ op }) =>
50+
op === "nop" || op === "jmp"
51+
);
52+
modStep !== -1 && instructionPointer < instructions.length;
53+
modStep = instructions.slice(modIndex + 1).findIndex(({ op }) =>
54+
op === "nop" || op === "jmp"
55+
), modIndex += 1 + modStep
56+
) {
57+
ipTracker = new Set<number>([]);
58+
accumulator = 0;
59+
instructionPointer = 0;
60+
61+
const { op: modOp, arg: modArg } = instructions[modIndex];
62+
63+
const modifiedInstructions = [
64+
...instructions.slice(0, modIndex),
65+
{ arg: modArg, op: modOp === "nop" ? "jmp" : "nop" },
66+
...instructions.slice(modIndex + 1),
67+
];
68+
69+
while (
70+
!ipTracker.has(instructionPointer) &&
71+
instructionPointer < instructions.length
72+
) {
73+
ipTracker.add(instructionPointer);
74+
75+
const { op, arg } = modifiedInstructions[instructionPointer];
76+
77+
switch (op) {
78+
case "nop":
79+
instructionPointer += 1;
80+
break;
81+
case "acc":
82+
instructionPointer += 1;
83+
accumulator += arg;
84+
break;
85+
case "jmp":
86+
instructionPointer += arg;
87+
break;
88+
}
89+
}
90+
}
91+
92+
console.log(`${accumulator}`);
93+
}

days/day-08/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ D=$(dirname $(realpath $0))
77
echo ""
88
echo "--- Day 8: Handheld Halting ---"
99
$D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/tholok97.go
10+
$D/../../languages/deno.sh $D/input.txt $D/output.txt $D/solutions/day08.ts
1011
echo ""

0 commit comments

Comments
 (0)