Browse Source

Day 1 in nim

master
Chris Smith 5 years ago
parent
commit
665f0309b3
5 changed files with 34 additions and 3 deletions
  1. 3
    0
      .gitignore
  2. 20
    0
      day01.nim
  3. 3
    1
      docker/Dockerfile
  4. 7
    1
      docker/entrypoint.sh
  5. 1
    1
      run.sh

+ 3
- 0
.gitignore View File

@@ -1,2 +1,5 @@
1 1
 /.idea
2
+/.vscode
2 3
 /venv
4
+/*.exe
5
+/day??

+ 20
- 0
day01.nim View File

@@ -0,0 +1,20 @@
1
+import intsets, math, sequtils, strutils
2
+
3
+let input = readFile("data/01.txt").splitLines.map(parseInt)
4
+
5
+proc part1(freqs: seq[int]): int =
6
+    freqs.sum
7
+
8
+proc part2(freqs: seq[int]): int =
9
+    var seen = initIntSet()
10
+    var talley: int
11
+
12
+    while true:
13
+        for n in freqs:
14
+            talley += n
15
+            if talley in seen:
16
+                return talley
17
+            seen.incl(talley)
18
+
19
+echo part1(input)
20
+echo part2(input)

+ 3
- 1
docker/Dockerfile View File

@@ -1,4 +1,6 @@
1
-FROM pypy:3-slim
1
+FROM nimlang/nim
2
+
3
+RUN apt-get update && apt-get install -y pypy
2 4
 
3 5
 ADD entrypoint.sh /entrypoint.sh
4 6
 RUN chmod +x /entrypoint.sh

+ 7
- 1
docker/entrypoint.sh View File

@@ -1,4 +1,10 @@
1 1
 #!/bin/bash
2 2
 
3 3
 cd /code
4
-time pypy3 ${1:-$(find -regex './[0-9]+.py' | sort | tail -n 1)}
4
+
5
+if [ -f "day$1.nim" ]; then
6
+    HOME=/tmp nim c --opt:speed -d:release day$1.nim >/dev/null 2>&1
7
+    time ./day$1
8
+else
9
+    time pypy "$1.py"
10
+fi

+ 1
- 1
run.sh View File

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 
3
-IMAGE=csmith/aoc-2018-02
3
+IMAGE=csmith/aoc-2018-03
4 4
 
5 5
 docker image inspect $IMAGE >/dev/null 2>&1
6 6
 if [ $? -ne 0 ]