Skip to content

Commit 9de068f

Browse files
committed
day14
1 parent 0984375 commit 9de068f

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

day14/Program.fs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,105 @@
1-
module Program = let [<EntryPoint>] main _ = 0
1+
open Xunit
2+
open FsUnit.Xunit
3+
4+
type Robot = { P: int * int; V: int * int }
5+
6+
type Quadrant =
7+
| TopRight
8+
| TopLeft
9+
| BottomLeft
10+
| BottomRight
11+
12+
// x 右
13+
// y 下
14+
15+
let rec move times w h (robot: Robot) =
16+
if times = 0 then
17+
robot
18+
else
19+
let px, py = robot.P
20+
let vx, vy = robot.V
21+
let newP = (w + px + vx) % w, (h + py + vy) % h
22+
move (times - 1) w h { robot with P = newP }
23+
24+
let part1 ((robots, w, h): (Robot seq * int * int)) =
25+
let quadratics =
26+
robots
27+
|> Seq.map (move 100 w h)
28+
|> Seq.choose (fun { P = (px, py) } ->
29+
if px > w / 2 && py < h / 2 then Some TopRight
30+
else if px < w / 2 && py < h / 2 then Some TopLeft
31+
else if px < w / 2 && py > h / 2 then Some BottomLeft
32+
else if px > w / 2 && py > h / 2 then Some BottomRight
33+
else None)
34+
35+
(1, Seq.countBy id quadratics) ||> Seq.fold (fun acc (_, count) -> acc * count)
36+
37+
let part2 ((robots, w, h): (Robot seq * int * int)) =
38+
let rec search elapsed robots =
39+
if elapsed >= 1000 then
40+
()
41+
else
42+
let positions = robots |> Seq.map (fun robot -> robot.P) |> Set.ofSeq
43+
44+
let map =
45+
Array.init h (fun i -> Array.init w (fun j -> if Set.contains (i, j) positions then '@' else ' '))
46+
47+
printfn "t = %d" elapsed
48+
map |> Array.map System.String |> String.concat ("\n") |> printfn "%s"
49+
printfn "\n\n\n\n\n"
50+
51+
search (elapsed + 1) (robots |> Seq.map (move 1 w h))
52+
53+
search 0 robots
54+
55+
// この周期でアスキーアートっぽいものが見える
56+
// t = 81, 81 + w, 81 + 2w, 81 + 3w, ...
57+
// t = 30, 30 + h, 30 + 2h, 30 + 3h, ...
58+
59+
// 適当な範囲で検索する
60+
[ 0..1000 ]
61+
|> List.choose (fun p ->
62+
// 81 + p * w = 30 + q * h
63+
let numQ = (81 - 30) + p * w
64+
if numQ % h = 0 then Some(81 + p * w) else None)
65+
|> List.head
66+
67+
let parse (input: string) =
68+
input.Split("\n")
69+
|> Array.map (fun line -> line.Split(" "))
70+
|> Array.map (fun line ->
71+
let p, v = line[0], line[1]
72+
let p = p.Replace("p=", "").Split(",")
73+
let v = v.Replace("v=", "").Split(",")
74+
75+
{ P = (int p[0], int p[1])
76+
V = (int v[0], int v[1]) })
77+
78+
module Example =
79+
let input =
80+
"p=0,4 v=3,-3
81+
p=6,3 v=-1,-3
82+
p=10,3 v=-1,2
83+
p=2,0 v=2,-1
84+
p=0,0 v=1,3
85+
p=3,0 v=-2,-2
86+
p=7,6 v=-1,-3
87+
p=3,0 v=-1,-2
88+
p=9,3 v=2,3
89+
p=7,3 v=-1,2
90+
p=2,4 v=2,-3
91+
p=9,5 v=-3,-3"
92+
93+
[<Fact>]
94+
let testPart1 () =
95+
(parse input, 7, 11) |> part1 |> should equal 12
96+
97+
[<EntryPoint>]
98+
let main _ =
99+
let input = stdin.ReadToEnd().TrimEnd()
100+
let robots = parse input
101+
102+
(robots, 101, 103) |> part1 |> printfn "Part 1: %d"
103+
(robots, 101, 103) |> part2 |> printfn "Part 2: %d"
104+
105+
0

day14/Tests.fs

Lines changed: 0 additions & 8 deletions
This file was deleted.

day14/day14.fsproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<Compile Include="Tests.fs" />
1312
<Compile Include="Program.fs" />
1413
</ItemGroup>
1514

0 commit comments

Comments
 (0)