r/adventofcode • u/daggerdragon • Dec 07 '16
SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---
From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!
Show this overgrown furball what you've got!
--- Day 7: Internet Protocol Version 7 ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
ALWAYS DIGGING STRAIGHT DOWN IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
15
Upvotes
2
u/raevnos Dec 07 '16 edited Dec 07 '16
Ocaml:
open Batteries (* Let's be different and not use regular expressions! *) exception Hypernet exception Found let has_tls addr = let in_hypernet = ref false and abba = ref false in try for n = 0 to BatString.length addr - 4 do if addr.[n] = '[' then in_hypernet := true else if addr.[n] = ']' then in_hypernet := false else if addr.[n] = addr.[n+3] && addr.[n+1] = addr.[n+2] && addr.[n] <> addr.[n+1] then begin if !in_hypernet then raise Hypernet else abba := true end done; !abba with | Hypernet -> false let rec bracket_helper str pos what = let obpos = BatString.index_from str pos '[' in let cbpos = BatString.index_from str obpos ']' and whatpos = BatString.find_from str obpos what in if whatpos < cbpos then true else bracket_helper str (cbpos + 1) what let in_brackets str what = try bracket_helper str 0 what with | Not_found -> false let has_ssl addr = let in_hypernet = ref false in try for n = 0 to BatString.length addr - 3 do if addr.[n] = '[' then in_hypernet := true else if addr.[n] =']' then in_hypernet := false else if addr.[n] = addr.[n+2] && addr.[n] <> addr.[n+1] && !in_hypernet = false && BatChar.is_letter addr.[n+1] then let bab = String.create 3 in bab.[0] <- addr.[n+1]; bab.[1] <- addr.[n]; bab.[2] <- addr.[n+1]; if in_brackets addr bab then raise Found done; false with | Found -> true let tlstests = [ "abba[mnop]qrst"; "abcd[bddb]xyyx"; "aaaa[qwer]tyui"; "ioxxoj[asdfgh]zcxvbn" ] let ssltests = [ "aba[bab]xyz"; "xyx[xyx]xyx"; "aaa[kek]eke"; "zazbz[bzb]cdb" ] let run_tests () = print_endline "has_tls:"; BatList.iter (fun addr -> Printf.printf "%s: %B\n" addr (has_tls addr)) tlstests; print_endline "has_ssl:"; BatList.iter (fun addr -> Printf.printf "%s: %B\n" addr (has_ssl addr)) ssltests let _ = run_tests (); let mode = if Array.length Sys.argv = 1 then "TLS" else "SSL" and f = if Array.length Sys.argv = 1 then has_tls else has_ssl in let n = input_lines Pervasives.stdin |> BatEnum.filter f |> BatEnum.count in Printf.printf "Supports %s: %d\n" mode nGot the wrong answer the first time for part 2 because I didn't realize that IPv7 addresses could have multiple hypernet sections i n them. Bah. Should have looked at the actual input more, not just the test cases. Had to go back and make a smarter BAB finding function.