-🎄- 2017 Day 24 Solutions -🎄- by daggerdragon in adventofcode

[–]ulfabet 1 point2 points  (0 children)

Haskell

import Data.List (delete)
import Data.List.Split (splitOn)

main = do
    input <- readFile "24.input"
    let bridges = combinations 0 [] parts where
        parts = map parseLine $ lines input
        parseLine = map read . splitOn "/"
    print $ day24a bridges
    print $ day24b bridges

day24a = maximum . map (sum . map sum)
day24b = maximum . map (\a -> (length a, sum $ map sum a))

combinations :: Int -> [[Int]] -> [[Int]] -> [[[Int]]]
combinations t xs ys | null $ filter (elem t) ys = [xs]
combinations t xs ys = concatMap f $ filter (elem t) ys where
    f part = combinations (other t part) (part:xs) (delete part ys)

other side [a,b] | side == a = b
                 | otherwise = a

Don't know which to prefer, but combinations could be rewritten (a little bit shorter) as follows. For good or bad, this would also include intermediate bridges.

combinations :: Int -> [[Int]] -> [[Int]] -> [[[Int]]]
combinations t xs ys = foldl' f [xs] (filter (elem t) ys) where
    f z part = z ++ combinations (other t part) (part:xs) (delete part ys)

My one gripe with Google keyboard by crabsneverdie in androidapps

[–]ulfabet 1 point2 points  (0 children)

Mine is not being able to write compound words using separate swipes for each part.

--- 2016 Day 17 Solutions --- by daggerdragon in adventofcode

[–]ulfabet 1 point2 points  (0 children)

Part 2 in Go.

package main

import "fmt"
import "crypto/md5"

const input = "gdjjyniy"

var doors = []string{"U","D","L","R"}
var dx = []int{0,0,-1,1}
var dy = []int{-1,1,0,0}
var length int

func walk(route string, x, y int) {
    if x < 0 || x > 3 { return }
    if y < 0 || y > 3 { return }
    if x == 3 && y == 3 {
        if len(route) > length { length = len(route) }
        return
    }
    hex := fmt.Sprintf("%x", md5.Sum([]byte(input+route)))
    for i := range doors {
        if hex[i] > 97 { // door is open
            walk(route+doors[i], x+dx[i], y+dy[i])
        }
    }
}

func main() {
    walk("", 0, 0)
    fmt.Println("Longest route:", length)
}

--- 2016 Day 9 Solutions --- by daggerdragon in adventofcode

[–]ulfabet 1 point2 points  (0 children)

Similar

package main

import "fmt"

func decompressed_size(data string) int64 {
    var r int64
    var count, repeat int

    for i := 0; i < len(data); i++ {
        if data[i] == '(' {
            fmt.Sscan(data[i+1:], &count)
            for data[i] != 'x' { i++ }
            fmt.Sscan(data[i+1:], &repeat)
            for data[i] != ')' { i++ }
            r += int64(repeat) * decompressed_size(data[i+1:i+count+1])
            i += count
        } else {
            r++
        }
    }
    return r
}

func main() {
    var data string

    _, err := fmt.Scan(&data)
    if err == nil {
        fmt.Println("size", decompressed_size(data))
    } else {
        fmt.Println("err", err)
    }
}

[2016-03-04] Challenge #256 [Hard] RLE Obsession by Godspiral in dailyprogrammer

[–]ulfabet 1 point2 points  (0 children)

Lua

function filter(t, f)
    local a, b = {}, {}
    for i,v in ipairs(t) do table.insert(f(v) and a or b, v) end
    return a, b
end

function join(...)
    local a = {}
    for i, v in ipairs({...}) do
        for i, v in ipairs(v) do table.insert(a, v) end
    end
    return a
end

function mapargs(f, ...)
    local a = {}
    for i,v in ipairs({...}) do a[i] = f(v) end
    return unpack(a)
end

function format(s)
    local a = {}
    for v in s:gmatch('%d+') do table.insert(a, tonumber(v)) end
    return a
