diff options
| author | Louis Burda <quent.burda@gmail.com> | 2023-04-08 12:40:30 -0400 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2023-04-09 10:21:36 -0400 |
| commit | 9282e95e8844afe856ba76ceb6d2c3010df8bb1a (patch) | |
| tree | e35affc89b20324371381e079f7cb5f8a06aa81b /src/15 | |
| parent | 2b5d4232879dc74491dabf54a0ddc958d66ebcec (diff) | |
| download | aoc2020-zig-master.tar.gz aoc2020-zig-master.zip | |
Diffstat (limited to 'src/15')
| -rw-r--r-- | src/15/input | 1 | ||||
| -rw-r--r-- | src/15/main.zig | 63 | ||||
| -rw-r--r-- | src/15/part1 | 64 | ||||
| -rw-r--r-- | src/15/part2 | 18 |
4 files changed, 146 insertions, 0 deletions
diff --git a/src/15/input b/src/15/input new file mode 100644 index 0000000..75e31b4 --- /dev/null +++ b/src/15/input @@ -0,0 +1 @@ +15,5,1,4,7,0 diff --git a/src/15/main.zig b/src/15/main.zig new file mode 100644 index 0000000..3fcb555 --- /dev/null +++ b/src/15/main.zig @@ -0,0 +1,63 @@ +const std = @import("std"); +const aoc = @import("aoc"); + +const Occurance = struct { last: u32, beforelast: u32 }; + +fn getNthSpoken(allocator: std.mem.Allocator, input: []u8, spoken: u32) !u32 { + var occurances = std.AutoHashMap(u32, Occurance).init(allocator); + defer occurances.deinit(); + + var intlist = std.ArrayList(u32).init(allocator); + defer intlist.deinit(); + + var numit = std.mem.tokenize(u8, input, "\n,"); + var i: u32 = 0; + while (numit.next()) |numstr| : (i += 1) { + const num = try std.fmt.parseInt(u32, numstr, 10); + try intlist.append(num); + try occurances.put(num, Occurance{ .last = i, .beforelast = i }); + } + + while (i < spoken) : (i += 1) { + if (aoc.debug and i % 100000 == 0) + aoc.debugfmt("\r{}", .{i}); + + const num = intlist.items[i - 1]; + var entry = occurances.getEntry(num); + var diff: u32 = 0; + if (entry) |occ_entry| { + diff = occ_entry.value_ptr.last - occ_entry.value_ptr.beforelast; + } + entry = occurances.getEntry(diff); + if (entry) |occ_entry| { + occ_entry.value_ptr.beforelast = occ_entry.value_ptr.last; + occ_entry.value_ptr.last = i; + } else { + try occurances.put(diff, Occurance{ .last = i, .beforelast = i }); + } + try intlist.append(diff); + } + + if (aoc.debug) + aoc.debugfmt("\r \r", .{}); + + return intlist.items[spoken - 1]; +} + +fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try getNthSpoken(allocator, input, 2020); + + return try std.fmt.allocPrint(allocator, "{}", .{answer}); +} + +fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { + _ = args; + + const answer = try getNthSpoken(allocator, input, 30000000); + + return try std.fmt.allocPrint(allocator, "{}", .{answer}); +} + +pub const main = aoc.main(part1, part2, .{ "1259", "689" }); diff --git a/src/15/part1 b/src/15/part1 new file mode 100644 index 0000000..7a0aa08 --- /dev/null +++ b/src/15/part1 @@ -0,0 +1,64 @@ +--- Day 15: Rambunctious Recitation --- + +You catch the airport shuttle and try to book a new flight to your vacation island. Due to the +storm, all direct flights have been cancelled, but a route is available to get around the storm. You +take it. + +While you wait for your flight, you decide to check in with the Elves back at the North Pole. +They're playing a [1m[37mmemory game[0m and are ever so excited to explain the rules! + +In this game, the players take turns saying [1m[37mnumbers[0m. They begin by taking turns reading +from a list of [1m[37mstarting numbers[0m (your puzzle input). Then, each turn consists of +considering the [1m[37mmost recently spoken number[0m: + + + - If that was the [1m[37mfirst[0m time the number has been spoken, the current player says +[1m[37m0[0m. + - Otherwise, the number had been spoken before; the current player announces [1m[37mhow many +turns apart[0m the number is from when it was previously spoken. + + +So, after the starting numbers, each turn results in that player speaking aloud either +[1m[37m0[0m (if the last number is new) or an [1m[37mage[0m (if the last number is a repeat). + +For example, suppose the starting numbers are 0,3,6: + + + - [1m[37mTurn 1[0m: The 1st number spoken is a starting number, [1m[37m0[0m. + - [1m[37mTurn 2[0m: The 2nd number spoken is a starting number, [1m[37m3[0m. + - [1m[37mTurn 3[0m: The 3rd number spoken is a starting number, [1m[37m6[0m. + - [1m[37mTurn 4[0m: Now, consider the last number spoken, 6. Since that was the first time the +number had been spoken, the 4th number spoken is [1m[37m0[0m. + - [1m[37mTurn 5[0m: Next, again consider the last number spoken, 0. Since it [1m[37mhad[0m +been spoken before, the next number to speak is the difference between the turn number when it was +last spoken (the previous turn, 4) and the turn number of the time it was most recently spoken +before then (turn 1). Thus, the 5th number spoken is 4 - 1, [1m[37m3[0m. + - [1m[37mTurn 6[0m: The last number spoken, 3 had also been spoken before, most recently on +turns 5 and 2. So, the 6th number spoken is 5 - 2, [1m[37m3[0m. + - [1m[37mTurn 7[0m: Since 3 was just spoken twice in a row, and the last two turns are 1 turn +apart, the 7th number spoken is [1m[37m1[0m. + - [1m[37mTurn 8[0m: Since 1 is new, the 8th number spoken is [1m[37m0[0m. + - [1m[37mTurn 9[0m: 0 was last spoken on turns 8 and 4, so the 9th number spoken is the +difference between them, [1m[37m4[0m. + - [1m[37mTurn 10[0m: 4 is new, so the 10th number spoken is [1m[37m0[0m. + + +(The game ends when the Elves get sick of playing or dinner is ready, whichever comes first.) + +Their question for you is: what will be the [1m[37m2020th[0m number spoken? In the example above, +the 2020th number spoken will be 436. + +Here are a few more examples: + + + - Given the starting numbers 1,3,2, the 2020th number spoken is 1. + - Given the starting numbers 2,1,3, the 2020th number spoken is 10. + - Given the starting numbers 1,2,3, the 2020th number spoken is 27. + - Given the starting numbers 2,3,1, the 2020th number spoken is 78. + - Given the starting numbers 3,2,1, the 2020th number spoken is 438. + - Given the starting numbers 3,1,2, the 2020th number spoken is 1836. + + +Given your starting numbers, [1m[37mwhat will be the 2020th number spoken?[0m + + diff --git a/src/15/part2 b/src/15/part2 new file mode 100644 index 0000000..3be53a9 --- /dev/null +++ b/src/15/part2 @@ -0,0 +1,18 @@ +--- Part Two --- + +Impressed, the Elves issue you a challenge: determine the 30000000th number spoken. For example, +given the same starting numbers as above: + + + - Given 0,3,6, the 30000000th number spoken is 175594. + - Given 1,3,2, the 30000000th number spoken is 2578. + - Given 2,1,3, the 30000000th number spoken is 3544142. + - Given 1,2,3, the 30000000th number spoken is 261214. + - Given 2,3,1, the 30000000th number spoken is 6895259. + - Given 3,2,1, the 30000000th number spoken is 18. + - Given 3,1,2, the 30000000th number spoken is 362. + + +Given your starting numbers, [1m[37mwhat will be the 30000000th number spoken?[0m + + |
