day 1 + day 2
This commit is contained in:
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Ignore all
|
||||||
|
*
|
||||||
|
|
||||||
|
# Unignore all with extensions
|
||||||
|
!*.*
|
||||||
|
|
||||||
|
# Unignore all dirs
|
||||||
|
!*/
|
||||||
|
|
||||||
|
# Ignore exe
|
||||||
|
*.exe
|
||||||
31
day_01/day_01.nim
Normal file
31
day_01/day_01.nim
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import std/[strutils, sequtils, sugar, algorithm]
|
||||||
|
|
||||||
|
iterator parseElves(data: seq[string]): seq[string] =
|
||||||
|
var items = newSeq[string]()
|
||||||
|
for line in data:
|
||||||
|
if line.isEmptyOrWhitespace:
|
||||||
|
yield items
|
||||||
|
items.setLen 0
|
||||||
|
else:
|
||||||
|
items.add(line)
|
||||||
|
|
||||||
|
const lines = readFile("day_01/day_01_input.txt").splitLines
|
||||||
|
|
||||||
|
proc solveMostCalories() =
|
||||||
|
var mostCalories = 0
|
||||||
|
for item in parseElves(lines):
|
||||||
|
mostCalories = max(mostCalories, item.map(it => it.parseInt).foldl(a + b))
|
||||||
|
echo "Most calories: " & $mostCalories
|
||||||
|
|
||||||
|
proc solveTop3() =
|
||||||
|
var allElves = newSeq[int]()
|
||||||
|
for inventory in parseElves(lines):
|
||||||
|
allElves.add(inventory.map(it => it.parseInt).foldl(a + b))
|
||||||
|
allElves.sort(SortOrder.Descending)
|
||||||
|
allElves.setLen(3)
|
||||||
|
let top3Calories = allElves.foldl(a + b)
|
||||||
|
echo "Top 3 calories: " & $top3Calories
|
||||||
|
|
||||||
|
if isMainModule:
|
||||||
|
solveMostCalories()
|
||||||
|
solveTop3()
|
||||||
2269
day_01/day_01_input.txt
Normal file
2269
day_01/day_01_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
88
day_02/day_02.nim
Normal file
88
day_02/day_02.nim
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import std/[strutils, tables]
|
||||||
|
|
||||||
|
type Shape {.pure.} = enum
|
||||||
|
Rock = 1,
|
||||||
|
Paper = 2,
|
||||||
|
Scissor = 3
|
||||||
|
|
||||||
|
let shapeTable = {
|
||||||
|
"A": Rock,
|
||||||
|
"B": Paper,
|
||||||
|
"C": Scissor,
|
||||||
|
"X": Rock,
|
||||||
|
"Y": Paper,
|
||||||
|
"Z": Scissor
|
||||||
|
}.toTable
|
||||||
|
|
||||||
|
let scoringTable = {
|
||||||
|
Rock: {
|
||||||
|
Rock: 3,
|
||||||
|
Paper: 0,
|
||||||
|
Scissor: 6
|
||||||
|
}.toTable,
|
||||||
|
Paper: {
|
||||||
|
Rock: 6,
|
||||||
|
Paper: 3,
|
||||||
|
Scissor: 0
|
||||||
|
}.toTable,
|
||||||
|
Scissor: {
|
||||||
|
Rock: 0,
|
||||||
|
Paper: 6,
|
||||||
|
Scissor: 3
|
||||||
|
}.toTable
|
||||||
|
}.toTable
|
||||||
|
|
||||||
|
type Response {.pure.} = enum
|
||||||
|
Lose = 0,
|
||||||
|
Draw = 3,
|
||||||
|
Win = 6
|
||||||
|
|
||||||
|
let resultTable = {
|
||||||
|
"X": Lose,
|
||||||
|
"Y": Draw,
|
||||||
|
"Z": Win
|
||||||
|
}.toTable
|
||||||
|
|
||||||
|
let responseShape = {
|
||||||
|
Lose: {
|
||||||
|
Rock: Scissor,
|
||||||
|
Paper: Rock,
|
||||||
|
Scissor: Paper
|
||||||
|
}.toTable,
|
||||||
|
Draw: {
|
||||||
|
Rock: Rock,
|
||||||
|
Paper: Paper,
|
||||||
|
Scissor: Scissor
|
||||||
|
}.toTable,
|
||||||
|
Win: {
|
||||||
|
Rock: Paper,
|
||||||
|
Paper: Scissor,
|
||||||
|
Scissor: Rock
|
||||||
|
}.toTable
|
||||||
|
}.toTable
|
||||||
|
|
||||||
|
const strategyGuide: seq[string] = readFile("day_02/input.txt").splitLines
|
||||||
|
|
||||||
|
proc solveTotalScore() =
|
||||||
|
var score = 0
|
||||||
|
for step in strategyGuide:
|
||||||
|
let opponent = shapeTable[step.substr(0, 0)]
|
||||||
|
let response = shapeTable[step.substr(2, 2)]
|
||||||
|
score += response.int
|
||||||
|
score += scoringTable[response][opponent]
|
||||||
|
echo "Total socre: " & $score
|
||||||
|
|
||||||
|
proc solveDecryptedScore() =
|
||||||
|
var score = 0
|
||||||
|
for step in strategyGuide:
|
||||||
|
let opponent = shapeTable[step.substr(0, 0)]
|
||||||
|
let desiredResult = resultTable[step.substr(2, 2)]
|
||||||
|
let response = responseShape[desiredResult][opponent]
|
||||||
|
score += response.int
|
||||||
|
score += desiredResult.int
|
||||||
|
echo "Descrypted score: " & $score
|
||||||
|
|
||||||
|
|
||||||
|
if isMainModule:
|
||||||
|
solveTotalScore()
|
||||||
|
solveDecryptedScore()
|
||||||
2500
day_02/input.txt
Normal file
2500
day_02/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user