one file per day
This commit is contained in:
		
							parent
							
								
									24ed30fbe6
								
							
						
					
					
						commit
						5886e0b12c
					
				
					 4 changed files with 49 additions and 43 deletions
				
			
		| 
						 | 
					@ -1,5 +1,4 @@
 | 
				
			||||||
import std/[math, sequtils, strutils]
 | 
					import std/[math, sequtils, strutils]
 | 
				
			||||||
import d01_1
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
func calcDigitLookup(): seq[(string, string)] =
 | 
					func calcDigitLookup(): seq[(string, string)] =
 | 
				
			||||||
  let digits = {
 | 
					  let digits = {
 | 
				
			||||||
| 
						 | 
					@ -21,9 +20,25 @@ func calcDigitLookup(): seq[(string, string)] =
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const digits = calcDigitLookup()
 | 
					const digits = calcDigitLookup()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
when isMainModule:
 | 
					proc calibrationValue(line: string): int =
 | 
				
			||||||
 | 
					  var digits: seq[char]
 | 
				
			||||||
 | 
					  for ch in line:
 | 
				
			||||||
 | 
					    if ch.isDigit():
 | 
				
			||||||
 | 
					      digits.add(ch)
 | 
				
			||||||
 | 
					  result = parseInt(digits[0] & digits[^1])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					proc p1(input: string) =
 | 
				
			||||||
 | 
					  let lines = input.strip().splitLines()
 | 
				
			||||||
 | 
					  echo lines.map(calibrationValue).sum()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					proc p2(input: string) =
 | 
				
			||||||
  let
 | 
					  let
 | 
				
			||||||
    input = stdin.readAll().strip()
 | 
					    input = input.strip()
 | 
				
			||||||
    realInput = input.multiReplace(digits)
 | 
					    realInput = input.multiReplace(digits)
 | 
				
			||||||
    lines = realInput.splitLines()
 | 
					    lines = realInput.splitLines()
 | 
				
			||||||
  echo lines.map(calibrationValue).sum()
 | 
					  echo lines.map(calibrationValue).sum()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					when isMainModule:
 | 
				
			||||||
 | 
					  let input = stdin.readAll()
 | 
				
			||||||
 | 
					  p1(input)
 | 
				
			||||||
 | 
					  p2(input)
 | 
				
			||||||
| 
						 | 
					@ -1,12 +0,0 @@
 | 
				
			||||||
import std/[math, sequtils, strutils]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc calibrationValue*(line: string): int =
 | 
					 | 
				
			||||||
  var digits: seq[char]
 | 
					 | 
				
			||||||
  for ch in line:
 | 
					 | 
				
			||||||
    if ch.isDigit():
 | 
					 | 
				
			||||||
      digits.add(ch)
 | 
					 | 
				
			||||||
  result = parseInt(digits[0] & digits[^1])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
when isMainModule:
 | 
					 | 
				
			||||||
  var input = stdin.readAll().strip().splitLines()
 | 
					 | 
				
			||||||
  echo input.map(calibrationValue).sum()
 | 
					 | 
				
			||||||
| 
						 | 
					@ -6,14 +6,14 @@ type
 | 
				
			||||||
    green: int
 | 
					    green: int
 | 
				
			||||||
    blue: int
 | 
					    blue: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  CubeSet* = object
 | 
					  CubeSet = object
 | 
				
			||||||
    red*: int
 | 
					    red: int
 | 
				
			||||||
    green*: int
 | 
					    green: int
 | 
				
			||||||
    blue*: int
 | 
					    blue: int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Game* = object
 | 
					  Game = object
 | 
				
			||||||
    id*: int
 | 
					    id: int
 | 
				
			||||||
    sets*: seq[CubeSet]
 | 
					    sets: seq[CubeSet]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const rules = Rules(red: 12, green: 13, blue: 14)
 | 
					const rules = Rules(red: 12, green: 13, blue: 14)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,7 +26,7 @@ func isPossible(rules: Rules, cubes: CubeSet): bool =
 | 
				
			||||||
    return false
 | 
					    return false
 | 
				
			||||||
  return true
 | 
					  return true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseGame*(game: string): Game =
 | 
					func parseGame(game: string): Game =
 | 
				
			||||||
  let
 | 
					  let
 | 
				
			||||||
    sp = game.split({':', ';'})
 | 
					    sp = game.split({':', ';'})
 | 
				
			||||||
    sets = sp[1..^1]
 | 
					    sets = sp[1..^1]
 | 
				
			||||||
| 
						 | 
					@ -50,9 +50,18 @@ func parseGame*(game: string): Game =
 | 
				
			||||||
        cubes.blue = amount
 | 
					        cubes.blue = amount
 | 
				
			||||||
    result.sets.add(cubes)
 | 
					    result.sets.add(cubes)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
when isMainModule:
 | 
					func fewestCubes(sets: seq[CubeSet]): CubeSet =
 | 
				
			||||||
 | 
					  for cubes in sets:
 | 
				
			||||||
 | 
					    if cubes.red > result.red:
 | 
				
			||||||
 | 
					      result.red = cubes.red
 | 
				
			||||||
 | 
					    if cubes.green > result.green:
 | 
				
			||||||
 | 
					      result.green = cubes.green
 | 
				
			||||||
 | 
					    if cubes.blue > result.blue:
 | 
				
			||||||
 | 
					      result.blue = cubes.blue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					proc p1(input: string) =
 | 
				
			||||||
  let
 | 
					  let
 | 
				
			||||||
    input = stdin.readAll().strip().splitLines()
 | 
					    input = input.strip().splitLines()
 | 
				
			||||||
    games = input.map(parseGame)
 | 
					    games = input.map(parseGame)
 | 
				
			||||||
    possibleIds =
 | 
					    possibleIds =
 | 
				
			||||||
      collect:
 | 
					      collect:
 | 
				
			||||||
| 
						 | 
					@ -63,3 +72,15 @@ when isMainModule:
 | 
				
			||||||
                break gameBlock
 | 
					                break gameBlock
 | 
				
			||||||
            game.id
 | 
					            game.id
 | 
				
			||||||
  echo possibleIds.sum()
 | 
					  echo possibleIds.sum()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					proc p2(input: string) =
 | 
				
			||||||
 | 
					  let
 | 
				
			||||||
 | 
					    input = input.strip().splitLines()
 | 
				
			||||||
 | 
					    games = input.map(parseGame).mapIt(it.sets)
 | 
				
			||||||
 | 
					    fewest = games.map(fewestCubes)
 | 
				
			||||||
 | 
					  echo fewest.mapIt(it.red * it.green * it.blue).sum()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					when isMainModule:
 | 
				
			||||||
 | 
					  let input = stdin.readAll()
 | 
				
			||||||
 | 
					  p1(input)
 | 
				
			||||||
 | 
					  p2(input)
 | 
				
			||||||
| 
						 | 
					@ -1,18 +0,0 @@
 | 
				
			||||||
import std/[math, sequtils, strutils, sugar]
 | 
					 | 
				
			||||||
import d02_1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func fewestCubes(sets: seq[CubeSet]): CubeSet =
 | 
					 | 
				
			||||||
  for cubes in sets:
 | 
					 | 
				
			||||||
    if cubes.red > result.red:
 | 
					 | 
				
			||||||
      result.red = cubes.red
 | 
					 | 
				
			||||||
    if cubes.green > result.green:
 | 
					 | 
				
			||||||
      result.green = cubes.green
 | 
					 | 
				
			||||||
    if cubes.blue > result.blue:
 | 
					 | 
				
			||||||
      result.blue = cubes.blue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
when isMainModule:
 | 
					 | 
				
			||||||
  let
 | 
					 | 
				
			||||||
    input = stdin.readAll().strip().splitLines()
 | 
					 | 
				
			||||||
    games = input.map(parseGame).mapIt(it.sets)
 | 
					 | 
				
			||||||
    fewest = games.map(fewestCubes)
 | 
					 | 
				
			||||||
  echo fewest.mapIt(it.red * it.green * it.blue).sum()
 | 
					 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue