Skip to content

Instantly share code, notes, and snippets.

View AntonFagerberg's full-sized avatar

Anton Fagerberg AntonFagerberg

View GitHub Profile
@AntonFagerberg
AntonFagerberg / aoc_1.hs
Created November 2, 2016 20:58
Advent of code 2015 - 1 - Haskell
num :: Char -> Int
num '(' = 1
num ')' = -1
main = readFile "input" >>= putStrLn . show . sum . map num
f :: (Int, Int) -> Int -> (Int, Int)
f (a, b) c = (a + c, b + 1)
cellar :: (Int, Int) -> Bool
object Example extends App {
def DynamicOption(value: DynamicType) = Option(value)
val a = DynamicOption(1)
val b = DynamicOption("hello")
val result: Option[DynamicType] =
for {
aa <- a
bb <- b
} yield {
inputs = ["turn on 499,499 through 500,500"]
res = inputs |> Enum.reduce(%{}, fn (input, acc) ->
[[_, cmd, x1, y1, x2, y2]] = Regex.scan(~r/(turn (?:on|off)|toggle) (\d+),(\d+) through (\d+),(\d+)/, input)
x1 = String.to_integer(x1)
x2 = String.to_integer(x2)
y1 = String.to_integer(y1)
object Day24 extends App {
val test = List(1,2,3,4,5,7,8,9,10,11).mkString("\n")
val input =
"""
|1
|3
|5
|11
|13
defmodule Day18 do
def neighbor_index(x, y) do
Enum.flat_map((x - 1)..(x + 1), fn xx ->
Enum.flat_map((y - 1)..(y + 1), fn yy ->
if x == xx && y == yy, do: [], else: [{xx, yy}]
end)
end)
end
def solve(input, iterations, part_2 \\ false) do
defmodule Day_17 do
def solve(input, target) do
Regex.scan(~r/-?\d+/, input)
|> Enum.map(&hd/1)
|> Enum.map(&Integer.parse/1)
|> Enum.map(&(elem(&1, 0)))
|> process([], target)
end
defp process([], acc, target) do
defmodule Day12 do
def run do
"/Users/anton/Desktop/input"
|> File.read!
|> Poison.decode!
|> process
end
def process(item) when is_map(item) do
val input = """Tristram to AlphaCentauri = 34
Tristram to Snowdin = 100
Tristram to Tambi = 63
Tristram to Faerun = 108
Tristram to Norrath = 111
Tristram to Straylight = 89
Tristram to Arbre = 132
AlphaCentauri to Snowdin = 4
AlphaCentauri to Tambi = 79
AlphaCentauri to Faerun = 44
input = File.read!("/Users/anton/Desktop/input") |> String.split("\n")
a = input |> Enum.map(&String.length/1) |> Enum.sum
b = input |> Enum.map(fn a -> Regex.scan(~r/(\\x[a-f0-9]{2}|\\\"|\\\\|\")/, a) |> length end) |> Enum.map(&(&1 - 2)) |> Enum.sum
c = input |> Enum.map(fn a -> Regex.split(~r/(\\x[a-f0-9]{2}|\\\"|\\\\|\")/, a, trim: true) |> Enum.map(&String.length/1) |> Enum.sum end) |> Enum.sum
a - b - c
## Part 2
import Bitwise
# inputs = [
# "123 -> x",
# "456 -> y",
# "x AND y -> d",
# "x OR y -> e",
# "x LSHIFT 2 -> f",
# "y RSHIFT 2 -> g",
# "NOT x -> h",