Merge remote-tracking branch '2021/main'
This commit is contained in:
		
						commit
						255a90bd07
					
				|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day1", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							|  | @ -0,0 +1 @@ | ||||||
|  | -bash: ./zig-out/bin/day2: No such file or directory | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | info: [Part 1] There were 1521 increases | ||||||
|  | info: [Part 2] There were 1543 increases | ||||||
|  | @ -0,0 +1,54 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     // Part 1 | ||||||
|  |     var depths = std.ArrayList(u32).init(alloc); | ||||||
|  |     defer depths.deinit(); | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var max: u32 = 0; | ||||||
|  |     var increases: u32 = 0; | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var v = try std.fmt.parseInt(u32, line, 10); | ||||||
|  |         // This will always happen | ||||||
|  |         defer max = v; | ||||||
|  |         // Fill in the depths for part2 | ||||||
|  |         try depths.append(v); | ||||||
|  |         if (max == 0) { | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  |         if (v > max) { | ||||||
|  |             increases += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] There were {} increases", .{increases}); | ||||||
|  | 
 | ||||||
|  |     // Part 2 | ||||||
|  |     // Working with a sliding window of 3 | ||||||
|  |     var i: usize = 0; | ||||||
|  |     increases = 0; | ||||||
|  |     max = 0; | ||||||
|  |     while (i < depths.items.len - 3) : (i += 1) { | ||||||
|  |         // Current window | ||||||
|  |         var a: u32 = depths.items[i] + depths.items[i+1] + depths.items[i+2]; | ||||||
|  |         var b: u32 = depths.items[i+1] + depths.items[i+2] + depths.items[i+3]; | ||||||
|  |         // Visualize it | ||||||
|  |         //std.log.info("A: {} // B: {}\n{} A\n{} A B\n{} A B\n{}   B\n", | ||||||
|  |         //             .{a, b, depths.items[i], depths.items[i+1], | ||||||
|  |         //               depths.items[i+2], depths.items[i+3]}); | ||||||
|  |         if (b > a) { | ||||||
|  |             increases += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 2] There were {} increases", .{increases}); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day10", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,94 @@ | ||||||
|  | {{<{{{{([{[([[()<>]{<>{}}]<([]())(()<>)>)((({}())[()[]])<<[][]>[{}[]]>)]{{(<{}<>>{<><>}]([<>[]]< | ||||||
|  | [(<{{[{(<({{<<[]()><<>{}>>([<>[]]{<><>})}})>)}]}}>[{(<{({[{[[({}())((){})]({{}[]})]<<[<>{}]([][])>({<>()} | ||||||
|  | (({<{[{({(([[([]())({}())]]({[[]{}]([][]))<((){})<{}<>>>))[(([<>[]]<[]>)(([]{}){{}{}}))])})[({<[{ | ||||||
|  | ([{{[([<({<<<([]())[()[]]>{<()[]>[[]()]}>[{<[]{}><[]>>{<<>()>{[]()}}]>[[[[[]{}]([]<>)]<{<>{}} | ||||||
|  | [[((<({<(<{<<{{}()}{[][]}>[((){})]>}>{((<({}<>)<{}()>>[[<>()]])<<<[][]><<>[]>>{<{}[]>(<>())}>)<{[[{ | ||||||
|  | [{<{{{{<([{[(<[]<>>(<>[])){({}<>)([]<>)}]{{([][])[<>{}]}{<[]<>>(<>{})}}}])<<{[<[<>{}]<(){}>>{<{}<>><<> | ||||||
|  | (({({([(<[[([[{}{}]([]<>}][(<>()){(){}}])]](({{{{}<>}<{}[]>}([{}[]][(){}])}[(<[]{}>({}()))<<<><>>>])<[{ | ||||||
|  | [{<((<{(<{<<{[()()](()<>)}<({}[])([]<>)>>{(<<><>>[[]{}])[[[]()](<>[])]}>{[[({}<>)([]<>)][[{}[] | ||||||
|  | {<{<{{(<[[{[[({}[])[()[]]]([()<>][{}[]])]<<({}())[{}{}]><[<>[]](<><>)>>}(([([]{})((){})]((<>)([]{}))))]<{<<< | ||||||
|  | <{(<<(<[<{<[{[<>[]][{}{}]}{{()<>}{<><>}>]>[<(([]{})[[]])>]}<[<([{}{}][<><>]){([]{})}>]({<{<>()}>{ | ||||||
|  | {[[<{{{<<<[<((()<>)({}{}))<{{}()}>>]<((([]{}){{}<>})[{{}<>}([]{})])({{()[]}[[]()]}[{{}()}({ | ||||||
|  | <[[[{{([{[({[<{}[]>[{}()]]{<()>}}({<()()>}<<<>()>{[]}>))]((({({}[])>{[(){}]{<><>}})(({()[]}[()<>] | ||||||
|  | [([([([<[{{<[[{}[]]{<>{}}][<{}>(()[])]>[({<>()}(()[])){<[][]>{()[]}}]}}<{(({()}([]()))(([]())<()[]>))<((<>{} | ||||||
|  | <{[{(({([([[{((){})[[]]>{{{}[]}[<>()]}]])]{([([<[]{}>((){})]<[()<>]<()()>>)][<<{<>{}}{<><> | ||||||
|  | [[{{(([{(({[[{{}[]}]][<<<>()>({}{})>[[{}<>]{{}<>}]]}[(({{}}{[][]}){[{}{}][<>[]]})(<<<>{}><<><>>>)])<[ | ||||||
|  | [[{([<<([[<(<{<>()}<[][]>>{[<>[]]{()[]}}){[<[][]><{}()>][([]())<<><>>]}>]((<<[(){}][(){}]><([])>>)) | ||||||
|  | [{[<{(([(([([(<>[]}(<>{})][<{}[]>[()[]]])<{[{}<>]<[]<>>}{{[]<>}{{}{}}}>]<<{[<>{}](<>())}{{()[]}{<>{}}}>[ | ||||||
|  | {({<[(((<{<<[[()<>]]<({}<>)>>>[<({[]<>}{()<>})[[()<>][<><>]]>({[{}[]]({}<>)}[<(){}>(<>())])]}><[{<[[<> | ||||||
|  | <(((({{(<({[<<[]<>>>([()()]{<>()})][<[{}{}]{<>()}>{{{}{}}[[][]]}]}{(<<<><>>(()<>)>(<<>()>[()[]])) | ||||||
|  | [<{{[{[[[<(({<()()>[(){}]}[([]())[(){}]]))>{[<[[[]]<[][]>]{[[][]](()())}>[(<<>()>{[]<>})(({}())<{}[]>) | ||||||
|  | (<[[((<<({[(([()[]]([]<>)){{(){}}})]{<(<<><>><[]{}>)<<{}<>]([]<>)>>}}){(<{<{[]{}}[{}<>]>}>{({(<><>)<()[]>}{(( | ||||||
|  | {[{((<{<<<<([<[]{}>[[][]]]<[[]()][<>[]]>)[[<{}[]>{{}()}](<()<>>[<>[]])]>>[{([<()[]>{{}[]}](({}{} | ||||||
|  | (<([<((<{[<<(((){})<<><>>)<[{}()]<[]<>>}>[([[]{}](()[]))]>[<{({}()){{}()}}>[(([]<>)({}[])){ | ||||||
|  | {((((<(({[(<{<()]([][])}>){[<[[][]]>]<{{{}<>}[[][]]}>}]}[<{([{[]{}}]{[(){}]<<>()>})[(<()<>>{()<> | ||||||
|  | <<{([{<[[[<{([[]<>]){[<>()][(){}]}}[{(()[])[()[]]}]>][{{[{{}[]}<()<>>]>[[{(){}}[<>[]]][[<>[]]({}() | ||||||
|  | <((<[<(([<[{({<><>}(()[]))}]<[<[{}()]<()[]>>(<()()>)][[(<>())<()())]<{<>}<{}>>]>>({{<<<>[]><<>( | ||||||
|  | [({{{{{{(([[<<()()><<>[]>>[{[]<>}]]{(<{}<>>(()[]))<<[][]>(()())>}]([<({}()><[][]>>]<{[[][]]<[]<>>}( | ||||||
|  | (<(<{{<[{{{[<[<>[]]<[]{}>>[{<>{}}{()()}]]{<<{}()>(<>())>[[{}[]]]}}}}][{([{[([]{})<()[]>][[<>{}]{()[]}]}(([() | ||||||
|  | {[<<(<<<{[<<<<[]<>><()[]>>[({}[])[[]<>]]><((<><>)[{}<>])[{{}{}}[[]()]]>>{{{[<><>]<<>()>}[<[]{}>([][])] | ||||||
|  | <(<{<(<({[[{((()[])[{}<>])([<>()]>}{[({}{})[(){}]]<[()<>]({}[])>}]{[<{[]{}}><{[]{}}[[][]]>][[<{}<>><()[]>]<[ | ||||||
|  | {<({[<((<<<{{([]<>}{<><>}}<{<>{}}[[]<>]>}>{<[{{}}(()())]>}>(<<{[(){}]<()[]>}({(){}}[()<>])>[<<<>[]>{[] | ||||||
|  | {(({<{{{{{((<{[]<>>(<>{})>[[<>()]])<<(()())[<>]><[(){}][{}{}]>>)}({([[[]]]{<{}{}><()[]>})<{[(){}][[]()]}> | ||||||
|  | <<[<<[((({[[(<{}[]>({}())){[()()]}][{{<>{}}<<>()>}[<{}[]>[<>[]]]]]{<(([]())([]()))>{([()()]<<>[]>)[< | ||||||
|  | {([<<<({((<{{[<>[]][<>{}]}<<<>[]>[[][]]>}[{[()[]]<<>[]>}<[{}{}]<<><>>>]>({[<[]<>>]<<()()>([][])>}(((< | ||||||
|  | (<{<((<{[[<[(<[]{}>{<>{}})][<([][])[{}<>]>[<{}<>>]]><<[(<>[])[{}<>]]{<{}{}><{}[]>}>(<([]<>){()[]}>)>]]}{({[<( | ||||||
|  | [{{(({<<<<{{<({}()){{}{}}>[(()[])]}[([(){}]{<>[]})[(<>())[{}]]]}>[<<<{[]{}}{{}{}}>]{<[{}<>]>{(()())}}>[{(<[]< | ||||||
|  | {[<<{<[[({{<<{()[]}<<><>>>([[]<>>[{}[]])>}[[[([]<>)[<>{}]]]<<[<>()](<>{})>>]})(((({({}[])<<><>>}([<>])))((< | ||||||
|  | [{(({({<({(<{{[][]}({}<>)}[{()()}{[][]}]>(((<><>)(<>()))([{}()][{}()])))([{[[]()]<[]{}>}{( | ||||||
|  | {<{{<[<<[(<([({}())[{}()]]{{{}<>}<[]>}){[((){})<[][]>]<([]())({}[])>}>{<(((){})[()<>])(<()[]>[(){}] | ||||||
|  | {({({<{{(<([<[<><>]((){})>{{()[]}<<>{}>}]{[(<>())<{}[]>][{()<>}[[]()]>})>[[<{([])[()[]]}><<[(){}]({}())>>]{ | ||||||
|  | (<{{(<<<{(<(({<><>}{()[]})<[{}[]]([]())>)([(()<>)[[]<>]]({[]()}{{}{}}))))[(<{{()[]}<()[]>}((<><>))>[[ | ||||||
|  | <<[<([{<<[[({<[]<>><<>()>}{([]<>)[[]<>]}){[<{}{}><()[]>]<{<>()}{<>()}>}]{{<({}{})>})]>[<({< | ||||||
|  | (<<[[({[({(<{{[]{}}<[]{}}}>[<<()<>><{}<>>>(<()()>{{}<>})])}{<({({}[])<()>}<([]())<{}<>>>)<<[( | ||||||
|  | [<(<{(([(<([[<<>{}>(<>{})]](((()[])[[]()])[[{}<>]({}{})]))><<{<([]<>){[][]}>[{<>[]}<()<>>]}>(<((<>[]){()<>} | ||||||
|  | [<{[[{(([[{[{<[]<>>(<>[])}<<<>()>{{}{}}>][({[][]}<<>>){(<>[])[<><>]}]}(([([][])(<>[])]{(()())}))](<<{ | ||||||
|  | ((<{<<([<[(([{()()}((){})][<()[]><()[]>])<{({}<>){{}<>}}>)[{(<<><>>)<{{}[]}>}({(<>()){()}}{([ | ||||||
|  | [({{{{((([({{(<>[])[[]<>]}([{}])}(({<><>}{<>()})[[{}[]]]))<{[[<>{}](()[])](<()<>>{[]()})}{{<()() | ||||||
|  | [<[<({[[({[[[<<>[]>]({[]}[<><>])]{[[()<>]([]())]<({}{})[[][]]>}]}<<(([()()]<()<>>)<{()()}[()[]]> | ||||||
|  | {<([[<[<<(([<(()[])(()<>)>{[<><>](<>[])]]{({{}})}){<((<><>)<{}<>>)((()[])[[]()])>})<<(<(())( | ||||||
|  | <[<((<{(<[[({{{}()}({}{})}}<[[()<>]]<[()()][<>]>>]<[([()()]({}<>))[<{}()>([]<>)]]>][{[[(()())[()]][[<>[] | ||||||
|  | [{(<(<[((<{(({(){}}[()[]])<{[][]}(<>{})>)[(<<><>>[()[]]]{[{}[]]{()<>}}]}><[<[[{}()](()[])][(<>()){<>()}]>[{ | ||||||
|  | ([({<({{{[[<[{{}<>}([][])]{[(){}]{()<>}}>][{[{[]<>}][<{}()>]}]]}{((<[{{}[]}(()()))[{<>[]}]>({(<>{})<[]<>>} | ||||||
|  | {[({[<[([(<{((<>{})<{}{}>)}(<[[]()]<{}{}>>({{}[]}{[]<>}))><[((()[])(())){([]<>)[()[]]}]<({() | ||||||
|  | [(<<<([{[[[<<<<>>{{}<>}>[{[]()}]>[([[]{}]{[]<>}){[<>()]}]]]]}]<<((<([{[][]}<{}<>>]<[[]{}][[]<>])){{[<>[] | ||||||
|  | {[{{[[{{[<[<<{{}<>}<[]{}>><<()[]>[<>[]]>>[([<>{}](()()))]]>{({([{}[]](<><>))<<(){}>(<>{})>}({([]<>)[{}[])}(<< | ||||||
|  | <{([[{<[<{{([(<>{})[<>()]]){{[()[]]([])}}}[{<((){})([]())>}(<{[][]}<<>{}>>[[[][]]<()()>])]}>[ | ||||||
|  | {(<[({{(<<<{[(()())]<[<>[]][{}{}]>}[[<()[]>{[][]}]]>(<((<>())[()[]]){{()[]}{[][]}}>({{[]<>}<[][]>}(<[]{} | ||||||
|  | <{{[{((((<[{({[]{}}<(){}>)}[[{[][]}<<>{}>]<<[][]>{[]}>]]>{{([((){})<<>{}>]{{()()}})<[<<>()>](<<>>[() | ||||||
|  | <([({(<<<{[<{(()<>)(<>[])}([<>]{<>{}})>]<(<<<><>><{}()>>[([][]){[]<>}])>}{[([[[]<>]{(){}}]<[{}<>]{{}<>} | ||||||
|  | {<<<[{[(<[([([[]()]<()[]>)(([]())({}[]))]<[[()<>]]<[(){}]<[][]>>>)(<{<[][]>)<(()<>)<{}[]>> | ||||||
|  | {[[<[[<(<[[{(<()<>>)<{{}<>}[[][]]>}<<(()()){[]{}}>{<[]()>>>]]>)({<<<<[{}{}]{<>()}>{<<><>>([]{})}>>[([<()()>( | ||||||
|  | [[[{({{({[{[(({}())(<>[]))<<<>[]>[[]<>]>]<<<()()><<>>>[({}())(<>[])]>}[({<(){}>({}{})}[[{}()]]){[[[]<> | ||||||
|  | <(((<<[{[[<({({}<>)[{}()]}[{[]{}}([]())]){([[]<>]<[]<>>)[(()[])[{}[]]]}>(<<<{}[]><()[]>)>[[<{}( | ||||||
|  | [<<{[{{<<[[{<<()[]>(())><<{}()][<>()]>}({[{}{}][<>()]}<([]())([])>)]({{{[][]}[<>{}]}<[()]{{}()}>}(({ | ||||||
|  | {([(<[[<(<<<(<<><>><{}<>>)({{}()}[<>()])>{([(){}][[][]]){[<><>]{<><>}}}>>(({(<<><>><()<>>){{()<>}{(){}}}} | ||||||
|  | [(<([[[([(({[[<>[]]]}))([{{[{}()]([]<>)}{{<>[]}[[][]]}}(<([]())>[([]{}){()<>}])][<{<()[]><()()>}>{<([ | ||||||
|  | {([[<<[[<[[[<[{}[]]{[]<>}>[<{}()>{<>[]}]]]<[<{[][]}(<>[])>[[{}[]]<[][]>]][((()[])[<>{}]){{{}()}[(){}]}]>]> | ||||||
|  | {[[[<<{{[<<<([{}[]]<<>[]>)([()]<{}{}>)>({<<>{}>(()[])})>[(((<><>>{<>{}})<<<>[]>[[]()]>)({(()( | ||||||
|  | [[[{<<(({{[((<{}<>>[{}])({<>{}}<{}{}>))[(<{}()>(<>{}))(<<><>>{{}{}})]]({{<()>}}(<{(){}](<><>) | ||||||
|  | (({{[[[<[{<{[<{}()><<>{}>]}({[[]()]<{}[]>])>}[<{{(<><>)<{}()>}{{{}<>}{{}[]}}}(([<>[]]([][])) | ||||||
|  | <[<<{({[(<(<{({}<>)<[][]>}{(()[])({}[])}>{(<{}{}>>})>([<<<<>{}>((){})>[(()())[[]()]]><{[{}<>]<<> | ||||||
|  | [{<{[(<[[[{({({}<>)(()<>)}{<[][]>})}{[<[()[]]{[]<>]>{<{}()>(()())}]{[<()()>{<>[]}]<[<><>]>}}] | ||||||
|  | ({{<{[[[([({{<<>()>(()<>)}(<{}()><()[]>)}<[[{}()]<{}[]>]{((){}){{}[]}}>)[([{<><>}<()<>>][(<><>)(()())] | ||||||
|  | (({<{{{[([{<<<{}{}>{[]{}}>([<>[]]{[]{}})>}{<<{[]{}}[[]{}]>{<<>{}>([]())}><(<(){}>({}[]))[(<>[])(()<>)]> | ||||||
|  | <[<(<{[[<([({[<>()][<>{}]}{{()()}[<><>]})]<[[<[]()>]<[{}<>]([])>]{<[<>[]]<<>()>]((<>())[<>{}])}>)([{<[[][]] | ||||||
|  | <<{{((({<<[[<{<>()}(()())>[(()[])[()<>]]]{({{}[]}[<><>])}]<{<<<><>>{{}[]}>}<[<[][]>[{}[]]](<()()>{[]{}}) | ||||||
|  | [<<{<{([{(({[{()[]}{()}][({}{})(()[])]}{({<>()})({[]()}({}[]))}))}][[[([[{<>[]}[{}[]]][[<><>]<()[]>]]([ | ||||||
|  | [([[{{<(<[{(<<()<>>><[[]()]{()()}>)<(({}()){[]<>})>}]>)>{<[<{{((()())({}<>)>{([]{}){{}<>}}}}<({ | ||||||
|  | ((<<{(<[(<[[<({})>[{[][]}[<>[]]]]]>)]>)}([(<[[([(([][]))[{{}()}([][])]][((<><>){[]()})])]]<<( | ||||||
|  | <<<<(<{[{[({<{(){}}[<>]>[<[]<>>{<>()}]}[<[<>()]>{[[]<>][{}[]}}])(<{<(){}>[{}{}]}<(()<>)<()[]>>>)][<{{<[]< | ||||||
|  | (([[<{[({{{[<<<><>>(()())>({[]<>}(<>()))]{<[<>()][{}{}]>([[]()]((){}))}}((<({}{})[<>]>([<>()]{(){}})))}[<{([< | ||||||
|  | {[([<<[([[<[(<[]()>{()[]})<{<>()}{(){}}>]><<{<[][]><{}()>}(<[]<>>([]<>])>>]{[[<<{}<>>({}[] | ||||||
|  | {{[<<(<({[<<{<[]<>>}{(<>())}>[{[<>]{(){}}}]>]}{{{<([()<>]{<>{}})(<()[]>[[]()])><[<<>[]>({}{})]((()())[{ | ||||||
|  | [{{[<{<<{{<[<<()()>[{}]>([{}{}]({}{}))]{(<[][]>([][]))(([]{})<<>[]>)}>}({{<{<>[]}[<>()]>(({ | ||||||
|  | [<{[(<<[<<(([[<>[]]][([]())({}<>}])){(({[]()}{()<>})[<[]{}>(<><>)])[<[{}[]][{}{}]><{(){}}<{}{}>>]}>({<<[[]() | ||||||
|  | [({[<<<({<({({{}<>}[[]{}])}({({}<>)<<>[]>}))([<{(){}}>])>}{<{[[{{}{}}(<>{})]]<[[()[]]<<><>>]{[<><>]<{}()> | ||||||
|  | [<{<<<<(([([[{{}()}[<>]][((){})[[]{}]]](({[]()}[[]()])[{[]()}(<>[])])){[{<<><>>{{}<>}}<{()[]}[[] | ||||||
|  | [{({<(((<({{({[]()}))[((()())<[][]>)<{<>{}}(<><>)>]}{[<[[][]](<><>)>][[{<>{}}[<>{}]](<<>[]>{[][]})]})>)))>} | ||||||
|  | [[{[[{({[([(<<<>[]>[<><>]>([{}()][<>()]))<<[<>{}]{{}<>}>[<<>{}>([]{}]]>])<(<(<[]()>)[(()[]) | ||||||
|  | <([(<<[<<({{{([]{})}[{()<>}<[]()>]}}{(<{[]{}}([][])>)})[[[<[[][]]<<><>>><{{}{}}(<><>)>]<<(()())[(){}]>{ | ||||||
|  | [(<{[{({[({<{<{}[]><<>()>}{<{}()>[[][]]}><<{{}()}{(){}}>>}[<{{<>{}}{()())}>]){[<[{()()}<[]()>]><[[{ | ||||||
|  | <{<[{([([<<[<[<>()]{(){}}>][({{}[]}((){}))[<[][]><()<>>]]>>{{{<{<><>}{(){}}>({<><>}<()))}({<()><{}[]>}(<( | ||||||
|  | ({((([[{([<[(([]{})){[()<>][<><>]}]>{[<<{}{}><()[]>>(<<>>)]({<<><>>[<>{}]}{<[]{}><<>{}>})}])[(<(([<><>][{}{ | ||||||
|  | <[<(([(({([[<(()[])[[]{}]>{<<>{}>(<>{})}]]([([<>{}]{[]{}})][{<()>{()[]}}(({}[])(<>[]))]))})({{[(({(){}}[<> | ||||||
|  | @ -0,0 +1,424 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var lit = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var corrupt_score: u32 = 0; | ||||||
|  |     var completion_scores = std.ArrayList(u64).init(alloc); | ||||||
|  |     defer completion_scores.deinit(); | ||||||
|  |     var line_id: usize = 0; | ||||||
|  |     while (lit.next()) |line| { | ||||||
|  |         //std.log.debug("{any}", .{nodes}); | ||||||
|  |         var tree = try line_to_tree(alloc, line, line_id); | ||||||
|  |         corrupt_score += tree.corrupt; | ||||||
|  |         if (tree.corrupt == 0) { | ||||||
|  |             // Try completion | ||||||
|  |             var score = try tree.autocomplete(); | ||||||
|  |             std.log.debug("Line {} autocompletion score {}", .{line_id, score}); | ||||||
|  |             try completion_scores.append(score); // score | ||||||
|  |         } | ||||||
|  |         line_id += 1; | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] Sum of corrupt line scores: {}", .{corrupt_score}); | ||||||
|  |     std.sort.sort(u64, completion_scores.items, {}, comptime std.sort.asc(u64)); | ||||||
|  | 
 | ||||||
|  |     std.log.info( | ||||||
|  |         "[Part 2] Completion scores: {}", | ||||||
|  |         .{completion_scores.items[completion_scores.items.len/2]} | ||||||
|  |     ); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn line_to_tree(alloc: *std.mem.Allocator, line: []const u8, id: usize) !Tree { | ||||||
|  |     var tkzr = Tokenizer.init(line); | ||||||
|  |     var tkns = std.ArrayList(Token).init(alloc); | ||||||
|  |     var corrupt_score: u32 = 0; | ||||||
|  |     while (true) { | ||||||
|  |         var tkn = tkzr.next(); | ||||||
|  |         if (tkn.tag != .eos) { | ||||||
|  |             try tkns.append(tkn); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     //std.log.debug("{} tokens", .{tkns.items.len}); | ||||||
|  |     var nodes = std.ArrayList(Node).init(alloc); | ||||||
|  |     try nodes.append(.{ | ||||||
|  |         .index = 0, | ||||||
|  |         .parent = 0, | ||||||
|  |         .tag = .root, | ||||||
|  |         .start_index = 0, | ||||||
|  |     }); | ||||||
|  |     var current_node: usize = 0; | ||||||
|  |     for (tkns.items) |tkn, index| { | ||||||
|  |         switch(tkn.tag) { | ||||||
|  |             .r_paren, | ||||||
|  |             .r_brace, | ||||||
|  |             .r_bracket, | ||||||
|  |             .r_angle_bracket, | ||||||
|  |             => { | ||||||
|  |                 if (current_node == 0) { | ||||||
|  |                     std.log.debug("Closing token before opening token: {}", | ||||||
|  |                                   .{tkn}); | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |                 else { | ||||||
|  |                     //std.log.warn("Current node: {}", .{current_node}); | ||||||
|  |                     nodes.items[current_node].end_index = index; | ||||||
|  |                     if (nodes.items[current_node].tag != token_tag_to_node_tag(tkns.items[index].tag)) { | ||||||
|  |                         std.log.warn( | ||||||
|  |                             "Line {}: mismatched tags: {} != {}", | ||||||
|  |                             .{id, tkns.items[nodes.items[current_node].start_index].tag, | ||||||
|  |                               tkns.items[index].tag} | ||||||
|  |                         ); | ||||||
|  |                         corrupt_score += switch(tkns.items[index].tag) { | ||||||
|  |                             .r_paren => @as(u32, 3), | ||||||
|  |                             .r_bracket => @as(u32, 57), | ||||||
|  |                             .r_brace => @as(u32, 1197), | ||||||
|  |                             .r_angle_bracket => @as(u32, 25137), | ||||||
|  |                             else => @as(u32, 0), | ||||||
|  |                         }; | ||||||
|  |                         break; | ||||||
|  |                     } | ||||||
|  |                     current_node = nodes.items[current_node].parent; | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             .eos, | ||||||
|  |             .invalid, | ||||||
|  |             => unreachable, | ||||||
|  |             else => { | ||||||
|  |                 // Opens a new node | ||||||
|  |                 var node = Node { | ||||||
|  |                     .index = nodes.items.len + 1, | ||||||
|  |                     .parent = current_node, | ||||||
|  |                     .start_index = index, | ||||||
|  |                     .tag = switch(tkn.tag) { | ||||||
|  |                         .l_paren => .paren, | ||||||
|  |                         .l_brace => .brace, | ||||||
|  |                         .l_bracket => .bracket, | ||||||
|  |                         .l_angle_bracket => .angle_bracket, | ||||||
|  |                         else => unreachable, | ||||||
|  |                     } | ||||||
|  |                 }; | ||||||
|  |                 try nodes.append(node); | ||||||
|  |                 try nodes.items[current_node].children.append(alloc, nodes.items.len - 1); | ||||||
|  |                 current_node = nodes.items.len - 1; | ||||||
|  |                 //std.log.warn("Start new node from token index {} at {}: {any}", | ||||||
|  |                 //            .{index, current_node, node}); | ||||||
|  |             }, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return Tree { | ||||||
|  |         .nodes = nodes, | ||||||
|  |         .tokens = tkns, | ||||||
|  |         .corrupt = corrupt_score, | ||||||
|  |     }; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub const Tree = struct { | ||||||
|  |     nodes: std.ArrayList(Node), | ||||||
|  |     tokens: std.ArrayList(Token), | ||||||
|  |     corrupt: u32 = 0, | ||||||
|  | 
 | ||||||
|  |     pub fn autocomplete(self: *Tree) anyerror!u64 { | ||||||
|  |         // I guess we want to traverse the tree, adding in tokens for end_index | ||||||
|  |         // where necessary | ||||||
|  |         var node = self.nodes.items[0]; | ||||||
|  |         var score: u64 = 0; | ||||||
|  |         for (node.children.items) |n| { | ||||||
|  |             score += try self.complete(&self.nodes.items[n], score); | ||||||
|  |         } | ||||||
|  |         return score; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fn complete(self: *Tree, node: *Node, score: u64) anyerror!u64 { | ||||||
|  |         var s = score; | ||||||
|  |         for (node.*.children.items) |n| { | ||||||
|  |             s += try self.complete(&self.nodes.items[n], score); | ||||||
|  |         } | ||||||
|  |         if (node.*.end_index) |ei| { | ||||||
|  |             return s; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             try self.tokens.append(.{ | ||||||
|  |                 .tag = switch(node.*.tag) { | ||||||
|  |                     .paren => .r_paren, | ||||||
|  |                     .brace => .r_brace, | ||||||
|  |                     .bracket => .r_bracket, | ||||||
|  |                     .angle_bracket => .r_angle_bracket, | ||||||
|  |                     else => .invalid, | ||||||
|  |                 }, | ||||||
|  |                 .loc = .{.start = 0, .end = 0}, // fake | ||||||
|  |             }); | ||||||
|  |             node.*.end_index = self.tokens.items.len - 1; | ||||||
|  |             s *= 5; | ||||||
|  |             s += switch(node.*.tag) { | ||||||
|  |                 .paren => @as(u64, 1), | ||||||
|  |                 .brace => @as(u64, 3), | ||||||
|  |                 .bracket => @as(u64, 2), | ||||||
|  |                 .angle_bracket => @as(u64, 4), | ||||||
|  |                 else => @as(u64, 0), | ||||||
|  |             }; | ||||||
|  |         } | ||||||
|  |         return s; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | pub const Node = struct { | ||||||
|  |     index: usize, | ||||||
|  |     parent: usize, | ||||||
|  |     children: std.ArrayListUnmanaged(usize) = .{}, | ||||||
|  |     tag: Tag, | ||||||
|  |     start_index: usize, | ||||||
|  |     end_index: ?usize = null, | ||||||
|  |     pub const Tag = enum { | ||||||
|  |         root, | ||||||
|  |         paren, | ||||||
|  |         brace, | ||||||
|  |         bracket, | ||||||
|  |         angle_bracket, | ||||||
|  |         invalid, | ||||||
|  |     }; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | fn token_tag_to_node_tag(t: Token.Tag) Node.Tag { | ||||||
|  |     switch(t) { | ||||||
|  |         .l_paren, | ||||||
|  |         .r_paren, | ||||||
|  |         => return .paren, | ||||||
|  |         .l_brace, | ||||||
|  |         .r_brace, | ||||||
|  |         => return .brace, | ||||||
|  |         .l_bracket, | ||||||
|  |         .r_bracket, | ||||||
|  |         => return .bracket, | ||||||
|  |         .l_angle_bracket, | ||||||
|  |         .r_angle_bracket, | ||||||
|  |         => return .angle_bracket, | ||||||
|  |         else => return .invalid | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub const Token = struct { | ||||||
|  |     tag: Tag, | ||||||
|  |     loc: Loc, | ||||||
|  | 
 | ||||||
|  |     pub const Loc = struct { | ||||||
|  |         start: usize, | ||||||
|  |         end: usize, | ||||||
|  |     }; | ||||||
|  |     pub const Tag = enum { | ||||||
|  |         l_paren, | ||||||
|  |         r_paren, | ||||||
|  |         l_brace, | ||||||
|  |         r_brace, | ||||||
|  |         l_bracket, | ||||||
|  |         r_bracket, | ||||||
|  |         l_angle_bracket, | ||||||
|  |         r_angle_bracket, | ||||||
|  |         eos, | ||||||
|  |         invalid, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     pub fn lexeme(tag: Tag) ?[]const u8 { | ||||||
|  |         return switch (tag) { | ||||||
|  |             .l_paren => "(", | ||||||
|  |             .r_paren => ")", | ||||||
|  |             .l_brace => "{", | ||||||
|  |             .r_brace => "}", | ||||||
|  |             .l_bracket => "[", | ||||||
|  |             .r_bracket => "]", | ||||||
|  |             .l_angle_bracket => "<", | ||||||
|  |             .r_angle_bracket => ">", | ||||||
|  |             else => "", | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | pub const Tokenizer = struct { | ||||||
|  |     index: usize, | ||||||
|  |     buffer: []const u8, | ||||||
|  | 
 | ||||||
|  |     pub fn init(buffer: []const u8) Tokenizer { | ||||||
|  |         return Tokenizer{ | ||||||
|  |             .buffer = buffer, | ||||||
|  |             .index = 0, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn next(self: *Tokenizer) Token { | ||||||
|  |         const start_index = self.index; | ||||||
|  |         var result = Token { | ||||||
|  |             .tag = .eos, | ||||||
|  |             .loc = .{ | ||||||
|  |                 .start = start_index, | ||||||
|  |                 .end   = undefined, | ||||||
|  |             }, | ||||||
|  |         }; | ||||||
|  |         while (self.index < self.buffer.len) : (self.index += 1) { | ||||||
|  |             const c = self.buffer[self.index]; | ||||||
|  |             switch (c) { | ||||||
|  |                 '(' => { | ||||||
|  |                     result.tag = .l_paren; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 ')' => { | ||||||
|  |                     result.tag = .r_paren; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 '{' => { | ||||||
|  |                     result.tag = .l_brace; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 '}' => { | ||||||
|  |                     result.tag = .r_brace; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 '[' => { | ||||||
|  |                     result.tag = .l_bracket; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 ']' => { | ||||||
|  |                     result.tag = .r_bracket; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 '<' => { | ||||||
|  |                     result.tag = .l_angle_bracket; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 '>' => { | ||||||
|  |                     result.tag = .r_angle_bracket; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |                 else => { | ||||||
|  |                     result.tag = .invalid; | ||||||
|  |                     self.index += 1; | ||||||
|  |                     break; | ||||||
|  |                 }, | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if (result.tag == .eos) { | ||||||
|  |             result.loc.start = self.index; | ||||||
|  |         } | ||||||
|  |         result.loc.end = self.index; | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | test "tokenizer - all" { | ||||||
|  |     try testTokenize("()[]{}<>", &.{ | ||||||
|  |         .l_paren, .r_paren, .l_bracket, .r_bracket, .l_brace, .r_brace, | ||||||
|  |         .l_angle_bracket, .r_angle_bracket | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "tokenizer - early end" { | ||||||
|  |     try testTokenize("({", &.{ | ||||||
|  |         .l_paren, .l_brace, | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "tokenizer - invalid" { | ||||||
|  |     try testTokenize("<d", &.{ | ||||||
|  |         .l_angle_bracket, .invalid, | ||||||
|  |     }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn testTokenize(source: []const u8, expected_tokens: []const Token.Tag) !void { | ||||||
|  |     var tokenizer = Tokenizer.init(source); | ||||||
|  |     for (expected_tokens) |expected_token_id| { | ||||||
|  |         const token = tokenizer.next(); | ||||||
|  |         if (token.tag != expected_token_id) { | ||||||
|  |             std.debug.panic("expected {s}, found {s}\n", .{ @tagName(expected_token_id), @tagName(token.tag) }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     const last_token = tokenizer.next(); | ||||||
|  |     try std.testing.expect(last_token.tag == .eos); | ||||||
|  |     try std.testing.expect(last_token.loc.start == source.len); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "ok" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "[<>({}){}[([])<>]]", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 0), tree.corrupt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "a" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "{([(<{}[<>[]}>{[]{[(<()>", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 1197), tree.corrupt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "b" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "[[<[([]))<([[{}[[()]]]", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 3), tree.corrupt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "c" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "[{[{({}]{}}([{[{{{}}([]", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 57), tree.corrupt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "d" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "[<(<(<(<{}))><([]([]()", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 3), tree.corrupt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "e" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "<{([([[(<>()){}]>(<<{{", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 25137), tree.corrupt); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "autocomplete simple" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "<", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 0), tree.corrupt); | ||||||
|  |     var score = try tree.autocomplete(); | ||||||
|  |     try std.testing.expectEqual(@as(u64, 4), score); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "autocomplete" { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  |     var tree = try line_to_tree(alloc, "<{([{{}}[<[[[<>{}]]]>[]]", 1); | ||||||
|  |     try std.testing.expectEqual(@as(u32, 0), tree.corrupt); | ||||||
|  |     var score = try tree.autocomplete(); | ||||||
|  |     try std.testing.expectEqual(@as(u64, 294), score); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day11", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | 1564524226 | ||||||
|  | 1384554685 | ||||||
|  | 7582264835 | ||||||
|  | 8812672272 | ||||||
|  | 1161463137 | ||||||
|  | 7831762344 | ||||||
|  | 2855527748 | ||||||
|  | 6141737874 | ||||||
|  | 8611458313 | ||||||
|  | 8215372443 | ||||||
|  | @ -0,0 +1,133 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var map: [100]u8 = undefined; | ||||||
|  |     var lit = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var line_number: u16 = 0; | ||||||
|  |     while (lit.next()) |line| { | ||||||
|  |         for (line) |c, i| { | ||||||
|  |             map[i + @as(usize, line_number * 10)] = try std.fmt.parseInt(u8, line[i..i+1], 10); | ||||||
|  |         } | ||||||
|  |         line_number += 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var step: u16 = 0; | ||||||
|  |     var flashed = std.AutoHashMap(usize, void).init(alloc); | ||||||
|  |     defer flashed.deinit(); | ||||||
|  |     var flashes: u32 = 0; | ||||||
|  |     while (step < 1000) { | ||||||
|  |         // Increase energy level of octopuses by 1 | ||||||
|  |         for (map) |v, i| { | ||||||
|  |             map[i] += 1; | ||||||
|  |         } | ||||||
|  |         // Check for high energy octopi | ||||||
|  |         for (map) |v, i| { | ||||||
|  |             if (v > 9) { | ||||||
|  |                 // Flash! | ||||||
|  |                 try flash(i, map[0..], &flashed); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         //std.log.debug("Step {} had {} octopi flash", .{step, flashed.count()}); | ||||||
|  |         flashes += flashed.count(); | ||||||
|  |         if (flashed.count() == 100) { | ||||||
|  |             std.log.info("[Part 2] On step {}, all octopi flashed", .{step+1}); | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |         // Return all flashed octopi to 0 energy | ||||||
|  |         var fit = flashed.keyIterator(); | ||||||
|  |         while (fit.next()) |fi| { | ||||||
|  |             map[fi.*] = 0; | ||||||
|  |         } | ||||||
|  |         flashed.clearRetainingCapacity(); | ||||||
|  |         step += 1; | ||||||
|  |         if (step == 100) { | ||||||
|  |             std.log.info("[Part 1] After {} steps, {} octopi have flashed", | ||||||
|  |                          .{step, flashes}); | ||||||
|  |         } | ||||||
|  |         // std.log.debug("After step:\n", .{}); | ||||||
|  |         // var i: usize = 0; | ||||||
|  |         // while (i < 10) : (i += 1) { | ||||||
|  |         //     std.log.debug("{any}", .{map[i..i+10]}); | ||||||
|  |         // } | ||||||
|  |         // std.log.debug("\n", .{}); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn flash(i: usize, map: []u8, flashed: *std.AutoHashMap(usize, void)) anyerror!void { | ||||||
|  |     if (flashed.contains(i)) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |     try flashed.putNoClobber(i, undefined); | ||||||
|  |     var adjacents = get_adjacent_points(i, 10, 10); | ||||||
|  |     for (adjacents) |adj| { | ||||||
|  |         if (adj) |a| { | ||||||
|  |             if (!flashed.contains(a)) { | ||||||
|  |                 map[a] += 1; | ||||||
|  |                 if (map[a] > 9) { | ||||||
|  |                     try flash(a, map, flashed); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_adjacent_points(i: usize, width: usize, height: usize) [8]?usize { | ||||||
|  |     var adjacents = [8] ?usize { | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |     }; | ||||||
|  |     var ai: usize = 0; | ||||||
|  |     var bottom = i < ((height - 1) * width); | ||||||
|  |     var top = i >= width; | ||||||
|  |     var left = (i % width) != 0; | ||||||
|  |     var right = (i % width) != (width - 1); | ||||||
|  |     if (top) { | ||||||
|  |         if (left) { | ||||||
|  |             adjacents[ai] = i - width - 1; | ||||||
|  |             ai += 1; | ||||||
|  |         } | ||||||
|  |         adjacents[ai] = i - width; | ||||||
|  |         ai += 1; | ||||||
|  |         if (right) { | ||||||
|  |             adjacents[ai] = i - width + 1; | ||||||
|  |             ai += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     if (left) { | ||||||
|  |         adjacents[ai] = i - 1; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (right) { | ||||||
|  |         adjacents[ai] = i + 1; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (bottom) { | ||||||
|  |         if (left) { | ||||||
|  |             adjacents[ai] = i + width - 1; | ||||||
|  |             ai += 1; | ||||||
|  |         } | ||||||
|  |         adjacents[ai] = i + width; | ||||||
|  |         ai += 1; | ||||||
|  |         if (right) { | ||||||
|  |             adjacents[ai] = i + width + 1; | ||||||
|  |             ai += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return adjacents; | ||||||
|  | } | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | 5483143223 | ||||||
|  | 2745854711 | ||||||
|  | 5264556173 | ||||||
|  | 6141336146 | ||||||
|  | 6357385478 | ||||||
|  | 4167524645 | ||||||
|  | 2176841721 | ||||||
|  | 6882881134 | ||||||
|  | 4846848554 | ||||||
|  | 5283751526 | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day12", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,16 @@ | ||||||
|  | digraph D { | ||||||
|  | start [shape=box] | ||||||
|  | end [shape=box] | ||||||
|  | A [shape=box] | ||||||
|  | b [shape=box] | ||||||
|  | c [shape=box] | ||||||
|  | d [shape=box] | ||||||
|  | start -> {A, b} | ||||||
|  | end -> {A, b} | ||||||
|  | A -> {c, b, end} | ||||||
|  | A -> {start} | ||||||
|  | b -> {d, end} | ||||||
|  | b -> {start, A} | ||||||
|  | c -> {A} | ||||||
|  | d -> {b} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,20 @@ | ||||||
|  | digraph D { | ||||||
|  | start [shape=box] | ||||||
|  | end [shape=box] | ||||||
|  | dc [shape=box] | ||||||
|  | HN [shape=box] | ||||||
|  | kj [shape=box] | ||||||
|  | LN [shape=box] | ||||||
|  | sa [shape=box] | ||||||
|  | start -> {kj} | ||||||
|  | start -> {HN, dc} | ||||||
|  | end -> {dc, HN} | ||||||
|  | dc -> {end, start, HN} | ||||||
|  | dc -> {LN, kj} | ||||||
|  | HN -> {start, end} | ||||||
|  | HN -> {dc, kj} | ||||||
|  | kj -> {sa, HN, dc} | ||||||
|  | kj -> {start} | ||||||
|  | LN -> {dc} | ||||||
|  | sa -> {kj} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,28 @@ | ||||||
|  | digraph D { | ||||||
|  | start [shape=box] | ||||||
|  | end [shape=box] | ||||||
|  | fs [shape=box] | ||||||
|  | he [shape=box] | ||||||
|  | DX [shape=box] | ||||||
|  | pj [shape=box] | ||||||
|  | zg [shape=box] | ||||||
|  | sl [shape=box] | ||||||
|  | RW [shape=box] | ||||||
|  | WI [shape=box] | ||||||
|  | start -> {DX, pj, RW} | ||||||
|  | end -> {zg} | ||||||
|  | end -> {fs} | ||||||
|  | fs -> {end, he, DX} | ||||||
|  | fs -> {pj} | ||||||
|  | he -> {DX, WI} | ||||||
|  | he -> {fs, pj, RW, zg} | ||||||
|  | DX -> {he, start, pj, fs} | ||||||
|  | pj -> {DX, he, RW, fs} | ||||||
|  | pj -> {zg, start} | ||||||
|  | zg -> {sl, pj, RW, he} | ||||||
|  | zg -> {end} | ||||||
|  | sl -> {zg} | ||||||
|  | RW -> {he} | ||||||
|  | RW -> {pj, zg, start} | ||||||
|  | WI -> {he} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,39 @@ | ||||||
|  | digraph D { | ||||||
|  | start [shape=box] | ||||||
|  | end [shape=box] | ||||||
|  | mj [shape=box] | ||||||
|  | TZ [shape=box] | ||||||
|  | LY [shape=box] | ||||||
|  | TX [shape=box] | ||||||
|  | ez [shape=box] | ||||||
|  | uw [shape=box] | ||||||
|  | TH [shape=box] | ||||||
|  | vn [shape=box] | ||||||
|  | sb [shape=box] | ||||||
|  | RR [shape=box] | ||||||
|  | mt [shape=box] | ||||||
|  | start -> {LY, TZ} | ||||||
|  | start -> {mj} | ||||||
|  | end -> {LY} | ||||||
|  | end -> {TH, sb} | ||||||
|  | mj -> {TZ, start, TH} | ||||||
|  | mj -> {LY} | ||||||
|  | TZ -> {sb} | ||||||
|  | TZ -> {mj, ez, start, uw} | ||||||
|  | LY -> {mj, ez} | ||||||
|  | LY -> {start, uw, end} | ||||||
|  | TX -> {ez, mt} | ||||||
|  | TX -> {sb} | ||||||
|  | ez -> {TZ, TH} | ||||||
|  | ez -> {TX, uw, LY} | ||||||
|  | uw -> {ez, LY, RR, vn, TZ} | ||||||
|  | uw -> {sb} | ||||||
|  | TH -> {vn, end} | ||||||
|  | TH -> {mj, ez} | ||||||
|  | vn -> {sb} | ||||||
|  | vn -> {TH, uw} | ||||||
|  | sb -> {uw, TX, end} | ||||||
|  | sb -> {TZ, vn} | ||||||
|  | RR -> {uw} | ||||||
|  | mt -> {TX} | ||||||
|  | } | ||||||
|  | @ -0,0 +1,24 @@ | ||||||
|  | mj-TZ | ||||||
|  | start-LY | ||||||
|  | TX-ez | ||||||
|  | uw-ez | ||||||
|  | ez-TZ | ||||||
|  | TH-vn | ||||||
|  | sb-uw | ||||||
|  | uw-LY | ||||||
|  | LY-mj | ||||||
|  | sb-TX | ||||||
|  | TH-end | ||||||
|  | end-LY | ||||||
|  | mj-start | ||||||
|  | TZ-sb | ||||||
|  | uw-RR | ||||||
|  | start-TZ | ||||||
|  | mj-TH | ||||||
|  | ez-TH | ||||||
|  | sb-end | ||||||
|  | LY-ez | ||||||
|  | TX-mt | ||||||
|  | vn-sb | ||||||
|  | uw-vn | ||||||
|  | uw-TZ | ||||||
|  | @ -0,0 +1,362 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | const example = | ||||||
|  |     \\start-A | ||||||
|  |     \\start-b | ||||||
|  |     \\A-c | ||||||
|  |     \\A-b | ||||||
|  |     \\b-d | ||||||
|  |     \\A-end | ||||||
|  |     \\b-end | ||||||
|  | ; | ||||||
|  | 
 | ||||||
|  | const example2 = | ||||||
|  |     \\dc-end | ||||||
|  |     \\HN-start | ||||||
|  |     \\start-kj | ||||||
|  |     \\dc-start | ||||||
|  |     \\dc-HN | ||||||
|  |     \\LN-dc | ||||||
|  |     \\HN-end | ||||||
|  |     \\kj-sa | ||||||
|  |     \\kj-HN | ||||||
|  |     \\kj-dc | ||||||
|  | ; | ||||||
|  | 
 | ||||||
|  | const example3 = | ||||||
|  |     \\fs-end | ||||||
|  |     \\he-DX | ||||||
|  |     \\fs-he | ||||||
|  |     \\start-DX | ||||||
|  |     \\pj-DX | ||||||
|  |     \\end-zg | ||||||
|  |     \\zg-sl | ||||||
|  |     \\zg-pj | ||||||
|  |     \\pj-he | ||||||
|  |     \\RW-he | ||||||
|  |     \\fs-DX | ||||||
|  |     \\pj-RW | ||||||
|  |     \\zg-RW | ||||||
|  |     \\start-pj | ||||||
|  |     \\he-WI | ||||||
|  |     \\zg-he | ||||||
|  |     \\pj-fs | ||||||
|  |     \\start-RW | ||||||
|  | ; | ||||||
|  | 
 | ||||||
|  | const input = @embedFile("../input"); | ||||||
|  | 
 | ||||||
|  | const Graph = struct { | ||||||
|  |     alloc: std.mem.Allocator, | ||||||
|  |     nodes: std.ArrayListUnmanaged(*Node) = .{}, | ||||||
|  |     const Self = @This(); | ||||||
|  | 
 | ||||||
|  |     pub fn deinit(self: *Self) void { | ||||||
|  |         for (self.nodes.items) |n| { | ||||||
|  |             n.parents.deinit(self.alloc); | ||||||
|  |             n.children.deinit(self.alloc); | ||||||
|  |             self.alloc.destroy(n); | ||||||
|  |         } | ||||||
|  |         self.nodes.deinit(self.alloc); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn build_graph(alloc: std.mem.Allocator, text: []const u8) !Self { | ||||||
|  |         var self: Self = .{ | ||||||
|  |             .alloc = alloc, | ||||||
|  |         }; | ||||||
|  |         var start =  try alloc.create(Node); | ||||||
|  |         errdefer alloc.destroy(start); | ||||||
|  |         start.id = "start"; | ||||||
|  |         start.children = .{}; | ||||||
|  |         start.parents = .{}; | ||||||
|  |         var end = try alloc.create(Node); | ||||||
|  |         errdefer alloc.destroy(end); | ||||||
|  |         end.id = "end"; | ||||||
|  |         end.children = .{}; | ||||||
|  |         end.parents = .{}; | ||||||
|  | 
 | ||||||
|  |         // Our input text does not necessarrily contain the Nodes in an order | ||||||
|  |         // that will just assemble into a graph. We temporarily hold an array | ||||||
|  |         // of pointers to all the nodes we're making to facilitate building | ||||||
|  |         // the graph. | ||||||
|  |         try self.nodes.append(self.alloc, start); | ||||||
|  |         try self.nodes.append(self.alloc, end); | ||||||
|  | 
 | ||||||
|  |         var lit = std.mem.tokenize(u8, text, "\n"); | ||||||
|  |         while (lit.next()) |line| { | ||||||
|  |             var it = std.mem.tokenize(u8, line, "-"); | ||||||
|  |             var first: ?*Node = null; | ||||||
|  |             var second: ?*Node = null; | ||||||
|  |             while (it.next()) |id| { | ||||||
|  |                 var n  = self.node_exists(id); | ||||||
|  |                 if (n == null) { | ||||||
|  |                     n = try alloc.create(Node); | ||||||
|  |                     errdefer alloc.destroy(n.?); | ||||||
|  |                     n.?.id = id; | ||||||
|  |                     var big = true; | ||||||
|  |                     for (id) |c| { | ||||||
|  |                         if (!std.ascii.isUpper(c)) { | ||||||
|  |                             big = false; | ||||||
|  |                             break; | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                     n.?.big = big; | ||||||
|  |                     n.?.children = .{}; | ||||||
|  |                     n.?.parents = .{}; | ||||||
|  |                     try self.nodes.append(self.alloc, n.?); | ||||||
|  |                 } | ||||||
|  |                 if (first == null) { | ||||||
|  |                     first = n; | ||||||
|  |                 } | ||||||
|  |                 else if (second == null) { | ||||||
|  |                     second = n; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             // @LEAK When there is an error, the unmanaged array lists aren't properly freed | ||||||
|  |             //std.log.debug("Adding '{s}' as parent of '{s}'", .{first.?.id, second.?.id}); | ||||||
|  |             try second.?.parents.append(self.alloc, first.?); | ||||||
|  |             try first.?.children.append(self.alloc, second.?); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         //std.debug.assert(start.parent == null); | ||||||
|  |         //std.debug.assert(end.parent != null); | ||||||
|  |         return self; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn paths_part1(self: *Self) !Paths { | ||||||
|  |         var paths = try self.node_exists("start").?.walk(alloc, null); | ||||||
|  |         return paths; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn node_exists(self: *Self, id: []const u8) ?*Node { | ||||||
|  |         for (self.nodes.items) |n| { | ||||||
|  |             if (std.mem.eql(u8, id, n.id)) { | ||||||
|  |                 return n; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn to_dot(self: *Self, f: std.fs.File) !void { | ||||||
|  |         defer f.close(); | ||||||
|  |         _ = try f.writeAll("digraph D {\n"); | ||||||
|  |         for (self.nodes.items) |n| { | ||||||
|  |             _ = try f.write(n.id); | ||||||
|  |             _ = try f.write(" [shape=box]\n"); | ||||||
|  |         } | ||||||
|  |         for(self.nodes.items) |n| { | ||||||
|  |             if (n.children.items.len > 0) { | ||||||
|  |                 _ = try f.write(n.id); | ||||||
|  |                 _ = try f.write(" -> {"); | ||||||
|  |                 for (n.children.items) |c, k| { | ||||||
|  |                     if (std.mem.eql(u8, c.id, n.id)) { | ||||||
|  |                         continue; | ||||||
|  |                     } | ||||||
|  |                     _ = try f.write(c.id); | ||||||
|  |                     if (k != n.children.items.len - 1) { | ||||||
|  |                         _ = try f.write(", "); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 _ = try f.write("}\n"); | ||||||
|  |             } | ||||||
|  |             if (n.parents.items.len > 0) { | ||||||
|  |                 _ = try f.write(n.id); | ||||||
|  |                 _ = try f.write(" -> {"); | ||||||
|  |                 for (n.parents.items) |c, k| { | ||||||
|  |                     if (std.mem.eql(u8, c.id, n.id)) { | ||||||
|  |                         continue; | ||||||
|  |                     } | ||||||
|  |                     _ = try f.write(c.id); | ||||||
|  |                     if (k != n.parents.items.len - 1) { | ||||||
|  |                         _ = try f.write(", "); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 _ = try f.write("}\n"); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         try f.writeAll("}\n"); | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | const Path = std.ArrayListUnmanaged(*Node); | ||||||
|  | const Paths = struct { | ||||||
|  |     alloc: std.mem.Allocator, | ||||||
|  |     paths: std.ArrayListUnmanaged(*Path), | ||||||
|  |     const Self = @This(), | ||||||
|  | 
 | ||||||
|  |     pub fn init(alloc: std.mem.Allocator) !Self { | ||||||
|  |         return Self { | ||||||
|  |             .alloc = alloc, | ||||||
|  |             .paths = .{} | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | const Node = struct { | ||||||
|  |     id: [] const u8, | ||||||
|  |     big: bool = false, | ||||||
|  |     parents: std.ArrayListUnmanaged(*Node) = .{}, | ||||||
|  |     children: std.ArrayListUnmanaged(*Node) = .{}, | ||||||
|  |     const Self = @This(); | ||||||
|  | 
 | ||||||
|  |     fn valid_child_part1(self: *Self, path: std.ArrayList(*Node)) bool { | ||||||
|  |         if (self.big) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |         for (path.items) |p| { | ||||||
|  |             if (std.mem.eql(u8, p.id, self.id)) { | ||||||
|  |                 return false; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn walk(self: *Self, alloc: std.mem.Allocator, visited: ?Path) !Paths { | ||||||
|  |         var paths = Path.init(alloc); | ||||||
|  |         // Depth first | ||||||
|  |         if (visited == null) { | ||||||
|  |             var new_path = try alloc.create(Path); | ||||||
|  |             errdefer alloc.destroy(new_path); | ||||||
|  |             try new_path.append(self); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             try visited.append(alloc, self); | ||||||
|  |         } | ||||||
|  |         return paths; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // pub fn paths(self: *Self, alloc: std.mem.Allocator) !std.ArrayList(*Path) { | ||||||
|  |     //     var ps = std.ArrayList(*Path).init(alloc); | ||||||
|  |     //     if (ps.items.len == 0) { | ||||||
|  |     //         var path = try alloc.create(Path); | ||||||
|  |     //         errdefer alloc.destroy(path); | ||||||
|  |     //         path.* = Path.init(alloc); | ||||||
|  |     //         try ps.append(path); | ||||||
|  |     //     } | ||||||
|  |     //     for (ps.items) |path| { | ||||||
|  |     //         try path.append(self); | ||||||
|  |     //     } | ||||||
|  |     //     if (std.mem.eql(u8, self.id, "end")) { | ||||||
|  |     //         return ps; | ||||||
|  |     //     } | ||||||
|  |     //     return ps; | ||||||
|  |     // } | ||||||
|  | 
 | ||||||
|  |     // pub fn paths_from(self: *Self, alloc: std.mem.Allocator, from: Path) !std.ArrayList(Path) { | ||||||
|  |     //     var ps = std.ArrayList(*Path).init(alloc); | ||||||
|  |     //     for (self.children.items) |c| { | ||||||
|  |     //         if (!c.valid_child_part1(from)) { | ||||||
|  |     //             continue; | ||||||
|  |     //         } | ||||||
|  |     //         var path = alloc.create(std.ArrayList(Path)); | ||||||
|  |     //         path.* = std.ArrayList(Path).init(alloc); | ||||||
|  |     //         for (from.items) |f| { | ||||||
|  |     //             try path.append(f); | ||||||
|  |     //         } | ||||||
|  |     //         try path.append(c); | ||||||
|  |     //         try ps.append(path); | ||||||
|  |     //     } | ||||||
|  |     //     return ps; | ||||||
|  |     // } | ||||||
|  | 
 | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | test "example 1" { | ||||||
|  |     var graph = try Graph.build_graph(std.testing.allocator, example); | ||||||
|  |     defer graph.deinit(); | ||||||
|  |     var end = graph.node_exists("end"); | ||||||
|  |     var end_paths = try end.?.paths(std.testing.allocator); | ||||||
|  |     for (end_paths.items) |p, k| { | ||||||
|  |         std.log.warn("Path number {}", .{k}); | ||||||
|  |         for (p.items) |n| { | ||||||
|  |             std.log.warn("\t{s}", .{n.id}); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Cleanup | ||||||
|  |     for (end_paths.items) |p| { | ||||||
|  |         p.deinit(); | ||||||
|  |         std.testing.allocator.destroy(p); | ||||||
|  |     } | ||||||
|  |     end_paths.deinit(); | ||||||
|  | 
 | ||||||
|  |     var paths = try graph.paths_part1(); | ||||||
|  |     for (paths.items) |p, k| { | ||||||
|  |         std.log.warn("Path number {}", .{k}); | ||||||
|  |         for (p.items) |n| { | ||||||
|  |             std.log.warn("\t{s}", .{n.id}); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     for (paths.items) |p| { | ||||||
|  |         p.deinit(); | ||||||
|  |         std.testing.allocator.destroy(p); | ||||||
|  |     } | ||||||
|  |     paths.deinit(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "example 1 graph" { | ||||||
|  |     var graph = try Graph.build_graph(std.testing.allocator, example); | ||||||
|  |     defer graph.deinit(); | ||||||
|  | 
 | ||||||
|  |     var tmp = std.testing.tmpDir(.{}); | ||||||
|  |     defer tmp.cleanup(); | ||||||
|  | 
 | ||||||
|  |     var file = try tmp.dir.createFile("example1.dot", .{}); | ||||||
|  |     // this closes the file descriptor. | ||||||
|  |     try graph.to_dot(file); | ||||||
|  |     var expected = @embedFile("../examples/1.dot"); | ||||||
|  |     var f = try tmp.dir.openFile("example1.dot", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     const actual = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u16)); | ||||||
|  |     defer std.testing.allocator.free(actual); | ||||||
|  |     try std.testing.expect(std.mem.eql(u8, expected, actual)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "example 2 graph" { | ||||||
|  |     var graph = try Graph.build_graph(std.testing.allocator, example2); | ||||||
|  |     defer graph.deinit(); | ||||||
|  | 
 | ||||||
|  |     var tmp = std.testing.tmpDir(.{}); | ||||||
|  |     defer tmp.cleanup(); | ||||||
|  | 
 | ||||||
|  |     var file = try tmp.dir.createFile("example2.dot", .{}); | ||||||
|  |     // this closes the file descriptor. | ||||||
|  |     try graph.to_dot(file); | ||||||
|  |     var expected = @embedFile("../examples/2.dot"); | ||||||
|  |     var f = try tmp.dir.openFile("example2.dot", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     const actual = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u16)); | ||||||
|  |     defer std.testing.allocator.free(actual); | ||||||
|  |     try std.testing.expect(std.mem.eql(u8, expected, actual)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "example 3 graph" { | ||||||
|  |     var graph = try Graph.build_graph(std.testing.allocator, example3); | ||||||
|  |     defer graph.deinit(); | ||||||
|  | 
 | ||||||
|  |     var tmp = std.testing.tmpDir(.{}); | ||||||
|  |     defer tmp.cleanup(); | ||||||
|  | 
 | ||||||
|  |     var file = try tmp.dir.createFile("example3.dot", .{}); | ||||||
|  |     // this closes the file descriptor. | ||||||
|  |     try graph.to_dot(file); | ||||||
|  |     var expected = @embedFile("../examples/3.dot"); | ||||||
|  |     var f = try tmp.dir.openFile("example3.dot", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     const actual = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u16)); | ||||||
|  |     defer std.testing.allocator.free(actual); | ||||||
|  |     try std.testing.expect(std.mem.eql(u8, expected, actual)); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = arena.allocator(); | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var graph = try Graph.build_graph(alloc, input); | ||||||
|  |     defer graph.deinit(); | ||||||
|  | 
 | ||||||
|  |     var f = try std.fs.cwd().createFile("graph.dot", .{}); | ||||||
|  |     try graph.to_dot(f); | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day13", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,817 @@ | ||||||
|  | 323,305 | ||||||
|  | 512,845 | ||||||
|  | 780,175 | ||||||
|  | 308,50 | ||||||
|  | 266,604 | ||||||
|  | 152,425 | ||||||
|  | 199,618 | ||||||
|  | 214,572 | ||||||
|  | 977,596 | ||||||
|  | 1113,756 | ||||||
|  | 1019,316 | ||||||
|  | 972,322 | ||||||
|  | 1181,138 | ||||||
|  | 785,756 | ||||||
|  | 504,378 | ||||||
|  | 597,537 | ||||||
|  | 1068,485 | ||||||
|  | 738,108 | ||||||
|  | 437,359 | ||||||
|  | 1163,567 | ||||||
|  | 585,549 | ||||||
|  | 490,527 | ||||||
|  | 616,5 | ||||||
|  | 258,605 | ||||||
|  | 1265,473 | ||||||
|  | 728,421 | ||||||
|  | 659,626 | ||||||
|  | 162,781 | ||||||
|  | 725,549 | ||||||
|  | 165,23 | ||||||
|  | 1148,554 | ||||||
|  | 169,710 | ||||||
|  | 388,290 | ||||||
|  | 989,746 | ||||||
|  | 619,361 | ||||||
|  | 951,850 | ||||||
|  | 1136,770 | ||||||
|  | 592,284 | ||||||
|  | 298,430 | ||||||
|  | 441,408 | ||||||
|  | 1014,534 | ||||||
|  | 147,77 | ||||||
|  | 822,429 | ||||||
|  | 1210,162 | ||||||
|  | 109,647 | ||||||
|  | 65,605 | ||||||
|  | 319,595 | ||||||
|  | 535,315 | ||||||
|  | 691,420 | ||||||
|  | 266,290 | ||||||
|  | 127,824 | ||||||
|  | 435,779 | ||||||
|  | 306,161 | ||||||
|  | 248,653 | ||||||
|  | 23,478 | ||||||
|  | 751,276 | ||||||
|  | 1295,752 | ||||||
|  | 60,855 | ||||||
|  | 1041,757 | ||||||
|  | 639,773 | ||||||
|  | 564,757 | ||||||
|  | 1295,142 | ||||||
|  | 1158,21 | ||||||
|  | 311,868 | ||||||
|  | 1265,421 | ||||||
|  | 1285,884 | ||||||
|  | 89,26 | ||||||
|  | 45,677 | ||||||
|  | 358,222 | ||||||
|  | 261,880 | ||||||
|  | 216,173 | ||||||
|  | 440,213 | ||||||
|  | 738,666 | ||||||
|  | 1183,294 | ||||||
|  | 527,829 | ||||||
|  | 447,483 | ||||||
|  | 112,225 | ||||||
|  | 970,364 | ||||||
|  | 269,137 | ||||||
|  | 1163,631 | ||||||
|  | 1004,161 | ||||||
|  | 952,584 | ||||||
|  | 616,889 | ||||||
|  | 589,737 | ||||||
|  | 957,275 | ||||||
|  | 142,581 | ||||||
|  | 238,131 | ||||||
|  | 378,30 | ||||||
|  | 152,770 | ||||||
|  | 127,600 | ||||||
|  | 311,569 | ||||||
|  | 994,852 | ||||||
|  | 180,254 | ||||||
|  | 850,590 | ||||||
|  | 1091,173 | ||||||
|  | 927,115 | ||||||
|  | 246,536 | ||||||
|  | 574,341 | ||||||
|  | 325,891 | ||||||
|  | 460,590 | ||||||
|  | 1004,682 | ||||||
|  | 248,857 | ||||||
|  | 629,660 | ||||||
|  | 912,107 | ||||||
|  | 850,30 | ||||||
|  | 480,507 | ||||||
|  | 454,691 | ||||||
|  | 818,106 | ||||||
|  | 802,142 | ||||||
|  | 498,490 | ||||||
|  | 112,290 | ||||||
|  | 1019,644 | ||||||
|  | 589,856 | ||||||
|  | 256,339 | ||||||
|  | 314,364 | ||||||
|  | 186,191 | ||||||
|  | 920,564 | ||||||
|  | 119,747 | ||||||
|  | 914,737 | ||||||
|  | 566,131 | ||||||
|  | 1203,372 | ||||||
|  | 0,578 | ||||||
|  | 807,121 | ||||||
|  | 882,484 | ||||||
|  | 805,477 | ||||||
|  | 633,446 | ||||||
|  | 438,309 | ||||||
|  | 142,458 | ||||||
|  | 485,354 | ||||||
|  | 147,406 | ||||||
|  | 1225,757 | ||||||
|  | 873,359 | ||||||
|  | 1039,235 | ||||||
|  | 544,217 | ||||||
|  | 825,51 | ||||||
|  | 750,801 | ||||||
|  | 818,815 | ||||||
|  | 187,30 | ||||||
|  | 360,325 | ||||||
|  | 199,177 | ||||||
|  | 1305,242 | ||||||
|  | 569,757 | ||||||
|  | 510,40 | ||||||
|  | 853,756 | ||||||
|  | 964,182 | ||||||
|  | 1083,332 | ||||||
|  | 1136,322 | ||||||
|  | 1248,663 | ||||||
|  | 187,526 | ||||||
|  | 894,259 | ||||||
|  | 470,173 | ||||||
|  | 990,35 | ||||||
|  | 457,427 | ||||||
|  | 433,620 | ||||||
|  | 1248,834 | ||||||
|  | 169,582 | ||||||
|  | 1158,737 | ||||||
|  | 157,632 | ||||||
|  | 459,626 | ||||||
|  | 1289,112 | ||||||
|  | 850,752 | ||||||
|  | 62,834 | ||||||
|  | 1200,777 | ||||||
|  | 711,435 | ||||||
|  | 1287,507 | ||||||
|  | 82,532 | ||||||
|  | 646,816 | ||||||
|  | 80,756 | ||||||
|  | 323,813 | ||||||
|  | 691,690 | ||||||
|  | 1016,569 | ||||||
|  | 892,585 | ||||||
|  | 557,350 | ||||||
|  | 972,200 | ||||||
|  | 179,38 | ||||||
|  | 1083,226 | ||||||
|  | 853,467 | ||||||
|  | 1153,428 | ||||||
|  | 1081,350 | ||||||
|  | 1275,578 | ||||||
|  | 856,203 | ||||||
|  | 338,200 | ||||||
|  | 157,578 | ||||||
|  | 1300,246 | ||||||
|  | 363,809 | ||||||
|  | 1245,856 | ||||||
|  | 857,691 | ||||||
|  | 236,585 | ||||||
|  | 950,325 | ||||||
|  | 480,322 | ||||||
|  | 525,875 | ||||||
|  | 1046,508 | ||||||
|  | 1125,110 | ||||||
|  | 348,234 | ||||||
|  | 985,443 | ||||||
|  | 346,712 | ||||||
|  | 818,113 | ||||||
|  | 560,93 | ||||||
|  | 1044,537 | ||||||
|  | 1041,309 | ||||||
|  | 53,269 | ||||||
|  | 510,187 | ||||||
|  | 110,777 | ||||||
|  | 898,801 | ||||||
|  | 1136,613 | ||||||
|  | 694,453 | ||||||
|  | 1044,290 | ||||||
|  | 15,133 | ||||||
|  | 1289,894 | ||||||
|  | 1245,84 | ||||||
|  | 562,374 | ||||||
|  | 619,499 | ||||||
|  | 482,144 | ||||||
|  | 877,722 | ||||||
|  | 1253,144 | ||||||
|  | 868,256 | ||||||
|  | 855,152 | ||||||
|  | 1300,648 | ||||||
|  | 691,586 | ||||||
|  | 676,234 | ||||||
|  | 1275,196 | ||||||
|  | 652,247 | ||||||
|  | 144,259 | ||||||
|  | 668,516 | ||||||
|  | 460,870 | ||||||
|  | 843,365 | ||||||
|  | 281,809 | ||||||
|  | 351,556 | ||||||
|  | 494,656 | ||||||
|  | 1290,749 | ||||||
|  | 554,746 | ||||||
|  | 527,294 | ||||||
|  | 353,499 | ||||||
|  | 996,812 | ||||||
|  | 880,353 | ||||||
|  | 1153,578 | ||||||
|  | 119,203 | ||||||
|  | 15,142 | ||||||
|  | 393,761 | ||||||
|  | 816,208 | ||||||
|  | 0,92 | ||||||
|  | 557,296 | ||||||
|  | 460,534 | ||||||
|  | 117,5 | ||||||
|  | 766,117 | ||||||
|  | 231,276 | ||||||
|  | 507,775 | ||||||
|  | 1245,810 | ||||||
|  | 1111,600 | ||||||
|  | 363,44 | ||||||
|  | 110,341 | ||||||
|  | 1305,298 | ||||||
|  | 338,710 | ||||||
|  | 227,668 | ||||||
|  | 343,868 | ||||||
|  | 850,669 | ||||||
|  | 72,197 | ||||||
|  | 870,233 | ||||||
|  | 497,276 | ||||||
|  | 800,707 | ||||||
|  | 336,750 | ||||||
|  | 72,25 | ||||||
|  | 738,786 | ||||||
|  | 1265,453 | ||||||
|  | 1181,427 | ||||||
|  | 480,572 | ||||||
|  | 311,89 | ||||||
|  | 157,170 | ||||||
|  | 922,255 | ||||||
|  | 179,182 | ||||||
|  | 1002,498 | ||||||
|  | 634,301 | ||||||
|  | 825,312 | ||||||
|  | 843,529 | ||||||
|  | 398,787 | ||||||
|  | 209,560 | ||||||
|  | 438,361 | ||||||
|  | 490,336 | ||||||
|  | 1203,277 | ||||||
|  | 135,872 | ||||||
|  | 321,302 | ||||||
|  | 57,144 | ||||||
|  | 987,589 | ||||||
|  | 1029,670 | ||||||
|  | 1200,677 | ||||||
|  | 1285,10 | ||||||
|  | 594,639 | ||||||
|  | 1193,633 | ||||||
|  | 320,859 | ||||||
|  | 8,816 | ||||||
|  | 20,145 | ||||||
|  | 649,596 | ||||||
|  | 756,430 | ||||||
|  | 490,367 | ||||||
|  | 803,119 | ||||||
|  | 773,185 | ||||||
|  | 851,626 | ||||||
|  | 266,703 | ||||||
|  | 492,781 | ||||||
|  | 343,569 | ||||||
|  | 768,793 | ||||||
|  | 634,833 | ||||||
|  | 109,269 | ||||||
|  | 872,585 | ||||||
|  | 440,777 | ||||||
|  | 246,470 | ||||||
|  | 1153,262 | ||||||
|  | 15,316 | ||||||
|  | 1128,488 | ||||||
|  | 594,761 | ||||||
|  | 296,528 | ||||||
|  | 1238,25 | ||||||
|  | 748,374 | ||||||
|  | 989,354 | ||||||
|  | 1201,359 | ||||||
|  | 412,409 | ||||||
|  | 1292,171 | ||||||
|  | 920,711 | ||||||
|  | 721,352 | ||||||
|  | 977,200 | ||||||
|  | 457,756 | ||||||
|  | 750,93 | ||||||
|  | 682,96 | ||||||
|  | 725,345 | ||||||
|  | 1116,142 | ||||||
|  | 964,712 | ||||||
|  | 30,248 | ||||||
|  | 1002,264 | ||||||
|  | 1084,679 | ||||||
|  | 1081,805 | ||||||
|  | 355,246 | ||||||
|  | 1250,639 | ||||||
|  | 1014,254 | ||||||
|  | 283,371 | ||||||
|  | 127,177 | ||||||
|  | 989,298 | ||||||
|  | 453,203 | ||||||
|  | 65,856 | ||||||
|  | 169,354 | ||||||
|  | 502,59 | ||||||
|  | 373,229 | ||||||
|  | 187,309 | ||||||
|  | 1126,143 | ||||||
|  | 1250,581 | ||||||
|  | 291,250 | ||||||
|  | 569,529 | ||||||
|  | 636,764 | ||||||
|  | 383,779 | ||||||
|  | 1210,264 | ||||||
|  | 117,633 | ||||||
|  | 565,110 | ||||||
|  | 460,478 | ||||||
|  | 518,409 | ||||||
|  | 825,876 | ||||||
|  | 333,694 | ||||||
|  | 631,416 | ||||||
|  | 599,459 | ||||||
|  | 152,280 | ||||||
|  | 1126,173 | ||||||
|  | 542,792 | ||||||
|  | 271,235 | ||||||
|  | 569,137 | ||||||
|  | 785,875 | ||||||
|  | 1153,316 | ||||||
|  | 333,724 | ||||||
|  | 512,591 | ||||||
|  | 957,499 | ||||||
|  | 999,325 | ||||||
|  | 512,581 | ||||||
|  | 435,721 | ||||||
|  | 231,724 | ||||||
|  | 1004,733 | ||||||
|  | 100,264 | ||||||
|  | 488,465 | ||||||
|  | 846,572 | ||||||
|  | 674,764 | ||||||
|  | 836,864 | ||||||
|  | 800,75 | ||||||
|  | 980,768 | ||||||
|  | 65,220 | ||||||
|  | 850,416 | ||||||
|  | 343,343 | ||||||
|  | 835,856 | ||||||
|  | 470,537 | ||||||
|  | 460,30 | ||||||
|  | 321,536 | ||||||
|  | 182,187 | ||||||
|  | 1191,131 | ||||||
|  | 463,424 | ||||||
|  | 474,254 | ||||||
|  | 321,540 | ||||||
|  | 31,850 | ||||||
|  | 85,757 | ||||||
|  | 803,631 | ||||||
|  | 152,852 | ||||||
|  | 505,477 | ||||||
|  | 910,249 | ||||||
|  | 333,802 | ||||||
|  | 766,217 | ||||||
|  | 464,322 | ||||||
|  | 147,567 | ||||||
|  | 763,372 | ||||||
|  | 1128,406 | ||||||
|  | 502,322 | ||||||
|  | 537,409 | ||||||
|  | 904,205 | ||||||
|  | 321,648 | ||||||
|  | 525,756 | ||||||
|  | 544,777 | ||||||
|  | 679,478 | ||||||
|  | 346,182 | ||||||
|  | 490,760 | ||||||
|  | 825,746 | ||||||
|  | 952,310 | ||||||
|  | 62,679 | ||||||
|  | 18,746 | ||||||
|  | 348,857 | ||||||
|  | 999,327 | ||||||
|  | 117,392 | ||||||
|  | 1292,723 | ||||||
|  | 880,541 | ||||||
|  | 281,869 | ||||||
|  | 773,236 | ||||||
|  | 248,205 | ||||||
|  | 753,350 | ||||||
|  | 119,147 | ||||||
|  | 808,59 | ||||||
|  | 830,572 | ||||||
|  | 264,576 | ||||||
|  | 1052,289 | ||||||
|  | 1067,322 | ||||||
|  | 1191,147 | ||||||
|  | 346,731 | ||||||
|  | 467,365 | ||||||
|  | 1198,604 | ||||||
|  | 552,353 | ||||||
|  | 266,357 | ||||||
|  | 480,59 | ||||||
|  | 30,672 | ||||||
|  | 1128,187 | ||||||
|  | 880,31 | ||||||
|  | 996,530 | ||||||
|  | 1113,138 | ||||||
|  | 502,729 | ||||||
|  | 147,460 | ||||||
|  | 1193,502 | ||||||
|  | 333,799 | ||||||
|  | 745,334 | ||||||
|  | 408,536 | ||||||
|  | 328,313 | ||||||
|  | 494,208 | ||||||
|  | 271,211 | ||||||
|  | 982,114 | ||||||
|  | 238,579 | ||||||
|  | 716,761 | ||||||
|  | 1179,850 | ||||||
|  | 219,280 | ||||||
|  | 830,281 | ||||||
|  | 592,215 | ||||||
|  | 328,786 | ||||||
|  | 863,483 | ||||||
|  | 485,648 | ||||||
|  | 440,98 | ||||||
|  | 669,29 | ||||||
|  | 109,625 | ||||||
|  | 1124,191 | ||||||
|  | 589,84 | ||||||
|  | 164,367 | ||||||
|  | 1039,504 | ||||||
|  | 69,560 | ||||||
|  | 741,529 | ||||||
|  | 470,50 | ||||||
|  | 199,742 | ||||||
|  | 437,535 | ||||||
|  | 989,799 | ||||||
|  | 686,248 | ||||||
|  | 991,299 | ||||||
|  | 112,604 | ||||||
|  | 475,856 | ||||||
|  | 184,50 | ||||||
|  | 226,231 | ||||||
|  | 1002,182 | ||||||
|  | 129,467 | ||||||
|  | 457,467 | ||||||
|  | 855,45 | ||||||
|  | 1245,289 | ||||||
|  | 5,652 | ||||||
|  | 338,470 | ||||||
|  | 267,555 | ||||||
|  | 15,752 | ||||||
|  | 72,869 | ||||||
|  | 311,855 | ||||||
|  | 475,712 | ||||||
|  | 1175,872 | ||||||
|  | 504,516 | ||||||
|  | 247,12 | ||||||
|  | 3,813 | ||||||
|  | 582,645 | ||||||
|  | 1163,775 | ||||||
|  | 974,144 | ||||||
|  | 217,712 | ||||||
|  | 152,21 | ||||||
|  | 633,275 | ||||||
|  | 21,446 | ||||||
|  | 162,554 | ||||||
|  | 343,39 | ||||||
|  | 557,213 | ||||||
|  | 1136,611 | ||||||
|  | 1068,37 | ||||||
|  | 897,473 | ||||||
|  | 1007,142 | ||||||
|  | 592,610 | ||||||
|  | 979,813 | ||||||
|  | 1230,536 | ||||||
|  | 609,339 | ||||||
|  | 902,536 | ||||||
|  | 736,553 | ||||||
|  | 194,640 | ||||||
|  | 1310,578 | ||||||
|  | 378,478 | ||||||
|  | 1275,462 | ||||||
|  | 467,368 | ||||||
|  | 947,872 | ||||||
|  | 830,59 | ||||||
|  | 659,529 | ||||||
|  | 783,276 | ||||||
|  | 430,31 | ||||||
|  | 1131,182 | ||||||
|  | 741,305 | ||||||
|  | 1305,652 | ||||||
|  | 855,618 | ||||||
|  | 639,142 | ||||||
|  | 10,54 | ||||||
|  | 1044,156 | ||||||
|  | 174,611 | ||||||
|  | 493,471 | ||||||
|  | 492,106 | ||||||
|  | 870,322 | ||||||
|  | 316,852 | ||||||
|  | 701,555 | ||||||
|  | 920,186 | ||||||
|  | 112,792 | ||||||
|  | 1098,270 | ||||||
|  | 438,809 | ||||||
|  | 398,555 | ||||||
|  | 686,222 | ||||||
|  | 1164,304 | ||||||
|  | 1228,532 | ||||||
|  | 1074,585 | ||||||
|  | 127,294 | ||||||
|  | 1163,406 | ||||||
|  | 1131,38 | ||||||
|  | 1279,492 | ||||||
|  | 820,880 | ||||||
|  | 1191,763 | ||||||
|  | 482,592 | ||||||
|  | 1158,469 | ||||||
|  | 661,806 | ||||||
|  | 199,294 | ||||||
|  | 80,134 | ||||||
|  | 746,757 | ||||||
|  | 1198,290 | ||||||
|  | 967,262 | ||||||
|  | 60,581 | ||||||
|  | 1111,275 | ||||||
|  | 428,679 | ||||||
|  | 291,316 | ||||||
|  | 65,457 | ||||||
|  | 32,673 | ||||||
|  | 117,684 | ||||||
|  | 711,420 | ||||||
|  | 766,397 | ||||||
|  | 35,698 | ||||||
|  | 323,589 | ||||||
|  | 825,18 | ||||||
|  | 976,490 | ||||||
|  | 718,4 | ||||||
|  | 745,784 | ||||||
|  | 537,658 | ||||||
|  | 1044,39 | ||||||
|  | 1111,294 | ||||||
|  | 952,528 | ||||||
|  | 808,490 | ||||||
|  | 219,721 | ||||||
|  | 676,833 | ||||||
|  | 1123,526 | ||||||
|  | 431,787 | ||||||
|  | 187,611 | ||||||
|  | 460,304 | ||||||
|  | 97,598 | ||||||
|  | 209,334 | ||||||
|  | 227,332 | ||||||
|  | 348,733 | ||||||
|  | 172,368 | ||||||
|  | 385,361 | ||||||
|  | 35,204 | ||||||
|  | 920,330 | ||||||
|  | 619,756 | ||||||
|  | 641,29 | ||||||
|  | 1255,420 | ||||||
|  | 174,283 | ||||||
|  | 15,119 | ||||||
|  | 962,485 | ||||||
|  | 920,708 | ||||||
|  | 447,147 | ||||||
|  | 989,302 | ||||||
|  | 1101,782 | ||||||
|  | 340,530 | ||||||
|  | 165,871 | ||||||
|  | 1007,30 | ||||||
|  | 1225,589 | ||||||
|  | 1148,340 | ||||||
|  | 320,707 | ||||||
|  | 172,144 | ||||||
|  | 182,747 | ||||||
|  | 800,676 | ||||||
|  | 604,550 | ||||||
|  | 972,470 | ||||||
|  | 535,147 | ||||||
|  | 681,884 | ||||||
|  | 753,744 | ||||||
|  | 691,361 | ||||||
|  | 226,36 | ||||||
|  | 1201,269 | ||||||
|  | 1265,665 | ||||||
|  | 433,674 | ||||||
|  | 1153,632 | ||||||
|  | 110,117 | ||||||
|  | 1096,572 | ||||||
|  | 316,42 | ||||||
|  | 1248,610 | ||||||
|  | 738,413 | ||||||
|  | 1011,694 | ||||||
|  | 686,360 | ||||||
|  | 1288,754 | ||||||
|  | 604,553 | ||||||
|  | 22,754 | ||||||
|  | 477,641 | ||||||
|  | 321,298 | ||||||
|  | 885,350 | ||||||
|  | 1072,763 | ||||||
|  | 877,805 | ||||||
|  | 126,467 | ||||||
|  | 535,579 | ||||||
|  | 55,196 | ||||||
|  | 1163,460 | ||||||
|  | 898,485 | ||||||
|  | 199,724 | ||||||
|  | 977,802 | ||||||
|  | 169,802 | ||||||
|  | 413,421 | ||||||
|  | 1158,165 | ||||||
|  | 18,316 | ||||||
|  | 1123,368 | ||||||
|  | 820,134 | ||||||
|  | 718,610 | ||||||
|  | 624,222 | ||||||
|  | 1278,221 | ||||||
|  | 455,605 | ||||||
|  | 870,661 | ||||||
|  | 1111,177 | ||||||
|  | 1310,316 | ||||||
|  | 574,760 | ||||||
|  | 318,750 | ||||||
|  | 1265,677 | ||||||
|  | 152,56 | ||||||
|  | 990,859 | ||||||
|  | 507,142 | ||||||
|  | 718,215 | ||||||
|  | 1307,813 | ||||||
|  | 838,147 | ||||||
|  | 798,49 | ||||||
|  | 49,486 | ||||||
|  | 1138,368 | ||||||
|  | 445,670 | ||||||
|  | 103,339 | ||||||
|  | 674,130 | ||||||
|  | 999,474 | ||||||
|  | 527,618 | ||||||
|  | 825,582 | ||||||
|  | 197,756 | ||||||
|  | 1280,646 | ||||||
|  | 701,441 | ||||||
|  | 1196,718 | ||||||
|  | 1029,224 | ||||||
|  | 308,498 | ||||||
|  | 470,261 | ||||||
|  | 813,276 | ||||||
|  | 1131,108 | ||||||
|  | 651,529 | ||||||
|  | 569,305 | ||||||
|  | 1062,857 | ||||||
|  | 741,757 | ||||||
|  | 592,890 | ||||||
|  | 1295,295 | ||||||
|  | 972,710 | ||||||
|  | 187,864 | ||||||
|  | 709,247 | ||||||
|  | 907,842 | ||||||
|  | 741,137 | ||||||
|  | 291,764 | ||||||
|  | 557,96 | ||||||
|  | 927,698 | ||||||
|  | 1044,191 | ||||||
|  | 238,187 | ||||||
|  | 527,65 | ||||||
|  | 547,372 | ||||||
|  | 1158,770 | ||||||
|  | 340,628 | ||||||
|  | 21,894 | ||||||
|  | 721,674 | ||||||
|  | 927,196 | ||||||
|  | 1287,478 | ||||||
|  | 1041,361 | ||||||
|  | 537,185 | ||||||
|  | 1198,796 | ||||||
|  | 1198,156 | ||||||
|  | 1049,133 | ||||||
|  | 502,381 | ||||||
|  | 748,53 | ||||||
|  | 1131,712 | ||||||
|  | 977,694 | ||||||
|  | 907,108 | ||||||
|  | 964,264 | ||||||
|  | 169,312 | ||||||
|  | 485,312 | ||||||
|  | 843,368 | ||||||
|  | 455,45 | ||||||
|  | 425,798 | ||||||
|  | 475,12 | ||||||
|  | 15,540 | ||||||
|  | 393,374 | ||||||
|  | 1125,334 | ||||||
|  | 318,144 | ||||||
|  | 146,478 | ||||||
|  | 207,131 | ||||||
|  | 840,261 | ||||||
|  | 455,401 | ||||||
|  | 1029,809 | ||||||
|  | 803,142 | ||||||
|  | 306,37 | ||||||
|  | 152,614 | ||||||
|  | 812,490 | ||||||
|  | 468,639 | ||||||
|  | 582,249 | ||||||
|  | 31,44 | ||||||
|  | 870,669 | ||||||
|  | 1144,877 | ||||||
|  | 999,773 | ||||||
|  | 545,199 | ||||||
|  | 400,249 | ||||||
|  | 231,618 | ||||||
|  | 140,639 | ||||||
|  | 609,453 | ||||||
|  | 65,542 | ||||||
|  | 1136,572 | ||||||
|  | 853,427 | ||||||
|  | 266,156 | ||||||
|  | 482,302 | ||||||
|  | 117,658 | ||||||
|  | 877,674 | ||||||
|  | 808,165 | ||||||
|  | 649,592 | ||||||
|  | 1113,586 | ||||||
|  | 1062,653 | ||||||
|  | 783,518 | ||||||
|  | 1059,786 | ||||||
|  | 490,880 | ||||||
|  | 850,192 | ||||||
|  | 132,198 | ||||||
|  | 972,872 | ||||||
|  | 1093,712 | ||||||
|  | 870,213 | ||||||
|  | 1141,354 | ||||||
|  | 1014,366 | ||||||
|  | 485,876 | ||||||
|  | 226,663 | ||||||
|  | 691,196 | ||||||
|  | 60,255 | ||||||
|  | 887,724 | ||||||
|  | 55,698 | ||||||
|  | 694,889 | ||||||
|  | 194,142 | ||||||
|  | 5,596 | ||||||
|  | 1310,92 | ||||||
|  | 639,121 | ||||||
|  | 1072,187 | ||||||
|  | 691,200 | ||||||
|  | 314,812 | ||||||
|  | 1063,12 | ||||||
|  | 932,416 | ||||||
|  | 1168,709 | ||||||
|  | 705,486 | ||||||
|  | 1272,217 | ||||||
|  | 214,225 | ||||||
|  | 989,358 | ||||||
|  | 65,437 | ||||||
|  | 1158,56 | ||||||
|  | 475,38 | ||||||
|  | 38,217 | ||||||
|  | 678,588 | ||||||
|  | 311,26 | ||||||
|  | 840,751 | ||||||
|  | 1207,339 | ||||||
|  | 
 | ||||||
|  | fold along x=655 | ||||||
|  | fold along y=447 | ||||||
|  | fold along x=327 | ||||||
|  | fold along y=223 | ||||||
|  | fold along x=163 | ||||||
|  | fold along y=111 | ||||||
|  | fold along x=81 | ||||||
|  | fold along y=55 | ||||||
|  | fold along x=40 | ||||||
|  | fold along y=27 | ||||||
|  | fold along y=13 | ||||||
|  | fold along y=6 | ||||||
|  | @ -0,0 +1,127 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var reading_folds = false; | ||||||
|  |     var map = std.AutoHashMap(Point, void).init(alloc); | ||||||
|  |     var folds = std.ArrayList(Fold).init(alloc); | ||||||
|  |     defer folds.deinit(); | ||||||
|  |     defer map.deinit(); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         if (reading_folds or std.mem.eql(u8, line[0..4], "fold")) { | ||||||
|  |             reading_folds = true; | ||||||
|  |             var lit = std.mem.tokenize(line, " "); | ||||||
|  |             var l = lit.next().?; | ||||||
|  |             l = lit.next().?; | ||||||
|  |             l = lit.next().?; | ||||||
|  |             var fit = std.mem.tokenize(l, "="); | ||||||
|  |             var axis = fit.next().?[0]; | ||||||
|  |             var value = try std.fmt.parseInt(u16, fit.next().?, 10); | ||||||
|  |             try folds.append(.{ | ||||||
|  |                 .axis = switch(axis) { | ||||||
|  |                     'x' => .x, | ||||||
|  |                     'y' => .y, | ||||||
|  |                     else => unreachable, | ||||||
|  |                 }, | ||||||
|  |                 .value = value, | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             var lit = std.mem.tokenize(line, ","); | ||||||
|  |             var x: u16 = try std.fmt.parseInt(u16, lit.next().?, 10); | ||||||
|  |             var y: u16 = try std.fmt.parseInt(u16, lit.next().?, 10); | ||||||
|  |             try map.put(.{.x = x, .y = y}, undefined); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.debug("Map has {} items", .{map.count()}); | ||||||
|  |     var points_to_change = std.AutoHashMap(Point, Point).init(alloc); | ||||||
|  |     defer points_to_change.deinit(); | ||||||
|  |     for (folds.items) |fold, i| { | ||||||
|  |         // We want the points where the axis field is > fold.value | ||||||
|  |         // To fold it over the axis, we get the distance of the | ||||||
|  |         // point from the fold line, then reposition it to | ||||||
|  |         // foldline - distance | ||||||
|  |         var mit = map.keyIterator(); | ||||||
|  |         while (mit.next()) |k| { | ||||||
|  |             switch(fold.axis) { | ||||||
|  |                 .x => { | ||||||
|  |                     if (@field(k.*, "x") > fold.value) { | ||||||
|  |                         var new_point: Point = .{}; | ||||||
|  |                         @field(new_point, "x") = fold.value - (@field(k.*, "x") - fold.value); | ||||||
|  |                         @field(new_point, "y") = @field(k.*, "y"); | ||||||
|  |                         try points_to_change.put(k.*, new_point); | ||||||
|  |                     } | ||||||
|  |                 }, | ||||||
|  |                 .y => { | ||||||
|  |                     if (@field(k.*, "y") > fold.value) { | ||||||
|  |                         var new_point: Point = .{}; | ||||||
|  |                         @field(new_point, "y") = fold.value - (@field(k.*, "y") - fold.value); | ||||||
|  |                         @field(new_point, "x") = @field(k.*, "x"); | ||||||
|  |                         try points_to_change.put(k.*, new_point); | ||||||
|  |                     } | ||||||
|  | 
 | ||||||
|  |                 }, | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         var pit = points_to_change.iterator(); | ||||||
|  |         while (pit.next()) |kv| { | ||||||
|  |             _ = map.remove(kv.key_ptr.*); | ||||||
|  |             try map.put(kv.value_ptr.*, undefined); | ||||||
|  |         } | ||||||
|  |         std.log.debug("After {} folds, there are {} points", .{i+1, map.count()}); | ||||||
|  |         points_to_change.clearRetainingCapacity(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Determine max x, y for printing | ||||||
|  |     var mit = map.keyIterator(); | ||||||
|  |     var max_x: u16 = 0; | ||||||
|  |     var max_y: u16 = 0; | ||||||
|  |     while (mit.next()) |p| { | ||||||
|  |         if (p.*.x > max_x) { | ||||||
|  |             max_x = p.*.x; | ||||||
|  |         } | ||||||
|  |         if (p.*.y > max_y) { | ||||||
|  |             max_y = p.*.y; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.debug("Max dimensions are {} x {}", .{max_x, max_y}); | ||||||
|  |     var x: u16 = 0; | ||||||
|  |     var y: u16 = 0; | ||||||
|  |     const stdout = std.io.getStdOut().writer(); | ||||||
|  |     while (y <= max_y) : (y += 1) { | ||||||
|  |         x = 0; | ||||||
|  |         while (x <= max_x) : (x += 1) { | ||||||
|  |             var p = Point { .x = x, .y = y }; | ||||||
|  |             if (map.contains(p)) { | ||||||
|  |                 try stdout.writeAll("*"); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 try stdout.writeAll(" "); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         try stdout.writeAll("\n"); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const Point = struct { | ||||||
|  |     x: u16 = 0, | ||||||
|  |     y: u16 = 0, | ||||||
|  | }; | ||||||
|  | const Fold = struct { | ||||||
|  |     axis: FoldAxis = .x, | ||||||
|  |     value: u16 = 0, | ||||||
|  |     pub const FoldAxis = enum { | ||||||
|  |         x, | ||||||
|  |         y, | ||||||
|  |     }; | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day14", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,102 @@ | ||||||
|  | ONHOOSCKBSVHBNKFKSBK | ||||||
|  | 
 | ||||||
|  | HO -> B | ||||||
|  | KB -> O | ||||||
|  | PV -> B | ||||||
|  | BV -> C | ||||||
|  | HK -> N | ||||||
|  | FK -> H | ||||||
|  | NV -> C | ||||||
|  | PF -> K | ||||||
|  | FV -> B | ||||||
|  | NH -> P | ||||||
|  | CO -> N | ||||||
|  | HV -> P | ||||||
|  | OH -> H | ||||||
|  | BC -> H | ||||||
|  | SP -> C | ||||||
|  | OK -> F | ||||||
|  | KH -> N | ||||||
|  | HB -> V | ||||||
|  | FP -> N | ||||||
|  | KP -> O | ||||||
|  | FB -> O | ||||||
|  | FH -> F | ||||||
|  | CN -> K | ||||||
|  | BP -> P | ||||||
|  | SF -> O | ||||||
|  | CK -> K | ||||||
|  | KN -> O | ||||||
|  | VK -> C | ||||||
|  | HP -> N | ||||||
|  | KK -> V | ||||||
|  | KO -> C | ||||||
|  | OO -> P | ||||||
|  | BH -> B | ||||||
|  | OC -> O | ||||||
|  | HC -> V | ||||||
|  | HS -> O | ||||||
|  | SH -> V | ||||||
|  | SO -> C | ||||||
|  | FS -> N | ||||||
|  | CH -> O | ||||||
|  | PC -> O | ||||||
|  | FC -> S | ||||||
|  | VO -> H | ||||||
|  | NS -> H | ||||||
|  | PH -> C | ||||||
|  | SS -> F | ||||||
|  | BN -> B | ||||||
|  | BF -> F | ||||||
|  | NC -> F | ||||||
|  | CS -> F | ||||||
|  | NN -> O | ||||||
|  | FF -> P | ||||||
|  | OF -> H | ||||||
|  | NF -> O | ||||||
|  | SC -> F | ||||||
|  | KC -> F | ||||||
|  | CP -> H | ||||||
|  | CF -> K | ||||||
|  | BS -> S | ||||||
|  | HN -> K | ||||||
|  | CB -> P | ||||||
|  | PB -> V | ||||||
|  | VP -> C | ||||||
|  | OS -> C | ||||||
|  | FN -> B | ||||||
|  | NB -> V | ||||||
|  | BB -> C | ||||||
|  | BK -> V | ||||||
|  | VF -> V | ||||||
|  | VC -> O | ||||||
|  | NO -> K | ||||||
|  | KF -> P | ||||||
|  | FO -> C | ||||||
|  | OB -> K | ||||||
|  | ON -> S | ||||||
|  | BO -> V | ||||||
|  | KV -> H | ||||||
|  | CC -> O | ||||||
|  | HF -> N | ||||||
|  | VS -> S | ||||||
|  | PN -> P | ||||||
|  | SK -> F | ||||||
|  | PO -> V | ||||||
|  | HH -> F | ||||||
|  | VV -> N | ||||||
|  | VH -> N | ||||||
|  | SV -> S | ||||||
|  | CV -> B | ||||||
|  | KS -> K | ||||||
|  | PS -> V | ||||||
|  | OV -> S | ||||||
|  | SB -> V | ||||||
|  | NP -> K | ||||||
|  | SN -> C | ||||||
|  | NK -> O | ||||||
|  | PK -> F | ||||||
|  | VN -> P | ||||||
|  | PP -> K | ||||||
|  | VB -> C | ||||||
|  | OP -> P | ||||||
|  | @ -0,0 +1,50 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var template: []const u8 = it.next().?; | ||||||
|  |     var replacements = std.ArrayList(Replacement).init(alloc); | ||||||
|  |     defer replacements.deinit(); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var lit = std.mem.tokenize(line, " "); | ||||||
|  |         var pair = lit.next().?; | ||||||
|  |         _ = lit.next(); // skip | ||||||
|  |         var insert = lit.next().?; | ||||||
|  |         var r: Replacement = undefined; | ||||||
|  |         r.pair[0] = pair[0]; | ||||||
|  |         r.pair[1] = pair[1]; | ||||||
|  |         r.insert = insert[0]; | ||||||
|  |         try replacements.append(r); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var compound = std.ArrayList(u8).init(alloc); | ||||||
|  |     defer compound.deinit(); | ||||||
|  | 
 | ||||||
|  |     // Clone our template | ||||||
|  |     for (template) |t| { | ||||||
|  |         try compound.append(t); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var step: u8 = 0; | ||||||
|  |     while (step < 10) : (step += 1) { | ||||||
|  |         var index: usize = 0; | ||||||
|  |         while (index < (compound.items.len - 1)) : (index += 1) { | ||||||
|  |              | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const Replacement = struct { | ||||||
|  |     pair: [2]u8, | ||||||
|  |     insert: u8, | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day15", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,101 @@ | ||||||
|  | 7257173117871274148119191397411866741961893915191233591781237171134183111991588567743913598398999129 | ||||||
|  | 9199956198317542321811795167539333593959845233586245178771259721629672984999396181817424129792113429 | ||||||
|  | 5678391114239515741618386378916979112716269165195269215584939279279177393199175187199511833999819385 | ||||||
|  | 7149919968927397171639311441114199789621399783958844626227879671633692776519178119978312379854918472 | ||||||
|  | 9571133571872511883799281362819193417112413972551887416264299171789218539132957196979811132721291672 | ||||||
|  | 8172584925581929988661688649146133971138896125391412411926754121134983167188929529959966385437588844 | ||||||
|  | 4914518468152915388917729744941583692326581473756415317665271248313684872717734981831164961169161441 | ||||||
|  | 2519894881987711813817528992187872878321614865385871812543911299816979364869972221856158852969592112 | ||||||
|  | 9859391668949943293812278997458981968712239911133451788394169935992685191958894671638974814216612719 | ||||||
|  | 2192529153611518191595896814996141119517992288611125714717119399491112913829241231917539211119881849 | ||||||
|  | 2337843263693226789699931111398112969887535286492296226999311848178157169949315516494848924316823474 | ||||||
|  | 1191281328169873275751982595447194592918411459316261266123821567822849396139339812112776322962112239 | ||||||
|  | 4587143466247931931363896317279914687959418292417719618921579781314932674121288194141812419924513491 | ||||||
|  | 9188217988495399118711317519126435116785399685763129885168131465437119937687542179912685827611129973 | ||||||
|  | 9281999423754831489181352222362255825969879819312357829319722961312991751988497399311813231979551234 | ||||||
|  | 8411642818749914959745792191319718911388132253292482346295961171634916811938584616484592994128913896 | ||||||
|  | 3388255416913192166863912671592797172349778144861261162626679169122118178816999781511482128586841931 | ||||||
|  | 1544111521579847285289829899211419185418871795132727999177828715458885628798278694521919225186212479 | ||||||
|  | 6112394533149912224244478554154888237766677192511968451313528663845219413639188496229811993163929889 | ||||||
|  | 7198784899159217522265428239842119185341314211991273329136173711277137989451533985143943379599711279 | ||||||
|  | 5322174898961411153515736312827315967799457744138498991267929611147897735269199298727139863199825449 | ||||||
|  | 9389598982325839969741219429682597992913484299849122148988986313319119891881251311761168419988993984 | ||||||
|  | 9731391996727591141584769372292618329676991274389449899178113115589128811112539136473142798111999659 | ||||||
|  | 5158499781719185191335698726337225419942769281296219215168381368296377583192114793345154114939919138 | ||||||
|  | 9489436472297254387595375823195948188155998979224885397929111219894329444244314131186498373329899628 | ||||||
|  | 5115948294552681116994961691838819122926817811428719719487293692225795236769313128246954783896581419 | ||||||
|  | 7842378538599781529689123131749873922371451111511113114844145628981899497289212258729489151386198379 | ||||||
|  | 5657182954384884192389177113923946799271142897712314759172399912237297817994565459889643847449996591 | ||||||
|  | 2114463882686822117918787199822775881529895139151499196692246274147217497821149527235387719991114714 | ||||||
|  | 2911917115481432912158219697929535145897818898214661343833193385121919978318122826712681212138729941 | ||||||
|  | 7919482212881442617559399592111184197611192148899237895295211389594619418194519266268597128875922164 | ||||||
|  | 5122782781762591222295946745991662147485323299849117125858933893496129327282821421189644352917799563 | ||||||
|  | 1136291298829627566691527929262125187126922289732289227716821815619551911944779995415278137991622552 | ||||||
|  | 4929328679851289388213321419985631998929472889692146329429231293698273199118713262892523784612611999 | ||||||
|  | 1327734151191996421344997185478394257228713274441955249899986342244731153572974191897991393662691931 | ||||||
|  | 2781117154312271979199389368319489151943763236781711191122134726691762925242929616867585411791129159 | ||||||
|  | 1439186637181196485238285593819594866112889871679966657834249176324314497819969991879491995926214924 | ||||||
|  | 8198715159568382591431231227339698151116662552918333813978498567586137499214111391961922138831958891 | ||||||
|  | 1791636999843894411347725615937522999231159118674816321332412988177251964743241117549699136494419193 | ||||||
|  | 2347191949569898652196299127952395252161168176339428456658821198913454439135327112298841851793131139 | ||||||
|  | 6517373396832351522818699933865532731822114597879322225665521379149517111835298818315911951121182358 | ||||||
|  | 9989219811198438961112789214341437915858986541454335893139567183897549391541933121816392249992757195 | ||||||
|  | 2751242393169519965822749148113198418478749175225597965669585119898274413133594511599239813819261729 | ||||||
|  | 2333615561781383963589211869624423159983279225123169449222598319329695159532597919931391498257683522 | ||||||
|  | 2326925593466238521963381246934599431321918392819124294951311331116521125288316176431186199751635491 | ||||||
|  | 9341367399225381779967242896675118399271292593248972922271792198875912499185119129993955719191131219 | ||||||
|  | 9419488192386241298831213479619891591112812195761439362996925799233613913141343831817117189287994755 | ||||||
|  | 9695968821621727482419983392371417945181396516317947384995853931879231912925789698357961179754119511 | ||||||
|  | 9819267839831973911319938469292179778511998125121931891146223692296192299952153342285175781729419271 | ||||||
|  | 1863816696271622173271985549176326189359343784972161815896794319879938399995919111197381311974788614 | ||||||
|  | 9951198498951459991236295523134837224482241663114944798913138999819143627393989956711324837996945589 | ||||||
|  | 5636493872291432645969776969437321939689259195165871611951271619514731193819693277839717689838523535 | ||||||
|  | 1599873877797591958659883217591162792939133915996748148738729819416279661719716399869947198411871934 | ||||||
|  | 8794639995351381527537286278892222682139243726394685977188691436889598769141824739519232576663818623 | ||||||
|  | 9312694112319895396113225123691699114916381799538178372616115397895462387932975997638239393799315212 | ||||||
|  | 5832652557695274383198568431192817489141326899824429893139485722158832888768989154554522478498498714 | ||||||
|  | 8782845924111831912127921565587919113818125774499381223562975114549551996121626761137916739478221568 | ||||||
|  | 9811929922686225152399211122397597158513891191615161416432783564767191169849849511985178913416153852 | ||||||
|  | 1137711214593792114391112795839813891552389836983814596893489181536998977653386261925692938856897897 | ||||||
|  | 2891454257881276297539979515161138819723112914812122617911912212922972482812982987377897974782821188 | ||||||
|  | 7671589249839457412545235186153935156941884381134221489611219191921272892597772389234492565914198258 | ||||||
|  | 1552483399116635372974898931188883743171689242311789612968114416972112951486134993116143393399793998 | ||||||
|  | 8711188851122588629852724495172527356897321495229616597821333465358285422331924491418522517979118993 | ||||||
|  | 9593315278649975951219952268211871741223727356181132397183838894499829221111978873973591418997289513 | ||||||
|  | 3969997988729933184995613429919894232321166598283911973373115779211228919166877393889434215381781389 | ||||||
|  | 5588221472469964598623295656797377199853717996631331495111278692241929496187813341637872397899624175 | ||||||
|  | 9921637195158912596622263381891921897978588191413981249169189173199699294541171879989121615634351342 | ||||||
|  | 3798931227871974751799777995169123924251123997219388112822621636398239724484812154741112812741472923 | ||||||
|  | 1318745799964813125829815417129787311384489283415939129112478189111179617966392125599951899588399933 | ||||||
|  | 9919168243159595589113819319611976889991646271868112472392818222893941139743434115977476121333191818 | ||||||
|  | 3115972825613468816687921319481739413919883837749478584158912513417673612383549994987313463899975111 | ||||||
|  | 9819245879871957475393932424291294189961391917998628819131487621819218799996929997391516748191973218 | ||||||
|  | 2543119159599118542114782197275925148321199297951316238291197471953953113117231732381348855871885341 | ||||||
|  | 8597192929968786896989279839331134411223192189898926119875521124153282436126568329811999323445191679 | ||||||
|  | 3975785449971831818312568579176154978689122133184732928183381881141862482849263341279915278911683894 | ||||||
|  | 3179111996949988298153199151389479257689125228811639199771592599998978715664289996372311512944263139 | ||||||
|  | 3717731949295466943999325285695444691229948881923328813265999285999127111938229969918689613331778988 | ||||||
|  | 3899774572142914257631398127999644341916848491762691872194259535697139312281868155191287342929193183 | ||||||
|  | 3419531741121147372119481389754229993641415577459396295899232815823975392974927395711568972974293925 | ||||||
|  | 1722176361244419285779288543598751211392531919462574932268633933695192352441119172662173735119515712 | ||||||
|  | 8259287379497359112197399192874789929899857534134534892997117372192299398799696792833998919968157692 | ||||||
|  | 9113588331521199442922126152433321148852971919741151158559612459789766141191421226791434779238851542 | ||||||
|  | 9934485995397918323116671821935121371411639119196141711221111598878781853442188129954293234742142941 | ||||||
|  | 5863996738774189559631311914763988372133998564893667341114799688887419869498499571261945352924543659 | ||||||
|  | 1219173111621632848325722799121771234351313226119137299611516914998734631224397849155891733316457236 | ||||||
|  | 2581119126696683198948223933432829124786552193519396275241779267529656466959196852322344769712989425 | ||||||
|  | 9967141179556989992264146994141761832265411668193157248414232474791714884711961318296968187812563731 | ||||||
|  | 2489879759889926897312354996751186797873869898953112749555984922414591917338857833991412625172181498 | ||||||
|  | 9389457158881973888911321813181681241492541783691881299169396894618761158125679495966134894392982199 | ||||||
|  | 9619992269841241296891999961333629572281414928995452682633291959449286127129612124227252269791663654 | ||||||
|  | 1614835267511194414153578263179929291589313129387133978158911722592272783869578732691311815861241491 | ||||||
|  | 9652116592834327221168619761719533625339966299211384947128885411279177514867942937963481178919941711 | ||||||
|  | 2933298721252991578245341817819977199267549571111491876711439883927615991991196211296294697272313774 | ||||||
|  | 1431715794741427753127381461152185596778586461419451428969132784392522194651111917169662896769767812 | ||||||
|  | 5919195742129923589897349316296264144952749521471522921641452152978765849165829872135669664214112327 | ||||||
|  | 6339474432243943991959119983361889756918344251994119969825144749941178381321286493218961118191911875 | ||||||
|  | 5517228343299391617317192214769719192189853799115811159493116197825637676714396432979816281155693495 | ||||||
|  | 6388461935842284623142112416943162883259817194297799981283439458172297191119123521515932483495126961 | ||||||
|  | 7843191912999868941538811232841971381689242136381989791436989848478992513449168132392761291119899184 | ||||||
|  | 1424114349573937523917871789194999469999119751164761884221519813273935995996288159921682217898434989 | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,135 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = arena.allocator(); | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(u8, contents, "\n"); | ||||||
|  |     var map = std.ArrayList(u8).init(alloc); | ||||||
|  |     defer map.deinit(); | ||||||
|  |     var width: u8 = 0; | ||||||
|  |     var height: u8 = 0; | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         height += 1; | ||||||
|  |         if (@intCast(u8, line.len) > width) { | ||||||
|  |             width = @intCast(u8, line.len); | ||||||
|  |         } | ||||||
|  |         for (line) |_, i| { | ||||||
|  |             try map.append(try std.fmt.parseInt(u8, line[i..i+1], 10)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var path = try find_path(alloc, map.items, width, height); | ||||||
|  |     defer path.deinit(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const Point = struct { | ||||||
|  |     x: u8 = 0, | ||||||
|  |     y: u8 = 0, | ||||||
|  | 
 | ||||||
|  |     pub fn get_index(p: *Point, width: u8, height: u8) usize { | ||||||
|  |         _ = width; | ||||||
|  |         return @as(usize, height) * @as(usize, p.y) + @as(usize, p.x); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn from_index(index: usize, width: u8, height: u8) Point { | ||||||
|  |         return Point { | ||||||
|  |             .x = @intCast(u8, index % width), | ||||||
|  |             .y = @intCast(u8, index / height), | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | fn find_path(alloc: std.mem.Allocator, map: []u8, width: u8, | ||||||
|  |              height: u8) !std.ArrayList(Point) { | ||||||
|  |     var path = std.ArrayList(Point).init(alloc); | ||||||
|  |     var open_set = std.AutoHashMap(Point, void).init(alloc); | ||||||
|  |     defer open_set.deinit(); | ||||||
|  |     // Our starting point | ||||||
|  |     try open_set.put(.{.x = width - 1, .y = height - 1}, undefined); | ||||||
|  | 
 | ||||||
|  |     var came_from = std.AutoHashMap(Point, void).init(alloc); | ||||||
|  |     defer came_from.deinit(); | ||||||
|  | 
 | ||||||
|  |     while (open_set.count() > 0) { | ||||||
|  |         // Find lowest score in openset | ||||||
|  |         var lowest: Point = undefined; | ||||||
|  |         var score: u8 = std.math.maxInt(u8); | ||||||
|  |         var oit = open_set.iterator(); | ||||||
|  |         while(oit.next()) |kv| { | ||||||
|  |             var p = kv.key_ptr.*; | ||||||
|  |             _ = kv.value_ptr.*; | ||||||
|  |             // We might have to modify this, since our open_set may | ||||||
|  |             // contain points that aren't directly adjacent? | ||||||
|  |             if (map[p.get_index(width, height)] < score) { | ||||||
|  |                 lowest = p; | ||||||
|  |                 score = map[p.get_index(width, height)]; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         std.log.debug("Went to point {}, {}: score {}", | ||||||
|  |                       .{lowest.x, lowest.y, score}); | ||||||
|  |         try path.append(lowest); | ||||||
|  |         try came_from.put(lowest, undefined); | ||||||
|  | 
 | ||||||
|  |         if (lowest.x == 0 and lowest.y == 0) { | ||||||
|  |             // We have arrived | ||||||
|  |             return path; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         _ = open_set.remove(lowest); | ||||||
|  | 
 | ||||||
|  |         // Let's clear our open_set, but this might be an error | ||||||
|  |         open_set.clearRetainingCapacity(); | ||||||
|  |         var adjacents = get_adjacent_points(lowest.get_index(width, height), | ||||||
|  |                                            width, height); | ||||||
|  |         for (adjacents) |adj| { | ||||||
|  |             if (adj) |a| { | ||||||
|  |                 var p = Point.from_index(a, width, height); | ||||||
|  |                 if (came_from.contains(p)) { | ||||||
|  |                     continue; | ||||||
|  |                 } | ||||||
|  |                 try open_set.put(p, undefined); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     // Failure to find path | ||||||
|  |     unreachable; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_adjacent_points(i: usize, width: usize, height: usize) [4]?usize { | ||||||
|  |     var adjacents = [4] ?usize { | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |     }; | ||||||
|  |     var ai: usize = 0; | ||||||
|  |     var bottom = i < ((height - 1) * width); | ||||||
|  |     var top = i >= width; | ||||||
|  |     var left = (i % width) != 0; | ||||||
|  |     var right = (i % width) != (width - 1); | ||||||
|  |     if (top) { | ||||||
|  |         adjacents[ai] = i - width; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (left) { | ||||||
|  |         adjacents[ai] = i - 1; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (right) { | ||||||
|  |         adjacents[ai] = i + 1; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (bottom) { | ||||||
|  |         adjacents[ai] = i + width; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     return adjacents; | ||||||
|  | } | ||||||
|  | @ -0,0 +1,34 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day16", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | 
 | ||||||
|  |     const exe_tests = b.addTest("src/main.zig"); | ||||||
|  |     exe_tests.setTarget(target); | ||||||
|  |     exe_tests.setBuildMode(mode); | ||||||
|  | 
 | ||||||
|  |     const test_step = b.step("test", "Run unit tests"); | ||||||
|  |     test_step.dependOn(&exe_tests.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     std.log.info("All your codebase are belong to us.", .{}); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "basic test" { | ||||||
|  |     try std.testing.expectEqual(10, 3 + 7); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day2", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							|  | @ -0,0 +1,2 @@ | ||||||
|  | info: [Part 1] Depth 916, Horizontal Position 1845, Sum 1690020 | ||||||
|  | info: [Part 2] Depth 763408, Horizontal Position 1845, Sum 1408487760 | ||||||
|  | @ -0,0 +1,58 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     // Part 1 | ||||||
|  |     var pos_x: i32 = 0; | ||||||
|  |     var depth: i32 = 0; | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var lit = std.mem.tokenize(line, " "); | ||||||
|  |         var dir = lit.next().?; | ||||||
|  |         var amount = try std.fmt.parseInt(i32, lit.next().?, 10); | ||||||
|  |         if (std.mem.eql(u8, dir, "up")) { | ||||||
|  |             depth -= amount; | ||||||
|  |         } | ||||||
|  |         else if (std.mem.eql(u8, dir, "forward")) { | ||||||
|  |             pos_x += amount; | ||||||
|  |         } | ||||||
|  |         else if (std.mem.eql(u8, dir, "down")) { | ||||||
|  |             depth += amount; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] Depth {}, Horizontal Position {}, Sum {}", | ||||||
|  |                  .{depth, pos_x, depth * pos_x}); | ||||||
|  | 
 | ||||||
|  |     // Part 2 | ||||||
|  |     pos_x = 0; | ||||||
|  |     var aim: i32 = 0; | ||||||
|  |     depth = 0; | ||||||
|  |     it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var lit = std.mem.tokenize(line, " "); | ||||||
|  |         var dir = lit.next().?; | ||||||
|  |         var amount = try std.fmt.parseInt(i32, lit.next().?, 10); | ||||||
|  |         if (std.mem.eql(u8, dir, "up")) { | ||||||
|  |             aim -= amount; | ||||||
|  |         } | ||||||
|  |         else if (std.mem.eql(u8, dir, "forward")) { | ||||||
|  |             depth += amount * aim; | ||||||
|  |             pos_x += amount; | ||||||
|  |         } | ||||||
|  |         else if (std.mem.eql(u8, dir, "down")) { | ||||||
|  |             aim += amount; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 2] Depth {}, Horizontal Position {}, Sum {}", | ||||||
|  |                  .{depth, pos_x, depth * pos_x}); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day3", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							|  | @ -0,0 +1,2 @@ | ||||||
|  | info: [Part 1] Gamma: 3004, Epsilon: 1091, Product: 3277364 | ||||||
|  | info: [Part 2] Generator: 3583, Scrubber 1601, Product 5736383 | ||||||
|  | @ -0,0 +1,236 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     // Part 1 | ||||||
|  |     // gamma_rate * epsilon_rate = power_consumption | ||||||
|  |     // gamma_rate = for each bit position in a number | ||||||
|  |     //   consider all numbers, this bit position is the most common | ||||||
|  |     // epsilon_rate = for each bit position in a number | ||||||
|  |     //   consider all numbers, this bit position is the least common | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var first = it.next().?; | ||||||
|  |     var length = first.len; | ||||||
|  |     var n_zero_bits = try std.ArrayList(u32).initCapacity(alloc, length); | ||||||
|  |     defer n_zero_bits.deinit(); | ||||||
|  |     n_zero_bits.appendNTimesAssumeCapacity(0, length); | ||||||
|  |     var n_total_numbers: u32 = 0; | ||||||
|  | 
 | ||||||
|  |     // This is used in the 2nd part | ||||||
|  |     var numbers = std.ArrayList(u32).init(alloc); | ||||||
|  | 
 | ||||||
|  |     it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         n_total_numbers += 1; | ||||||
|  |         try numbers.append(try std.fmt.parseInt(u32, line, 2)); | ||||||
|  |         for (line) |c, i| { | ||||||
|  |             if (c == '0') { | ||||||
|  |                 n_zero_bits.items[i] += 1; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     //std.log.debug("{} items processed; n_zero: '{any}'", | ||||||
|  |     //              .{n_total_numbers, n_zero_bits.items}); | ||||||
|  |     var gamma: u32 = 0; | ||||||
|  |     var epsilon: u32 = 0; | ||||||
|  |     var half = n_total_numbers / 2; | ||||||
|  |     for (n_zero_bits.items) |value, i| { | ||||||
|  |         // Not clear what should happen if the bits are | ||||||
|  |         // equally common, but we do not appear to have | ||||||
|  |         // that case in our input. | ||||||
|  |         if(value > half) { | ||||||
|  |             gamma = gamma |  (@as(u32, 0) << @intCast(u5, length - i - 1)); | ||||||
|  |             epsilon = epsilon | (@as(u32, 1) << @intCast(u5, length - i - 1)); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             gamma = gamma | (@as(u32, 1) << @intCast(u5, length - i - 1)); | ||||||
|  |             epsilon = epsilon | (@as(u32, 0) << @intCast(u5, length - i - 1)); | ||||||
|  |         } | ||||||
|  |         //std.log.debug("After round {}, gamma: {b}, epsilon: {b}", | ||||||
|  |         //              .{i, gamma, epsilon}); | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] Gamma: {}, Epsilon: {}, Product: {}", | ||||||
|  |                  .{gamma, epsilon, gamma * epsilon}); | ||||||
|  | 
 | ||||||
|  |     // Part 2 | ||||||
|  |     var scrubber_rating: u32 = 0; | ||||||
|  |     var generator_rating: u32 = 0; | ||||||
|  |     var index: i32 = @intCast(i32, length) - 1; | ||||||
|  |     var generator_values = try std.mem.dupe(alloc, u32, numbers.items); | ||||||
|  |     defer alloc.free(generator_values); | ||||||
|  |     var scrubber_values = try std.mem.dupe(alloc, u32, numbers.items); | ||||||
|  |     defer alloc.free(scrubber_values); | ||||||
|  |     //std.log.warn("G: {b:0>5}", .{generator_values}); | ||||||
|  |     //std.log.warn("S: {b:0>5}", .{scrubber_values}); | ||||||
|  |     while (index >= 0) : (index -= 1) { | ||||||
|  |         if (generator_values.len > 1) { | ||||||
|  |             var generator_result = try filter( | ||||||
|  |                 alloc, generator_values, @intCast(u32, index), true); | ||||||
|  |             //std.log.warn("G: {b:0>5}", .{generator_result}); | ||||||
|  |             alloc.free(generator_values); | ||||||
|  |             generator_values = generator_result; | ||||||
|  |         } | ||||||
|  |         if (scrubber_values.len > 1) { | ||||||
|  |             var scrubber_result = try filter( | ||||||
|  |                 alloc, scrubber_values, @intCast(u32, index), false); | ||||||
|  |             //std.log.warn("S: {b:0>5}", .{scrubber_result}); | ||||||
|  |             alloc.free(scrubber_values); | ||||||
|  |             scrubber_values = scrubber_result; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     //std.log.debug("Generator values: {any}\nScrubber values: {any}", | ||||||
|  |     //              .{generator_values, scrubber_values}); | ||||||
|  |     std.log.info("[Part 2] Generator: {}, Scrubber {}, Product {}", | ||||||
|  |                  .{generator_values[0], scrubber_values[0], | ||||||
|  |                    generator_values[0] * scrubber_values[0],}); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /// caller owns returned memory | ||||||
|  | pub fn filter(alloc: *std.mem.Allocator, values: []u32, index: u32, most_common: bool) ![]u32 { | ||||||
|  |     var n_one: u32 = 0; | ||||||
|  |     var n_zero: u32 = 0; | ||||||
|  |     // One pass to count | ||||||
|  |     for (values) |v| { | ||||||
|  |         var r =  v & (@as(u32, 1) << @intCast(u5, index)); | ||||||
|  |         //std.log.warn("{b} & {b} = {b}", .{ | ||||||
|  |         //    v, @as(u32, 1) << @intCast(u5, index), r}); | ||||||
|  |         if (v & (@as(u32, 1) << @intCast(u5, index)) != 0) { | ||||||
|  |             n_one += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     n_zero = @intCast(u32, values.len) - n_one; | ||||||
|  |     //std.log.warn("{}, {}", .{n_zero, n_one}); | ||||||
|  |     // One pass to filter into new slice | ||||||
|  |     var new_values = std.ArrayList(u32).init(alloc); | ||||||
|  |     defer new_values.deinit(); | ||||||
|  |     var most_common_value: u32 = 0; | ||||||
|  |     if (n_zero < n_one) { | ||||||
|  |         if (most_common) { | ||||||
|  |             most_common_value = 1; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             most_common_value = 0; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     else if (n_zero == n_one) { | ||||||
|  |         if (most_common) { | ||||||
|  |             most_common_value = 1; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             most_common_value = 0; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         if (most_common) { | ||||||
|  |             most_common_value = 0; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             most_common_value = 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     // std.log.warn("Most common value at index {} is {}", | ||||||
|  |     //              .{index, most_common_value}); | ||||||
|  |     for (values) |v, i| { | ||||||
|  |         var bit_at_index = (v & (@as(u32, 1) << @intCast(u4, index))) | ||||||
|  |             >> @intCast(u4, index); | ||||||
|  |         //std.log.warn("Value {} Bit at index {} is {b}", .{i, index, bit_at_index}); | ||||||
|  |         if (bit_at_index == most_common_value) { | ||||||
|  |             try new_values.append(v); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return new_values.toOwnedSlice(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "filter most common" { | ||||||
|  |     var values = [_]u32 { | ||||||
|  |         0b00100, | ||||||
|  |         0b11110, | ||||||
|  |         0b10110, | ||||||
|  |         0b10111, | ||||||
|  |         0b10101, | ||||||
|  |         0b01111, | ||||||
|  |         0b00111, | ||||||
|  |         0b11100, | ||||||
|  |         0b10000, | ||||||
|  |         0b11001, | ||||||
|  |         0b00010, | ||||||
|  |         0b01010, | ||||||
|  |     }; | ||||||
|  |     var most_common_at_4 = [_]u32 { | ||||||
|  |         0b11110, | ||||||
|  |         0b10110, | ||||||
|  |         0b10111, | ||||||
|  |         0b10101, | ||||||
|  |         0b11100, | ||||||
|  |         0b10000, | ||||||
|  |         0b11001, | ||||||
|  |     }; | ||||||
|  |     var alloc = std.testing.allocator; | ||||||
|  |     var result = try filter(alloc, values[0..], 4, true); | ||||||
|  |     try std.testing.expectEqual(true, std.mem.eql(u32, most_common_at_4[0..], result[0..])); | ||||||
|  |     alloc.free(result); | ||||||
|  | 
 | ||||||
|  |     // Example with a tie | ||||||
|  |     var tie_values = [_] u32 { | ||||||
|  |         0b10110, | ||||||
|  |         0b10111, | ||||||
|  |     }; | ||||||
|  |     var most_common_at_0 = [_]u32 { | ||||||
|  |         0b10111, | ||||||
|  |     }; | ||||||
|  |     result = try filter(alloc, tie_values[0..], 0, true); | ||||||
|  |     defer alloc.free(result); | ||||||
|  |     try std.testing.expectEqual(true, std.mem.eql(u32, most_common_at_0[0..], result[0..])); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | test "filter least common" { | ||||||
|  |     var values = [_]u32 { | ||||||
|  |         0b00100, | ||||||
|  |         0b11110, | ||||||
|  |         0b10110, | ||||||
|  |         0b10111, | ||||||
|  |         0b10101, | ||||||
|  |         0b01111, | ||||||
|  |         0b00111, | ||||||
|  |         0b11100, | ||||||
|  |         0b10000, | ||||||
|  |         0b11001, | ||||||
|  |         0b00010, | ||||||
|  |         0b01010, | ||||||
|  |     }; | ||||||
|  |     var least_common_at_4 = [_]u32 { | ||||||
|  |         0b00100, | ||||||
|  |         0b01111, | ||||||
|  |         0b00111, | ||||||
|  |         0b00010, | ||||||
|  |         0b01010, | ||||||
|  |     }; | ||||||
|  |     var alloc = std.testing.allocator; | ||||||
|  |     var result = try filter(alloc, values[0..], 4, false); | ||||||
|  |     //std.log.warn("{b}", .{result}); | ||||||
|  |     try std.testing.expectEqual(true, std.mem.eql(u32, least_common_at_4[0..], result[0..])); | ||||||
|  |     alloc.free(result); | ||||||
|  | 
 | ||||||
|  |     // Example with a tie | ||||||
|  |     var tie_values = [_] u32 { | ||||||
|  |         0b01111, | ||||||
|  |         0b01010, | ||||||
|  |     }; | ||||||
|  |     var least_common_at_2 = [_]u32 { | ||||||
|  |         0b01010, | ||||||
|  |     }; | ||||||
|  |     result = try filter(alloc, tie_values[0..], 2, false); | ||||||
|  |     //std.log.warn("{any}", .{result}); | ||||||
|  |     defer alloc.free(result); | ||||||
|  |     try std.testing.expectEqual(true, std.mem.eql(u32, least_common_at_2[0..], result[0..])); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day4", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,602 @@ | ||||||
|  | 49,48,98,84,71,59,37,36,6,21,46,30,5,33,3,62,63,45,43,35,65,77,57,75,19,44,4,76,88,92,12,27,7,51,14,72,96,9,0,17,83,64,38,95,54,20,1,74,69,80,81,56,10,68,42,15,99,53,93,94,47,13,29,34,60,41,82,90,25,85,78,91,32,70,58,28,61,24,55,87,39,11,79,50,22,8,89,26,16,2,73,23,18,66,52,31,86,97,67,40 | ||||||
|  | 
 | ||||||
|  | 86 46 47 61 57 | ||||||
|  | 44 74 17  5 87 | ||||||
|  | 78  8 54 55 97 | ||||||
|  | 11 90  7 75 70 | ||||||
|  | 81 50 84 10 60 | ||||||
|  | 
 | ||||||
|  | 47 28 64 52 44 | ||||||
|  | 73 48 30 15 53 | ||||||
|  | 57 21 78 75 26 | ||||||
|  | 51 39 72 18 25 | ||||||
|  | 29 76 83 54 82 | ||||||
|  | 
 | ||||||
|  | 81  1 18 24 12 | ||||||
|  |  3 38 15 85 50 | ||||||
|  | 32 10 74 86 84 | ||||||
|  | 30 64 56 79 95 | ||||||
|  | 78 94 35 93  8 | ||||||
|  | 
 | ||||||
|  | 48 30 79 85 87 | ||||||
|  | 66 35 13 17 95 | ||||||
|  | 32 22 94 61 20 | ||||||
|  | 50 42  0  3 93 | ||||||
|  | 69 44 68  1  9 | ||||||
|  | 
 | ||||||
|  | 91 79 93 41 33 | ||||||
|  | 98 51 39  9 10 | ||||||
|  | 24 70 99  2 11 | ||||||
|  | 32 13 21  6 68 | ||||||
|  | 40 27 48 89  7 | ||||||
|  | 
 | ||||||
|  | 40 29 34  1 23 | ||||||
|  | 79 36 75 57 95 | ||||||
|  | 61 50  4 21 48 | ||||||
|  | 54  0 81 98 72 | ||||||
|  | 24 30 15 31 52 | ||||||
|  | 
 | ||||||
|  | 18 68 17 25 34 | ||||||
|  | 97 36 77  6 30 | ||||||
|  | 79 72 38 94 60 | ||||||
|  | 54 45 16 67 12 | ||||||
|  | 58 31 57 71 92 | ||||||
|  | 
 | ||||||
|  | 40 58 80 86 85 | ||||||
|  | 91 57 51 23 10 | ||||||
|  | 61 78  4 36 66 | ||||||
|  | 24 41 88 25 99 | ||||||
|  | 15 68 12 55 75 | ||||||
|  | 
 | ||||||
|  | 82 99 17  5 76 | ||||||
|  | 65 42 73 78 61 | ||||||
|  | 34 62 14 23 68 | ||||||
|  |  9 79 72 45  0 | ||||||
|  | 43 96 11 13 10 | ||||||
|  | 
 | ||||||
|  | 98 47 90 14 12 | ||||||
|  | 80 63 35 42 11 | ||||||
|  | 27 66  1  9 32 | ||||||
|  | 17 85 61 71 68 | ||||||
|  |  6 29  7 94 67 | ||||||
|  | 
 | ||||||
|  | 38 35 70 18 59 | ||||||
|  | 62 54 84 10 27 | ||||||
|  | 60 92 90 64 86 | ||||||
|  | 25 99 49 43  4 | ||||||
|  | 23 50 39 16 40 | ||||||
|  | 
 | ||||||
|  | 72  1 73  8 33 | ||||||
|  | 86 65 99 49 66 | ||||||
|  | 56 79 23 41 46 | ||||||
|  |  4 48 43 55 93 | ||||||
|  | 98 63 47 37 30 | ||||||
|  | 
 | ||||||
|  | 33 96 72 93 99 | ||||||
|  | 30 12 56 46 65 | ||||||
|  | 39 40 59 94 50 | ||||||
|  |  0  8 67 27 47 | ||||||
|  | 53 57 24 77 42 | ||||||
|  | 
 | ||||||
|  |  9 57  8 28 12 | ||||||
|  | 90 81 21 25 51 | ||||||
|  | 88 18 78  3 64 | ||||||
|  | 20 87 97 45 85 | ||||||
|  | 92 40 52 29 17 | ||||||
|  | 
 | ||||||
|  | 99 89 15 54 32 | ||||||
|  | 93 81 36 14 91 | ||||||
|  | 86  7 67 18 92 | ||||||
|  | 65 21 55 38  8 | ||||||
|  | 12 88 27 90 94 | ||||||
|  | 
 | ||||||
|  | 96  0  6 91 44 | ||||||
|  | 28 60 10 70 75 | ||||||
|  | 69 37 51 21 87 | ||||||
|  | 93 59 14 53 15 | ||||||
|  | 64 66  9 50 27 | ||||||
|  | 
 | ||||||
|  | 33 46 38  2 41 | ||||||
|  | 24 51 50 72 57 | ||||||
|  | 42 85 99 97 56 | ||||||
|  | 35 69 12 86 73 | ||||||
|  |  4 47 34 80 17 | ||||||
|  | 
 | ||||||
|  | 70 28 77 73 53 | ||||||
|  | 67 94 83 79 82 | ||||||
|  | 89  9 96 48 17 | ||||||
|  | 47 86 88 12  3 | ||||||
|  | 55 39 98 14 90 | ||||||
|  | 
 | ||||||
|  | 64 82 85 45 10 | ||||||
|  | 27  5 12 72 40 | ||||||
|  | 52 31 25 79 65 | ||||||
|  |  6 26  3 43 57 | ||||||
|  | 89 49 36 59 35 | ||||||
|  | 
 | ||||||
|  | 70  0 58 98 65 | ||||||
|  | 54 93 75 14 26 | ||||||
|  | 28 69 17 29 78 | ||||||
|  | 46 22 47 85 87 | ||||||
|  | 44 38 10 11 63 | ||||||
|  | 
 | ||||||
|  | 18 52 66 42 58 | ||||||
|  | 99 78 44 28 73 | ||||||
|  | 24 71  5 14 82 | ||||||
|  | 77 35 45 76 19 | ||||||
|  | 70 20  0 43 48 | ||||||
|  | 
 | ||||||
|  | 42 82 85 87 51 | ||||||
|  | 40 49 93 95 74 | ||||||
|  | 25 79 37 67 55 | ||||||
|  | 26 27 90 47 22 | ||||||
|  | 38 50 33 10 75 | ||||||
|  | 
 | ||||||
|  | 15 99 60 28 79 | ||||||
|  | 94 42 63 20 57 | ||||||
|  | 44 55 96 67 53 | ||||||
|  | 64  3 29 61 33 | ||||||
|  | 51 12 39 97 30 | ||||||
|  | 
 | ||||||
|  |  7 95 28 39 76 | ||||||
|  | 87 31 23 47 75 | ||||||
|  | 88 10 78 24 20 | ||||||
|  | 30 81 22 51 62 | ||||||
|  | 53 93 55 38  0 | ||||||
|  | 
 | ||||||
|  | 12 99  2 89 17 | ||||||
|  | 30 23 92 66 10 | ||||||
|  | 39 60 74 82 15 | ||||||
|  |  1 28 49  0 29 | ||||||
|  | 90 55  9 69 83 | ||||||
|  | 
 | ||||||
|  | 79 44 70 59 88 | ||||||
|  | 90  8 81 23  5 | ||||||
|  | 40 67 66 55 17 | ||||||
|  | 95 61 75 48 91 | ||||||
|  | 98 71 24 38 29 | ||||||
|  | 
 | ||||||
|  | 95 28  8 76 13 | ||||||
|  | 86 21 48  3  6 | ||||||
|  | 34 47 31 50  2 | ||||||
|  | 52 40 77 60 61 | ||||||
|  |  0 88 87 23 25 | ||||||
|  | 
 | ||||||
|  | 63 70 34 91 17 | ||||||
|  | 98 49  8  2 14 | ||||||
|  | 25 22 92 65 18 | ||||||
|  | 78 61 97 73 20 | ||||||
|  | 57 83 16  7 68 | ||||||
|  | 
 | ||||||
|  | 43 23 70 39 16 | ||||||
|  |  0 60 76  7 58 | ||||||
|  | 89 40 38 17  5 | ||||||
|  | 86 50 10 77 37 | ||||||
|  | 26 65 25 69 92 | ||||||
|  | 
 | ||||||
|  | 34 71 92 19 80 | ||||||
|  | 93  6 24 42 45 | ||||||
|  | 96  9 50 85 21 | ||||||
|  | 36 49 13 25 17 | ||||||
|  | 20 98 74 70 57 | ||||||
|  | 
 | ||||||
|  | 25 96 65 77 30 | ||||||
|  | 22 34 41 36 91 | ||||||
|  | 62 18 61 15 19 | ||||||
|  | 42 74 86 58 97 | ||||||
|  | 87 31 53  8 52 | ||||||
|  | 
 | ||||||
|  | 40 37 15 53 91 | ||||||
|  | 14 11 35 49 55 | ||||||
|  | 73 32 83 66 87 | ||||||
|  | 98 31 70 58 88 | ||||||
|  |  7 61  8 76 16 | ||||||
|  | 
 | ||||||
|  | 27 94 87 57 80 | ||||||
|  | 54 35 40 59 72 | ||||||
|  | 88 84 70 98 92 | ||||||
|  | 37 52 45  7 16 | ||||||
|  |  0 30 12 22 41 | ||||||
|  | 
 | ||||||
|  | 44 65 68 14 70 | ||||||
|  |  5 35 17 90  7 | ||||||
|  | 56 89 48 84 32 | ||||||
|  | 73 69 74 51 72 | ||||||
|  | 24 10 94 78 60 | ||||||
|  | 
 | ||||||
|  | 81 15  3 42 90 | ||||||
|  | 54 52 74 84 71 | ||||||
|  | 97 78 20  9  2 | ||||||
|  | 59 66  1 91 87 | ||||||
|  | 70 56 93 47 37 | ||||||
|  | 
 | ||||||
|  | 93 36 19 69 94 | ||||||
|  | 17 20 48 58 52 | ||||||
|  | 85 57 90 42 14 | ||||||
|  | 16 92  4 49 65 | ||||||
|  | 22  9  2 24 44 | ||||||
|  | 
 | ||||||
|  | 47 99 13 31 62 | ||||||
|  | 81 58 88 91 94 | ||||||
|  | 29 11 96 95  1 | ||||||
|  | 14 20 82 34 37 | ||||||
|  | 84 39 76 41 22 | ||||||
|  | 
 | ||||||
|  |  1 74 21  2 67 | ||||||
|  | 38 79 96 26 88 | ||||||
|  | 19 17 94 71 52 | ||||||
|  | 31 28 69  8 51 | ||||||
|  | 41 77 45 95 82 | ||||||
|  | 
 | ||||||
|  | 24  9 94 69 65 | ||||||
|  | 97 84 85 53  5 | ||||||
|  | 92 11 61 77  8 | ||||||
|  | 21 75 33 57 63 | ||||||
|  | 43 68 55 52 93 | ||||||
|  | 
 | ||||||
|  | 27 21  8 75 73 | ||||||
|  |  4 53 23 56 47 | ||||||
|  | 28 94 50 80 19 | ||||||
|  | 89 58 24 12 13 | ||||||
|  | 60  5 99 96  9 | ||||||
|  | 
 | ||||||
|  | 31 43 59 65 33 | ||||||
|  | 51 32 14 58  4 | ||||||
|  | 11 41 70 78 12 | ||||||
|  |  1 25 57 80 49 | ||||||
|  | 91 66  0 27 17 | ||||||
|  | 
 | ||||||
|  | 64 67 37 24 35 | ||||||
|  |  4 22 54 75 21 | ||||||
|  | 19 91  9 52 83 | ||||||
|  | 20 68 53 12  0 | ||||||
|  | 28 76 51 49 89 | ||||||
|  | 
 | ||||||
|  | 67 89 14 54 81 | ||||||
|  |  0 59 51 63 56 | ||||||
|  | 85 88 95  7 36 | ||||||
|  | 40 27 47 86 19 | ||||||
|  | 52 92 22 16 30 | ||||||
|  | 
 | ||||||
|  | 75 25 39  9 19 | ||||||
|  | 59 92 24  6 22 | ||||||
|  | 79 73 34 66 49 | ||||||
|  | 16 89 56 76 55 | ||||||
|  |  5 45  4 46 62 | ||||||
|  | 
 | ||||||
|  | 71 44 63 97 47 | ||||||
|  | 27 61 70 52 46 | ||||||
|  | 19 80 21 68 65 | ||||||
|  | 28 45 84 14 94 | ||||||
|  | 38 73 66 78 92 | ||||||
|  | 
 | ||||||
|  | 47 45 41 96 54 | ||||||
|  | 38 14 62 55 91 | ||||||
|  |  2 11 97 12 51 | ||||||
|  | 36 49  3 95 76 | ||||||
|  |  5 75  7 94 87 | ||||||
|  | 
 | ||||||
|  | 70 24 93 96 86 | ||||||
|  | 49 51 73 50 83 | ||||||
|  | 97  0 57 13  9 | ||||||
|  | 99 46 22 67 39 | ||||||
|  | 56 21 29 52 27 | ||||||
|  | 
 | ||||||
|  | 42 82 80 65 19 | ||||||
|  | 78 41 56 83 75 | ||||||
|  | 51 72 10  1 33 | ||||||
|  | 84 63 21 87 86 | ||||||
|  | 77 64 31 68  6 | ||||||
|  | 
 | ||||||
|  | 60 50 31  5 58 | ||||||
|  | 83  9 87 98 13 | ||||||
|  |  4 35 24 33 88 | ||||||
|  | 54 59 71 64  3 | ||||||
|  | 16 57 48 15 86 | ||||||
|  | 
 | ||||||
|  | 45 29 81 25 14 | ||||||
|  | 13 21 79 90  0 | ||||||
|  | 88 38 56 11 15 | ||||||
|  | 47  2 40 35 75 | ||||||
|  | 91 28 48 32 98 | ||||||
|  | 
 | ||||||
|  | 87 58 78 65 69 | ||||||
|  | 89 35 45 26 13 | ||||||
|  | 28 61 15  3 44 | ||||||
|  | 64 57 92 93 50 | ||||||
|  | 90 39  4 70  9 | ||||||
|  | 
 | ||||||
|  | 16 35 41 40 81 | ||||||
|  | 48 92 94 83 79 | ||||||
|  | 54 50 62  8 53 | ||||||
|  | 14  5 85 68 22 | ||||||
|  | 42 26 33 23 93 | ||||||
|  | 
 | ||||||
|  |  7 13 82 89 49 | ||||||
|  | 43 21 79 38 56 | ||||||
|  |  6 31 90 58 81 | ||||||
|  | 39 47 77 30 54 | ||||||
|  | 23 41 86 19  8 | ||||||
|  | 
 | ||||||
|  | 69 20 95 33 63 | ||||||
|  | 64 34  4 79 36 | ||||||
|  | 13 21 78 56  6 | ||||||
|  | 35 44 85 27 76 | ||||||
|  | 75 15 14 52 39 | ||||||
|  | 
 | ||||||
|  | 42 71 73  1 45 | ||||||
|  | 66 75  7 40 54 | ||||||
|  | 91 83 65 53 20 | ||||||
|  | 34 97 88  5 61 | ||||||
|  | 63 82 50 74 38 | ||||||
|  | 
 | ||||||
|  | 62 89 40 70 91 | ||||||
|  | 84 12 19 96 79 | ||||||
|  | 72 15 35 23 14 | ||||||
|  |  4 69  0 55 17 | ||||||
|  | 85 90 20 28 13 | ||||||
|  | 
 | ||||||
|  | 27 93 23  1 38 | ||||||
|  | 67 28 62  9 96 | ||||||
|  | 31 35 47 44 88 | ||||||
|  | 78 57 53  5 69 | ||||||
|  | 91 15 82 75 61 | ||||||
|  | 
 | ||||||
|  | 17 44 85 92 94 | ||||||
|  |  0 67  5 50 64 | ||||||
|  | 66 65 98 58 56 | ||||||
|  | 62  4 57 99 34 | ||||||
|  | 83 43 76 12 69 | ||||||
|  | 
 | ||||||
|  |  0 28 13 68 86 | ||||||
|  | 84 24 50 32 40 | ||||||
|  | 25 71 72 96 94 | ||||||
|  | 89  1 64 81 23 | ||||||
|  | 97 66  5 15 91 | ||||||
|  | 
 | ||||||
|  | 59 67 79 84 44 | ||||||
|  | 74 61 81 20 68 | ||||||
|  | 24 92 55 99 11 | ||||||
|  | 76 60 97 43 66 | ||||||
|  | 31 30 89 45 53 | ||||||
|  | 
 | ||||||
|  |  1 97 15 34 85 | ||||||
|  | 45 59 54 66 24 | ||||||
|  | 53 36 51 58 27 | ||||||
|  | 84 83 71  5 95 | ||||||
|  | 70  6 65 79 13 | ||||||
|  | 
 | ||||||
|  | 84 86  0 25  2 | ||||||
|  |  1 59 92 39 56 | ||||||
|  | 17  7 88 78 24 | ||||||
|  | 51 87 89 44 31 | ||||||
|  | 54 63 50 18 36 | ||||||
|  | 
 | ||||||
|  | 98 86 30 70 12 | ||||||
|  | 11 52 49 39 14 | ||||||
|  | 16 35 56 87 72 | ||||||
|  | 85 65 93 92 60 | ||||||
|  | 20 43 77 41 79 | ||||||
|  | 
 | ||||||
|  | 50 31 71 78 21 | ||||||
|  | 70 94 99 35 29 | ||||||
|  | 56 58 27 65 28 | ||||||
|  | 45 36 47 69 98 | ||||||
|  |  5 48 61 19 93 | ||||||
|  | 
 | ||||||
|  | 64 65 86 14 53 | ||||||
|  |  7 43 75 39 38 | ||||||
|  | 20 59 80 88 54 | ||||||
|  | 12 32 66 34 87 | ||||||
|  | 29 15 25 19 45 | ||||||
|  | 
 | ||||||
|  | 88 20 42  5 32 | ||||||
|  | 56  8 80 15 98 | ||||||
|  | 36 99 35 27 16 | ||||||
|  | 92 66 75 91 10 | ||||||
|  | 81 96 65  0 57 | ||||||
|  | 
 | ||||||
|  | 29 78  8 76 41 | ||||||
|  | 99 18 60 90 47 | ||||||
|  | 50 51 40  2 31 | ||||||
|  | 38 70 25 52 39 | ||||||
|  | 26 35 84  6 80 | ||||||
|  | 
 | ||||||
|  |  6 68 56 15 53 | ||||||
|  | 99 60 69 25  7 | ||||||
|  | 65 35  9 11 66 | ||||||
|  | 92 85 48 40 97 | ||||||
|  | 63 59 57 17 55 | ||||||
|  | 
 | ||||||
|  | 46 95 75 99 21 | ||||||
|  | 50 24 64 35 63 | ||||||
|  | 93 39  3 67 82 | ||||||
|  | 41 42 84 15 55 | ||||||
|  | 79 81 97 60 17 | ||||||
|  | 
 | ||||||
|  | 33 14 60 42 40 | ||||||
|  | 76 73 56 71 88 | ||||||
|  | 91 41 83 74 16 | ||||||
|  | 57 85 35 44 47 | ||||||
|  | 99 59 46 12 45 | ||||||
|  | 
 | ||||||
|  | 53 83 54 21 68 | ||||||
|  | 79 97 85  0 67 | ||||||
|  | 41 90 48 95  3 | ||||||
|  | 96 70 65 22 25 | ||||||
|  | 60 77 33 15 28 | ||||||
|  | 
 | ||||||
|  | 17 12  5 51 15 | ||||||
|  | 75 92 72 16 65 | ||||||
|  | 59 85 29 23 57 | ||||||
|  | 14 53 97 68 84 | ||||||
|  |  1 93 49 38 28 | ||||||
|  | 
 | ||||||
|  | 27 40 24 12 57 | ||||||
|  | 84 13  9 43 31 | ||||||
|  | 70 23 51 94 34 | ||||||
|  |  1 80 91 16 29 | ||||||
|  | 99 75 49 52 54 | ||||||
|  | 
 | ||||||
|  | 52 85 23 72 40 | ||||||
|  |  6 88 16 41 67 | ||||||
|  | 53 94  8 32 33 | ||||||
|  | 75 62 24 13 64 | ||||||
|  | 65  0 60 86 47 | ||||||
|  | 
 | ||||||
|  |  5 28 27 15 41 | ||||||
|  | 19 77 38 83 45 | ||||||
|  | 32 70 78 26 90 | ||||||
|  | 82 80 85 22 84 | ||||||
|  | 59 73 24  9 63 | ||||||
|  | 
 | ||||||
|  | 29 58 28 82 13 | ||||||
|  | 78 55 63 43 51 | ||||||
|  | 19 33 90 91 48 | ||||||
|  | 93  7 35 22 71 | ||||||
|  | 40 95 38 24 46 | ||||||
|  | 
 | ||||||
|  | 38 30 13 16 74 | ||||||
|  | 69 68 42  6  4 | ||||||
|  | 62 82 29 79  1 | ||||||
|  | 61  7 15 25 85 | ||||||
|  |  5 66 45 43 90 | ||||||
|  | 
 | ||||||
|  | 19 65 12 91 34 | ||||||
|  | 17  6 30 32 64 | ||||||
|  | 37 53  4 35 62 | ||||||
|  | 41 22 13 11 25 | ||||||
|  | 60 27 93 76 51 | ||||||
|  | 
 | ||||||
|  |  3 92 25 88 14 | ||||||
|  | 40 30 55 10 37 | ||||||
|  | 19 94 56 34 74 | ||||||
|  | 75 87 80 54 83 | ||||||
|  |  2 20 70 45 16 | ||||||
|  | 
 | ||||||
|  | 52 93 87 60 11 | ||||||
|  | 82 66 88 59 95 | ||||||
|  | 58 31 49 33 28 | ||||||
|  | 77 39 43  9 51 | ||||||
|  | 20 80 98 47 16 | ||||||
|  | 
 | ||||||
|  | 35 48 47 11 82 | ||||||
|  |  8 36 54 20 40 | ||||||
|  | 90 95 85  4 66 | ||||||
|  | 22 75 64 81 10 | ||||||
|  | 27 62 89 30 12 | ||||||
|  | 
 | ||||||
|  | 75 40 11 63 19 | ||||||
|  |  4 43  6 93 48 | ||||||
|  | 85 58 82 66 52 | ||||||
|  | 32 28  0 14 20 | ||||||
|  | 78 61 83 95 87 | ||||||
|  | 
 | ||||||
|  | 57 79 16 37 33 | ||||||
|  | 20 17 27 38 63 | ||||||
|  | 35 77 60 97 34 | ||||||
|  | 22 78 72 43 26 | ||||||
|  | 29 12  9 46 54 | ||||||
|  | 
 | ||||||
|  | 26 94 37 57  1 | ||||||
|  | 49  6 65 80 55 | ||||||
|  | 46 38 33 89 99 | ||||||
|  | 42 18 86 97 98 | ||||||
|  | 45 76 41  9 12 | ||||||
|  | 
 | ||||||
|  | 83 70 31 61 30 | ||||||
|  | 16 78 84 12 18 | ||||||
|  | 15 65 62 55 98 | ||||||
|  |  6 21 80 41 69 | ||||||
|  | 25  2 24 10 79 | ||||||
|  | 
 | ||||||
|  | 98 75  5 66 37 | ||||||
|  | 90  7 26 61 15 | ||||||
|  | 48 70 20 60 41 | ||||||
|  | 23 58 82 22 74 | ||||||
|  | 80  8 51 67 55 | ||||||
|  | 
 | ||||||
|  | 84 86 77 97 28 | ||||||
|  | 37 87  2 93  5 | ||||||
|  | 16 64 35 61 27 | ||||||
|  |  8  3 36 10 73 | ||||||
|  | 31 65 94 63 13 | ||||||
|  | 
 | ||||||
|  | 91  9 64 67 19 | ||||||
|  | 56 35 11 62 28 | ||||||
|  |  0 65 59 72 45 | ||||||
|  | 34 24 51 26 80 | ||||||
|  | 93 50 58 53 27 | ||||||
|  | 
 | ||||||
|  | 27 54 77 57 94 | ||||||
|  | 60 46 55 74 62 | ||||||
|  | 16  9 19 48  6 | ||||||
|  | 69  1 26 78  2 | ||||||
|  | 45 75 41 25 90 | ||||||
|  | 
 | ||||||
|  | 45 83 97 81 95 | ||||||
|  | 26 64 40 94  7 | ||||||
|  | 57 28 86  8 36 | ||||||
|  | 98 92 16 13 20 | ||||||
|  | 99 79 50 65 51 | ||||||
|  | 
 | ||||||
|  | 38  6 96 71 10 | ||||||
|  | 51 55  2 44 74 | ||||||
|  | 31 61 98 72 73 | ||||||
|  | 79 54 91 34 62 | ||||||
|  | 88 17 46 45 43 | ||||||
|  | 
 | ||||||
|  | 17 18 39 59 26 | ||||||
|  | 45 40 91 47 74 | ||||||
|  | 46 97 94 12 79 | ||||||
|  | 61  7  8 56 50 | ||||||
|  |  0 77 20 57  9 | ||||||
|  | 
 | ||||||
|  | 74  4 65  2 23 | ||||||
|  | 45 56 90 94 96 | ||||||
|  | 80 71 69 86 85 | ||||||
|  | 19 78 35 47 98 | ||||||
|  | 51 73  6 33 14 | ||||||
|  | 
 | ||||||
|  |  8 60 40  2 37 | ||||||
|  | 10 68 44 50 73 | ||||||
|  | 69 26  6 52 93 | ||||||
|  | 33 65 46 24 11 | ||||||
|  | 71 59 15 28 84 | ||||||
|  | 
 | ||||||
|  | 57 65 70 98 68 | ||||||
|  | 80  3 13 39 20 | ||||||
|  | 11 71 47 78 42 | ||||||
|  | 31 61 72 86  9 | ||||||
|  | 53 43 87 28 77 | ||||||
|  | 
 | ||||||
|  | 67 32 59 34 77 | ||||||
|  | 29 23 80 27 62 | ||||||
|  | 81 97 46 14 42 | ||||||
|  | 19 47 44 85 24 | ||||||
|  | 53  9 71 37  1 | ||||||
|  | 
 | ||||||
|  | 83 13 27 41  9 | ||||||
|  | 95 62 65 86 63 | ||||||
|  |  0 17 33 11 76 | ||||||
|  | 45 64 39 71 55 | ||||||
|  | 84 52 21 59 20 | ||||||
|  | 
 | ||||||
|  | 63 45 55 80  3 | ||||||
|  | 14 73 47 96 10 | ||||||
|  | 82 26 85  0 11 | ||||||
|  | 53  6 28 57 60 | ||||||
|  | 49 99 18 50 71 | ||||||
|  | 
 | ||||||
|  | 30 67 16 22 84 | ||||||
|  | 81  4 34 61 65 | ||||||
|  | 57 69 51 94 58 | ||||||
|  |  6 89 37 75 47 | ||||||
|  | 19 14 97  2 86 | ||||||
|  | 
 | ||||||
|  | 64 83  1 66 70 | ||||||
|  | 30 82 96  3 67 | ||||||
|  | 79 11 22 95 14 | ||||||
|  | 87 60  4 15 26 | ||||||
|  | 84 69 99 19 74 | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,4 @@ | ||||||
|  | debug: 100 numbers, 100 boards in play | ||||||
|  | info: [Part 1] Board 38 won with call 75, unmarked value 871, product 65325 | ||||||
|  | info: 99: BoardWinState{ .index = 86, .call = 16, .state = { { true, false, true, false, true }, { true, true, false, true, true }, { true, true, true, true, true }, { true, true, true, true, false }, { false, true, true, true, true } } } | ||||||
|  | info: [Part 2] Board 86 won last with call 16, unmarked value 289, product 4624 | ||||||
|  | @ -0,0 +1,197 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var numbers = std.ArrayList(u8).init(alloc); | ||||||
|  |     defer numbers.deinit(); | ||||||
|  |     var boards = std.ArrayList(Board).init(alloc); | ||||||
|  |     boards.deinit(); | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var number_line = it.next().?; | ||||||
|  |     var number_it = std.mem.tokenize(number_line, ","); | ||||||
|  |     while (number_it.next()) |line| { | ||||||
|  |         try numbers.append(try std.fmt.parseInt(u8, line, 10)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var current_board: Board = try Board.init(alloc); | ||||||
|  |     var current_number: u8 = 0; | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var line_it = std.mem.tokenize(line, " "); | ||||||
|  |         var row = current_number / 5; | ||||||
|  |         while (line_it.next()) |n| { | ||||||
|  |             var col = current_number % 5; | ||||||
|  |             current_number += 1; | ||||||
|  |             current_board.add_number(row, col, try std.fmt.parseInt(u8, n, 10)); | ||||||
|  |         } | ||||||
|  |         if (current_number == 25) { | ||||||
|  |             current_number = 0; | ||||||
|  |             try boards.append(current_board); | ||||||
|  |             current_board = try Board.init(alloc); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.debug("{} numbers, {} boards in play", .{numbers.items.len, | ||||||
|  |                                                      boards.items.len}); | ||||||
|  |     const BoardWinState = struct { | ||||||
|  |         index: usize, | ||||||
|  |         call: u8, | ||||||
|  |         state: [5][5]bool, | ||||||
|  |     }; | ||||||
|  |     var boards_that_have_won = std.AutoHashMap(usize, void).init(alloc); | ||||||
|  |     defer boards_that_have_won.deinit(); | ||||||
|  |     try boards_that_have_won.ensureCapacity(@intCast(u32, boards.items.len)); | ||||||
|  |     var winners = std.ArrayList(BoardWinState).init(alloc); | ||||||
|  |     defer winners.deinit(); | ||||||
|  |     try winners.ensureCapacity(@intCast(u32, boards.items.len)); | ||||||
|  |     for (numbers.items) |value, index| { | ||||||
|  |         // std.log.debug("Running iteration {}, value {}", .{index, value}); | ||||||
|  |         for (boards.items) |board, i| { | ||||||
|  |             if (board.board_numbers.get(value)) |point| { | ||||||
|  |                 //std.log.debug("Board {} has {} at point {any}", | ||||||
|  |                 //              .{i, value, point}); | ||||||
|  |                 boards.items[i].mark_seen(point); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if (index >= 5) { | ||||||
|  |             for (boards.items) |board, i| { | ||||||
|  |                 // Check if we know this winner | ||||||
|  |                 if (boards_that_have_won.get(i)) |b_index| { | ||||||
|  |                     continue; | ||||||
|  |                 } | ||||||
|  |                 if (boards.items[i].has_won()) { | ||||||
|  |                     //std.log.debug("Board {} has just won on iter {} call {}", | ||||||
|  |                     //              .{i, index, value}); | ||||||
|  |                     boards_that_have_won.putAssumeCapacityNoClobber(i, undefined); | ||||||
|  |                     winners.appendAssumeCapacity(.{ | ||||||
|  |                         .index = i, | ||||||
|  |                         .call = value, | ||||||
|  |                         .state = boards.items[i].row_col_marked, | ||||||
|  |                     }); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     // Winner | ||||||
|  |     var winner_value = boards.items[winners.items[0].index].value_at_state( | ||||||
|  |         winners.items[0].state); | ||||||
|  |     std.log.info("[Part 1] Board {} won with call {}, unmarked value {}, product {}", | ||||||
|  |                  .{winners.items[0].index, winners.items[0].call, winner_value, | ||||||
|  |                    winner_value * winners.items[0].call}); | ||||||
|  | 
 | ||||||
|  |     // Last winner | ||||||
|  |     var last_winner = winners.items.len - 1; | ||||||
|  |     winner_value = boards.items[winners.items[last_winner].index].value_at_state( | ||||||
|  |         winners.items[last_winner].state); | ||||||
|  |     std.log.info("{}: {any}", .{last_winner, winners.items[last_winner]}); | ||||||
|  |     std.log.info( | ||||||
|  |         "[Part 2] Board {} won last with call {}, unmarked value {}, product {}", | ||||||
|  |         .{winners.items[last_winner].index, winners.items[last_winner].call, | ||||||
|  |           winner_value, winner_value * winners.items[last_winner].call} | ||||||
|  |     ); | ||||||
|  | 
 | ||||||
|  |     // Cleanup | ||||||
|  |     for (boards.items) |b, i| { | ||||||
|  |         boards.items[i].board_numbers.deinit(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub const Point = struct { | ||||||
|  |     row: u8 = 0, | ||||||
|  |     col: u8 = 0, | ||||||
|  | }; | ||||||
|  | const FullRow = [_] bool { | ||||||
|  |     true, true, true, true, true | ||||||
|  | }; | ||||||
|  | pub const Board = struct { | ||||||
|  |     row_col_marked: [5][5] bool, | ||||||
|  |     board_numbers: std.AutoHashMap(u8, Point), | ||||||
|  |     const Self = @This(); | ||||||
|  |     pub fn init(alloc: *std.mem.Allocator) !Self { | ||||||
|  |         var self = Board { | ||||||
|  |             .row_col_marked = std.mem.zeroes([5][5] bool), | ||||||
|  |             .board_numbers = std.AutoHashMap(u8, Point).init(alloc), | ||||||
|  |         }; | ||||||
|  |         try self.board_numbers.ensureCapacity(25); | ||||||
|  |         return self; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn deinit(alloc: *std.mem.Allocator) void { | ||||||
|  |         self.board_numbers.deinit(alloc); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn add_number(self: *Self, row: u8, col: u8, value: u8) void { | ||||||
|  |         var p = Point { .row = row, .col = col, }; | ||||||
|  |         self.board_numbers.putAssumeCapacityNoClobber(value, p); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn mark_seen(self: *Self, p: Point) void { | ||||||
|  |         self.row_col_marked[p.row][p.col] = true; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn get_value(self: *Self) u32 { | ||||||
|  |         var value: u32 = 0; | ||||||
|  |         var it = self.board_numbers.iterator(); | ||||||
|  |         while (it.next()) |kv| { | ||||||
|  |             if(!self.row_col_marked[kv.value_ptr.*.row][kv.value_ptr.*.col]) { | ||||||
|  |                 value += kv.key_ptr.*; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return value; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn value_at_state(self: *Self, state: [5][5]bool) u32 { | ||||||
|  |         var value: u32 = 0; | ||||||
|  |         var it = self.board_numbers.iterator(); | ||||||
|  |         while (it.next()) |kv| { | ||||||
|  |             if(!state[kv.value_ptr.*.row][kv.value_ptr.*.col]) { | ||||||
|  |                 value += kv.key_ptr.*; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return value; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // pub fn print(self: *Self) void { | ||||||
|  |     //     const stdout = std.io.getStdOut(); | ||||||
|  |     //     const held = std.debug.getStdoutMutex().acquire(); | ||||||
|  |     //     defer held.release(); | ||||||
|  |     //     var r: u8 = 0; | ||||||
|  |     //     while (r < 5) : (r += 1) { | ||||||
|  |     //         stdout.print("{any}", .{self.row_col_marked[r]}); | ||||||
|  |     //     } | ||||||
|  |     // } | ||||||
|  | 
 | ||||||
|  |     pub fn has_won(self: *Self) bool { | ||||||
|  |         var i: usize = 0; | ||||||
|  |         var data: [5]bool = undefined; | ||||||
|  |         while (i < 5) : (i += 1) { | ||||||
|  |             data = self.row_col_marked[i]; | ||||||
|  |             if (std.mem.eql(bool, data[0..], FullRow[0..])) { | ||||||
|  |                 //std.log.debug("Row {} is filled", .{i}); | ||||||
|  |                 return true; | ||||||
|  |             } | ||||||
|  |             data = [_] bool { | ||||||
|  |                 self.row_col_marked[0][i], | ||||||
|  |                 self.row_col_marked[1][i], | ||||||
|  |                 self.row_col_marked[2][i], | ||||||
|  |                 self.row_col_marked[3][i], | ||||||
|  |                 self.row_col_marked[4][i], | ||||||
|  |             }; | ||||||
|  |             //std.log.debug("Row / Col {}: {any} / {any}", | ||||||
|  |             //              .{i, self.row_col_marked[i], data}); | ||||||
|  |             if (std.mem.eql(bool, data[0..], FullRow[0..])) { | ||||||
|  |                 //std.log.debug("Col {} is filled", .{i}); | ||||||
|  |                 return true; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day5", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,500 @@ | ||||||
|  | 959,103 -> 139,923 | ||||||
|  | 899,63 -> 899,53 | ||||||
|  | 591,871 -> 364,644 | ||||||
|  | 739,913 -> 310,484 | ||||||
|  | 460,309 -> 460,705 | ||||||
|  | 351,389 -> 351,837 | ||||||
|  | 420,55 -> 420,541 | ||||||
|  | 916,520 -> 382,520 | ||||||
|  | 136,604 -> 295,604 | ||||||
|  | 343,59 -> 142,59 | ||||||
|  | 951,206 -> 806,206 | ||||||
|  | 984,950 -> 61,27 | ||||||
|  | 739,388 -> 988,388 | ||||||
|  | 374,370 -> 644,370 | ||||||
|  | 403,504 -> 798,899 | ||||||
|  | 943,535 -> 229,535 | ||||||
|  | 149,227 -> 583,661 | ||||||
|  | 62,126 -> 62,352 | ||||||
|  | 131,391 -> 131,717 | ||||||
|  | 254,944 -> 254,220 | ||||||
|  | 572,111 -> 572,47 | ||||||
|  | 336,249 -> 830,743 | ||||||
|  | 898,858 -> 203,163 | ||||||
|  | 203,320 -> 825,942 | ||||||
|  | 19,318 -> 19,120 | ||||||
|  | 740,432 -> 740,39 | ||||||
|  | 591,383 -> 220,754 | ||||||
|  | 445,592 -> 19,592 | ||||||
|  | 202,370 -> 837,370 | ||||||
|  | 473,916 -> 600,789 | ||||||
|  | 118,955 -> 884,189 | ||||||
|  | 377,376 -> 533,532 | ||||||
|  | 160,264 -> 160,62 | ||||||
|  | 627,572 -> 627,679 | ||||||
|  | 217,690 -> 217,629 | ||||||
|  | 685,323 -> 866,504 | ||||||
|  | 391,309 -> 493,207 | ||||||
|  | 872,776 -> 357,776 | ||||||
|  | 116,326 -> 116,426 | ||||||
|  | 82,900 -> 832,900 | ||||||
|  | 594,862 -> 594,593 | ||||||
|  | 802,636 -> 802,223 | ||||||
|  | 862,226 -> 862,787 | ||||||
|  | 313,573 -> 834,573 | ||||||
|  | 145,631 -> 13,499 | ||||||
|  | 545,433 -> 420,308 | ||||||
|  | 427,623 -> 427,808 | ||||||
|  | 898,120 -> 511,120 | ||||||
|  | 859,811 -> 859,28 | ||||||
|  | 715,958 -> 715,893 | ||||||
|  | 115,234 -> 484,234 | ||||||
|  | 125,253 -> 50,253 | ||||||
|  | 737,265 -> 158,265 | ||||||
|  | 523,965 -> 523,983 | ||||||
|  | 118,51 -> 118,766 | ||||||
|  | 455,774 -> 455,357 | ||||||
|  | 680,881 -> 925,881 | ||||||
|  | 191,186 -> 187,186 | ||||||
|  | 821,629 -> 792,658 | ||||||
|  | 397,906 -> 397,962 | ||||||
|  | 988,811 -> 988,427 | ||||||
|  | 91,733 -> 519,733 | ||||||
|  | 46,172 -> 566,172 | ||||||
|  | 740,619 -> 880,759 | ||||||
|  | 609,465 -> 609,702 | ||||||
|  | 289,211 -> 289,620 | ||||||
|  | 622,135 -> 622,929 | ||||||
|  | 113,53 -> 872,53 | ||||||
|  | 559,713 -> 559,132 | ||||||
|  | 894,237 -> 211,920 | ||||||
|  | 237,259 -> 237,39 | ||||||
|  | 738,542 -> 976,542 | ||||||
|  | 163,34 -> 525,34 | ||||||
|  | 681,672 -> 264,255 | ||||||
|  | 37,827 -> 722,827 | ||||||
|  | 60,803 -> 514,349 | ||||||
|  | 433,866 -> 433,257 | ||||||
|  | 379,493 -> 379,643 | ||||||
|  | 697,588 -> 192,83 | ||||||
|  | 875,646 -> 318,89 | ||||||
|  | 634,983 -> 634,111 | ||||||
|  | 636,69 -> 636,41 | ||||||
|  | 911,780 -> 701,570 | ||||||
|  | 792,71 -> 956,71 | ||||||
|  | 682,338 -> 608,412 | ||||||
|  | 257,768 -> 450,575 | ||||||
|  | 112,25 -> 795,708 | ||||||
|  | 730,86 -> 730,65 | ||||||
|  | 966,785 -> 789,608 | ||||||
|  | 390,263 -> 483,356 | ||||||
|  | 90,852 -> 90,471 | ||||||
|  | 507,914 -> 769,914 | ||||||
|  | 803,535 -> 803,245 | ||||||
|  | 710,787 -> 570,787 | ||||||
|  | 138,842 -> 270,710 | ||||||
|  | 862,988 -> 862,656 | ||||||
|  | 56,408 -> 849,408 | ||||||
|  | 16,10 -> 979,973 | ||||||
|  | 982,14 -> 12,984 | ||||||
|  | 647,915 -> 38,306 | ||||||
|  | 797,487 -> 19,487 | ||||||
|  | 539,933 -> 924,933 | ||||||
|  | 509,734 -> 176,734 | ||||||
|  | 813,505 -> 976,505 | ||||||
|  | 474,987 -> 474,896 | ||||||
|  | 21,200 -> 164,200 | ||||||
|  | 986,973 -> 31,18 | ||||||
|  | 919,830 -> 111,22 | ||||||
|  | 32,574 -> 456,150 | ||||||
|  | 743,595 -> 842,595 | ||||||
|  | 623,306 -> 722,306 | ||||||
|  | 878,367 -> 519,367 | ||||||
|  | 924,221 -> 924,231 | ||||||
|  | 86,950 -> 773,263 | ||||||
|  | 950,248 -> 537,248 | ||||||
|  | 149,155 -> 962,968 | ||||||
|  | 449,568 -> 179,568 | ||||||
|  | 186,304 -> 868,986 | ||||||
|  | 921,320 -> 639,602 | ||||||
|  | 602,262 -> 602,500 | ||||||
|  | 602,33 -> 602,248 | ||||||
|  | 380,731 -> 423,774 | ||||||
|  | 535,110 -> 638,110 | ||||||
|  | 552,317 -> 552,75 | ||||||
|  | 173,667 -> 173,847 | ||||||
|  | 707,480 -> 195,480 | ||||||
|  | 833,398 -> 267,964 | ||||||
|  | 276,716 -> 413,716 | ||||||
|  | 342,816 -> 922,816 | ||||||
|  | 24,184 -> 715,875 | ||||||
|  | 762,330 -> 717,285 | ||||||
|  | 718,886 -> 718,551 | ||||||
|  | 707,834 -> 707,704 | ||||||
|  | 479,578 -> 161,896 | ||||||
|  | 145,297 -> 145,435 | ||||||
|  | 760,651 -> 536,875 | ||||||
|  | 954,629 -> 954,816 | ||||||
|  | 305,949 -> 305,919 | ||||||
|  | 55,132 -> 55,233 | ||||||
|  | 469,85 -> 439,85 | ||||||
|  | 653,990 -> 536,990 | ||||||
|  | 876,531 -> 432,87 | ||||||
|  | 698,207 -> 698,672 | ||||||
|  | 11,70 -> 766,825 | ||||||
|  | 591,357 -> 30,918 | ||||||
|  | 697,987 -> 697,823 | ||||||
|  | 610,903 -> 370,663 | ||||||
|  | 319,678 -> 319,504 | ||||||
|  | 337,150 -> 309,150 | ||||||
|  | 876,57 -> 311,57 | ||||||
|  | 673,268 -> 345,596 | ||||||
|  | 895,364 -> 518,741 | ||||||
|  | 327,662 -> 941,48 | ||||||
|  | 77,709 -> 110,742 | ||||||
|  | 194,78 -> 661,78 | ||||||
|  | 587,24 -> 825,24 | ||||||
|  | 503,317 -> 719,317 | ||||||
|  | 459,632 -> 704,387 | ||||||
|  | 717,292 -> 835,292 | ||||||
|  | 912,927 -> 72,87 | ||||||
|  | 510,527 -> 146,527 | ||||||
|  | 336,771 -> 336,266 | ||||||
|  | 566,961 -> 496,961 | ||||||
|  | 969,335 -> 122,335 | ||||||
|  | 925,443 -> 925,397 | ||||||
|  | 316,812 -> 606,812 | ||||||
|  | 815,795 -> 116,795 | ||||||
|  | 169,36 -> 354,36 | ||||||
|  | 358,274 -> 389,274 | ||||||
|  | 302,147 -> 839,684 | ||||||
|  | 762,372 -> 972,372 | ||||||
|  | 172,721 -> 682,211 | ||||||
|  | 265,150 -> 248,167 | ||||||
|  | 753,559 -> 307,559 | ||||||
|  | 823,121 -> 823,126 | ||||||
|  | 498,856 -> 498,135 | ||||||
|  | 75,977 -> 75,381 | ||||||
|  | 541,297 -> 541,320 | ||||||
|  | 735,108 -> 866,108 | ||||||
|  | 434,907 -> 868,907 | ||||||
|  | 915,959 -> 255,959 | ||||||
|  | 967,666 -> 967,209 | ||||||
|  | 361,600 -> 361,222 | ||||||
|  | 314,580 -> 314,497 | ||||||
|  | 175,989 -> 523,641 | ||||||
|  | 957,97 -> 311,743 | ||||||
|  | 956,227 -> 12,227 | ||||||
|  | 95,364 -> 95,742 | ||||||
|  | 857,141 -> 193,805 | ||||||
|  | 388,651 -> 468,731 | ||||||
|  | 582,177 -> 324,177 | ||||||
|  | 68,272 -> 68,720 | ||||||
|  | 543,490 -> 910,490 | ||||||
|  | 508,281 -> 902,281 | ||||||
|  | 823,380 -> 823,296 | ||||||
|  | 23,10 -> 946,933 | ||||||
|  | 813,70 -> 813,450 | ||||||
|  | 881,893 -> 598,893 | ||||||
|  | 535,781 -> 973,781 | ||||||
|  | 80,890 -> 909,61 | ||||||
|  | 604,630 -> 307,927 | ||||||
|  | 836,917 -> 184,917 | ||||||
|  | 76,727 -> 10,727 | ||||||
|  | 727,235 -> 727,578 | ||||||
|  | 629,80 -> 892,80 | ||||||
|  | 110,655 -> 663,102 | ||||||
|  | 985,12 -> 11,986 | ||||||
|  | 830,656 -> 830,761 | ||||||
|  | 660,869 -> 660,543 | ||||||
|  | 381,340 -> 381,562 | ||||||
|  | 392,735 -> 417,735 | ||||||
|  | 855,24 -> 320,24 | ||||||
|  | 801,669 -> 278,146 | ||||||
|  | 730,964 -> 107,964 | ||||||
|  | 523,158 -> 385,20 | ||||||
|  | 27,833 -> 27,987 | ||||||
|  | 569,707 -> 500,707 | ||||||
|  | 527,732 -> 527,424 | ||||||
|  | 74,88 -> 273,287 | ||||||
|  | 143,974 -> 143,735 | ||||||
|  | 247,388 -> 813,954 | ||||||
|  | 577,14 -> 945,382 | ||||||
|  | 49,43 -> 953,947 | ||||||
|  | 332,210 -> 332,143 | ||||||
|  | 69,280 -> 949,280 | ||||||
|  | 25,923 -> 904,44 | ||||||
|  | 306,569 -> 306,470 | ||||||
|  | 158,273 -> 113,228 | ||||||
|  | 771,355 -> 694,278 | ||||||
|  | 515,115 -> 245,385 | ||||||
|  | 427,381 -> 427,729 | ||||||
|  | 16,987 -> 987,16 | ||||||
|  | 319,463 -> 319,234 | ||||||
|  | 854,977 -> 66,189 | ||||||
|  | 794,194 -> 794,183 | ||||||
|  | 576,65 -> 576,843 | ||||||
|  | 37,964 -> 734,964 | ||||||
|  | 740,920 -> 740,877 | ||||||
|  | 245,487 -> 245,957 | ||||||
|  | 404,794 -> 853,794 | ||||||
|  | 660,656 -> 660,756 | ||||||
|  | 921,605 -> 127,605 | ||||||
|  | 650,894 -> 916,894 | ||||||
|  | 968,893 -> 481,406 | ||||||
|  | 986,979 -> 21,14 | ||||||
|  | 154,303 -> 498,647 | ||||||
|  | 720,338 -> 229,338 | ||||||
|  | 62,936 -> 62,897 | ||||||
|  | 55,820 -> 55,923 | ||||||
|  | 812,31 -> 551,31 | ||||||
|  | 338,466 -> 951,466 | ||||||
|  | 663,492 -> 775,604 | ||||||
|  | 449,602 -> 39,602 | ||||||
|  | 44,403 -> 44,144 | ||||||
|  | 58,62 -> 339,62 | ||||||
|  | 713,730 -> 713,502 | ||||||
|  | 704,525 -> 976,797 | ||||||
|  | 372,709 -> 372,680 | ||||||
|  | 709,387 -> 153,387 | ||||||
|  | 922,103 -> 615,103 | ||||||
|  | 629,839 -> 121,839 | ||||||
|  | 206,722 -> 529,722 | ||||||
|  | 232,556 -> 422,746 | ||||||
|  | 300,470 -> 300,726 | ||||||
|  | 376,820 -> 622,574 | ||||||
|  | 834,25 -> 255,604 | ||||||
|  | 271,200 -> 271,875 | ||||||
|  | 804,934 -> 872,934 | ||||||
|  | 900,753 -> 900,632 | ||||||
|  | 604,323 -> 604,70 | ||||||
|  | 890,911 -> 890,41 | ||||||
|  | 464,169 -> 812,169 | ||||||
|  | 850,196 -> 850,903 | ||||||
|  | 34,574 -> 34,54 | ||||||
|  | 718,59 -> 462,315 | ||||||
|  | 431,923 -> 737,923 | ||||||
|  | 433,573 -> 433,420 | ||||||
|  | 297,478 -> 297,775 | ||||||
|  | 756,545 -> 544,545 | ||||||
|  | 247,708 -> 247,702 | ||||||
|  | 736,835 -> 173,272 | ||||||
|  | 319,85 -> 319,827 | ||||||
|  | 931,775 -> 683,775 | ||||||
|  | 292,315 -> 451,315 | ||||||
|  | 397,435 -> 380,435 | ||||||
|  | 987,978 -> 82,73 | ||||||
|  | 227,349 -> 227,724 | ||||||
|  | 349,741 -> 899,191 | ||||||
|  | 965,325 -> 765,125 | ||||||
|  | 849,306 -> 88,306 | ||||||
|  | 516,548 -> 516,902 | ||||||
|  | 919,395 -> 568,395 | ||||||
|  | 736,507 -> 192,507 | ||||||
|  | 960,782 -> 196,18 | ||||||
|  | 431,413 -> 510,492 | ||||||
|  | 911,696 -> 911,830 | ||||||
|  | 888,225 -> 174,225 | ||||||
|  | 57,790 -> 57,953 | ||||||
|  | 858,399 -> 119,399 | ||||||
|  | 59,302 -> 290,302 | ||||||
|  | 456,907 -> 456,599 | ||||||
|  | 374,743 -> 374,565 | ||||||
|  | 183,107 -> 183,171 | ||||||
|  | 58,699 -> 288,699 | ||||||
|  | 886,970 -> 109,193 | ||||||
|  | 940,395 -> 806,261 | ||||||
|  | 781,480 -> 596,665 | ||||||
|  | 456,724 -> 265,724 | ||||||
|  | 414,406 -> 299,521 | ||||||
|  | 115,898 -> 115,863 | ||||||
|  | 34,543 -> 34,496 | ||||||
|  | 900,843 -> 900,457 | ||||||
|  | 165,209 -> 189,209 | ||||||
|  | 976,627 -> 539,190 | ||||||
|  | 252,202 -> 137,202 | ||||||
|  | 584,339 -> 550,373 | ||||||
|  | 580,153 -> 380,353 | ||||||
|  | 232,412 -> 650,830 | ||||||
|  | 910,833 -> 88,11 | ||||||
|  | 418,245 -> 829,245 | ||||||
|  | 298,823 -> 907,214 | ||||||
|  | 91,876 -> 495,876 | ||||||
|  | 315,874 -> 650,539 | ||||||
|  | 907,635 -> 365,635 | ||||||
|  | 339,313 -> 320,313 | ||||||
|  | 362,435 -> 362,938 | ||||||
|  | 152,664 -> 152,391 | ||||||
|  | 253,210 -> 272,210 | ||||||
|  | 216,396 -> 216,726 | ||||||
|  | 852,912 -> 15,75 | ||||||
|  | 882,828 -> 689,828 | ||||||
|  | 674,533 -> 674,523 | ||||||
|  | 469,719 -> 469,79 | ||||||
|  | 733,169 -> 665,101 | ||||||
|  | 734,632 -> 717,632 | ||||||
|  | 615,565 -> 615,114 | ||||||
|  | 979,720 -> 243,720 | ||||||
|  | 827,125 -> 827,919 | ||||||
|  | 605,419 -> 601,419 | ||||||
|  | 749,13 -> 433,329 | ||||||
|  | 990,902 -> 990,843 | ||||||
|  | 186,679 -> 186,457 | ||||||
|  | 374,796 -> 736,796 | ||||||
|  | 133,867 -> 133,801 | ||||||
|  | 757,622 -> 812,567 | ||||||
|  | 351,179 -> 351,509 | ||||||
|  | 214,748 -> 575,748 | ||||||
|  | 177,903 -> 861,219 | ||||||
|  | 747,981 -> 747,64 | ||||||
|  | 588,125 -> 588,557 | ||||||
|  | 464,338 -> 769,338 | ||||||
|  | 645,669 -> 125,149 | ||||||
|  | 579,352 -> 138,352 | ||||||
|  | 77,605 -> 520,605 | ||||||
|  | 698,816 -> 698,917 | ||||||
|  | 112,943 -> 112,834 | ||||||
|  | 731,720 -> 724,720 | ||||||
|  | 887,440 -> 976,351 | ||||||
|  | 676,301 -> 676,741 | ||||||
|  | 870,732 -> 870,648 | ||||||
|  | 250,826 -> 413,826 | ||||||
|  | 399,720 -> 543,864 | ||||||
|  | 834,93 -> 468,459 | ||||||
|  | 415,475 -> 415,641 | ||||||
|  | 793,415 -> 47,415 | ||||||
|  | 365,476 -> 365,31 | ||||||
|  | 195,154 -> 813,154 | ||||||
|  | 503,605 -> 773,605 | ||||||
|  | 553,121 -> 851,121 | ||||||
|  | 25,420 -> 423,818 | ||||||
|  | 943,110 -> 258,110 | ||||||
|  | 775,436 -> 826,436 | ||||||
|  | 16,161 -> 16,889 | ||||||
|  | 702,555 -> 920,555 | ||||||
|  | 589,858 -> 533,802 | ||||||
|  | 932,404 -> 932,539 | ||||||
|  | 647,275 -> 647,962 | ||||||
|  | 87,179 -> 326,179 | ||||||
|  | 931,588 -> 931,287 | ||||||
|  | 868,96 -> 557,96 | ||||||
|  | 879,28 -> 875,28 | ||||||
|  | 375,132 -> 287,44 | ||||||
|  | 484,352 -> 644,512 | ||||||
|  | 448,566 -> 448,214 | ||||||
|  | 734,460 -> 717,460 | ||||||
|  | 550,379 -> 550,674 | ||||||
|  | 964,184 -> 820,328 | ||||||
|  | 167,504 -> 387,504 | ||||||
|  | 594,777 -> 952,777 | ||||||
|  | 328,712 -> 837,712 | ||||||
|  | 600,773 -> 546,773 | ||||||
|  | 955,954 -> 82,81 | ||||||
|  | 863,790 -> 863,86 | ||||||
|  | 831,773 -> 32,773 | ||||||
|  | 987,11 -> 19,979 | ||||||
|  | 901,878 -> 901,177 | ||||||
|  | 427,341 -> 721,635 | ||||||
|  | 690,835 -> 567,835 | ||||||
|  | 557,724 -> 14,181 | ||||||
|  | 591,20 -> 205,406 | ||||||
|  | 846,865 -> 846,859 | ||||||
|  | 644,646 -> 742,548 | ||||||
|  | 187,376 -> 187,563 | ||||||
|  | 367,806 -> 250,923 | ||||||
|  | 332,731 -> 468,731 | ||||||
|  | 378,431 -> 469,431 | ||||||
|  | 844,949 -> 844,452 | ||||||
|  | 172,320 -> 735,320 | ||||||
|  | 597,639 -> 633,639 | ||||||
|  | 353,831 -> 353,307 | ||||||
|  | 355,392 -> 465,392 | ||||||
|  | 624,179 -> 548,255 | ||||||
|  | 441,928 -> 401,888 | ||||||
|  | 442,680 -> 442,569 | ||||||
|  | 567,385 -> 908,44 | ||||||
|  | 10,561 -> 603,561 | ||||||
|  | 851,289 -> 13,289 | ||||||
|  | 832,143 -> 832,64 | ||||||
|  | 366,851 -> 67,851 | ||||||
|  | 890,404 -> 333,961 | ||||||
|  | 83,22 -> 963,902 | ||||||
|  | 10,783 -> 821,783 | ||||||
|  | 369,481 -> 369,611 | ||||||
|  | 943,356 -> 846,356 | ||||||
|  | 675,95 -> 335,435 | ||||||
|  | 442,928 -> 442,764 | ||||||
|  | 500,643 -> 334,643 | ||||||
|  | 90,207 -> 620,207 | ||||||
|  | 520,412 -> 745,187 | ||||||
|  | 586,89 -> 613,89 | ||||||
|  | 411,424 -> 595,424 | ||||||
|  | 938,650 -> 232,650 | ||||||
|  | 216,773 -> 76,773 | ||||||
|  | 895,690 -> 895,294 | ||||||
|  | 250,886 -> 250,605 | ||||||
|  | 296,422 -> 863,989 | ||||||
|  | 534,626 -> 534,707 | ||||||
|  | 577,608 -> 52,83 | ||||||
|  | 61,674 -> 714,21 | ||||||
|  | 844,126 -> 844,694 | ||||||
|  | 565,541 -> 253,229 | ||||||
|  | 62,24 -> 986,948 | ||||||
|  | 588,901 -> 588,212 | ||||||
|  | 541,508 -> 541,141 | ||||||
|  | 516,376 -> 589,449 | ||||||
|  | 390,215 -> 749,215 | ||||||
|  | 324,878 -> 296,850 | ||||||
|  | 592,408 -> 592,158 | ||||||
|  | 433,207 -> 172,207 | ||||||
|  | 139,72 -> 139,121 | ||||||
|  | 471,676 -> 268,676 | ||||||
|  | 374,433 -> 374,95 | ||||||
|  | 672,459 -> 640,427 | ||||||
|  | 348,577 -> 843,82 | ||||||
|  | 903,466 -> 903,348 | ||||||
|  | 437,759 -> 726,470 | ||||||
|  | 152,101 -> 325,274 | ||||||
|  | 933,897 -> 335,897 | ||||||
|  | 516,877 -> 505,866 | ||||||
|  | 890,715 -> 570,715 | ||||||
|  | 78,124 -> 871,917 | ||||||
|  | 360,645 -> 967,645 | ||||||
|  | 645,271 -> 645,57 | ||||||
|  | 693,878 -> 693,159 | ||||||
|  | 49,77 -> 49,744 | ||||||
|  | 935,914 -> 97,76 | ||||||
|  | 941,726 -> 941,464 | ||||||
|  | 756,985 -> 756,480 | ||||||
|  | 887,378 -> 887,529 | ||||||
|  | 405,925 -> 405,533 | ||||||
|  | 533,156 -> 201,156 | ||||||
|  | 565,535 -> 120,90 | ||||||
|  | 51,15 -> 967,931 | ||||||
|  | 660,218 -> 660,339 | ||||||
|  | 522,682 -> 571,682 | ||||||
|  | 958,899 -> 729,899 | ||||||
|  | 521,687 -> 288,687 | ||||||
|  | 643,148 -> 468,323 | ||||||
|  | 989,971 -> 68,50 | ||||||
|  | 729,273 -> 311,691 | ||||||
|  | 245,205 -> 305,205 | ||||||
|  | 634,747 -> 634,605 | ||||||
|  | 280,407 -> 488,199 | ||||||
|  | 109,931 -> 706,334 | ||||||
|  | 849,694 -> 615,928 | ||||||
|  | 794,84 -> 218,84 | ||||||
|  | 669,184 -> 865,184 | ||||||
|  | 936,834 -> 234,132 | ||||||
|  | 691,445 -> 914,668 | ||||||
|  | 423,161 -> 515,69 | ||||||
|  | 81,674 -> 37,674 | ||||||
|  | 292,423 -> 292,741 | ||||||
|  | 188,306 -> 844,962 | ||||||
|  | 204,309 -> 204,705 | ||||||
|  | 961,652 -> 746,652 | ||||||
|  | 985,987 -> 11,13 | ||||||
|  | 139,153 -> 936,950 | ||||||
|  | 436,978 -> 244,978 | ||||||
|  | 921,633 -> 921,340 | ||||||
|  | 872,63 -> 233,63 | ||||||
|  | @ -0,0 +1,128 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var points = std.AutoHashMap(Point, u8).init(alloc); | ||||||
|  |     defer points.deinit(); | ||||||
|  |     var points_p2 = std.AutoHashMap(Point, u8).init(alloc); | ||||||
|  |     defer points_p2.deinit(); | ||||||
|  | 
 | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var begin: Point = undefined; | ||||||
|  |         var end: Point = undefined; | ||||||
|  |         var lit = std.mem.tokenize(line, " "); | ||||||
|  |         var iter: u8 = 0; | ||||||
|  |         var diagonal: bool = false; | ||||||
|  |         while (lit.next()) |segment| { | ||||||
|  |             if (iter % 2 == 0) { | ||||||
|  |                 var sit = std.mem.tokenize(segment, ","); | ||||||
|  |                 var x: i16 = try std.fmt.parseInt(i16, sit.next().?, 10); | ||||||
|  |                 var y: i16 = try std.fmt.parseInt(i16, sit.next().?, 10); | ||||||
|  |                 if (iter == 0) { | ||||||
|  |                     begin = .{ .x = x, .y = y }; | ||||||
|  |                 } | ||||||
|  |                 else { | ||||||
|  |                     end = .{ .x = x, .y = y }; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             iter += 1; | ||||||
|  |         } | ||||||
|  |         var step = Point { .x = 0, .y = 0}; | ||||||
|  |         var steps_required: i16 = 0; | ||||||
|  |         if (begin.x != end.x and begin.y != end.y) { | ||||||
|  |             // Diagonal line | ||||||
|  |             diagonal = true; | ||||||
|  |             step = Point { .x = end.x - begin.x, .y = end.y - begin.y }; | ||||||
|  |             //std.log.warn("Diagonal line from {any} to {any} detected, step start {}", | ||||||
|  |             //             .{begin, end, step}); | ||||||
|  |             var sx = try std.math.absInt(step.x); | ||||||
|  |             var sy = try std.math.absInt(step.y); | ||||||
|  |             std.debug.assert(sx == sy); | ||||||
|  |             // This means our step is always +/-1,+/-1. Based on the input, | ||||||
|  |             // there aren't diagonals that are +/-N,+/-N2 where N, N2 != 1 | ||||||
|  |             steps_required = sx; | ||||||
|  |             step = .{ .x = @divFloor(step.x, sx), .y = @divFloor(step.y, sx) }; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             step = Point { .x = end.x - begin.x, .y = end.y - begin.y }; | ||||||
|  |             steps_required = try std.math.absInt(step.x + step.y ); | ||||||
|  |             if (step.x != 0) { | ||||||
|  |                 step.x = @divFloor(step.x, try std.math.absInt(step.x)); | ||||||
|  |             } | ||||||
|  |             if (step.y != 0) { | ||||||
|  |                 step.y = @divFloor(step.y, try std.math.absInt(step.y)); | ||||||
|  |             } | ||||||
|  |             //std.log.debug("Line {any} to {any} step is {any}", | ||||||
|  |             //              .{begin, end, step}); | ||||||
|  |         } | ||||||
|  |         var p: Point = begin; | ||||||
|  |         var step_iter: u16 = 0; | ||||||
|  |         while (step_iter <= steps_required) : (step_iter += 1) { | ||||||
|  |             //std.log.debug("Adding point {any}", .{p}); | ||||||
|  |             try register_point(&points_p2, p); | ||||||
|  |             if (!diagonal) { | ||||||
|  |                 try register_point(&points, p); | ||||||
|  |             } | ||||||
|  |             p = p.add(step); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Iterate over our points | ||||||
|  |     var n_dangerous_points: u32 = 0; | ||||||
|  |     var pit = points.iterator(); | ||||||
|  |     while (pit.next()) |kv| { | ||||||
|  |         if (kv.value_ptr.* >= 2) { | ||||||
|  |             n_dangerous_points += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] There are {} dangerous points", .{n_dangerous_points}); | ||||||
|  | 
 | ||||||
|  |     n_dangerous_points = 0; | ||||||
|  |     pit = points_p2.iterator(); | ||||||
|  |     while (pit.next()) |kv| { | ||||||
|  |         if (kv.value_ptr.* >= 2) { | ||||||
|  |             n_dangerous_points += 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 2] There are {} dangerous points", .{n_dangerous_points}); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn register_point(points: *std.AutoHashMap(Point, u8),p: Point) !void { | ||||||
|  |     if (points.*.get(p)) |n| { | ||||||
|  |         try points.*.put(p, n+1); | ||||||
|  |     } | ||||||
|  |     else { | ||||||
|  |         try points.*.put(p, 1); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub const Point = struct { | ||||||
|  |     x: i16, | ||||||
|  |     y: i16, | ||||||
|  | 
 | ||||||
|  |     pub fn eql(p1: Point, p2: Point) bool { | ||||||
|  |         return p1.x == p2.x and p1.y == p2.y; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub fn add(p1: Point, p2: Point) Point { | ||||||
|  |         return .{ | ||||||
|  |             .x = p1.x + p2.x, | ||||||
|  |             .y = p1.y + p2.y, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | pub const Line = struct { | ||||||
|  |     a: Point, | ||||||
|  |     b: Point, | ||||||
|  | }; | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day6", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | 1,1,1,1,1,5,1,1,1,5,1,1,3,1,5,1,4,1,5,1,2,5,1,1,1,1,3,1,4,5,1,1,2,1,1,1,2,4,3,2,1,1,2,1,5,4,4,1,4,1,1,1,4,1,3,1,1,1,2,1,1,1,1,1,1,1,5,4,4,2,4,5,2,1,5,3,1,3,3,1,1,5,4,1,1,3,5,1,1,1,4,4,2,4,1,1,4,1,1,2,1,1,1,2,1,5,2,5,1,1,1,4,1,2,1,1,1,2,2,1,3,1,4,4,1,1,3,1,4,1,1,1,2,5,5,1,4,1,4,4,1,4,1,2,4,1,1,4,1,3,4,4,1,1,5,3,1,1,5,1,3,4,2,1,3,1,3,1,1,1,1,1,1,1,1,1,4,5,1,1,1,1,3,1,1,5,1,1,4,1,1,3,1,1,5,2,1,4,4,1,4,1,2,1,1,1,1,2,1,4,1,1,2,5,1,4,4,1,1,1,4,1,1,1,5,3,1,4,1,4,1,1,3,5,3,5,5,5,1,5,1,1,1,1,1,1,1,1,2,3,3,3,3,4,2,1,1,4,5,3,1,1,5,5,1,1,2,1,4,1,3,5,1,1,1,5,2,2,1,4,2,1,1,4,1,3,1,1,1,3,1,5,1,5,1,1,4,1,2,1 | ||||||
|  | @ -0,0 +1,100 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  |     var it = std.mem.tokenize(contents, ","); | ||||||
|  | 
 | ||||||
|  |     // not unsurprisingly, tracking all the fish individually | ||||||
|  |     // runs out of memory. maybe not? I might have misunderstood | ||||||
|  |     // an error regarding parsing of a integer with a string | ||||||
|  |     // that has a newline in it. | ||||||
|  |     // good thing I abandoned this approach, since part 2 | ||||||
|  |     // clearly is larger than the memory I have available. | ||||||
|  |     // var fish = std.ArrayList(i8).init(alloc); | ||||||
|  |     // defer fish.deinit(); | ||||||
|  | 
 | ||||||
|  |     // while (it.next()) |fi| { | ||||||
|  |     //     try fish.append(try std.fmt.parseInt(i8, fi, 10)); | ||||||
|  |     // } | ||||||
|  | 
 | ||||||
|  |     // var n_cycles: u8 = 0; | ||||||
|  |     // while (n_cycles < 80) : (n_cycles += 1) { | ||||||
|  |     //     var i: usize = 0; | ||||||
|  |     //     var len = fish.items.len; | ||||||
|  |     //     while (i < len) { | ||||||
|  |     //         fish.items[i] -= 1; | ||||||
|  |     //         if (fish.items[i] < 0) { | ||||||
|  |     //             fish.items[i] = 6; | ||||||
|  |     //             try fish.append(8); | ||||||
|  |     //         } | ||||||
|  |     //     } | ||||||
|  |     // } | ||||||
|  | 
 | ||||||
|  |     // std.log.info("[Part 1] After {} cycles, there are {} fish", | ||||||
|  |     //              .{n_cycles, fish.items.len}); | ||||||
|  | 
 | ||||||
|  |     var days = [_] u64 { | ||||||
|  |         0, // 0: ready to spawn | ||||||
|  |         0, // 1: day 0 | ||||||
|  |         0, // ... | ||||||
|  |         0, | ||||||
|  |         0, | ||||||
|  |         0, | ||||||
|  |         0, | ||||||
|  |         0, // 7: day 6 | ||||||
|  |         0, | ||||||
|  |         0, // 9: day 8 | ||||||
|  |     }; | ||||||
|  |     while (it.next()) |fi| { | ||||||
|  |         var val = try std.fmt.parseInt(u64, fi[0..1], 10); | ||||||
|  |         days[val+1] += 1; // our index is val+1, since | ||||||
|  |         // we have slot 0 storing currently reproducing | ||||||
|  |         // fish | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var n_cycles: u16 = 0; | ||||||
|  |     while (n_cycles < 80) : (n_cycles += 1) { | ||||||
|  |         var i: usize = 1; | ||||||
|  |         while (i < days.len) : (i += 1) { | ||||||
|  |             // Move all fish from i to i-1 | ||||||
|  |             days[i-1] = days[i]; | ||||||
|  |         } | ||||||
|  |         // Add new fish to days[9] | ||||||
|  |         days[9] = days[0]; | ||||||
|  |         // Move fish from days[0] to days[7] | ||||||
|  |         days[7] += days[0]; | ||||||
|  |         days[0] = 0; | ||||||
|  |     } | ||||||
|  |     var sum: u64 = 0; | ||||||
|  |     for (days) |v| { | ||||||
|  |         sum += v; | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] After {} cycles, there are {} fish", | ||||||
|  |                  .{n_cycles, sum}); | ||||||
|  |     while (n_cycles < 256) : (n_cycles += 1) { | ||||||
|  |         var i: usize = 1; | ||||||
|  |         while (i < days.len) : (i += 1) { | ||||||
|  |             // Move all fish from i to i-1 | ||||||
|  |             days[i-1] = days[i]; | ||||||
|  |         } | ||||||
|  |         // Add new fish to days[9] | ||||||
|  |         days[9] = days[0]; | ||||||
|  |         // Move fish from days[0] to days[7] | ||||||
|  |         days[7] += days[0]; | ||||||
|  |         days[0] = 0; | ||||||
|  |     } | ||||||
|  |     sum = 0; | ||||||
|  |     for (days) |v| { | ||||||
|  |         sum += v; | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 2] After {} cycles, there are {} fish", | ||||||
|  |                  .{n_cycles, sum}); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day7", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | 1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,20,1091,861,228,628,186,980,996,710,541,354,1611,69,1331,91,1220,447,523,38,1286,244,643,1069,566,70,155,1710,1266,120,302,72,232,387,1086,278,1122,605,1559,98,111,1816,795,543,1217,304,356,129,839,704,49,523,370,74,13,232,179,101,664,892,266,622,197,404,147,882,435,504,48,766,684,1362,136,830,1393,1259,925,68,879,251,43,1339,61,98,403,51,1008,197,659,195,1823,233,121,731,82,141,580,18,427,774,13,685,496,752,63,132,39,237,18,167,51,299,22,19,1442,305,1283,253,159,731,302,76,115,185,136,447,821,307,207,30,1427,251,589,0,1096,1240,261,442,757,5,172,847,858,382,425,79,402,166,1058,186,35,21,324,183,1293,95,410,321,12,155,88,409,40,428,1180,199,1444,487,148,57,1187,15,100,983,77,0,1667,359,513,659,339,142,968,994,787,0,443,183,133,538,4,332,1459,204,1156,710,1654,20,36,407,890,1265,1090,743,36,78,1033,781,608,476,103,1401,24,4,875,414,799,305,1047,842,72,497,362,270,73,12,9,0,21,11,51,1357,455,505,483,552,199,1108,214,238,686,1496,116,154,1403,35,272,738,1024,50,50,934,564,19,395,324,447,794,1326,14,407,1694,452,439,455,442,86,1515,588,809,224,112,156,21,1405,610,187,23,370,112,397,995,777,75,1281,32,60,284,388,916,555,200,675,20,320,32,398,104,113,447,113,351,396,322,36,1674,1708,232,1004,514,95,859,382,116,277,239,343,3,433,11,55,699,513,1465,319,44,306,224,615,482,695,421,300,321,283,579,323,102,275,17,723,632,713,277,801,222,130,189,1549,49,784,690,136,444,1315,259,1334,472,711,1109,276,70,315,838,35,328,766,1100,460,4,178,630,571,5,106,429,368,547,1210,840,162,166,10,403,880,287,44,1001,316,402,1054,174,7,1194,105,58,268,667,86,588,166,547,238,1586,77,112,244,107,63,873,1152,16,407,198,26,587,20,1449,775,653,1369,732,262,566,222,10,102,22,573,233,297,1238,789,291,93,206,1274,177,58,841,672,37,16,262,201,241,938,133,774,978,631,511,0,263,498,799,51,330,11,206,325,173,676,15,457,364,46,373,34,1475,530,672,295,55,4,297,274,1519,15,688,555,96,160,185,583,646,41,378,1572,67,219,572,143,16,286,65,788,886,243,883,1012,109,90,742,464,1099,388,1855,731,62,6,415,66,232,542,98,1123,11,414,1262,4,440,8,691,130,164,773,992,423,115,1807,1618,153,168,1213,719,291,316,311,110,24,608,0,127,131,142,196,232,75,248,412,275,1295,239,86,967,133,422,415,894,280,807,345,446,250,979,231,713,201,1009,208,444,43,1400,434,8,221,141,235,909,1018,340,0,178,1144,353,662,491,294,6,440,446,824,1392,379,269,1427,911,671,231,424,102,718,86,54,130,206,514,137,1075,1573,248,472,602,249,974,8,372,59,2,940,165,1132,327,1424,63,6,97,140,1302,439,1237,59,324,733,397,477,426,278,274,1636,745,41,269,257,51,173,503,88,1223,754,228,584,72,632,645,323,156,7,337,192,375,583,613,370,172,528,1282,360,208,17,208,802,22,67,290,242,458,809,6,1388,49,14,327,911,121,475,101,617,284,91,1,35,421,293,1419,602,143,142,168,657,472,219,345,411,115,387,5,494,383,348,85,1070,154,42,63,586,953,563,12,1263,788,762,222,351,730,42,643,877,522,775,698,564,604,155,191,430,5,386,787,120,470,433,564,955,411,1090,210,1096,933,109,70,279,287,736,390,1075,30,194,30,1318,473,727,100,584,1227,89,432,120,393,1080,185,500,847,117,319,54,160,32,114,700,12,36,681,53,1063,113,82,1625,1426,53,658,80,253,36,16,810,560,602,177,275,147,335,1237,1447,176,55,366,721,471,501,217,67,899,914,168,981,1177,898,1,9,704,602,787,1280,305,57,786,1696,211,63,55,351,14,151,932,757,810,1748,169,1018,125,849,234,102,823,300,127,6,849,1163,229,726,397,656,135,466,137,1247,811,807,366,209,1703,24,1219,45,161,353,274,572,397,899,646,32,137,439,1048,2,391,8,214,736,518,409,414,567,1262,155,102,178,1247,526,10,94,759,781,18,14,1518,68,295,905,478,1581,89,429,937,438,915,1110,659,615,1128,8,125,89,289,1084,905,254,1227,184,883,983,110,1,24,748,1408,828,1187,63,264,481,214 | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,77 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var values = std.ArrayList(u16).init(alloc); | ||||||
|  |     defer values.deinit(); | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var lit = std.mem.tokenize(line, ","); | ||||||
|  |         while (lit.next()) |value| { | ||||||
|  |             try values.append(try std.fmt.parseInt(u16, value, 10)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     //std.log.debug("{} items", .{values.items.len}); | ||||||
|  |     std.sort.sort(u16, values.items, {}, comptime std.sort.asc(u16)); | ||||||
|  |     //std.log.debug("{any}", .{values.items}); | ||||||
|  | 
 | ||||||
|  |     // Try the median | ||||||
|  |     var median = values.items[values.items.len/2]; | ||||||
|  |     std.log.debug("Median position {}, value: {}", | ||||||
|  |                   .{values.items.len/2, median}); | ||||||
|  | 
 | ||||||
|  |     var fuel_required: u64 = 0; | ||||||
|  |     for (values.items) |v| { | ||||||
|  |         if (v < median) { | ||||||
|  |             fuel_required += @as(u64, median - v); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             fuel_required += @as(u64, v - median); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] {} fuel required to get to median {}", | ||||||
|  |                  .{fuel_required, median}); | ||||||
|  | 
 | ||||||
|  |     var i = values.items[0]; | ||||||
|  |     const max = values.items[values.items.len-1]; | ||||||
|  |     fuel_required = std.math.maxInt(u32); | ||||||
|  |     var best_position: u16 = 0; | ||||||
|  |     while (i <= max) : (i += 1) { | ||||||
|  |         var fu = calculate_fuel_to_pos(i, values.items[0..]); | ||||||
|  |         if (fu < fuel_required) { | ||||||
|  |             fuel_required = fu; | ||||||
|  |             best_position = 1; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 2] {} fuel required to get to best pos {}", | ||||||
|  |                  .{fuel_required, best_position}); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // move 1: 1         = 1 | ||||||
|  | // move 2: 1 + 2     = 3 | ||||||
|  | // move 3: 1 + 2 + 3 = 6 | ||||||
|  | // move 4: (1+2+3)+4 = 10 | ||||||
|  | // move n: (1/2)n * (n+1) | ||||||
|  | fn calculate_fuel_to_pos(position: u16, crabs: []u16) u64 { | ||||||
|  |     var fuel_required: u64 = 0; | ||||||
|  |     for (crabs) |c| { | ||||||
|  |         var delta: u64 = 0; | ||||||
|  |         if (c < position) { | ||||||
|  |             delta = @as(u64, position - c); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             delta = @as(u64, c - position); | ||||||
|  |         } | ||||||
|  |         fuel_required += (delta*(delta+1)) / 2; | ||||||
|  |     } | ||||||
|  |     return fuel_required; | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day8", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,201 @@ | ||||||
|  | gbdfcae ebcg cfg gc facegb fecab acfge cbfgda fedag caebfd | ecbg bfcagd faegc gcf | ||||||
|  | eacgf efcab fgc fedagc gdeaf cged aebfgd adcgfbe gc bdgcaf | fgbacd cfega ecdg cg | ||||||
|  | dfgae gcadef efb eb dcabf bgde edfba bcfaeg egcdfab fbgade | bged eafdb eb gfdea | ||||||
|  | aefdb cafdgeb egdfac egdcba fcbd efd eadcb caefbd df aegbf | cfadeg abfedgc fde bfcd | ||||||
|  | dc deafg ecd dbaefc adcfeg cfged ecbfg acdg cafdegb gfeadb | dcga edc adfebcg ecgfb | ||||||
|  | befdc bcfge befad degfab cde aecbfd gcedaf eafcgdb dc bcad | bdeafc cde ebdcafg daebgcf | ||||||
|  | cd egdacfb fdc fecgb gabdf fbcdg fcgdeb gdcfea debc ecfbga | gfecb fbgecd bcgef dc | ||||||
|  | fagcdbe gec gbdea ce bgedc aecd cgbeaf cfbdg gebcda dbfgae | cfbaged cgbeaf ceg gadfbe | ||||||
|  | gcbaf gfdb bdafc df adf adcbefg dcfeag cebad adfgcb faecgb | afd agfedc dcbfa gdfabc | ||||||
|  | fbae cef fecdbg afbcg cefbga fe acdfgb gadce fbdacge fcage | fe abef gfbac cef | ||||||
|  | cfdebg bg beg cabfeg fbgd acged ecgbd fdcbe cfbeda cebgadf | fbedc gadce gdaec bcfed | ||||||
|  | gba ab fgbcd gedaf bcadge afbdgc cdgfbe agdfcbe bcfa bdfga | gab gdbfc ba fgdbcae | ||||||
|  | fabdgce bfdgac cbaed dcb bgade fbaec fcde edbfac gfabec cd | dc cefd dc cd | ||||||
|  | afc dbecafg fagce fbega fbgc edgca agecbf bafged eacdfb cf | cf bfdcaeg gfbc gfbc | ||||||
|  | gedcb ebda cgbdfa fcaeg gab bfgdce ba agbce bdgecfa dgebca | bdgfce cebagd cbdgfae dcfbeg | ||||||
|  | egbacf acdfe gfecb egdfcb dfgaceb dfb dgcb fdbce db bdeafg | fdb db db bdf | ||||||
|  | cfeg eabcf gdefcab bef geabc bfcad egbadf cfgbae egcdba fe | fgec gaecfdb cagbef ef | ||||||
|  | bafde dbgeac feacgbd cbaef bed bfgaed gafecd bgdf bd eafgd | db cfgdae caegdfb fabed | ||||||
|  | afg bacg cdbgf gcdfab fdcae gafdeb dbgfce ga dcafg cedabfg | gaf bdafgc agbc ga | ||||||
|  | edgca bafdcg acbdg cbdeag dce fgcea gdcfeb aebd ed gfdabce | ebgcad daeb edcga ced | ||||||
|  | agecf abfegc dbefcga ad dafc fegdb adg afgde bdcega ecagdf | fadeg dfac dga da | ||||||
|  | ceagdb gedac acfdbg cda bgecafd adgeb dcefg ca beac abgfde | aegbd baegd bdcagf abcdeg | ||||||
|  | egaf bagdce dcgabf ea eab cgbfa cdfebga fbcae dfcbe fgbace | gdfaceb gfea dbcfe eba | ||||||
|  | cfbega bge eg cgdebaf gedbc adeg bceadg bfadcg agbcd cdefb | ge ge cfedb fbgadec | ||||||
|  | afc ca cabe gbefcd aebcgdf acedf gbafdc eagfd edcfab dfbce | gbfdcea fca aecbfd ceba | ||||||
|  | af bcdag agbfde fdbca ebdfc adf dbefcga abedfc aefc dbgcfe | af acfe adf adcfbe | ||||||
|  | badfe agebd gfaced gbdcafe aecfd fab dbfc geacbf adbcfe bf | fb ebagfc beadf dface | ||||||
|  | bfeda gabefd agbedc ec fcadeb dfbaecg cea fdec afceb agfcb | fbcae cfde eac fedc | ||||||
|  | ebcf eafgcb egdfba dfcag bacge faceg afe gdceab ef dgcbaef | fcbe febc bcaeg cbfe | ||||||
|  | dafbc afdebg egfc ce eagbcd ceafgbd fgeba cbefa eac fcgeba | afegb cdagbe cbdaf gfaeb | ||||||
|  | ecd dface cdgafe gcabfd edbgcfa gdacf fecg cdbaeg ec efbad | edbaf gcafd dcgbaf dafgceb | ||||||
|  | gd fagbec bdfg gde cfgbe cefdbg gbdeacf agdcef bdceg bcdea | dg gcfeba dg ebdcg | ||||||
|  | bg bgdeac abdg cgb fcbade gdbceaf fbdgce dbeca gaebc aegfc | cgb befgdc fedgcb gbc | ||||||
|  | gac cgeda egcbafd gfdbce fadec ag degbca agecbf dagb gebcd | cdegb degcbaf cedaf edafc | ||||||
|  | ecfab bfacdg bdga gb cfgba bfg gefdacb cegdfb fagdc cafedg | bfg cefba gb aedbcgf | ||||||
|  | dgfce aeg cfbdgae acfgeb ebcafd ag gafed gaebfd bdga dbfea | gae cfbdae abedfg bgfdae | ||||||
|  | cgbad cbaf fbgeda dfgcab fdcebg dbfcg decag cdagbfe bga ab | dfcabg cbfa fcedgab adfcbeg | ||||||
|  | cegbadf edfgba cafeg dc agdebc dbgfe cdbefg dec dbcf edfcg | bdfc facdegb adfegb egbfd | ||||||
|  | bedf cefadg ced ed bcegd gefabc agcdbfe cdgab gcfbe fgcdbe | ed cde bdgec dbecfg | ||||||
|  | egfcab debfa ecd bfgec cdgb cd bdcef dgebcf ecdabgf egcadf | becfd cbgd gcdb fcbed | ||||||
|  | bc dbfge gbecf caefdg gefabc abgdcf bfc baec efcag cebgafd | baec fegbc agfec cgbef | ||||||
|  | bacegfd fbdgca egbcf fabce bdaefc dcaef dagefc ba dbae cab | daeb cbefa cab ab | ||||||
|  | fgab cgdfae fcgebd gb bcg dbaegcf cabed gadcb gcafd gbfadc | gbc adcgf dgecbf cgb | ||||||
|  | gdaeb df cgaebdf dfbe cbdagf eabdcg gfd aecgf aefdgb agedf | dfg gfcea febd ebdag | ||||||
|  | ecbaf cagfdb aecgf gac ebga defagcb baedfc gfcde ag gfbeca | cadbfg gaecbf agbfec fecba | ||||||
|  | edbgaf fbd egdbfca dagf gabde bdfge fd bgcfe efbcda eacdbg | fbd ebcdaf dbgaefc gefbda | ||||||
|  | acdbg aefcdg cdg cegb acebfd abgdf cg fbegadc gcbdae eabdc | gc edafbcg gadbc cg | ||||||
|  | fgcd bgdafe bacge dg facgde acfde agdec gdefbca deg aebcfd | gd eagdc ged ged | ||||||
|  | egfa efdcba dfbgc cbfea ga abcfg edgcab bga fecgdab aebfcg | bga faegcbd bfeadc badcge | ||||||
|  | bfeda ceagdb bf ebfacd edacb acefbdg begcdf bafc agfde feb | fb fb fbac bcegad | ||||||
|  | bfdcea dcbgaf cbega dbage cea ce cbafg gefc bgacfed bfecag | abgfc ec cegf acdefb | ||||||
|  | adgfce cdag cd fgecab cfgae ced fdbae gebdfc agbcefd ecafd | dfcae dc fdcae ebafcg | ||||||
|  | dbeag begfad bfdegac dcb cb gcfda ecdgfb ceab gcabd dcebag | cbd cb cegbda bgcdef | ||||||
|  | cdfaeb fgebad ea dcbgfa gcaebdf cegbd bdcae fabcd cfae eba | caef bafdce afec gfdbea | ||||||
|  | bcfega gcbea bdfgeca gabcd bd gdcabe dafcg ecdb dba ebfadg | fceadbg fbaceg db bd | ||||||
|  | gadcbe dgb debcaf fabgcd bcgfe bdgcf acfedbg gd cafdb gdaf | eafdcb bcfade badceg bdacfg | ||||||
|  | gafce cabde cdf aefbdgc efacgd fgeacb ceafd df efgd badfgc | edfg acbegf dfc gedf | ||||||
|  | afgce gbdecf bae adebcgf bgedca abecg abcd baefgd ba gbdec | ab dfagcbe gbeca ab | ||||||
|  | de dcgaf gcfbed gdebfca acegb cde bcgafe abed gdacbe gecda | ed afgcedb deagc gcdaf | ||||||
|  | gcdef ebfadc da dabefg daf fbaeg gaefcb dfeag gbdfcea bgad | gadfbe bafgce dgba afd | ||||||
|  | bdfeg gecf fc egdfcb gcbedaf bfc dfcbe cbdfga baced abedfg | fcge fgdecb dcgfabe fgdeb | ||||||
|  | bdf acbdfg bdaefg aefgbc abcde dagebfc fd dfcg cabdf cgfab | bfadc cabfg gfcdab fbaegc | ||||||
|  | af cdfbega edbfac dfbgc fac adef adecb becagd acdfb efcbga | bcagfde bdfcae gfdecab eacbd | ||||||
|  | egfcad bdgeac fdac fed cdage fd gcbfed baegf gfedcab gedfa | adceg fd afebg egdca | ||||||
|  | fegba ecbafgd cfegba cgdab efbgad ec fadcge ecg bcef cegba | bgadc ce fecgdba baceg | ||||||
|  | bdfaeg abgedc efadg bafd gfeba fcdebga adg aecgbf da ecfdg | ad ad dfba befcgad | ||||||
|  | efadcg dacfb cfgab bfde eafdc decfbag decbfa bd bcd acebgd | debf cafde gcdafe fcgebad | ||||||
|  | agd cgadef gcdebfa gd facge bdfgea eagbfc dcgaf cafdb ecdg | cbgfae ebfcga agcef bfdeag | ||||||
|  | dfbge abefgd aefbcg dcfegb ba fgbad gedafbc bdea fba dcafg | agfdb fab fba fcbgde | ||||||
|  | fcdab agbecd eacgfb gc fgeabd gdabc dgbea cbfdeag cag dceg | fcadb fbcad degba gca | ||||||
|  | eag cbefg dfeagbc cdfbga egcba dcegba dace agedbf ae gadbc | cegba age agbdc dbcafg | ||||||
|  | cdbag gfb dgceba cbfa gfdbac faebcgd deafg dgbaf fbegcd bf | eagdf febgcd fbdag gdfbce | ||||||
|  | cdbgf afcebg acfdbe ed ced bdefagc dcgbea fbeac cbdfe adef | cfbde de dec ecd | ||||||
|  | dfc fbcde dfcbga ecbgd egbdca fd eabfc edbfgc efdg cdfegab | df dgef fgbaedc df | ||||||
|  | fcg egcbd gacefb dfgbc abfcgde bfdga cfed cf gdebac gbecdf | bfdcg fgc fbcdg agdbf | ||||||
|  | fcabg acfbedg bfcgde ecbad agef ecfgba ecf fe dafbgc baecf | aegfbc cfe dbace fec | ||||||
|  | edbagc cefbda bfdeg af agcf gfebdca fgdcab dgcba baf fabdg | bcadef dfgcab abdcfe gedfb | ||||||
|  | acegf efbgcd dgbefac cgd bedga cbad agcedb dc egadc feagdb | badcgef cd dcgbfae cd | ||||||
|  | bfc cdafeb bcega gdcf afgdbe egbfd caebfdg gcefb cf debcgf | gcebf bgedf dfbgea fc | ||||||
|  | dgfaceb gcdf dabcg fd dfgba aefcbd bdacge gfabe afd dcagbf | daecgfb gbecda dacbfe gdcba | ||||||
|  | fbgedc bfcda cfg dafbge fecdbag cagfd aecg agfde cg adgcfe | ecdfagb gfceda gaebdf gbfdea | ||||||
|  | afbe gfa gfdcb af febcadg faegcb bcgea aedcbg eagfcd afgcb | bagcf gfdeca abfe af | ||||||
|  | caefg efd fd abedc fcgd egdcaf egafbd gfcbea dafec abgedcf | gaebcf fcedga def egcafbd | ||||||
|  | dcfab gefdcb bda da baefc baedcfg gfda dbceag cbfagd fbgdc | cdebga efdgbc fbcgead beafc | ||||||
|  | bgfed fdaegcb bedagc da bdacfg gda eacfgb cadf dafgb cabgf | dgceab da dfbeg bcegad | ||||||
|  | cedab fc adbfec faegcb adbgf cfa ecdf ebagcfd dcbfa ecgabd | becdaf cefd abdfc efcd | ||||||
|  | cdgbea aeg agdec dafgbc gbed aegbcf ge aecfbgd cagbd cafde | ceafd bagcd gacdb cgbad | ||||||
|  | dgfeb bedag cgdefa df gfdaeb cbdega gfdceba gbfec dafb gfd | fabecdg bdfa bdaf dfba | ||||||
|  | gdfe acdbf ed bdegcf cegbfa cebfd deb badegfc fbcge bgaced | ebd efgcabd bfcda dfeg | ||||||
|  | abfcedg gec fcgdbe gaebc gebfac efbdca gfea acbfe gbdca ge | agfe cge cdbgfe bcfdea | ||||||
|  | fgbaed dacegb dacef egafd aecbgf adfcebg bdfg ged agfbe gd | agdfceb gd dg fbgd | ||||||
|  | egdb acgbef edfac bd fgecb gbadfec bcafgd bcd bedcf gdecbf | bdc bedg gdbe efbgc | ||||||
|  | cagbef acgbf fgcabde cgd gbda dg dbfgec ecafd dgfca dagbcf | agbcfe dcgfa dbagcf cgdafeb | ||||||
|  | cb efbgc cgfae cgb gceafd beca fbcgae gefdb cbgadf dcfabeg | bgeacf bacedfg cbdgfa dfgbe | ||||||
|  | cgbedf facgdb agfdc cga cafed gfcbd gcdbeaf degacb fbag ag | gbaf acfed gafcd cgfda | ||||||
|  | egbdfca cadf bdafgc cbgfea cbgde bdcfg gfc cf fdbag gebdfa | eacdgfb fcda dfac adefcgb | ||||||
|  | fdgea abcd ac cfabge cdbfeg gdebac acg gcdebaf gadce bcedg | gac abefgc ac gcaed | ||||||
|  | eafdc eafbc agbe begcadf ba gcefba fgcbda gdbfce cba ecbgf | ba ab abeg adcgbf | ||||||
|  | afg ga bdafe eadfg dagc gefcab edbcgf ceagfd ecgdf aedcbfg | agdc daefb debcafg fdaeb | ||||||
|  | baf cbdgf afbecd gdcabfe dcfabg fadeg gbac gbadf ab cedgbf | fcdeba cbga afbdg bgca | ||||||
|  | ebdfgca ebfdag gb bgf fdeagc gdbe cdfgba egfab abcef agdfe | gb gfbdea gdeb eafbg | ||||||
|  | agbcdf fe gebad fea becf ecfbga cgabf aegfb fedgca acgbedf | ebgda ebdgacf fcbe aef | ||||||
|  | gdbef bgfadec gfeab eab gdcfbe ab bfadeg fdab dcaebg acgfe | fgadbe fbedgc dgefb aeb | ||||||
|  | adcbf fbdg bcf dcbag fecad cgafbd fb acdefgb bgefac dgcabe | dfcab cfb bf cdeaf | ||||||
|  | dfgbca cdebagf cfe fcbga aefdg geafc bdecgf ce bgefac aceb | bace aebc gfbac efc | ||||||
|  | adfegcb gcbaf fa adgcb acf efgacb fgcbed eadfbc agef fbgce | aefg acf gafe cfa | ||||||
|  | fdegbc fcbad ec ebgaf gecfdba feacb caefgb degfba ecb geac | ecb ecb cfadb ebafc | ||||||
|  | fegabc ebcgafd gfacde ge fcgeb ceg bagcf dcfbe ebga bdcfga | cefabdg fdceb abgcf gfeadc | ||||||
|  | fdabcg bgafc fcbeag edbfg gadfceb ad adb dfca dcbgae dfabg | bdegf faedbcg da caegfbd | ||||||
|  | cgfae gdbeca decaf fd dfbc fad dabegf daebc bedafgc efbcda | decfa fgcbdae fcbead dbegcaf | ||||||
|  | dbeaf cb fabecdg bcfda gabfdc cbfg agdfce abc edgcba cgadf | bcdefag bcgf dafbc badcgf | ||||||
|  | gbdcfa becfdg fdbcgea fgc cg fecdg efdgb ecadf gceb gadefb | gcbe fgcebda daecgbf aebcfgd | ||||||
|  | cdega dgcaeb cbgea egdcaf gfacedb begfda cebfg cbda bag ba | ab egdac beafdcg faegcdb | ||||||
|  | gab cgfab cfage fbaedc bcdg gb gacfbd defcagb gdebaf fadbc | bg fbacde gfabc dceagbf | ||||||
|  | ge ebg cgdfb cedba bedgc ecag deagcb bfceda gdcfeab egdbfa | bge beg bafged cadbeg | ||||||
|  | eabd ebacf ecd fdcbg dbafegc ed ceagdf fgceab ecfbda cdbef | dec dfcabe dec befca | ||||||
|  | afbgdec cedb dc agdbf cegbf bfcedg cdg bdgfc baecgf ecfdag | cd dgefbc fdgcb cd | ||||||
|  | gebdaf dgfae afe gafcbed agcfd febd edabg gbfeca bcdega fe | ebdag fedb egafd aegcbf | ||||||
|  | gebdcf abefdcg deg bgdfc fcde gbfea gedabc edbgf ed bgfcda | efdbg fedacbg gbfacd fbdeg | ||||||
|  | bcgd edagc gaebd bcdaeg fabedc bgefa fcadge agbfcde db adb | cgbdfae bdagcef badegc dab | ||||||
|  | df fdb bfadceg bgeadf afbdce efdg dgbae dafbg cafbg abdgec | gdfe afcgb ebcgafd fbgacde | ||||||
|  | gabce gdfaeb bcad cebdgfa fgcbae efgcd dga da cgdea cdeabg | ad agd gda gfedc | ||||||
|  | fcabge edbfg fcaebd ea dcbagef gace gafcdb bafcg eaf fgbea | dcfbage aceg afbdceg ebcafd | ||||||
|  | bafe egfcda be bfgaec gbe egcfbad fcaeg bdcaeg bgecf cfbdg | fabgced bfea gbcef gcfeb | ||||||
|  | badfeg fgecb af cgaebd acfdbge gaf afcged gfaeb dfab egbad | fa fga abfd gabde | ||||||
|  | afceg bcdfge fcaebg baegdcf cfg eabcdg bgfa efcad gcabe fg | abfg gf fbgecd acebgd | ||||||
|  | acbedgf fb efgdc bgf gbcfd bgfdac dagcb fdbeag ebcdga fcab | bf gdebaf fb bf | ||||||
|  | aecf cgbdefa bacged efbcga fdgcab febag af agf dgfeb ecabg | bcgdaf af cadfbge efca | ||||||
|  | egfda fgceab acd bfcadeg dc dcega bfdcea gcdb cdbega eacbg | gabecd cd befgcad gdcb | ||||||
|  | gecda dfegacb cgfae cdbfag fcbeag afg gf fbge dafecb caebf | dfaebc cfeab gf agdfbc | ||||||
|  | gdafe cdgaeb egfdba da bfdgec gbfed eagcf egdbafc fbad dag | agfde dga dcgbfe abcdfeg | ||||||
|  | acgdb gadfce cbefdga becad bcegdf bacgdf afbg ga dag dbcgf | abdgc gdeacf acdfbg ecdab | ||||||
|  | gdfa eadbc gcafe dgc dg fcagde bcgfea decgfb egcda fbecagd | cegda cgdea gd dg | ||||||
|  | ga egdbcfa aegcb cag egbcd gadceb gbcefd decafg badg cabfe | bagd cagfed gacdfeb cbegd | ||||||
|  | cb deafc bcgd agfceb dbfega ecb deacb aebgd cbafdge ebdacg | dcgb adceb edgba ceb | ||||||
|  | edgfab acgb becfdag bgf cgbdf cgfad dacbfg bg ecdbf afegdc | gfb bg gcfade gfbdeac | ||||||
|  | caebg cgaf acgdfbe begcaf cf fdgeb agedcb ebgfc cfe fbcdea | dbfeagc cfe fbeagdc cf | ||||||
|  | ageb bg adcbfge fcgea cbgaef cbdfa gfb fedacg bfgca fdcgeb | fbg afdcb ecgbdfa cgfdbe | ||||||
|  | dgbfe aeg ea dcea dbeagfc fbacgd ecbdag gadbc bcafge adgbe | aedc ae gbdef daec | ||||||
|  | dbafcg dbgaefc adfc egdab fdg df agefbc bdgcfe fbacg gdfba | bedgacf gcbaef cdfgab cafd | ||||||
|  | eg fdgcabe gcadbe aebdc gced bagfde gea gbeac afbcg ecdbaf | agfbc fgdabe fcdgaeb feabcd | ||||||
|  | fedb gfe defabgc caedg cgadbf feagd bdagfe fgbda efagbc ef | fe fgcbea ef dbfe | ||||||
|  | bc agcb dfceg bafgdc gfabd fbdcg bcd ecgfbda ebadgf fbaedc | cbd fabcgd cgbfd bc | ||||||
|  | ebfgc dbeg ed gefdc cde gfbecd fgdca caedfb gdbafec gefcab | fgdecb dgfce fbecg cabgfe | ||||||
|  | ab ebda bfagdc becgd aecfg bag daecgb ebcag gbacdfe cfgbde | abedgc eafgc ebacg ab | ||||||
|  | ecdbag dagbfe badef fcaed adc cd efbagdc cafge cfaebd dfbc | dcfb dca acd gcdabe | ||||||
|  | cedbgaf ecbfg dagfec dgbea bdca gfdaeb ecgbd dbecga cd edc | ecbdga cde edbag dc | ||||||
|  | egbfcd gbcafe ed defga dbgfa bcgdeaf cdea def eacdgf gacfe | bgfad ecda gbfad gcdefb | ||||||
|  | dafge cea fdace agdc dbgfea ebdfgca dcbfe acedfg abecfg ca | defac gcad ac ca | ||||||
|  | ecdba fbac eabcdf beadgfc acd efacgd ac agedbf bfdea ecgbd | aecbd agbefd bdgec dgebc | ||||||
|  | bfegda bdage gdf gfdae bdfgace fgeb afdec fgdacb gf deagcb | fbaegcd fegb aedgb dfg | ||||||
|  | cdaf fd bcdfge agebf dcgfae gfead baedcg gdcea egdbacf dgf | efgab df eagfd afgbe | ||||||
|  | egfca egcb ceafdb fbc fbcaeg dgbfa cdfeagb fgcba agdcef cb | cb gcdfae eabgfc fgbca | ||||||
|  | gdbca fdecba gdecfb feagbd ceb adfbgec bdgec ec efgdb cfge | ec ecbgd dacefb abfged | ||||||
|  | edbcag cabd ecfdgb aegfd eagfcbd ac cga feagcb ebdgc dgeca | acfbdeg dfgea cga dgeca | ||||||
|  | ebgacf adcf bcgedfa gdcabe abedf aef edabc fa befgd bfdaec | aefdb af cebad fae | ||||||
|  | dageb de gde dgfbce cgdba gabefd afbge cgdebfa aefd ceafbg | efgbda agbdecf bfaegcd edg | ||||||
|  | ce dec becf efdgb edfgcb ecbdag gdaefb beafgdc dagfc fecgd | ce fdbage gcdfeba gbcadef | ||||||
|  | ecgfa ebadcf ed gcdfab cdefa gfdaeb dbec dbfgace cdbfa dfe | dceb cdfab ed ceagf | ||||||
|  | cadg efgcdb gbcfad feabc agfcb afcdgbe dgbfa gcb fagbde gc | gdecbf adfgbc fbcgade gafbc | ||||||
|  | gdcefa gefdb bcfeg eabc fagdbc ecagf bcf afcebg cb bfgaedc | fcgdba agbecf fcgeba fgdceab | ||||||
|  | afgbc be cbeaf gfeb ebcdfag cfdbag cdfae cfegba cbeagd abe | gdecbaf cagfb bfgaec cabfdge | ||||||
|  | eb egdb dcagb abe gcdabe agbce dgabcf fedbca fdcebag gfeca | gdbe agdcb gfcae cgadbf | ||||||
|  | dbagc bcaedfg gade cadeb efgabc cabedg gca fgdbc ecafdb ag | dage cagdb dgcaeb cdgab | ||||||
|  | gbecd eacdg cgb egacdbf gfdbec efbg bfced bg bdfcea bafgdc | acbfde bg gb gb | ||||||
|  | fgbadc fbdag dbecg fbae dacgef ega ae afcbged ebgfad bgaed | ae bacfgd bfea agdbef | ||||||
|  | dacfeg gecfd ad gadbfe gadbfec gda acde fcedgb acbfg fdcag | gefcd cdegf agd ad | ||||||
|  | caegbd cfdeba edbac cfbegad fe fbea fdagc cef acdef bedgcf | ef cdafgeb fce edfca | ||||||
|  | dgcae cedbfag facbdg fdc eacdbf bfegda dfcae fdbae cf cfeb | cf febgcda dfc cdf | ||||||
|  | edfag dbeacg dbecafg agc gcefa gc bcfea cdfg fbdgae gefadc | dfacge fagbecd feacbdg bfaged | ||||||
|  | abefdg fgdec ecadf efcdab ac dbeaf acdb acf cdegfab gecbfa | fca fdbega decaf cgafdeb | ||||||
|  | bceafd fegcad edcagbf dbcage decba bafd df fcd bcfge cbfed | ebgfc eacbdf gadebc df | ||||||
|  | egba cgdafbe begfc gacdfb dceaf cefbdg gfa ag gcebaf aecfg | acbgfd gabe fbgeac agfec | ||||||
|  | cdgeb fdecag agdbefc gefbac cdaf ade fegdab cgead ad caefg | ecbgdaf dgceb cfgae dea | ||||||
|  | cafbde fbgac edcg agdfeb eg dacgef fgcea aefdc dceagfb eag | afgec fbcga ceafgd bgceadf | ||||||
|  | cefbg ceabd gd edbacgf aedfbc gaedcb cdbeg abfdcg gdc gead | cgd dgea gd cebad | ||||||
|  | fadbg dagecf fea fbecga adce ae bedfcg egacbdf fdgec fdage | dcae caed dbefcg adgfb | ||||||
|  | acged aec efgdcb ac fagc abdge abfdegc fdeacg cfaedb gedfc | egbcafd gaefcdb cae cagdfe | ||||||
|  | fcbdage bafgec cab fcbae ac abdegf dbfec fbgae eagc cbfadg | acb dafegb acb bfeagdc | ||||||
|  | af egfda gebdf gebdfa aefcdbg fbecda fgba daf cdeag dgbcfe | eagdc cefbgda daf daf | ||||||
|  | dgebcf gcf agcbd fbecd fbcdg ebacgf aebfcd fdeg fegdabc fg | dacgb dfge fcdbe gedf | ||||||
|  | bfacdg gafcb gefbd cd dgcfb dagc bdfagec bceadf fagceb bdc | begfd gbdfe afgcdbe fbeacgd | ||||||
|  | bagf afe bdcaeg fdcbe af dafbe cbedgaf gfebad gdaeb aefcdg | baedcgf fa af cgadbef | ||||||
|  | acdgf becgfa acd cagbf agcdbe fdba da bdcfag gfdec dcgafeb | bagfc da dca fdbacg | ||||||
|  | cdabfe adbfc ecbaf cdae cdbfge dabgfc bfgae bec ce gadcbef | faecbd beadcfg dcae cgfedab | ||||||
|  | bceag agd aced gdbafce bcfgae agdeb ad fabdcg fdbge egcbad | gaceb agd bdagec da | ||||||
|  | cag dfgbeac ecdbgf ag gfabc ebag fabgec deafgc adcfb fcegb | dgaecf aebg bgea dcbfa | ||||||
|  | afdb degbc agedcf da aed beacfg dgfecba efcab bdface cbdea | fbeca aecbd gdecaf ade | ||||||
|  | cafebg afedcb gfeabcd fgcb cfa afdeg fc ecgab dacebg cfeag | bcgf bgcae gdfea bgcf | ||||||
|  | feg gfcaed cfabdge gf degafb fdegc edfcab edgbc agcf cfade | gf gf gacf egf | ||||||
|  | ceadf ecdgf eacbd cebagd dcabfge eaf fdceab fbad fgebca af | cefadb cefad bfad abedc | ||||||
|  | gefabd cabdfeg dgceab dagef bdfacg egfdc edgab af feba fda | geadcb dagfceb fda af | ||||||
|  | dfcea adgce dfebc gecfdba fadgbe dcefga cfga aebcdg efa fa | fa fae af gaedbf | ||||||
|  | agbfd acbgedf faecb cegbad dgabfe gbc fcdg cfagb gc afdbgc | cg cg dfgbae cdfg | ||||||
|  | efg bfacg feab efcagd fe bcagedf edgbc ebgcf afbegc bcfgda | dgecfa ef fgcba efba | ||||||
|  | aebfdc bcfage dgbacf gface gbaecfd dfgce geab acfbg ea eac | cabfg ea cfagb eagb | ||||||
|  | gba gdcea cdbfg acgdb gbecadf bfgecd dgcfab abfc ba fdeagb | ab febagdc dfgbac fdagcb | ||||||
|  | dfcb gcebd dfgaec efdgc eagcb dfaebg afedcbg gbd gbcfde bd | gadebfc db febdga bgd | ||||||
|  | eabd adcbg ceafg ecb be eacdbg acegb gbfcde adgcbf ecfdgba | cgfebad cbe ebacg dfgceb | ||||||
|  | cefbgd cgdfa bagfced dabe fgceab abfeg gdbaef gbfad db gdb | aefdgb bd abdfegc fgabed | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,128 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var digits_in_output: u16 = 0; | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         var lit = std.mem.tokenize(line, "|"); | ||||||
|  |         _ = lit.next(); // skip output | ||||||
|  |         var oit = std.mem.tokenize(lit.next().?, " "); | ||||||
|  |         while (oit.next()) |digit| { | ||||||
|  |             // 1: 2 segments | ||||||
|  |             // 7: 3 segments | ||||||
|  |             // 4: 4 segments | ||||||
|  |             // 8: 7 segments | ||||||
|  |             //std.log.debug("'{s}': {} digits", .{digit, digit.len}); | ||||||
|  |             if (digit.len <= 4 or digit.len == 7) { | ||||||
|  |                 digits_in_output += 1; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] Output values contain {} digits that are 1, 4, 7 or 8", | ||||||
|  |                  .{digits_in_output}); | ||||||
|  | 
 | ||||||
|  |     it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     var output: u32 = 0; | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         // Each line in 'input | output', and has a different mapping | ||||||
|  |         var lit = std.mem.tokenize(line, "|"); | ||||||
|  |         var o: u32 = try find_output_value(alloc, lit.next().?, lit.next().?); | ||||||
|  |         output += 0; | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn find_output_value(alloc: *std.mem.Allocator, inputs: []const u8, outputs: []const u8) !u32 { | ||||||
|  |     // we use ips for storing both inputs and outputs to try and | ||||||
|  |     // solve which letters correspond to which display segment index | ||||||
|  |     var ips: [14][]const u8 = undefined; | ||||||
|  |     var ops: [4][]const u8 = undefined; | ||||||
|  | 
 | ||||||
|  |     // get pointers to our ips and ops | ||||||
|  |     var it = std.mem.tokenize(inputs, " "); | ||||||
|  |     var i: usize = 0; | ||||||
|  |     while (it.next()) |ip| { | ||||||
|  |         ips[i] = ip; | ||||||
|  |         i += 1; | ||||||
|  |     } | ||||||
|  |     it = std.mem.tokenize(outputs, " "); | ||||||
|  |     i = 0; | ||||||
|  |     while (it.next()) |op| { | ||||||
|  |         ops[i] = op; | ||||||
|  |         ips[i+10] = op; | ||||||
|  |         i += 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     for (ips) |v, k| { | ||||||
|  |         std.log.debug("Input {}: {s}", .{k, v}); | ||||||
|  |     } | ||||||
|  |     for (ops) |v, k| { | ||||||
|  |         std.log.debug("Output {}: {s}", .{k, v}); | ||||||
|  |     } | ||||||
|  |     // segments indices: | ||||||
|  |     //   0 | ||||||
|  |     // 1   2 | ||||||
|  |     //   3 | ||||||
|  |     // 4   5 | ||||||
|  |     //   6 | ||||||
|  |     // for each index, we can have some number of options | ||||||
|  |     var segment_possibilities: [7]std.AutoHashMap(u8, void) = undefined; | ||||||
|  |     i = 0; | ||||||
|  |     while (i < 7) : (i += 1) { | ||||||
|  |         segment_possibilities[i] = std.AutoHashMap(u8, void).init(alloc); | ||||||
|  |         try segment_possibilities[i].ensureCapacity(7); | ||||||
|  |     } | ||||||
|  |     for (ips) |v| { | ||||||
|  |         if (v.len == 2) { | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[1], undefined); | ||||||
|  |         } | ||||||
|  |         if (v.len == 3) { | ||||||
|  |             segment_possibilities[0].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[0].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[0].putAssumeCapacity(v[2], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[2], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[2], undefined); | ||||||
|  |         } | ||||||
|  |         if (v.len == 4) { | ||||||
|  |             segment_possibilities[1].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[1].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[1].putAssumeCapacity(v[2], undefined); | ||||||
|  |             segment_possibilities[1].putAssumeCapacity(v[3], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[2], undefined); | ||||||
|  |             segment_possibilities[2].putAssumeCapacity(v[3], undefined); | ||||||
|  |             segment_possibilities[4].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[4].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[4].putAssumeCapacity(v[2], undefined); | ||||||
|  |             segment_possibilities[4].putAssumeCapacity(v[3], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[0], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[1], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[2], undefined); | ||||||
|  |             segment_possibilities[5].putAssumeCapacity(v[3], undefined); | ||||||
|  | 
 | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     i = 0; | ||||||
|  |     while (i < 7) : (i += 1) { | ||||||
|  |         segment_possibilities[i].deinit(); | ||||||
|  |     } | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
|  | @ -0,0 +1,27 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn build(b: *std.build.Builder) void { | ||||||
|  |     // Standard target options allows the person running `zig build` to choose | ||||||
|  |     // what target to build for. Here we do not override the defaults, which | ||||||
|  |     // means any target is allowed, and the default is native. Other options | ||||||
|  |     // for restricting supported target set are available. | ||||||
|  |     const target = b.standardTargetOptions(.{}); | ||||||
|  | 
 | ||||||
|  |     // Standard release options allow the person running `zig build` to select | ||||||
|  |     // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. | ||||||
|  |     const mode = b.standardReleaseOptions(); | ||||||
|  | 
 | ||||||
|  |     const exe = b.addExecutable("day9", "src/main.zig"); | ||||||
|  |     exe.setTarget(target); | ||||||
|  |     exe.setBuildMode(mode); | ||||||
|  |     exe.install(); | ||||||
|  | 
 | ||||||
|  |     const run_cmd = exe.run(); | ||||||
|  |     run_cmd.step.dependOn(b.getInstallStep()); | ||||||
|  |     if (b.args) |args| { | ||||||
|  |         run_cmd.addArgs(args); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const run_step = b.step("run", "Run the app"); | ||||||
|  |     run_step.dependOn(&run_cmd.step); | ||||||
|  | } | ||||||
|  | @ -0,0 +1,101 @@ | ||||||
|  | 9865456792345678921298765421012345678934995432127995456789987662134568999876534567895456999123983210 | ||||||
|  | 2994359890156789992989896532929456799219876949246789349999876541023467899988727678976567898939874322 | ||||||
|  | 3989298991238999989878989659898967895403989898999891298878965432123456789543214567897678987899965453 | ||||||
|  | 9878987889347899878767879798767898943212498767988910197869896543264967899954323478998889376789876664 | ||||||
|  | 8766856678956998767456767999656799995689987659877899976756789655456898998768636789879993245678998775 | ||||||
|  | 6545344588967999854345458998545689989799976543456798865445678966597979459876545699765432156789329989 | ||||||
|  | 7432123457898999966212347897634599878999895432345697654324567977998965346988676789896574237895498899 | ||||||
|  | 8943234568999789984324556799547698767898789543486798865534678989999843215699789899989694356789987678 | ||||||
|  | 9654448799987659875485669898956789656798678954587999976645789599798754334789899999878989456789886567 | ||||||
|  | 8786569893498543976876789987967896741234567895698989998767993498679865645689999898769878997898765456 | ||||||
|  | 9887678912987656987989899976899965430456689986789678999878992989569876856790198789653457789999854345 | ||||||
|  | 7998789904599799798992999865678977321234789899894567987989789873456988767899986578942245699998765234 | ||||||
|  | 6549897895678988659990986654569986532345699765903469876595678964597899898998765456990124568999952123 | ||||||
|  | 7432956789789977546789874323467999654476789893212998765454578975989910999429974356889435979899843014 | ||||||
|  | 8510346999898765434598753212346898965587894989329899854312368989878939899912965245678949898798767925 | ||||||
|  | 4321245899999654329999862101234567897678943878999789765434457898768998769899854345789998789679879896 | ||||||
|  | 5434556789898765698897543214345678998789532367987679876576568967956899856798766456799998664567989797 | ||||||
|  | 6545787896789976987798654365769789999898721259876543987678679456346679643899876567899876543456797689 | ||||||
|  | 7656898945899989876549775878878997899987654345987432198788789327234578954988987978943998666567986574 | ||||||
|  | 8788929534789999987433989989989456789898765996954321019899893210145678969877898999995998777699875423 | ||||||
|  | 9899219645678999876522397896594345789799979879895432534976989321235689998766779878989899889987764512 | ||||||
|  | 8989398796789998982101236789989457998678998768779948549875378942476789987655568969876789994986542104 | ||||||
|  | 7678999989990987654212945678979968987567987654567897698986568943567899988543459757654569895987673323 | ||||||
|  | 2456988679891299865329896799567899999678992123456789987798779657798998777632469943212356799998765434 | ||||||
|  | 1369876567789498765456789895466789898999987012357998976549989769979987654321367892101234598999879875 | ||||||
|  | 0198985456667999876569999964345698767889996433767987895421299896567898865432456789233395997899989989 | ||||||
|  | 1987654327456789997778998943236987656469876545678976989210378987678999876546567896546789876789999898 | ||||||
|  | 9898954312347995398989567899197986543212989856789345678931267898789875987657689998658999765678999767 | ||||||
|  | 8759865523456789199795456798989997672101298768990234789542456789898764598798999999899109754599987658 | ||||||
|  | 8649876434568999987671237987878998783213499979321365679663477892949943679899879899978998743489998743 | ||||||
|  | 9432987678678968097532348976567899654324689989452376889764598901239874699923469789467987654568989932 | ||||||
|  | 8631098789789842198643469765456998785465678996543487999875678992398765789015998679323598795678979891 | ||||||
|  | 6542359899897654349799598976567899987589789987976568921989999879459876789239897598909999987899356789 | ||||||
|  | 7853457979998765456988987997678954198799891098998679930998798768969987894398765457898899998910245678 | ||||||
|  | 8964568964329976879877535998789543239899943989649789799787649879998999965987654346797698899432349789 | ||||||
|  | 9875678975439799998764424899899959445998959876439895698685434997987976799998765127896546789654678994 | ||||||
|  | 2996789998598678939893212698999898956987898764321934987565423456896545678999877898987634899878899543 | ||||||
|  | 3987897987696589323976104567898767899876529895210129896432101567997434567895998949999895689989978992 | ||||||
|  | 4998986498989695474985323489987656789767312976821298789543212356789323489954589432978976789597656789 | ||||||
|  | 9899894329878976569876765678998545678954324987942989678954323467891014567895678949869987993498767890 | ||||||
|  | 8789789498967997678987886789298758989765434598969876569975434568932325789976989298757498921019889921 | ||||||
|  | 7645678997656789789898999892129867999876546689878997879876545879545434678989899109843349432923997899 | ||||||
|  | 8756789987545678998769456989234998910988687899989898989998786798756645989998778998732235949895986678 | ||||||
|  | 9869898765434567987654345678945679999999798979998789999959897929767856799896565679641019899799875567 | ||||||
|  | 3978959977756689798963233457899789987859899567989656998845998939878978999765434569852199787678954345 | ||||||
|  | 2989543298987897679892101556998999876545999879876549876734349798989989998986323498963987697567893234 | ||||||
|  | 1097432109598923598783242345987899965436799989995421965621234567992398967895404987654996546456789345 | ||||||
|  | 2976543413459919679654653569876989875429988999986590954210156789101987856789512399869886431345898976 | ||||||
|  | 9898865524567898999965775789965678976598767898997979875663247893219876745999423567989767210124567897 | ||||||
|  | 4769986678978987689896989899876789987987656567899868986754356789498765535898994678998654323234788998 | ||||||
|  | 3456798789989876576789998943989899999876548489989654398869767899599854524657789989129769834545678959 | ||||||
|  | 2135699892598765465678987632399998932985432345678968239979878998987643212345699893298998765696889434 | ||||||
|  | 4396989901459854323456896543989897891096521234567892101989999767896532101237998765987999876787998923 | ||||||
|  | 5989878912398764212348998799876776789987430123979963312399989945987643212456789879876799987898967894 | ||||||
|  | 6975567893987653105479999987654545678995321239899654323998767896798965434767899998765789998969556789 | ||||||
|  | 9864348999876543235567899999643234989876542398788969439865449797899987645898998999654567899654345798 | ||||||
|  | 7943239898997864346778998998767367899998653997677898949654338689910297656999567987743578999543102987 | ||||||
|  | 6521098787698765489899987899976456789459769876566567898784213578954398997894329876542347898683219876 | ||||||
|  | 6432987654569896579978996432987678996569898765433478989832102459768569698989013989421456999795339865 | ||||||
|  | 7843499869678987678967987563598789987878987657321389569943212369877679549678929898732345689896949654 | ||||||
|  | 7654987998789798989655987689989899998989876543210123498954534678998798934569999789654556798989898643 | ||||||
|  | 8765976539895699996544398798879998999694989987321784567896645689899987897678987678966667987876789532 | ||||||
|  | 9876987421934789987431239987668687896593999876534567898998756789789896789789876567898779876545678901 | ||||||
|  | 7989873210125678998763459876544456798989891987545698969999867895698784898999975479989899996434799412 | ||||||
|  | 6599965322334589439654568988632345679878790198657789657899978954987673457899867365678998987645689323 | ||||||
|  | 5439986437645678998965679999741235698765689249878894545989989543499452349999954234567997898876898934 | ||||||
|  | 6598997598786789346896799876432346987654578998989943239879996532598321298987632123459876769987897895 | ||||||
|  | 7987698679897991249997989976545469876553456897699532198767897921987532457996543234567965457998956789 | ||||||
|  | 9876598799989892398789978898656798765432567989578956987656799890197643568919755365778994356789345991 | ||||||
|  | 4965429899876789987679856789987899887543569876456799876545678789987654578909979876799986467991234892 | ||||||
|  | 3989999998785678976598768993099989997678678989567898765432345678999765699698989989893599878910126789 | ||||||
|  | 2199878987654568968459899772134678999989789399878919874321234569329876789597696799931298989321345899 | ||||||
|  | 3298967898123978957345989654545789999799891265989109783210237789539987899986545678920987599865456789 | ||||||
|  | 5987656799357889543213478987679899987688910234594298654341546789998999998765434567939875476996768891 | ||||||
|  | 9876545678968997657302367898998999654567891345896498765656756789897932359654325899898764345987899992 | ||||||
|  | 0987858799878999898212456999897987653687992556789989898789867898776899498763216789799897657898956789 | ||||||
|  | 1298878892989789949323789899656899432456789968999876989893978986565778999865397997679998767939345899 | ||||||
|  | 2459999921094694339875698798645999754867999899998765678932989985434668999986468997544349898921234589 | ||||||
|  | 3456796539123989210976988659534678967989656789999754569321299876323456889987989985431239979310125679 | ||||||
|  | 4577987698999877921989876547623899978996545698789543998954398765412345678998999874310198765432457789 | ||||||
|  | 5699898987987656799999965432105678989998634987678959897899459898937856789239910965621349976653567897 | ||||||
|  | 6988769876576545678999876543212389997898745696599998756978999987656787890129899899435698999768678946 | ||||||
|  | 9876556997432434567989987655424678976789896985489999545658789899867898991998798778945997898979989236 | ||||||
|  | 7654445698521015998979899876435789435689987976567899431345698789878999989899654567899876777899896345 | ||||||
|  | 9943236997732129879767789976545678923567898987689998910123597698989998976778943456798765656789765456 | ||||||
|  | 8895349876545299765656687987676899212345999898789997893234986567999987755667892538569654545678996867 | ||||||
|  | 6699656987856987664335496798787932101398987789899986789359975436899876544459931023498763235678989978 | ||||||
|  | 4578967998967976543213345679898943214567986679998775678998765325798765432368892254797654357799678989 | ||||||
|  | 3456798959878987654101277893999654323678974567987654999469965434697654321236794345698765689897577991 | ||||||
|  | 1346789341989798794212368974698768634569765678996543894349987545698776450125689456799876799998456910 | ||||||
|  | 0256893210395679887533457965899977545678986899987654789498987656789986543234678967987989899329967891 | ||||||
|  | 2347894321234567997646567899934988678789398921098765699997898767893499767455789879976794978919878989 | ||||||
|  | 7658999459495678999857678998923598789891239999239879797896409898931239876566791998765623769901989878 | ||||||
|  | 8768998998989789999969899567899699897990198978999998986789212999540123987679890987654104457892398767 | ||||||
|  | 9979987997678998979878943457968987956789987866789876765678923987321234599989999996543212398943987658 | ||||||
|  | 0989876789567897569989632123459876545698896745998765653589899876595445678997898987985323689956988734 | ||||||
|  | 1998954453456789498996545234678995434987654235987654212456789987986556799656987799876434567899879823 | ||||||
|  | 9876543212345679357987654345789986524499875123598764343457897898997677897649876653987895678989954312 | ||||||
|  | 2998754523456789267999865856892987612398765435679875654667976549098788985434995432198976789679875324 | ||||||
|  | 1239895434579890178923988767921098201239976576799986765679989632129999876545986543249987893498765435 | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,143 @@ | ||||||
|  | const std = @import("std"); | ||||||
|  | 
 | ||||||
|  | pub fn main() anyerror!void { | ||||||
|  |     var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||||||
|  |     defer arena.deinit(); | ||||||
|  |     const alloc = &arena.allocator; | ||||||
|  | 
 | ||||||
|  |     // Read our input | ||||||
|  |     var f = try std.fs.cwd().openFile("input", .{}); | ||||||
|  |     defer f.close(); | ||||||
|  |     var contents = try f.readToEndAlloc(alloc, std.math.maxInt(u32)); | ||||||
|  |     defer alloc.free(contents); | ||||||
|  | 
 | ||||||
|  |     var x: usize = 0; | ||||||
|  |     var y: usize = 0; | ||||||
|  |     var it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     while (it.next()) |l| { | ||||||
|  |         x = l.len; | ||||||
|  |         y += 1; | ||||||
|  |     } | ||||||
|  |     std.log.debug("Map is {}, {}", .{x, y}); | ||||||
|  | 
 | ||||||
|  |     const width = x; | ||||||
|  |     const height = y; | ||||||
|  |     var map = try alloc.alloc(u8, x * y); | ||||||
|  |     defer alloc.free(map); | ||||||
|  | 
 | ||||||
|  |     it = std.mem.tokenize(contents, "\n"); | ||||||
|  |     y = 0; | ||||||
|  |     while (it.next()) |line| { | ||||||
|  |         x = 0; | ||||||
|  |         for (line) |c, i| { | ||||||
|  |             map[(width * y) + x] = try std.fmt.parseInt(u8, line[i..i+1], 10); | ||||||
|  |             x += 1; | ||||||
|  |         } | ||||||
|  |         y += 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Determine local minima | ||||||
|  |     var local_minima = std.AutoHashMap(Point, u8).init(alloc); | ||||||
|  |     defer local_minima.deinit(); | ||||||
|  |     for (map) |v, i| { | ||||||
|  |         var adjacents = get_adjacent_points(i, width, height); | ||||||
|  |         //std.log.debug("Index {}: adjacents {any}", .{i, adjacents}); | ||||||
|  |         var is_minima = true; | ||||||
|  |         for (adjacents) |adj| { | ||||||
|  |             if (adj) |a| { | ||||||
|  |                 if (map[a] <= v) { | ||||||
|  |                     // std.log.debug( | ||||||
|  |                     //     "Index {} (Point {},{}) not local minima due to index {} (Point {},{}): {} <= {}", | ||||||
|  |                     //     .{i, i % width, i / height, a, a % width, a / height, map[a], v}); | ||||||
|  |                     is_minima = false; | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         if (is_minima) { | ||||||
|  |             try local_minima.put(.{.x = i % width, .y = i / height}, v); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     std.log.debug("Found {} local minima", .{local_minima.count()}); | ||||||
|  |     var mit = local_minima.iterator(); | ||||||
|  |     var danger_value: u16 = 0; | ||||||
|  |     while (mit.next()) |kv| { | ||||||
|  |         danger_value += kv.value_ptr.* + 1; | ||||||
|  |     } | ||||||
|  |     std.log.info("[Part 1] The risk level for all the low points on the map is {}", | ||||||
|  |                  .{danger_value}); | ||||||
|  | 
 | ||||||
|  |     var basin_sizes = std.ArrayList(usize).init(alloc); | ||||||
|  |     defer basin_sizes.deinit(); | ||||||
|  |     mit = local_minima.iterator(); | ||||||
|  |     while (mit.next()) |kv| { | ||||||
|  |         var point = kv.key_ptr.*; | ||||||
|  |         var points_to_check = std.ArrayList(Point).init(alloc); | ||||||
|  |         defer points_to_check.deinit(); | ||||||
|  |         try points_to_check.append(point); | ||||||
|  |         var basin = std.AutoHashMap(Point, void).init(alloc); | ||||||
|  |         defer basin.deinit(); | ||||||
|  |         try basin.put(point, undefined); | ||||||
|  |         while (points_to_check.items.len > 0) { | ||||||
|  |             point = points_to_check.pop(); | ||||||
|  |             var i = point.x + point .y * width; | ||||||
|  |             var adjacents = get_adjacent_points(i, width, height); | ||||||
|  |             for (adjacents) |adj| { | ||||||
|  |                 if (adj) |a| { | ||||||
|  |                     if (map[a] != 9) { | ||||||
|  |                         point = .{.x = a % width, .y = a / width}; | ||||||
|  |                         if (basin.contains(point)) { | ||||||
|  |                             continue; | ||||||
|  |                         } | ||||||
|  |                         else { | ||||||
|  |                             try points_to_check.append(point); | ||||||
|  |                             try basin.put(point, undefined); | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         try basin_sizes.append(basin.count()); | ||||||
|  |         //std.log.debug("Basin for {}, {} is of size {}", | ||||||
|  |         // .{kv.key_ptr.*.x, kv.key_ptr.*.y, basin.count()}); | ||||||
|  |     } | ||||||
|  |     std.sort.sort(usize, basin_sizes.items, {}, comptime std.sort.desc(usize)); | ||||||
|  |     std.log.info("[Part 2] Product of 3 largest basins is: {}", | ||||||
|  |                  .{basin_sizes.items[0] * basin_sizes.items[1] * basin_sizes.items[2]}); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_adjacent_points(i: usize, width: usize, height: usize) [4]?usize { | ||||||
|  |     var adjacents = [4] ?usize { | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |         null, | ||||||
|  |     }; | ||||||
|  |     var ai: usize = 0; | ||||||
|  |     if ((i % width) != 0) { | ||||||
|  |         // left side | ||||||
|  |         adjacents[ai] = i - 1; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (i >= width) { | ||||||
|  |         // top side | ||||||
|  |         adjacents[ai] = i - width; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if ((i % width) != (width - 1)) { | ||||||
|  |         // right side | ||||||
|  |         adjacents[ai] = i + 1; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     if (i < ((height - 1) * width)) { | ||||||
|  |         // bottom side | ||||||
|  |         adjacents[ai] = i + width; | ||||||
|  |         ai += 1; | ||||||
|  |     } | ||||||
|  |     return adjacents; | ||||||
|  | } | ||||||
|  | pub const Point = struct { | ||||||
|  |     x: usize, | ||||||
|  |     y: usize, | ||||||
|  | }; | ||||||
		Loading…
	
		Reference in New Issue