using Pipe
elfCals = @pipe readlines("./input-day1-1.txt") |> cat([""], _, [""], dims=1) # read input
idx=findall(==(""), elfCals) # find where the spaces are
splitCals = @. getindex([elfCals], (:).(idx[1:end-1] + 1, idx[2:end] - 1)) # split on spaces
maxCal = @pipe map.(x->parse(Int, x), splitCals) |> # parse to ints
map(x->sum(x), _) |> # sum the vectors (each elf)
maximum # find the maximum
#lang racket
(require threading)
(define inp (file->lines "./input-day1-1.txt")) ; collect input
(define (partition-on lst elem) ; function to partition on the `elem's of a list
(define pred (lambda (x) (not (equal? x elem))))
(cond
((not (member elem lst)) (cons lst null))
((null? (cdr lst)) null)
(else (cons (takef lst pred)
(partition-on (cdr (dropf lst pred))
elem)))))
(define (tree-map proc tree) ; apply proc to all leaves in tree
(cond ((null? tree) null)
((pair? tree)
(cons
(tree-map proc (car tree))
(tree-map proc (cdr tree))))
(else (proc tree))))
;; Cleaning input
;; Part 1: Calories of elf with the most food
(~>> inp
(partition-on _ "") ; partition list on ""
(tree-map string->number) ; convert leaves to numbers
(map (lambda (x) (foldl + 0 x))) ; sum each sublist
(apply max)) ; find the max of these sums
;; Part 2: Sum of top 3 elves with most food
(~>> inp
(partition-on _ "") ; partition list on ""
(tree-map string->number) ; convert leaves to numbers
(map (lambda (x) (foldl + 0 x))) ; sum each sublist
(sort _ >) ; sort sums in descending order
(take _ 3) ; take 3 highest sums
(apply +)) ; add them together
#lang racket
(require advent-of-code)
(define calorie-data (fetch-aoc-input (find-session) 2022 1)) ; cannot get this working
(define calories (read-aoc-input)) ; cannot get this working
(define (calorie-+ elf)
(~>> elf
(map string->number)
(foldl + 0)))
(~> calories
(string-split "\n\n")
(map (lambda v (~> v first (string-split "\n") calorie-+)) _)
(sort >)
(take 3)
(foldl + 0 _))
using Pipe
ingames = @pipe readlines("./input-day2.txt") |> map(x->split(x, " "), _) # read input
chars = vcat('A':'C', 'X':'Z');
repl = zip(string.(chars), repeat(1:3, 2)) |> Dict
nums = @pipe ingames .|> replace(_, collect(repl)...)
scorefun = function(g)
p = g[1]*g[2]; # product of game scores
g[1]==g[2] ? (3 + g[2]) : # draw if two shapes are same
p==3 && g[2]==3 ? (g[2]) : # loss
p==6 && g[2]==2 ? (g[2]) : # loss
p==2 && g[2]==1 ? (g[2]) : # loss
(6 + g[2]) # otherwise win!
end
map(scorefun, nums) |> sum # score every game, then sum scores
win(n) = n==1 ? 2 : # give me the shape I need to win against n
n==2 ? 3 :
n==3 ? 1 : 0
lose(n) = n==1 ? 3 : # give me a losing shape
n==2 ? 1 :
n==3 ? 2 : 0
score2 = function(g) # calculate my score for a game
op = g[1]
t = g[2]
t==1 ? (lose(op)) :
t==2 ? (3 + op) :
(6 + win(op))
end
map(score2, nums) |> sum # calculate and sum the scores of every game
using Pipe
input = readlines("./day3-input.txt")
# construct score lookup dict
alphabet = cat('a':'z', 'A':'Z', dims=1);
scoreLookup = zip(alphabet, 1:52) |> Dict
inthalf(string) = @pipe string |> length(_)/2 |> convert(Int, _) # find half of contents size
operation(string) = @pipe string |>
[(first(_, inthalf(_))), (last(_, inthalf(_)))] |> # split contents
intersect(_...) |> # find intersection
scoreLookup[_...] # lookup score
operation.(input) |> sum # do this for all rucksacks
using IterTools
score2 = @pipe partition(input, 3, 3) .|>
intersect(_...) .|>
scoreLookup[_...] |>
sum