aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2023/nim/day01.nim38
-rw-r--r--README.md12
-rwxr-xr-xutil/get17
-rw-r--r--util/main.ml4
-rwxr-xr-xutil/run2
-rwxr-xr-xutil/watch2
6 files changed, 60 insertions, 15 deletions
diff --git a/2023/nim/day01.nim b/2023/nim/day01.nim
new file mode 100644
index 0000000..2dd1f70
--- /dev/null
+++ b/2023/nim/day01.nim
@@ -0,0 +1,38 @@
+# Day One: Trebuchet?!
+import std/[os, strutils, sequtils, sugar]
+
+let input = paramStr(1).readFile().strip().split("\n")
+
+var values: seq[int]
+for line in input:
+ var buf = ""
+ for c in line:
+ if c in '0'..'9':
+ buf.add(c)
+ buf = if buf.len == 1: buf & buf else: buf[0] & buf[^1]
+ values.add(buf.parseInt())
+echo values.foldl(a + b, 0)
+
+var parsed = input
+for _ in 0 ..< 100: # :-(
+ parsed = parsed.map(x => x.multiReplace([
+ ("one", "o1e"), # one1one does NOT work
+ ("two", "t2o"),
+ ("three", "t3e"),
+ ("four", "f4fr"),
+ ("five", "f5e"),
+ ("six", "s6x"),
+ ("seven", "s7n"),
+ ("eight", "e8t"),
+ ("nine", "n9e"),
+ ]))
+
+values = @[]
+for line in parsed:
+ var buf = ""
+ for c in line:
+ if c in '0'..'9':
+ buf.add(c)
+ buf = if buf.len == 1: buf & buf else: buf[0] & buf[^1]
+ values.add(buf.parseInt())
+echo values.foldl(a + b, 0)
diff --git a/README.md b/README.md
index f33bcd3..108aa21 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,15 @@
Yearly Advent of Code work.
-## 2022 ⭐ 22/22
+## 2023 ⭐ 2/2
+
+```bash
+# Nim
+cd 2023
+nim c -r nim/day01.nim input/day01.txt
+```
+
+## 2022 ⭐ 33/50
```bash
# Nim
@@ -26,7 +34,7 @@ cd 2021
python3 01.py input/01.txt
```
-## 2020 ⭐ 38/50
+## 2020 ⭐ 41/50
```bash
# Nim
diff --git a/util/get b/util/get
index 423dfca..8d9d43f 100755
--- a/util/get
+++ b/util/get
@@ -3,15 +3,10 @@
source "vars"
INPUT_URL="https://adventofcode.com/${YEAR}/day/$((10#${DAY}))/input"
-mkdir "nim/day${DAY}"
-mkdir "nim/day${DAY}/src"
-cp "main.nim" "nim/day${DAY}/src/main.nim"
-curl $INPUT_URL --cookie "session=${TOKEN}" > "nim/day${DAY}/input.txt"
+cp "main.nim" "nim/day${DAY}.nim"
+cp "main.ml" "ocaml/day${DAY}.ml"
+curl $INPUT_URL --cookie "session=${TOKEN}" > "input/day${DAY}.txt"
-cargo new "rust/day${DAY}"
-cp "main.rs" "rust/day${DAY}/src/main.rs"
-cp "nim/day${DAY}/input.txt" "rust/day${DAY}/input.txt"
-
-subl -a "nim/day${DAY}/input.txt"
-subl -a "nim/day${DAY}/src/main.nim"
-# chromium "https://adventofcode.com/${YEAR}/day/$((10#${DAY}))"
+subl -a "input/day${DAY}.txt"
+subl -a "nim/day${DAY}.nim"
+xdg-open "https://adventofcode.com/${YEAR}/day/$((10#${DAY}))"
diff --git a/util/main.ml b/util/main.ml
new file mode 100644
index 0000000..d50e352
--- /dev/null
+++ b/util/main.ml
@@ -0,0 +1,4 @@
+let input =
+ CCIO.(with_in filename read_lines_l)
+ |> List.map parse_line
+ |> Array.of_list
diff --git a/util/run b/util/run
index 3d068e1..ae7bb51 100755
--- a/util/run
+++ b/util/run
@@ -2,4 +2,4 @@
source "vars"
-nim c --warnings:off --hints:off -r "nim/day${DAY}/src/main.nim" "nim/day${DAY}/input.txt"
+nim c --warnings:off --hints:off -r "nim/day${DAY}.nim" "input/day${DAY}.txt"
diff --git a/util/watch b/util/watch
index 9c79c6e..143b9b7 100755
--- a/util/watch
+++ b/util/watch
@@ -2,6 +2,6 @@
source "vars"
-while inotifywait --quiet --event modify "nim/day${DAY}/src/main.nim"
+while inotifywait --quiet --event modify "nim/day${DAY}.nim"
do ./run
done