From b17f160cc27ba2611abfa4cf8c31dc193cc834e2 Mon Sep 17 00:00:00 2001 From: Dendy Date: Sun, 4 Dec 2022 23:44:05 +0100 Subject: [PATCH] Day 4 --- 4/1-2.zig | 51 +++ 4/input.asc | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1051 insertions(+) create mode 100644 4/1-2.zig create mode 100644 4/input.asc diff --git a/4/1-2.zig b/4/1-2.zig new file mode 100644 index 0000000..76564d7 --- /dev/null +++ b/4/1-2.zig @@ -0,0 +1,51 @@ +const std = @import("std"); + +// I don't have much time for this, it ain't gonna +// be as pretty as the other ones + +pub fn main() void { + var totalFit: usize = 0; + var totalAabb: usize = 0; + + const input = @embedFile("input.asc"); + var iter = std.mem.split(u8, input, "\n"); + + while(iter.next()) |line| { + if (line.len <= 0) continue; + + ////////////////// + // Split the nums into an array + //////////////// + + const delim = "-,-"; + if ((delim.len+1)&1 != 0) @panic("Amount of nums should always be even"); + var nums = [_]u8{undefined} ** (delim.len+1); + var startIdx: usize = 0; + + inline for(nums) |_,i| { + // Get the number + const numstr = if (i < delim.len) + std.mem.sliceTo(line[startIdx..], delim[i]) + else + line[startIdx..]; + + startIdx += numstr.len + 1; + nums[i] = std.fmt.parseInt(u8, numstr, 10) catch unreachable; + } + + if (fitTest(nums[0..2], nums[2..4])) totalFit += 1; + if (aabbTest(nums[0..2], nums[2..4])) totalAabb += 1; + + } + + std.debug.print("Part 1: {}\n", .{totalFit}); + std.debug.print("Part 2: {}\n", .{totalAabb}); +} + +fn fitTest(a: []const u8, b: []const u8) bool { + return (a[0] >= b[0] and a[1] <= b[1]) or (b[0] >= a[0] and b[1] <= a[1]); +} + +fn aabbTest(a: []const u8, b: []const u8) bool { + return a[1] >= b[0] and a[0] <= b[1]; +} diff --git a/4/input.asc b/4/input.asc new file mode 100644 index 0000000..34d7cda --- /dev/null +++ b/4/input.asc @@ -0,0 +1,1000 @@ +57-93,9-57 +55-55,55-83 +55-88,78-88 +24-24,24-95 +7-92,8-93 +25-84,84-85 +62-85,62-85 +66-78,65-76 +28-32,31-33 +24-81,2-25 +45-80,79-80 +75-99,75-91 +24-49,23-24 +22-69,49-69 +15-77,76-78 +25-50,21-50 +29-77,29-64 +47-93,46-48 +37-42,70-97 +5-95,4-6 +37-90,74-95 +17-48,18-85 +2-97,1-89 +2-48,1-48 +52-70,42-71 +17-90,90-94 +76-77,2-76 +10-89,10-96 +40-52,39-53 +9-97,8-96 +6-74,5-6 +3-89,2-89 +35-63,62-79 +13-39,18-39 +45-81,4-46 +8-76,39-67 +3-76,1-4 +1-96,1-1 +11-59,58-60 +41-53,25-42 +5-54,11-93 +15-91,95-95 +62-62,2-63 +11-41,40-42 +52-98,51-53 +29-52,47-51 +15-86,16-86 +13-98,28-99 +8-13,14-54 +47-61,64-95 +27-95,10-76 +9-27,10-10 +23-89,24-24 +98-98,15-99 +8-92,92-96 +2-38,27-87 +22-64,20-63 +1-6,5-66 +23-49,9-48 +63-64,35-63 +6-7,7-81 +23-91,22-92 +32-97,33-69 +21-24,20-25 +35-42,34-43 +82-82,15-83 +23-34,20-34 +37-92,37-86 +35-94,30-36 +48-52,51-53 +7-74,3-73 +41-43,42-86 +2-5,5-91 +98-99,8-98 +68-86,67-67 +37-54,36-37 +3-95,2-96 +50-79,25-78 +12-71,11-72 +91-92,74-91 +10-43,9-44 +30-93,2-91 +20-44,20-54 +36-84,35-85 +75-76,75-75 +54-54,18-55 +36-91,14-39 +8-25,8-25 +16-97,4-96 +7-83,2-8 +4-4,4-71 +1-98,1-98 +87-87,33-88 +22-88,21-88 +8-94,9-89 +99-99,5-96 +63-92,63-92 +42-68,10-69 +18-96,19-19 +16-90,16-90 +9-93,8-94 +48-88,87-89 +3-68,67-98 +72-93,97-98 +11-99,11-99 +3-99,2-99 +83-87,79-86 +14-14,5-14 +1-2,1-92 +9-90,91-94 +53-85,84-84 +11-89,3-11 +13-33,32-86 +40-74,74-91 +68-71,69-86 +17-51,18-51 +47-90,47-90 +2-3,3-85 +7-97,8-75 +21-29,28-75 +87-93,53-93 +49-74,27-95 +56-65,55-97 +14-16,15-81 +7-7,8-71 +24-26,23-82 +34-56,50-55 +10-97,11-96 +52-70,16-69 +20-56,20-21 +27-96,96-97 +23-98,15-24 +24-72,86-98 +44-77,60-88 +6-98,5-99 +1-96,99-99 +8-95,99-99 +10-81,4-62 +88-88,69-89 +30-67,29-68 +14-96,15-96 +58-86,73-86 +7-55,14-54 +3-97,62-98 +11-86,9-87 +41-79,16-41 +2-99,1-96 +7-56,5-7 +25-61,24-62 +25-83,26-82 +20-82,7-12 +36-58,41-58 +98-98,16-89 +98-99,86-99 +18-50,18-93 +44-67,40-48 +46-90,47-91 +21-49,48-66 +2-90,89-94 +12-25,13-25 +5-9,6-44 +68-75,70-76 +25-99,26-99 +95-95,17-96 +13-14,13-90 +79-99,76-77 +87-88,88-89 +77-77,6-78 +36-92,35-35 +10-11,9-9 +9-37,8-38 +80-91,81-81 +17-97,17-99 +4-19,18-71 +35-92,8-91 +30-31,22-30 +19-19,18-47 +51-79,52-55 +4-8,7-77 +3-98,3-99 +68-76,67-68 +86-86,74-93 +20-86,35-86 +8-98,7-98 +56-58,14-63 +41-47,42-47 +16-89,41-88 +21-22,22-52 +2-62,3-33 +3-83,3-3 +87-94,25-86 +80-95,95-98 +7-57,8-58 +3-24,23-23 +5-43,2-4 +32-85,86-86 +39-82,38-83 +73-87,21-79 +86-94,39-87 +87-88,4-87 +41-65,48-60 +40-70,62-70 +3-44,2-43 +3-19,19-98 +65-98,66-98 +79-84,80-83 +93-97,91-98 +21-96,21-96 +30-77,31-83 +5-82,2-5 +10-82,9-10 +84-94,47-50 +26-93,27-39 +47-95,77-90 +30-33,33-81 +45-73,46-72 +12-59,12-59 +31-60,31-59 +23-76,39-75 +66-88,80-93 +58-76,2-75 +9-56,36-55 +2-94,2-94 +75-81,72-80 +9-46,10-41 +7-93,6-6 +99-99,43-97 +33-83,33-83 +20-85,84-84 +61-76,60-94 +16-75,17-75 +16-18,17-71 +6-88,7-35 +38-79,6-38 +9-42,8-10 +18-60,86-95 +3-3,2-98 +2-99,3-99 +78-82,79-82 +34-75,23-76 +3-12,3-4 +16-95,20-95 +81-85,66-75 +8-87,15-61 +37-58,37-37 +2-87,1-86 +49-89,48-84 +56-58,57-88 +77-79,3-78 +25-92,25-98 +14-73,73-74 +15-17,1-16 +41-84,1-84 +34-64,6-63 +86-88,28-87 +2-87,2-87 +68-76,34-69 +42-78,54-83 +7-92,2-24 +2-12,1-11 +72-83,64-82 +49-93,48-92 +31-32,32-59 +4-90,4-86 +2-65,71-83 +21-88,14-78 +5-92,93-94 +1-10,2-9 +40-66,39-40 +47-77,46-46 +2-99,25-99 +37-95,37-95 +60-64,60-64 +32-51,33-51 +22-90,11-88 +13-54,12-54 +67-77,16-72 +7-37,9-90 +2-54,53-53 +5-5,6-68 +8-9,9-31 +4-39,40-61 +5-95,6-95 +1-11,11-95 +30-70,30-70 +58-58,59-97 +11-41,12-44 +2-6,1-7 +10-58,10-64 +3-95,1-3 +23-55,8-23 +37-60,52-59 +6-46,6-47 +8-45,27-53 +12-95,94-94 +43-52,43-44 +9-95,10-94 +2-9,5-26 +5-8,9-97 +2-60,3-60 +87-89,45-88 +5-9,9-90 +47-91,45-47 +39-47,27-39 +16-72,16-34 +89-90,35-89 +18-93,8-17 +4-35,9-71 +64-96,27-62 +30-68,7-30 +25-70,3-26 +41-77,40-77 +87-97,24-97 +2-97,97-98 +41-50,49-67 +49-53,50-80 +18-18,19-87 +36-39,37-40 +4-83,27-93 +28-92,29-91 +33-93,93-94 +2-94,2-94 +26-65,25-26 +5-14,15-27 +19-49,19-49 +56-56,2-55 +79-79,49-80 +79-80,55-80 +61-82,17-62 +61-63,1-62 +20-92,32-73 +57-57,5-58 +19-37,20-20 +13-75,75-76 +61-99,62-99 +19-92,19-91 +34-68,35-35 +4-76,91-99 +30-71,71-72 +51-95,28-51 +64-70,51-75 +28-90,89-91 +70-85,42-83 +2-91,5-80 +45-53,17-52 +70-80,20-69 +29-88,30-89 +5-5,5-89 +30-30,30-77 +20-71,21-40 +11-44,18-43 +6-90,6-90 +1-98,97-99 +3-98,3-98 +9-42,24-42 +75-75,73-76 +20-87,86-88 +62-86,63-91 +15-61,14-16 +46-58,45-58 +2-53,47-54 +3-89,3-35 +3-99,1-78 +50-80,9-81 +96-98,69-97 +6-78,6-78 +19-77,18-77 +17-75,18-75 +54-73,53-73 +92-97,73-85 +5-76,77-77 +43-93,18-94 +7-8,8-92 +49-87,50-88 +34-56,35-57 +25-98,24-26 +11-93,12-94 +68-99,67-67 +5-8,9-55 +8-83,4-43 +52-84,51-53 +4-96,3-71 +6-94,6-99 +19-67,18-68 +1-91,2-96 +24-35,36-54 +13-96,13-99 +19-47,32-36 +10-62,6-61 +3-4,4-98 +42-43,43-76 +17-58,54-58 +57-77,12-77 +78-99,25-77 +3-91,1-90 +13-28,29-83 +60-88,23-59 +78-95,79-95 +16-97,16-17 +14-71,12-70 +1-99,2-99 +11-84,5-11 +55-80,55-80 +28-67,68-68 +60-81,60-81 +7-74,7-74 +3-68,2-4 +5-70,6-70 +4-92,30-93 +44-79,43-99 +31-70,4-98 +2-93,1-93 +6-95,5-5 +16-36,5-36 +13-14,13-31 +53-84,54-73 +41-75,74-91 +31-90,48-91 +25-93,19-93 +9-95,6-9 +27-89,25-28 +19-86,18-73 +11-62,10-11 +73-75,23-75 +19-90,18-96 +5-5,4-51 +16-98,42-52 +11-81,12-82 +65-95,66-95 +61-61,61-82 +68-74,73-78 +31-42,1-32 +7-97,7-96 +58-59,42-58 +99-99,13-92 +63-94,20-95 +17-38,8-18 +60-71,15-52 +75-90,9-76 +14-99,13-98 +9-16,16-81 +54-83,54-90 +18-18,18-53 +16-64,15-16 +19-60,18-19 +2-90,1-91 +79-79,79-98 +32-58,31-33 +60-65,59-65 +13-34,13-34 +6-34,33-33 +59-64,58-84 +13-13,13-71 +4-76,3-82 +77-86,60-75 +63-96,52-95 +4-68,3-5 +21-45,5-21 +11-81,10-12 +1-97,96-96 +25-78,26-79 +17-38,20-48 +61-62,62-90 +16-99,16-99 +12-66,42-65 +3-85,2-84 +6-47,7-46 +3-76,4-85 +10-44,11-72 +12-47,47-48 +47-84,48-84 +43-99,24-77 +21-44,21-25 +75-85,44-84 +86-97,32-86 +24-92,23-88 +86-88,49-87 +96-96,2-79 +90-95,17-95 +11-88,10-89 +3-59,1-4 +10-56,11-83 +46-94,47-94 +3-98,98-99 +34-86,34-94 +9-17,11-16 +89-93,21-90 +15-98,15-98 +32-84,33-85 +46-46,45-80 +5-69,70-93 +8-99,9-98 +4-99,5-95 +1-35,2-67 +1-99,2-98 +11-22,14-22 +32-95,33-94 +48-77,72-77 +28-30,22-29 +35-86,36-86 +56-60,55-60 +44-75,44-59 +85-91,8-86 +21-22,21-59 +96-97,8-60 +7-50,51-85 +32-98,33-33 +11-12,11-81 +6-7,6-39 +32-68,33-69 +19-46,18-50 +45-96,45-49 +1-13,4-94 +86-87,14-87 +15-15,16-93 +29-48,28-28 +89-97,90-98 +6-59,7-60 +13-13,13-64 +21-64,81-95 +17-34,18-33 +3-87,86-90 +4-84,2-38 +40-76,39-76 +17-84,18-85 +66-96,25-65 +16-97,16-96 +64-65,4-66 +53-66,10-86 +15-78,16-77 +75-76,54-75 +4-98,20-97 +22-75,23-62 +42-89,42-89 +70-71,69-70 +18-90,19-89 +40-68,67-69 +7-73,6-7 +86-95,96-97 +40-58,39-72 +1-43,5-28 +10-60,5-49 +30-81,29-91 +7-77,8-11 +8-65,9-65 +3-57,4-11 +1-2,2-96 +56-58,41-57 +34-89,83-90 +59-97,58-98 +29-30,28-30 +54-82,31-57 +4-99,98-98 +14-36,34-39 +88-97,3-98 +16-53,17-99 +39-39,39-72 +39-61,62-80 +20-32,31-43 +49-70,50-70 +65-75,34-66 +1-99,1-2 +57-90,56-94 +28-62,33-62 +21-21,22-96 +18-65,17-88 +93-97,60-94 +74-93,92-94 +9-18,17-77 +46-89,30-45 +17-97,16-98 +2-99,98-98 +11-21,12-20 +99-99,4-96 +36-48,35-44 +70-74,63-81 +40-41,40-92 +2-94,22-93 +67-73,60-78 +29-30,22-29 +65-99,65-99 +7-20,6-21 +29-99,30-99 +40-98,86-99 +20-37,21-53 +86-86,7-87 +25-39,30-93 +55-78,56-88 +5-15,6-7 +16-17,2-16 +71-72,72-83 +9-21,8-21 +5-44,3-26 +77-77,12-84 +15-16,16-53 +44-69,43-69 +6-84,5-5 +34-77,33-77 +15-78,15-97 +28-86,15-29 +47-96,46-46 +57-59,21-58 +2-91,4-92 +11-14,13-75 +45-46,45-99 +26-57,24-26 +4-95,67-98 +15-54,15-15 +41-44,19-42 +19-20,19-98 +16-86,15-86 +13-47,46-82 +13-50,12-14 +11-98,6-89 +44-89,45-99 +7-82,82-83 +24-61,24-24 +23-60,23-59 +10-86,9-10 +74-86,73-74 +1-41,5-82 +3-4,4-84 +94-98,19-83 +2-97,93-96 +89-91,45-90 +7-12,11-94 +52-60,46-51 +3-3,2-85 +35-43,42-66 +29-92,28-92 +20-21,21-96 +91-91,90-91 +8-34,8-21 +8-95,95-96 +4-81,44-82 +2-84,1-2 +11-88,11-11 +17-69,16-70 +12-76,11-12 +13-64,66-75 +38-68,69-85 +86-86,85-91 +10-87,11-97 +10-78,36-77 +8-68,7-68 +4-87,3-87 +62-64,12-65 +32-79,33-79 +2-90,2-3 +3-8,8-26 +63-64,63-70 +25-30,7-30 +3-92,3-92 +4-98,3-99 +2-3,2-43 +43-65,43-85 +17-62,91-94 +88-90,48-89 +13-96,1-96 +44-71,44-93 +66-71,70-72 +28-63,27-62 +19-19,20-99 +23-23,24-93 +27-84,28-85 +77-82,21-78 +40-40,24-41 +88-89,6-88 +1-97,96-98 +1-88,2-42 +12-39,12-13 +47-62,61-65 +3-97,7-96 +30-66,29-67 +71-90,72-78 +98-98,6-97 +43-43,43-48 +36-99,35-95 +30-93,30-99 +5-28,6-11 +3-22,2-96 +9-87,8-71 +41-79,27-69 +6-99,7-98 +99-99,45-76 +97-99,14-96 +6-39,7-92 +5-49,13-64 +6-41,4-41 +93-98,16-94 +8-98,25-97 +41-64,41-42 +5-87,5-6 +12-68,13-67 +24-75,24-75 +41-42,5-41 +16-92,13-49 +16-31,27-96 +27-79,78-78 +32-71,33-72 +23-24,24-96 +4-4,4-78 +85-91,40-91 +6-10,9-75 +38-69,39-69 +8-8,8-74 +7-40,1-7 +33-88,34-88 +32-92,33-92 +7-19,6-64 +21-22,21-22 +7-8,7-99 +4-48,5-47 +27-27,26-61 +77-85,76-86 +22-39,29-40 +4-42,5-63 +85-87,43-79 +8-88,7-9 +55-90,54-90 +7-98,6-8 +6-85,7-84 +18-56,5-19 +54-81,42-50 +7-27,28-40 +96-98,81-97 +52-74,52-53 +61-64,20-63 +45-92,45-60 +18-89,19-89 +9-51,8-52 +19-19,20-95 +49-99,49-99 +17-78,16-16 +20-95,20-21 +11-94,96-99 +38-57,56-58 +94-97,18-90 +38-42,10-53 +67-86,68-86 +2-96,2-91 +33-77,33-89 +25-26,25-51 +86-98,16-97 +2-71,2-71 +28-29,6-28 +30-83,31-84 +13-85,13-84 +23-61,22-48 +32-74,74-92 +41-53,10-94 +15-89,20-90 +25-69,25-25 +1-96,1-96 +7-96,6-96 +1-98,2-2 +47-47,11-48 +35-94,93-95 +67-69,68-77 +43-63,44-64 +14-94,15-49 +7-95,6-96 +15-40,41-41 +8-83,82-90 +66-75,11-67 +40-52,45-51 +20-57,7-20 +4-87,3-87 +97-97,56-95 +45-45,45-45 +58-96,9-95 +33-45,44-44 +6-26,16-26 +46-59,47-60 +24-40,23-40 +6-91,94-98 +40-93,20-84 +32-42,41-42 +2-94,3-93 +59-59,2-60 +7-40,3-7 +47-89,88-88 +35-66,65-67 +84-99,52-85 +75-99,75-96 +8-21,7-8 +3-53,4-52 +22-62,22-62 +72-72,73-78 +39-72,38-96 +2-14,1-91 +4-98,5-76 +8-88,7-13 +5-6,5-99 +20-90,19-90 +26-29,27-34 +52-69,53-95 +56-56,50-57 +23-79,22-79 +16-93,16-78 +13-76,55-92 +36-70,35-70 +43-81,40-80 +4-94,95-99 +1-94,1-94 +48-84,47-49 +32-93,31-92 +11-19,18-77 +62-80,39-74 +62-63,62-96 +3-94,93-95 +15-66,16-66 +23-95,22-88 +57-57,25-58 +71-86,3-72 +2-79,7-78 +47-49,13-48 +46-94,45-45 +19-77,65-76 +31-37,38-87 +4-79,92-99 +17-19,17-19 +1-82,41-89 +47-91,48-49 +6-19,10-37 +14-42,18-39 +40-79,20-61 +2-84,1-31 +83-95,47-84 +68-71,2-69 +37-73,8-72 +85-86,36-85 +2-9,8-78 +17-59,58-68 +62-90,78-90 +77-94,41-93 +11-90,12-94 +34-79,33-97 +42-56,22-57 +38-44,44-45 +1-84,98-98 +63-74,61-73 +28-57,4-58 +48-48,47-90 +37-38,38-77 +72-84,72-72 +3-47,47-47 +50-91,25-59 +5-38,4-38 +24-86,23-55 +55-74,50-75 +33-83,34-83 +1-91,73-88 +13-13,14-85 +54-94,60-99 +21-85,84-86 +12-98,8-36 +89-98,4-90 +4-15,8-15 +98-98,1-99 +21-79,20-78 +2-63,62-63 +78-80,63-79 +24-68,67-86 +24-25,25-47 +53-68,4-55 +11-82,10-82 +23-92,5-99 +94-99,67-88 +11-51,32-86 +49-79,24-48 +22-73,91-99 +4-9,8-89 +23-85,23-54 +5-86,6-13 +13-14,14-94 +72-81,73-82 +56-98,55-98 +3-96,2-96 +88-88,20-89 +78-79,79-85 +29-94,20-30 +8-43,85-90 +96-98,7-71 +45-74,70-71 +23-79,94-94 +41-97,40-97 +73-96,72-72 +4-28,4-65 +89-98,20-90 +37-70,37-98 +26-36,26-35 +41-41,7-42 +7-22,21-90 +12-84,13-75 +98-98,1-99 +47-75,74-87 +7-33,33-90 +41-68,41-87 +64-92,65-88 +41-54,10-42 +6-89,93-96 +20-48,1-21 +8-91,8-8 +11-95,10-97 +72-98,31-90 +44-45,33-44 +54-56,18-55 +7-9,10-88 +44-45,45-90 +52-56,54-83 +68-70,1-69 +91-92,91-92 +13-74,14-75 +4-60,61-91 +9-94,10-95 +9-95,9-10 +50-61,49-68 +50-50,5-51 +38-46,43-43 +4-11,12-22 +42-42,43-96 +77-91,48-76 +23-89,24-94 +9-65,10-15 +53-81,7-73 +8-28,22-37 +76-95,31-77 +9-84,3-84 +12-98,13-99 +17-74,17-78 +60-89,59-89 +1-15,2-14 +14-91,13-88 +87-97,42-96 +77-77,73-76 +16-77,15-69 +80-95,81-96 +40-73,41-90 +47-63,46-62 +5-98,4-97 +46-93,42-92 +15-30,7-23 +7-90,91-95 +1-99,1-2 +5-97,4-99 +81-87,8-28 +56-69,24-68 +87-89,20-88 +78-99,1-98 +32-97,33-97 +19-20,20-33 +77-78,21-78 +2-6,5-42 +35-36,35-44 +2-83,82-84 +48-97,49-58 +36-53,53-75 +5-92,4-93 +49-85,50-86 +45-81,43-82 +3-97,1-43 +97-98,58-97 +6-88,3-88 +4-67,5-66 +43-88,32-44 +95-95,13-96 +4-54,13-97 +1-45,3-45 +2-9,8-98 +1-12,1-53 +13-98,14-84 +85-99,86-99 +4-16,3-16 +15-77,14-15 +18-67,67-68 +34-69,33-70 +97-97,9-92 +76-84,58-77 +76-84,42-55 +96-98,2-97 +50-98,51-72 +54-94,53-94 +23-91,23-91 +21-94,22-95 +41-89,42-89 +48-78,47-78 +18-51,51-76 +5-49,1-34 +40-55,40-47 +41-96,41-97 +8-10,9-94 +74-79,66-80 +6-15,15-91 +10-99,9-99 +12-92,12-96 +32-96,20-33 +5-67,1-66 +21-90,31-94 +22-23,22-60