Solve day 6
This commit is contained in:
parent
9fb88be5f4
commit
1db82043b9
58
src/day06.zig
Normal file
58
src/day06.zig
Normal file
@ -0,0 +1,58 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const input = @embedFile("input/day06.txt");
|
||||
const test_input1 = @embedFile("input/day06_test1.txt");
|
||||
const test_input2 = @embedFile("input/day06_test2.txt");
|
||||
const test_input3 = @embedFile("input/day06_test3.txt");
|
||||
const test_input4 = @embedFile("input/day06_test4.txt");
|
||||
const test_input5 = @embedFile("input/day06_test5.txt");
|
||||
|
||||
const CharSet = std.StaticBitSet(26);
|
||||
|
||||
fn ascii_to_bit(char: u8) error{InvalidChar}!u8 {
|
||||
return switch (char) {
|
||||
'a'...'z' => char - 'a',
|
||||
else => error.InvalidChar,
|
||||
};
|
||||
}
|
||||
|
||||
fn find_unique_marker(comptime length: usize, line: []const u8) !?usize {
|
||||
var right: usize = length;
|
||||
while (right < line.len) : (right += 1) {
|
||||
var charset = CharSet.initEmpty();
|
||||
var left: usize = right - length;
|
||||
while (left < right) : (left += 1) {
|
||||
charset.set(try ascii_to_bit(line[left]));
|
||||
}
|
||||
if (charset.count() == length) {
|
||||
return right;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn solve_part1(data: []const u8) !usize {
|
||||
var line = std.mem.trimRight(u8, data, "\n");
|
||||
return (try find_unique_marker(4, line)).?;
|
||||
}
|
||||
|
||||
pub fn solve_part2(data: []const u8) !usize {
|
||||
var line = std.mem.trimRight(u8, data, "\n");
|
||||
return (try find_unique_marker(14, line)).?;
|
||||
}
|
||||
|
||||
test "solves part1" {
|
||||
try std.testing.expectEqual(solve_part1(test_input1), 7);
|
||||
try std.testing.expectEqual(solve_part1(test_input2), 5);
|
||||
try std.testing.expectEqual(solve_part1(test_input3), 6);
|
||||
try std.testing.expectEqual(solve_part1(test_input4), 10);
|
||||
try std.testing.expectEqual(solve_part1(test_input5), 11);
|
||||
}
|
||||
|
||||
test "solves part2" {
|
||||
try std.testing.expectEqual(solve_part2(test_input1), 19);
|
||||
try std.testing.expectEqual(solve_part2(test_input2), 23);
|
||||
try std.testing.expectEqual(solve_part2(test_input3), 23);
|
||||
try std.testing.expectEqual(solve_part2(test_input4), 29);
|
||||
try std.testing.expectEqual(solve_part2(test_input5), 26);
|
||||
}
|
1
src/input/day06.txt
Executable file
1
src/input/day06.txt
Executable file
@ -0,0 +1 @@
|
||||
rqwqtwwrmrqqrcrtrpppjrjgrgjgqgttgbbhppftpfpdpmpsstffqvfqvvdjjpllltqltqtrrzlllbldbdsdqqgpqggzccgbgnnmjmljmljldjldjjlnnhndhdmmslmslmlhmmntnrtnrnbrbqrqhqfftwftfsfzfcfrrzllchcwwtwstwwjwrjjfwwgzwzqzpzhzqhqbhqhssltsszhsszdssrwssdppjcpjjrwjwnwjnnzjjhsjhsjsjvvtffffqfflglwglgbgpbgbgghfghfhdfhdfhfssqppvlvslltwwhwvwpwgggnpnnmqnntqnnlhhdbhdbbtvvcrrcgrrdrvrjvvlmlnmndnmnrncnznsnsqnsswrwzwmwzzbpbhphqhpqpbpqpjpjvpvwpvwppdvdhdccqbccfncctrcrjcjjdsdhdnhhwpwlwwbmbhmbhhpzhhccvffqqnsqqmnnspnnpfpzppspgsppgbbdwbbdcchfhgfhfllljtllnjlnlddvcdvdqvvqwqppprtpthtthqqfbqqpfpzfpfcfsscrsccbgbngbbgdbbpgpqqlmqlmmgffqqqsbqbtblbwbqqvbqvbqvvnzvzpvvqhvqqtltptllbflbflbflltddlblflvfvbffmsffhlhshmmgbbgjjdndqqmqllzjzvvhjhnhcnchhznnhhppwqpwpffwvwzzppwpfwftwtnnqbnqnrnrzznffzzblzlvzlzrrszscsqcssbggcgvvddsttgqqgwgcclzzszddlvddvbddrlrcllrblrrhlldzdtdccqllsblsszlszzdllhjljhhhzhffdwfdwfddwdtdrtdtntddldsllqffdvdhdpdhdrdprprrhggdqgghggmwgmghhchlhmllrnrdrcrqccbqqfbqbccjpjhhvppcwpwffmmzzfqflfjfdfbfqqtmthmttwftwwpmpzplzzpvvgppsbpbccbtcbcddzbzwwmsscrcctgtgjgtjthhjzzcqqmmmvccwjcwjwzzwnnwgwtgwwbsbvbccrscccnggfjfwfjfrfsfddncdncndnqqwzzmssbdsbswswdddpdqdbddcvcmmwwssrtrrlddndmdtmtdmttwwmsmffwqwvwbwhbbqfqqgcqcmctctntjntnbnrbrmbbqtqccqbcbvbnbqnnghgbbmfmwmdwmdmpmwppdwpdwwnzwnwrnwrnntqnttcmcqqbrqrttsjtstcscpcspsnswsggrhggjrjjbhhbnbmmdlmddtqdtdrdwwjvjgjjdhjdddbwwsfflppbwbcbpbjjzqznnrwnnvmvjvwvqqrlljgjmjjqhjhwjhwjjsvsnvvlwlqqjpjjbsjjcddtfthffrqfrqrfqfrfllfzzqllztltnltntpttqmmgwmwwwjjdrjrzrlrwlwzzzzphpphvphvphvpvdvfdflljgglrlrqlrrfpfqpqnpqpmmblbflbfbpfpwpjwpjpbjpjmjljblldbllnrnjjjljrlrcrrgzgbbzrznzzhnzhhtmhmqhmqqnzzvfzvzfftjftfvvpmvppwzzhnzhzvvwzwwqcwctwwgrrtgglgngsgvgwvwpvpbbrvrvwrvwvbwwpsprrrcbbpqppfjjjqnjndnmddvsvlslmmwwdwcchmmrjmjwwsrwrhwwpcgmpmvbdmlmqbnblbbgtmtgqslnvcnmwrflrnwhqzrsnwhncwdvbcpjrqsscwzrjvslhscpvmqjnltwhshjmbhgttbzllrvpnhgdmwtddbwmpgbhrndpgwzfvqdmpcvhlnjnqbwqmshgwhmfjgsbscsjtfbfvmlljqwwjtnltbzjpcqdfmdldvmsnmzqmghcvhlpjzzszcpvldrflbcppzwgppfpbslplrqwgbfwpwbtbnrmbrrrpqndzvhmlhvtlnjvhdrhhvmwzjntsgffjsdpdlhnlslnbmbsmdsvpmpbjpjcwhbpfnhbmttfglglnnhhcqvbpvnrwcszwjsdhqwctrpgsrcbfpzhbnrbllrlflqdlgzwpwgwwvmbsppbcjcdltbgwfndqjlvndhtclfmwnqrbfvvqdzspfcslplhtmsqqtrcdvbhfscvnmzplcglwnzbvgflclclqmdgfzjqqrrmpcqrzgqcnzbmncmvdzrmmvfqrrqzqbjmjgjwmfbnmmgvmzsgjjspjqbvfhrdvllgqsffjnfqdthjbjwhwqdqhsnctwgbszvrsbwbpptcjjvftnhdmwmhtjrjrlrdrqjznhqftgtldnpbrlprjmplmwdbzqpmwwvcqccjlhqbhcjprvrfmmfmljwmbrlhcglthgbmrfmbpsztfcglpzlfbpjhmvcmvrprmvmrvvgbbllzschwshhfbnmgwrbhlqvsdjdmmbtjzthwssghtqvhtqcswdhbwhphhsrwhwwtslwvrhpgqfmftnrtqpwfqqdgftdlqfndjlzhvlrthnqdqrzpprdgwwqlplrmqtdqgbdntmjcjhlbbmctvnnhtppfbjpsbmndbdplqlfqfhzqvtqcvmprwbhdtjrqrqvmcssqnfqwtchmjfjlpwsghplbthdnbhfbhhphdmlmtjcvmzjbnqbfldrvlmjfnnrlsnwrtnrpfqdhchltmbvjzhgjwzblqdthmffztqvzzfszmdqdzlfpbfgjfdjqscrhvjflbllnlghncczrlpcbwpnmzcnqhbfnlmnbqvmwvbchwhlfcssctsdcwmtfdgbhljwprfncdgwlgzzmvfqrdsmlwbrmbmtzqqdqdpzvtbstscglrwdpndnstrhgjchnpzsvcjngmzsddwsbllpvqqzzjlmsbncbndqmqphzqzcngjzpfnjmsvngtcvhhjsssrnhjrsmvbsrdjcrppvgsqbqjzbrhjqlgmqfclbrdhwwjdlsjnmjcjrmsstngswrhmdqthprcbrndfpdjgpqtfmmfvbztqdhzhzbzhjjpfgnvrmspdvmhfvbvztmlrhfqfdcfnzzndlfldpfhrbjrwlwnprdmqjwppbrdjhpbvnrvjlrbwdwttzhqqgcclbwghbgrhcvwjrrchqgzztrjgrltcpdscjdfcphndfzdqsbpsjrljdnflfvrzcrjflddfgpvnpmdqbhzwbfrpzsnpbcbgfchvvmqpnfpvvdvhfnpbfzwmbnlpqzgrpwtsjbpzdrsfmnfqwqvpgnrjhmvmphlfdpfhdjljlzftdzdvhjrvjdctwfrctglwmrtdlrvmhcqvfvpgmstghczmvclptzspdsrlvrgvjtvflhsmqswdpqjrrnhgrggzmcpqsctqbhpqrsbstwzgzghqgltcpbrhbcdqlhfjhlcrnhrpjdzlzqqprvzntbjztqgdqqttmbpbcfbjmdjqsflbczmfnsjdzcnnmjjllhvwbwlgrlpvbffwjvctwnrsfqdnwntscpsjnshhdmcqmcpscrmzltldwlmfnbhbtprsgtsbvzfrsfpvmqjzjqqhwdjjzvbhrvtbscrcpzrqsbgfbpwqfmbsrlhhtntjmtrtlwqsdbmgtdlrwfdnmwwzlltbrjgghbstbnffzmzzdlcdgvmrzsjnqjvczmhrgwbrblrqfmzbbcnfsbfpgjsmptzbclfdnwfgbzlpbgqrdhdmfjdnlzczdpvtbzdmrthslngbrldllfcjplhglmwsfpqjnrgpgqfmbbwchbrqqsncfwnmgdmtchdhlzzqjvtvtvgqbtwvwpsjqwdqhrlfmdgdzchvfbcmqjllpjtnphwqvfwpcjmzqhsbwlcmbsgththrsjtnzlsrcptgtrfcwptbstcsdzbwjljzjztzbqsjdvwglpbpgdjtthjjmsnljltglqcszbzqmblfpncntjzzhmjffldcrcvjjswvzlfffrmsgjtpzggtlpfwpwbmhmggpnmzwrgdjrhglnhfdcjdfjtdjvrnlgqtfqmpgjcvcmnwhhbczwwntfbmgssglngqlttfpcznswvmbprsrzljtlwlljnbbrnwdvvlsbdv
|
1
src/input/day06_test1.txt
Normal file
1
src/input/day06_test1.txt
Normal file
@ -0,0 +1 @@
|
||||
mjqjpqmgbljsphdztnvjfqwrcgsmlb
|
1
src/input/day06_test2.txt
Normal file
1
src/input/day06_test2.txt
Normal file
@ -0,0 +1 @@
|
||||
bvwbjplbgvbhsrlpgdmjqwftvncz
|
1
src/input/day06_test3.txt
Normal file
1
src/input/day06_test3.txt
Normal file
@ -0,0 +1 @@
|
||||
nppdvjthqldpwncqszvftbrmjlhg
|
1
src/input/day06_test4.txt
Normal file
1
src/input/day06_test4.txt
Normal file
@ -0,0 +1 @@
|
||||
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
|
1
src/input/day06_test5.txt
Normal file
1
src/input/day06_test5.txt
Normal file
@ -0,0 +1 @@
|
||||
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw
|
@ -5,6 +5,7 @@ const day02 = @import("day02.zig");
|
||||
const day03 = @import("day03.zig");
|
||||
const day04 = @import("day04.zig");
|
||||
const day05 = @import("day05.zig");
|
||||
const day06 = @import("day06.zig");
|
||||
const utils = @import("utils.zig");
|
||||
|
||||
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
|
||||
@ -42,6 +43,7 @@ pub fn main() !void {
|
||||
try solve_day(3, day03, &stdout, &timer);
|
||||
try solve_day(4, day04, &stdout, &timer);
|
||||
try solve_day(5, day05, &stdout, &timer);
|
||||
try solve_day(6, day06, &stdout, &timer);
|
||||
|
||||
try bw.flush();
|
||||
}
|
||||
@ -54,5 +56,6 @@ test {
|
||||
_ = day03;
|
||||
_ = day04;
|
||||
_ = day05;
|
||||
_ = day06;
|
||||
_ = utils;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user