r/adventofcode • • Dec 22 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 22 Solutions -๐ŸŽ„-

--- Day 22: Sporifica Virus ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


  • [T-10 to launch] AoC ops, /r/nocontext edition:

    • <Endorphion> You may now make your waffle.
    • <Endorphion> ... on Mars.
  • [Update @ 00:17] 50 gold, silver cap

    • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy
    • <Topaz> that doesn't seem necessary
    • <Aneurysm9> what does "necessary" have to do with anything!
  • [Update @ 00:20] Leaderboard cap!

    • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE

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!

8 Upvotes

174 comments sorted by

View all comments

Show parent comment

2

u/Arkoniak Dec 22 '17

And solution with initialized array instead of Dict (took only 0.1s on my machine for part 2)

function read_map(filename::String, w = 100001)
    res = ones(UInt8, (w, w))
    lns = readlines(joinpath(@__DIR__, filename))
    offset_y = div(w - length(lns[1]), 2)
    offset_x = div(w - length(lns), 2)
    for (i, ln) in enumerate(lns)
        for (j, c) in enumerate(collect(ln))
            (c == '#') && (res[offset_x + i, offset_y + j] = UInt8(3))
        end
    end
    res
end

const DIRS = [(-1, 0), (0, 1), (1, 0), (0, -1)]

function solve_puzzle2(filename::String, w = 100001, activity = 10000)
    dir = 1
    infected = read_map(filename, w)
    pos = (div(w - 1, 2) + 1, div(w - 1, 2) + 1)
    cnt = 0
    for _ in 1:activity
        node = infected[pos...]
        infected[pos...] += UInt8(1)
        (infected[pos...] == UInt8(5)) && (infected[pos...] = UInt8(1))
        if node == 1
            dir -= 1
            (dir == 0) && (dir = 4)
        elseif node == 2
            cnt += 1
        elseif node == 3
            dir += 1
            (dir == 5) && (dir = 1)
        else # node == 4
            dir += 2
            (dir == 5) && (dir = 1)
            (dir == 6) && (dir = 2)
        end
        pos = pos .+ DIRS[dir]
    end

    cnt
end

@test solve_puzzle2("puzzle22_1.txt", 101, 100) == 26
@test solve_puzzle2("puzzle22_1.txt", 10001, 10000000) == 2511944 

2

u/oantolin Dec 22 '17

Ooh! I should try that change in my solution.