aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authorj-james2022-12-01 11:37:15 +0000
committerj-james2022-12-01 11:37:15 +0000
commit13b3ae4211631c74b9249a6033869f75e036fad2 (patch)
tree2a0e423912b640cc02c7b08bf47db8ba5bfaf07f /2020
parent926b0949d12588641f29836a6e41712b3237b1b9 (diff)
Clean up 2020 code
like seriously nim has ufcs what was past me thinking
Diffstat (limited to '2020')
-rw-r--r--2020/eight.nim4
-rw-r--r--2020/eleven.nim16
-rw-r--r--2020/fifteen.nim4
-rw-r--r--2020/five.nim6
-rw-r--r--2020/four.nim8
-rw-r--r--2020/fourteen.nim2
-rw-r--r--2020/nine.nim6
-rw-r--r--2020/nineteen.nim2
-rw-r--r--2020/seven.nim8
-rw-r--r--2020/seventeen.nim2
-rw-r--r--2020/six.nim14
-rw-r--r--2020/sixteen.nim4
-rw-r--r--2020/ten.nim4
-rw-r--r--2020/thirteen.nim10
-rw-r--r--2020/twelve.nim4
15 files changed, 46 insertions, 48 deletions
diff --git a/2020/eight.nim b/2020/eight.nim
index b923f76..d4018bf 100644
--- a/2020/eight.nim
+++ b/2020/eight.nim
@@ -3,8 +3,8 @@ import os, strutils, sequtils, sugar
let input: string = paramStr(1)
var program: seq[tuple[op: string, arg: int]] =
- map(split(strip(readFile(input)), '\n'),
- instruction => (instruction[0..2], parseInt(instruction[4..^1])))
+ input.readFile().strip().split('\n')
+ .map(instruction => (instruction[0..2], parseInt(instruction[4..^1])))
func execute(program: seq[tuple[op: string, arg: int]]): (bool, int) =
var executed = newSeq[bool](len(program))
diff --git a/2020/eleven.nim b/2020/eleven.nim
index 7324b2d..914bbd0 100644
--- a/2020/eleven.nim
+++ b/2020/eleven.nim
@@ -2,16 +2,16 @@
import os, strutils, sequtils, sugar
let input: string = paramStr(1)
-var terminal: seq[seq[char]] = map(split(strip(readFile(input)), '\n'), row => @row)
-let height: int = len(terminal)
-let width: int = len(terminal[0])
+var terminal: seq[seq[char]] = input.readFile().strip().split('\n').map(row => @row)
+let height: int = terminal.len
+let width: int = terminal[0].len
while true:
- var prev, future: seq[char] = @(repeat('.', width)) # dummy floor
+ var prev, future: seq[char] = @('.'.repeat(width)) # dummy floor
var buffer: seq[seq[char]] = terminal
for i, row in buffer:
if height - i == 1:
- future = @(repeat('.', width))
+ future = @('.'.repeat(width))
else:
future = buffer[i+1]
for j, seat in row:
@@ -32,13 +32,13 @@ while true:
terminal = buffer
echo count($terminal, '#')
-terminal = map(split(strip(readFile(input)), '\n'), row => @row)
+terminal = input.readFile().strip().split('\n').map(row => @row)
while true:
- var prev, future: seq[char] = @(repeat('.', width)) # dummy floor
+ var prev, future: seq[char] = @('.'.repeat(width)) # dummy floor
var buffer: seq[seq[char]] = terminal
for i, row in buffer:
if height - i == 1:
- future = @(repeat('.', width))
+ future = @('.'.repeat(width))
else:
future = buffer[i+1]
for j, seat in row:
diff --git a/2020/fifteen.nim b/2020/fifteen.nim
index 0086054..ba6e465 100644
--- a/2020/fifteen.nim
+++ b/2020/fifteen.nim
@@ -1,10 +1,10 @@
# Day Fifteen: Rambunctious Recitation
-import os, strutils, sequtils
+import std/[os, strutils, sequtils]
let input: string = paramStr(1)
proc recite(nth: int): int =
- var numbers: seq[int] = map(split(strip(readFile(input)), ","), parseInt)
+ var numbers: seq[int] = input.readFile().strip().split(",").map(parseInt)
var history: seq[int] = newSeqWith[int](nth, -1)
for i in 0 .. nth - 2:
let previous: int = numbers[i]
diff --git a/2020/five.nim b/2020/five.nim
index ebad294..4706a57 100644
--- a/2020/five.nim
+++ b/2020/five.nim
@@ -1,5 +1,5 @@
# Day Five: Binary Boarding
-import os, math, std/enumerate
+import os, math
let input: string = paramStr(1)
@@ -9,10 +9,10 @@ var
flight: seq[int]
for seat in lines(input):
var row, column, id: int = 0
- for i, char in enumerate(seat[0..^4]):
+ for i, char in seat[0..^4]:
if char == 'B':
row += 2^(7-i) div 2
- for i, char in enumerate(seat[7..^1]):
+ for i, char in seat[7..^1]:
if char == 'R':
column += 2^(3-i) div 2
id = row * 8 + column
diff --git a/2020/four.nim b/2020/four.nim
index 89bc335..c469297 100644
--- a/2020/four.nim
+++ b/2020/four.nim
@@ -4,12 +4,12 @@ import os, strutils
let input: string = paramStr(1)
var fieldedPassports, validPassports: int = 0
-for passport in split(readFile(input), "\n\n"):
+for passport in input.readFile().split("\n\n"):
var fields, valid: int = 0
- for pairs in split(strip(replace(passport, "\n", " ")), " "):
+ for pairs in passport.replace("\n", " ").strip().split(" "):
let
- key: string = split(pairs, ":")[0]
- value: string = split(pairs, ":")[1]
+ key: string = pairs.split(":")[0]
+ value: string = pairs.split(":")[1]
case key
of "byr":
if parseInt(value) in 1920 .. 2002:
diff --git a/2020/fourteen.nim b/2020/fourteen.nim
index 3d6741d..92a881e 100644
--- a/2020/fourteen.nim
+++ b/2020/fourteen.nim
@@ -4,7 +4,7 @@ import std/[os, strutils, sequtils, sugar, tables]
let input: string = paramStr(1)
let program: seq[tuple[address, value: string]] =
input.readFile().strip().split('\n')
- .map(param => (param.split(" = ")[0], param.split(" = ")[1]))
+ .map(param => (param.split(" = ")[0], param.split(" = ")[1]))
var mask: string = 'X'.repeat(36)
var memory: seq[int] = newSeq[int](99999)
diff --git a/2020/nine.nim b/2020/nine.nim
index 8b15391..9a30c91 100644
--- a/2020/nine.nim
+++ b/2020/nine.nim
@@ -2,11 +2,11 @@
import os, strutils, sequtils
let input: string = paramStr(1)
-let xmas: seq[int] = map(split(strip(readFile(input)), '\n'), parseInt)
+let xmas: seq[int] = input.readFile().strip().split('\n').map(parseInt)
const preamble: int = 25
var encryption, weakness, smallest: int
-for i in preamble ..< len(xmas):
+for i in preamble ..< xmas.len:
var valid: bool
for j in 0 .. preamble:
for k in 0 .. preamble:
@@ -14,7 +14,7 @@ for i in preamble ..< len(xmas):
valid = true
if not valid:
encryption = xmas[i]
-for largest in 0 ..< len(xmas):
+for largest in 0 ..< xmas.len:
var sum: int = 0
for val in smallest .. largest:
inc(sum, xmas[val])
diff --git a/2020/nineteen.nim b/2020/nineteen.nim
index 1f815b4..c013e5e 100644
--- a/2020/nineteen.nim
+++ b/2020/nineteen.nim
@@ -2,7 +2,7 @@
import std/[os, strutils, sequtils, sugar, algorithm]
let input = paramStr(1).readFile().strip().split("\n\n")
-let rules: seq[string] = input[0].strip().split("\n")
+var rules: seq[string] = input[0].strip().split("\n")
.sortedByIt(it.split(": ")[0].parseInt()).mapIt(it.split(": ")[1])
let messages: seq[string] = input[1].strip().split("\n")
diff --git a/2020/seven.nim b/2020/seven.nim
index 7e20dcc..91f333f 100644
--- a/2020/seven.nim
+++ b/2020/seven.nim
@@ -3,9 +3,9 @@ import os, strutils, sequtils, sugar
let input: string = paramStr(1)
let rules: seq[tuple[outer: string, inner: seq[string]]] =
- map(split(strip(readFile(input)), '\n'),
- rule => (replace(split(rule, " contain ")[0], " bags"),
- split(replace(replace(replace(split(rule, " contain ")[1], " bags"), " bag"), "."), ", ")))
+ input.readFile().strip().split('\n')
+ .map(rule => (rule.split(" contain ")[0].replace(" bags"),
+ rule.split(" contain ")[1].replace(" bags").replace(" bag").replace(".").split(", ")))
proc outside(color: string): seq[string] =
for rule in rules:
@@ -22,5 +22,5 @@ proc inside(color: string): int =
else:
result += parseInt($bag[0]) * (inside(bag[2..^1]) + 1)
-echo len(deduplicate(outside("shiny gold")))
+echo outside("shiny gold").deduplicate().len
echo inside("shiny gold")
diff --git a/2020/seventeen.nim b/2020/seventeen.nim
index 8e4ab49..a37f445 100644
--- a/2020/seventeen.nim
+++ b/2020/seventeen.nim
@@ -1,5 +1,5 @@
# Day Seventeen: Conway Cubes
-import std/os, std/sets, std/enumerate
+import std/[os, sets, enumerate]
let input: string = paramStr(1)
type Coord = tuple[x,y,z,w: int]
diff --git a/2020/six.nim b/2020/six.nim
index cb9f887..b74b1f1 100644
--- a/2020/six.nim
+++ b/2020/six.nim
@@ -4,16 +4,16 @@ import os, strutils
let input: string = paramStr(1)
var sum, sumAll: int = 0
-for group in split(readFile(input), "\n\n"):
+for group in input.readFile().split("\n\n"):
var count, countAll: int = 0
- for i, answer in replace(group, "\n"):
- if find(replace(group, "\n"), answer) == i:
+ for i, answer in group.replace("\n"):
+ if group.replace("\n").find(answer) == i:
inc(count)
- for i, answer in split(group, "\n")[0]:
- if find(split(group, "\n")[0], answer) == i:
+ for i, answer in group.split("\n")[0]:
+ if group.split("\n")[0].find(answer) == i:
block everyone:
- for person in split(group, "\n"):
- if not contains(person, answer):
+ for person in group.split("\n"):
+ if not person.contains(answer):
break everyone
inc(countAll)
sum += count
diff --git a/2020/sixteen.nim b/2020/sixteen.nim
index 4896c38..b43d094 100644
--- a/2020/sixteen.nim
+++ b/2020/sixteen.nim
@@ -5,7 +5,7 @@ let input: string = paramStr(1)
type Rule = tuple[field: string, values: seq[int]]
-let data: seq[string] = readFile(input).split("\n\n")
+let data: seq[string] = input.readFile().split("\n\n")
let rules: seq[Rule] =
data[0].split("\n")
.map(foo => (foo.split(": ")[0], foo.split(": ")[1].split(" or ")
@@ -38,7 +38,7 @@ proc purge(tickets: seq[seq[int]], min: int, max: int): seq[seq[int]] =
result.add(ticket)
proc flip(tickets: seq[seq[int]]): seq[seq[int]] =
- for i in 0 ..< tickets[0].len():
+ for i in 0 ..< tickets[0].len:
var foo: seq[int]
for ticket in tickets:
foo.add(ticket[i])
diff --git a/2020/ten.nim b/2020/ten.nim
index 3d96d0f..340d9f9 100644
--- a/2020/ten.nim
+++ b/2020/ten.nim
@@ -2,9 +2,9 @@
import os, strutils, sequtils, algorithm, math
let input: string = paramStr(1)
-var adapters: seq[int] = map(split(strip(readFile(input)), '\n'), parseInt)
+var adapters: seq[int] = input.readFile().strip().split('\n').map(parseInt)
adapters.add(0)
-adapters = sorted(adapters)
+adapters = adapters.sorted()
adapters.add(adapters[^1] + 3)
var jolt, one, three: int = 0
diff --git a/2020/thirteen.nim b/2020/thirteen.nim
index d01e681..6c8bb54 100644
--- a/2020/thirteen.nim
+++ b/2020/thirteen.nim
@@ -2,13 +2,11 @@
import os, strutils, sequtils
let input: string = paramStr(1)
-let arrival: int = parseInt(splitLines(readFile(input))[0])
-let buses: seq[int] = map(split(splitLines(readFile(input))[1], ','),
+let arrival: int = input.readFile().splitLines()[0].parseInt()
+let buses: seq[int] = map(input.readFile().splitLines()[1].split(','),
func (bus: string): int =
- if bus == "x":
- 0
- else:
- parseInt(bus))
+ if bus == "x": 0
+ else: parseInt(bus))
var earliest: tuple[id, wait: int] = (999, 999)
for bus in buses:
diff --git a/2020/twelve.nim b/2020/twelve.nim
index fc93243..db98f3a 100644
--- a/2020/twelve.nim
+++ b/2020/twelve.nim
@@ -3,8 +3,8 @@ import os, strutils, sequtils, math, sugar
let input: string = paramStr(1)
let instructions: seq[tuple[action: char, value: int]] =
- map(split(strip(readFile(input)), '\n'),
- instruction => (instruction[0], parseInt(instruction[1..^1])))
+ input.readFile().strip().split('\n')
+ .map(instruction => (instruction[0], parseInt(instruction[1..^1])))
var ferry, actual: tuple[latitude, longitude, bearing: int] = (0, 0, 0)
var waypoint: tuple[latitude, longitude: int] = (1, 10)