Mix . install ( [ { :finch , "~> 0.14.0" } ] )
Finch . start_link ( name: NBFinch )
defmodule AOC do
def input ( day ) do
year = 2022
link = "https://adventofcode.com/#{ year } /day/#{ day } /input"
cookie = "session=#{ System . fetch_env! ( "LB_AOC_SESSION_TOKEN" ) } "
req = Finch . build ( :get , link , [ { "cookie" , cookie } ] )
{ :ok , resp } = Finch . request ( req , NBFinch )
resp . body
end
end
i01
|> String . split ( "\n \n " )
|> Enum . map ( fn x -> x |> String . split ( ) |> Enum . map ( & String . to_integer / 1 ) |> Enum . sum ( ) end )
|> Enum . sort ( :desc )
|> List . first ( )
i01
|> String . split ( "\n \n " )
|> Enum . map ( fn x -> x |> String . split ( ) |> Enum . map ( & String . to_integer / 1 ) |> Enum . sum ( ) end )
|> Enum . sort ( :desc )
|> Enum . take ( 3 )
|> Enum . sum ( )
Day II Common Set-Up & Input
you = % {
"A" => :rock ,
"X" => :rock ,
"B" => :paper
}
me = % {
"Y" => :paper ,
"C" => :scissors ,
"Z" => :scissors
}
code = Map . merge ( you , me )
score = % { rock: 1 , paper: 2 , scissors: 3 }
{ lost , drew , won } = { 0 , 3 , 6 }
outcome = % {
{ :rock , :rock } => drew ,
{ :rock , :paper } => won ,
{ :rock , :scissors } => lost ,
{ :paper , :rock } => lost ,
{ :paper , :paper } => drew ,
{ :paper , :scissors } => won ,
{ :scissors , :rock } => won ,
{ :scissors , :paper } => lost ,
{ :scissors , :scissors } => drew
}
i02
|> String . split ( "\n " , trim: true )
|> Enum . map ( & String . split / 1 )
|> List . flatten ( )
|> Enum . map ( & Map . fetch! ( code , & 1 ) )
|> Enum . chunk_every ( 2 )
|> Enum . map ( & List . to_tuple / 1 )
|> Enum . map ( fn { _ , me } = play ->
score [ me ] + outcome [ play ]
end )
|> Enum . sum ( )
you = % {
"A" => :rock ,
"B" => :paper ,
"C" => :scissors
}
me = % {
"X" => :lose ,
"Y" => :draw ,
"Z" => :win
}
code = Map . merge ( you , me )
play = % {
{ :rock , :lose } => :scissors ,
{ :rock , :draw } => :rock ,
{ :rock , :win } => :paper ,
{ :paper , :lose } => :rock ,
{ :paper , :draw } => :paper ,
{ :paper , :win } => :scissors ,
{ :scissors , :lose } => :paper ,
{ :scissors , :draw } => :scissors ,
{ :scissors , :win } => :rock
}
i02
|> String . split ( "\n " , trim: true )
|> Enum . map ( & String . split / 1 )
|> List . flatten ( )
|> Enum . map ( & Map . fetch! ( code , & 1 ) )
|> Enum . chunk_every ( 2 )
|> Enum . map ( & List . to_tuple / 1 )
|> Enum . map ( fn { _ , outcome } = strat ->
score [ play [ strat ] ] + % { lose: 0 , draw: 3 , win: 6 } [ outcome ]
end )
|> Enum . sum ( )
defmodule Day9Part1 do
def adjecent? ( { x , y } = _h , t ) do
square = for dx <- [ - 1 , 0 , 1 ] , dy <- [ - 1 , 0 , 1 ] , do: { x + dx , y + dy }
t in square
end
def same_row? ( { _x , y } , { _u , v } ) , do: y === v
def same_col? ( { x , _y } , { u , _v } ) , do: x === u
def h_trace ( i ) do
parity = % {
"L" => { - 1 , 0 } ,
"R" => { + 1 , 0 } ,
"U" => { 0 , + 1 } ,
"D" => { 0 , - 1 }
}
i
|> String . split ( )
|> Enum . chunk_every ( 2 )
|> Enum . map ( & List . to_tuple / 1 )
|> Enum . map ( fn { d , i } -> { d , String . to_integer ( i ) } end )
|> Enum . map ( fn { d , i } -> for _ <- 1 .. i , do: parity [ d ] end )
|> List . flatten ( )
|> Enum . scan ( { 0 , 0 } , fn { dx , dy } , { x , y } -> { x + dx , y + dy } end )
end
def t_trace ( h , { tx , ty } = t ) do
cond do
adjecent? ( h , t ) ->
t
same_row? ( h , t ) ->
opts = [
{ tx + 1 , ty } ,
{ tx - 1 , ty }
]
Enum . find ( opts , & adjecent? ( h , & 1 ) )
same_col? ( h , t ) ->
opts = [
{ tx , ty + 1 } ,
{ tx , ty - 1 }
]
Enum . find ( opts , & adjecent? ( h , & 1 ) )
true ->
opts = [
{ tx + 1 , ty + 1 } ,
{ tx + 1 , ty - 1 } ,
{ tx - 1 , ty + 1 } ,
{ tx - 1 , ty - 1 }
]
Enum . find ( opts , & adjecent? ( h , & 1 ) )
end
end
end
i09
|> h_trace ( )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . uniq ( )
|> Enum . count ( )
i09
|> h_trace ( )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . scan ( { 0 , 0 } , & t_trace / 2 )
|> Enum . uniq ( )
|> Enum . count ( )