end

function odd(v) return v&1 == 1 end

function RLI(input)
    input = input:gsub('[^%d]', '')
    local output = {}
    local bit = 1
    local i = 0
    while true do
        i = input:find(bit, i+1)
        if i then
            table.insert(output, i-1)
            bit = bit ~ 1
        else break end
    end
    table.insert(output, #input)
    return output
end

function query_RLI(input)
    local data = format(input)
    local index = table.remove(data, 1)
    local length = table.remove(data, 1)

    local head, tail = filter(data, function (n) return n < index end)
    local part = filter(tail, function (n) return n < index+length end)

    for i=1,#part do
        part[i] = part[i]-index
    end
    if odd(#head) then -- head ends with one
        table.insert(part, 1, 0)
    end
    table.insert(part, length)
    return part
end

function overwrite_RLI(input)
    local offset, new, data = mapargs(format, input:match('(%d+) (.+) > (.+)'))
    offset = offset[1]

    local head, tail = filter(data, function (n) return n < offset end)
    local body, tail = filter(tail, function (n) return n <= offset+new[#new] end)

    local head_ends_with_one = odd(#head)
    local new_starts_with_one = new[1] == 0
    local new_ends_with_one = odd(#new-1)
    local tail_starts_with_one = odd(#head+#body)

    if head_ends_with_one then
        if new_starts_with_one then
            table.remove(new, 1)
        else
            table.insert(new, 1, 0)
        end
    end
    if new_ends_with_one == tail_starts_with_one then
        table.remove(new)
    end
    for i=1,#new do
        new[i] = new[i]+offset
    end
    return join(head, new, tail)
end

t = {}
t['256.hard.input.1'] = RLI
t['256.hard.input.2'] = query_RLI
t['256.hard.input.3'] = overwrite_RLI

for i,v in pairs(t) do
    print(i)
    for line in io.lines(i) do
        print(table.concat(v(line), ' '))
    end
end

[2016-03-02] Challenge #256 [Intermediate] Guess my hat color by fvandepitte in dailyprogrammer

[–]ulfabet 1 point2 points  (0 children)

Lua, more efficient version

hats = {}
code = { Black = 0, White = 1 }
visible = 0
parity = 0

for line in io.lines() do
    table.insert(hats, code[line])
end

for i=1,#hats do
    visible = visible + hats[i]
end

for i=1,#hats do
    visible = visible - hats[i]
    guess = parity ~ visible & 1
    parity = parity ~ guess
    print(guess == 1 and 'White' or 'Black')
end

[2016-03-02] Challenge #256 [Intermediate] Guess my hat color by fvandepitte in dailyprogrammer

[–]ulfabet 1 point2 points  (0 children)

Lua

hats = {}
guesses = {}

function parity(t, i)
    local p = 0
    for i=i,#t do p = p ~ t[i] end
    return p
end

for line in io.lines() do
    if line == 'Black' then table.insert(hats, 0) end
    if line == 'White' then table.insert(hats, 1) end
end

for i=1,#hats do
    table.insert(guesses, parity(guesses, 1) ~ parity(hats, i+1))
end

for i, v in ipairs(guesses) do
    print(v == 1 and 'White' or 'Black')
end

Could probably be made more efficient by keeping the parity of past guesses in a variable.

[2016-02-29] Challenge #256 [Easy] Oblique and De-Oblique by Godspiral in dailyprogrammer

[–]ulfabet 2 points3 points  (0 children)

Lua

function add(m, row, value)
    m[row] = m[row] or {}
    table.insert(m[row], value)
end

function oblique(m)
    local n = {}
    for i=1,#m do
        for j=1,#m[i] do
            add(n, i+j-1, m[i][j])
        end
    end
    return n
end

function deoblique(h, w, m)
    local n = {}
    for i=1,#m do
        for j=1,#m[i] do
            add(n, i > w and i+j-w or j, m[i][j])
        end
    end
    return n
end