Compare commits
4 Commits
6352486a0a
...
302c187e9f
Author | SHA1 | Date |
---|---|---|
aru | 302c187e9f | |
aru | 76c20b36c5 | |
aru | 4a0560482d | |
aru | e274addde4 |
|
@ -7,3 +7,8 @@
|
|||
|
||||
# OCaml build artifacts
|
||||
**/_build/**
|
||||
|
||||
# Direnv
|
||||
.direnv
|
||||
|
||||
**/obj/**
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
|
||||
{:paths ["."]
|
||||
:aliases
|
||||
{:nREPL {:extra-deps
|
||||
{nrepl/nrepl {:mvn/version "1.0.0"}}}}}
|
|
@ -0,0 +1,4 @@
|
|||
(ns aoc)
|
||||
|
||||
(defn transpose [coll]
|
||||
(apply mapv vector coll))
|
|
@ -1,6 +1,7 @@
|
|||
(ns day13
|
||||
(:require [clojure.string :as s]
|
||||
[clojure.edn :as edn]))
|
||||
[clojure.edn :as edn]
|
||||
[aoc]))
|
||||
|
||||
(defn parse-input [input]
|
||||
(->> (s/split input #"\n\n")
|
||||
|
@ -23,10 +24,6 @@
|
|||
diff (different-field-counts l r)]
|
||||
(= diff smudge-count)))
|
||||
|
||||
(defn transpose [coll]
|
||||
(for [i (range 0 (count (first coll)))]
|
||||
(apply str (map #(nth %1 i) coll))))
|
||||
|
||||
(defn reflection-line-at [smudge-count pattern]
|
||||
(->> (range 1 (count pattern))
|
||||
(filter (fn [i] (apply (partial is-mirrored? smudge-count) (split-at i pattern))))
|
||||
|
@ -36,7 +33,7 @@
|
|||
(let [with-smudge-count (partial reflection-line-at smudge-count)
|
||||
v (with-smudge-count pattern)]
|
||||
(if (nil? v)
|
||||
(with-smudge-count (transpose pattern))
|
||||
(with-smudge-count (aoc/transpose pattern))
|
||||
(* 100 v))))
|
||||
|
||||
(defn solution [smudge-count input]
|
|
@ -0,0 +1,200 @@
|
|||
(ns day14
|
||||
(:require [clojure.string :as s]
|
||||
[aoc]))
|
||||
|
||||
(defn parse-input [input]
|
||||
(s/split-lines input))
|
||||
|
||||
;; (defn column-load [col]
|
||||
;; (let [mw (count col)]
|
||||
;; (->> (map-indexed vector col)
|
||||
;; (remove (fn [[idx e]] (= e \.)))
|
||||
;; (reduce (fn [[sum weight] [idx e]]
|
||||
;; (case e
|
||||
;; \# [sum (dec (- mw idx))]
|
||||
;; \O [(+ sum weight) (dec weight)]
|
||||
;; \. [sum weight]))
|
||||
;; [0 mw])
|
||||
;; first)))
|
||||
|
||||
;; (defn expand [width [round cube]]
|
||||
;; (for [x (range 0 width)]
|
||||
;; (cond
|
||||
;; (round x) \O
|
||||
;; (cube x) \#
|
||||
;; :else \.)))
|
||||
|
||||
;; (defn column-tilt [col]
|
||||
;; (let [mw (count col)]
|
||||
;; (->> (map-indexed vector col)
|
||||
;; (remove (fn [[idx e]] (= e \.)))
|
||||
;; (reduce (fn [[round cube pos] [idx e]]
|
||||
;; (case e
|
||||
;; \# [round (conj cube idx) (inc idx)]
|
||||
;; \O [(conj round pos) cube (inc pos)]
|
||||
;; \. [round cube pos]))
|
||||
;; [#{} #{} 0])
|
||||
;; (take 2)
|
||||
;; (expand mw))))
|
||||
|
||||
;; (defn tilt-west [col]
|
||||
;; (map column-tilt col))
|
||||
|
||||
;; (defn tilt-east [col]
|
||||
;; (->> col
|
||||
;; (map reverse)
|
||||
;; tilt-west
|
||||
;; (map reverse)))
|
||||
|
||||
;; (defn tilt-north [col]
|
||||
;; (->> col
|
||||
;; aoc/transpose
|
||||
;; tilt-west
|
||||
;; aoc/transpose))
|
||||
|
||||
;; (defn tilt-south [col]
|
||||
;; (->> col
|
||||
;; aoc/transpose
|
||||
;; tilt-east
|
||||
;; aoc/transpose))
|
||||
|
||||
;; (defn display [col]
|
||||
;; (doseq [l col]
|
||||
;; (println (apply str l)))
|
||||
;; (println))
|
||||
|
||||
;; (defn tilt-around [col]
|
||||
;; (-> col tilt-north tilt-west tilt-south tilt-east))
|
||||
|
||||
;; (defn crude-cycle-detect [col]
|
||||
;; (reduce (fn [[cache state] cur]
|
||||
;; (let [cached (cache state)]
|
||||
;; (if cached
|
||||
;; (do
|
||||
;; (println (str "Cache hit " cur " = " (cache state)))
|
||||
;; (reduced [cached (- cur cached) cache]))
|
||||
;; [(assoc cache state cur) (tilt-around state)])))
|
||||
;; [{} col]
|
||||
;; (range 1 1001)))
|
||||
|
||||
;; (defn tilt-n-times [n col]
|
||||
;; (reduce (fn [acc _] (tilt-around acc)) col (range n)))
|
||||
|
||||
;; (defn score-grid [col]
|
||||
;; (reduce + (map column-load (aoc/transpose (tilt-north col)))))
|
||||
|
||||
;; (defn part2 [input]
|
||||
;; (let [[start period cache] (crude-cycle-detect input)
|
||||
;; rcache (into {} (map (fn [[k v]] [v k]) cache))
|
||||
;; n (+ start (mod (- 1000000000 start) period))]
|
||||
;; (first (filter (fn [i] (= 64 (score-grid (rcache i)))) (range 1 (count rcache))))))
|
||||
|
||||
;; (defn part2-bf [input]
|
||||
;; ;; (let [[start period cache] (crude-cycle-detect input)
|
||||
;; (let [[cache state] (crude-cycle-detect input)
|
||||
;; rcache (into {} (map (fn [[k v]] [v k]) cache))]
|
||||
;; (println (count rcache))
|
||||
;; (first (filter (fn [i] (= 64 (reduce + (map column-load (aoc/transpose (rcache i)))))) (range 0 10)))))
|
||||
|
||||
;; (let [parsed (parse-input (slurp "../data/14/example"))]
|
||||
;; (println (part2 parsed)))
|
||||
;; ;; (par)
|
||||
;; ;; (println (reduce + (map column-load (tilt-n-times (+ 141 82) parsed)))))
|
||||
;; ;; (crude-cycle-detect parsed))
|
||||
;; ;; (reduce + (map column-load (aoc/transpose (tilt-north parsed)))))
|
||||
|
||||
(defn to-grid [col]
|
||||
(let [mx (count col)
|
||||
my (count (first col))
|
||||
points (for [x (range mx)
|
||||
y (range my)
|
||||
:let [e (nth (nth col x) y)]]
|
||||
[[x y] e])]
|
||||
{:dimensions [mx my] :points (into {} points)}))
|
||||
|
||||
(defn pt+ [[x1 y1] [x2 y2]]
|
||||
[(+ x1 x2) (+ y1 y2)])
|
||||
|
||||
(defn direction-range [{[mx my] :dimensions p :points} direction [x y]]
|
||||
(case direction
|
||||
"north" (reverse (for [x (range x)] [x y]))
|
||||
"south" (for [x (range (inc x) mx)] [x y])
|
||||
"west" (reverse (for [y (range y)] [x y]))
|
||||
"east" (for [y (range (inc y) my)] [x y])))
|
||||
|
||||
(defn direction-add [direction]
|
||||
(case direction
|
||||
"north" #(pt+ %1 [(- %2) 0])
|
||||
"south" #(pt+ %1 [%2 0])
|
||||
"west" #(pt+ %1 [0 (- %2)])
|
||||
"east" #(pt+ %1 [0 %2])))
|
||||
|
||||
(defn move [g direction p]
|
||||
(let [dir-range (direction-range g direction p)
|
||||
dir-f (direction-add direction)
|
||||
path (map (fn [p] (get-in g [:points p])) dir-range)
|
||||
remaining (take-while #(not= % \#) path)
|
||||
offset (count (filter #(= % \.) remaining))]
|
||||
(dir-f p offset)))
|
||||
|
||||
(defn find-rocks [{points :points}]
|
||||
(->> points
|
||||
(filter (fn [[_ v]] (= v \O)))
|
||||
(map first)))
|
||||
|
||||
(defn update-grid [g rocks moved]
|
||||
(as-> g g
|
||||
(reduce (fn [acc cur] (assoc-in acc [:points cur] \.)) g rocks)
|
||||
(reduce (fn [acc cur] (assoc-in acc [:points cur] \O)) g moved)))
|
||||
|
||||
(defn move-rocks [g direction]
|
||||
(let [rocks (find-rocks g)
|
||||
moved (map (partial move g direction) rocks)]
|
||||
(update-grid g rocks moved)))
|
||||
|
||||
(defn display-grid [{[mx my] :dimensions p :points}]
|
||||
(dotimes [x mx]
|
||||
(dotimes [y my]
|
||||
(let [c (p [x y])]
|
||||
(print (or c \.))))
|
||||
(println)))
|
||||
|
||||
(defn move-cycle [g]
|
||||
(-> g
|
||||
(move-rocks "north")
|
||||
(move-rocks "west")
|
||||
(move-rocks "south")
|
||||
(move-rocks "east")))
|
||||
|
||||
(defn rock-weight [{[mx _] :dimensions :as g}]
|
||||
(->> (find-rocks g)
|
||||
(map (fn [[x y]] (- mx x)))
|
||||
(reduce + 0)))
|
||||
|
||||
(defn part1 [input]
|
||||
(-> input
|
||||
(move-rocks "north")
|
||||
rock-weight))
|
||||
|
||||
(defn crude-cycle-detect [{p :points :as g}]
|
||||
(reduce (fn [[cache state] cur]
|
||||
(let [cached (cache state)]
|
||||
(if cached
|
||||
(reduced [cached (- cur cached) cache])
|
||||
(let [calculated (move-cycle state)]
|
||||
[(assoc cache state cur) (move-cycle state)]))))
|
||||
[{} g]
|
||||
(range)))
|
||||
|
||||
(defn part2 [g]
|
||||
(let [[start period cache] (crude-cycle-detect g)
|
||||
rcache (into {} (map (fn [[k v]] [v k]) cache))
|
||||
n (+ start (mod (- 1000000000 start) period))]
|
||||
(rock-weight (rcache n))))
|
||||
|
||||
;; ;; Invoke with clj -M -m day14 input
|
||||
(defn -main [file & args]
|
||||
(let [i (parse-input (slurp file))
|
||||
g (to-grid i)]
|
||||
(println (str "Part 1: " (part1 g)))
|
||||
(println (str "Part 2: " (part2 g)))))
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
{:paths ["."]
|
||||
:aliases
|
||||
{:nREPL {:extra-deps
|
|
@ -0,0 +1,3 @@
|
|||
0 3 6 9 12 15
|
||||
1 3 6 10 15 21
|
||||
10 13 16 21 30 45
|
|
@ -0,0 +1,10 @@
|
|||
O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....
|
|
@ -0,0 +1 @@
|
|||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
|
@ -0,0 +1 @@
|
|||
use flake
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
description = "A flake for pulling in dependencies.";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs.url = "github:nixos/nixpkgs";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
in rec {
|
||||
devShells.default = with pkgs; mkShell {
|
||||
buildInputs = [ clojure ];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#time
|
||||
|
||||
open System
|
||||
open System.Text.RegularExpressions
|
||||
|
||||
let (|Regex|_|) pattern input =
|
||||
let m = Regex.Match(input, pattern)
|
||||
|
||||
if m.Success then
|
||||
Some(List.tail [ for g in m.Groups -> g.Value ])
|
||||
else
|
||||
None
|
||||
|
||||
type Lens = String * int
|
||||
|
||||
let steps (s: string) = s.Split [| ',' |]
|
||||
|
||||
let hash = Seq.fold (fun a b -> (17 * (a + (int) b)) % 256) 0
|
||||
|
||||
let part1 input = input |> Seq.sumBy hash
|
||||
|
||||
let rec replaceOrAppend (l: Lens list) (s, n) =
|
||||
match l with
|
||||
| [] -> [ (s, n) ]
|
||||
| (c, _) :: rest when c = s -> (s, n) :: rest
|
||||
| x :: xs -> x :: replaceOrAppend xs (s, n)
|
||||
|
||||
let part2 input =
|
||||
let foldStep acc step =
|
||||
match step with
|
||||
| Regex @"([a-z]+)-" [ s ] ->
|
||||
let idx = hash s
|
||||
Array.updateAt idx (List.filter (fun (c, _) -> not (c = s)) (Array.get acc idx)) acc
|
||||
| Regex @"([a-z]+)=([0-9]+)" (s :: n :: []) ->
|
||||
let idx = hash s
|
||||
Array.updateAt idx (replaceOrAppend (Array.get acc idx) (s, int n)) acc
|
||||
| _ -> failwith "Unreachable"
|
||||
|
||||
let arr = Array.init 256 (fun _ -> List.empty)
|
||||
|
||||
let folded = Seq.fold foldStep arr input
|
||||
|
||||
folded
|
||||
|> Seq.indexed
|
||||
|> Seq.map (fun (i, entries) -> entries |> Seq.indexed |> Seq.map (fun (ii, (_, n)) -> (i + 1, ii + 1, n)))
|
||||
|> Seq.concat
|
||||
|> Seq.sumBy (fun (box, pos, foc) -> box * pos * foc)
|
||||
|
||||
let main f =
|
||||
let s = f |> IO.File.ReadLines |> Seq.head |> steps
|
||||
|
||||
printfn "%A" (part1 s)
|
||||
printfn "%A" (part2 s)
|
||||
0
|
||||
|
||||
main fsi.CommandLineArgs.[1]
|
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1701680307,
|
||||
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1701680448,
|
||||
"narHash": "sha256-jyBmBxAIOgSxuViuffo4ah8+NRHfY/9ByozjiLk/rf4=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "6f3d9c7288802bd463f1419c1354a2d8b5ad1d9a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
description = "A flake for pulling in dependencies.";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs.url = "github:nixos/nixpkgs";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
in rec {
|
||||
devShells.default = with pkgs; mkShell {
|
||||
buildInputs = [ dotnet-sdk_8 fsautocomplete fantomas ];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
use flake
|
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1701680307,
|
||||
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1701680448,
|
||||
"narHash": "sha256-jyBmBxAIOgSxuViuffo4ah8+NRHfY/9ByozjiLk/rf4=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "6f3d9c7288802bd463f1419c1354a2d8b5ad1d9a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
use flake
|
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1701680307,
|
||||
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1701680448,
|
||||
"narHash": "sha256-jyBmBxAIOgSxuViuffo4ah8+NRHfY/9ByozjiLk/rf4=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "6f3d9c7288802bd463f1419c1354a2d8b5ad1d9a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
description = "A flake for pulling in dependencies.";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs.url = "github:nixos/nixpkgs";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
in rec {
|
||||
devShells.default = with pkgs; mkShell {
|
||||
buildInputs = [ nim nimlsp ];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
use flake
|
|
@ -0,0 +1,72 @@
|
|||
(use-modules (ice-9 textual-ports)
|
||||
(srfi srfi-1)
|
||||
(srfi srfi-8))
|
||||
|
||||
(define (read-input)
|
||||
(get-string-all (current-input-port)))
|
||||
|
||||
(define (string-present? s)
|
||||
(not (string-null? s)))
|
||||
|
||||
(define (parse-input line)
|
||||
(map parse-line
|
||||
(filter string-present? (string-split line #\Newline))))
|
||||
|
||||
(define (parse-line line)
|
||||
(define (parse-number-list l)
|
||||
(map string->number (filter string-present? (string-split l #\Space))))
|
||||
|
||||
(let* ((num-part (cadr (string-split line #\:)))
|
||||
(parts (string-split num-part #\|)))
|
||||
(map parse-number-list parts)))
|
||||
|
||||
(define (winning-count l)
|
||||
(let ((winning (car l))
|
||||
(current (cadr l)))
|
||||
(count (lambda (c) (member c winning)) current)))
|
||||
|
||||
(define (score v)
|
||||
(cond
|
||||
((= v 0) 0)
|
||||
((= v 1) 1)
|
||||
(#t (expt 2 (- v 1)))))
|
||||
|
||||
;; SRFI-1's take/drop panic if the list doesn't have enough elements
|
||||
(define (my-take l n)
|
||||
(if (or (null? l) (= n 0))
|
||||
'()
|
||||
(cons (car l) (my-take (cdr l) (- n 1)))))
|
||||
|
||||
(define (my-drop l n)
|
||||
(if (or (null? l) (= n 0))
|
||||
l
|
||||
(my-drop (cdr l) (- n 1))))
|
||||
|
||||
(define (part1 winning-counts)
|
||||
(reduce + 0 (map score winning-counts)))
|
||||
|
||||
(define (part2 winning-counts)
|
||||
(define (propagate-forward l count multiplier)
|
||||
(let ((h (my-take l count))
|
||||
(t (my-drop l count)))
|
||||
(append (map (lambda (x) (cons (car x) (+ (cdr x) multiplier))) h) t)))
|
||||
|
||||
|
||||
(let ((with-multiplier (map (lambda (x) (cons x 1)) winning-counts)))
|
||||
(let loop ((counts with-multiplier)
|
||||
(n 0))
|
||||
(if (null? counts)
|
||||
n
|
||||
(let* ((current (car counts))
|
||||
(x (car current))
|
||||
(multiplier (cdr current)))
|
||||
(loop (propagate-forward (cdr counts) x multiplier)
|
||||
(+ n multiplier)))))))
|
||||
|
||||
(let* ((input (parse-input (read-input)))
|
||||
(winning-counts (map winning-count input)))
|
||||
(begin
|
||||
(display (part1 winning-counts))
|
||||
(newline)
|
||||
(display (part2 winning-counts))
|
||||
(newline)))
|
|
@ -0,0 +1,60 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1701680307,
|
||||
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1701680448,
|
||||
"narHash": "sha256-jyBmBxAIOgSxuViuffo4ah8+NRHfY/9ByozjiLk/rf4=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "6f3d9c7288802bd463f1419c1354a2d8b5ad1d9a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
description = "A flake for pulling in dependencies.";
|
||||
|
||||
inputs = {
|
||||
|
||||
nixpkgs.url = "github:nixos/nixpkgs";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils, ... }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs { inherit system; };
|
||||
in rec {
|
||||
devShells.default = with pkgs; mkShell {
|
||||
buildInputs = [ guile_3_0 ];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue