-
Notifications
You must be signed in to change notification settings - Fork 0
/
5b.ex
executable file
·42 lines (37 loc) · 836 Bytes
/
5b.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/env elixir
result =
File.read!("5.csv")
|> String.split("\n", trim: true)
|> Enum.map(fn <<row::binary-size(7), col::binary-size(3)>> ->
row =
for <<c <- row>>, into: "" do
if c == ?B do
<<1::unsigned-size(1)>>
else
<<0::unsigned-size(1)>>
end
end
<<row::unsigned-size(7)>> = row
col =
for <<c <- col>>, into: "" do
if c == ?R do
<<1::unsigned-size(1)>>
else
<<0::unsigned-size(1)>>
end
end
<<col::unsigned-size(3)>> = col
{row, col}
end)
|> Enum.map(fn {row, col} ->
row * 8 + col
end)
|> Enum.sort()
|> Enum.reduce_while(0, fn seat, prev ->
if prev + 2 == seat do
{:halt, seat - 1}
else
{:cont, seat}
end
end)
:io.format("~p~n", [result])