Skip to content

Commit d9d7184

Browse files
committed
day 5
1 parent d9df660 commit d9d7184

File tree

2 files changed

+561
-0
lines changed

2 files changed

+561
-0
lines changed

Diff for: day5.fsx

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
let stacks, moves =
2+
let lines = System.IO.File.ReadAllLines("day5.input")
3+
let sepIndex = lines |> Array.findIndex Seq.isEmpty
4+
let linesStacks = seq { 0 .. sepIndex - 2 } |> Seq.map (fun i -> lines[i])
5+
6+
let linesMoves =
7+
seq { sepIndex + 1 .. Array.length lines - 1 } |> Seq.map (fun i -> lines[i])
8+
9+
let stacks =
10+
linesStacks
11+
|> Seq.map (fun s -> s |> Seq.chunkBySize 4 |> Seq.map (fun x -> x[1]) |> Seq.toArray)
12+
|> Array.transpose
13+
|> Array.map (Array.filter ((<>) ' ') >> Array.toList)
14+
15+
let moves =
16+
let re =
17+
System.Text.RegularExpressions.Regex "move ([0-9]+) from ([0-9]+) to ([0-9]+)"
18+
19+
linesMoves
20+
|> Seq.map (fun s -> let g = re.Match(s).Groups in int g[1].Value, int g[2].Value, int g[3].Value)
21+
22+
stacks, moves
23+
24+
let applyMoves moves =
25+
(stacks, moves)
26+
||> Seq.fold (fun s (count, from, target) ->
27+
s
28+
|> Array.mapi (fun i _ ->
29+
if i = from - 1 then
30+
List.skip count s[i]
31+
elif i = target - 1 then
32+
List.take count s[from - 1] @ s[target - 1]
33+
else
34+
s[i]))
35+
36+
let getResult stacks =
37+
stacks |> Array.map List.head |> Seq.map string |> String.concat ""
38+
39+
let afterMoves1 =
40+
applyMoves (
41+
moves
42+
|> Seq.collect (fun (count, from, target) -> Seq.replicate count (1, from, target))
43+
)
44+
45+
let part1 = getResult afterMoves1
46+
printfn $"PART1: {part1}"
47+
48+
let afterMoves2 = applyMoves moves
49+
let part2 = getResult afterMoves2
50+
printfn $"PART2: {part2}"

0 commit comments

Comments
 (0)