1
0
Fork 0

Day 23, part 2 - decompiled input by hand

This commit is contained in:
Jonathan Chan 2017-12-23 11:44:16 -08:00
parent 746da09956
commit f4b9ddef4b
4 changed files with 44 additions and 7 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
* *
!.gitignore !.gitignore
!*.hs !*.hs
!*.c
!*.js !*.js
!*.txt !*.txt

37
23.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdbool.h>
// unoptimized translation of input
unsigned int naiveCount() {
unsigned short h = 0;
for (long b = 108100; b < 125100; b += 17) {
bool f = true;
for (long d = 2; d < b; d++) {
for (long e = 2; e < b; e++) {
if (d * e == b) {
f = false;
}
}
}
h += !f
}
return h;
}
unsigned int count() {
unsigned short h = 0;
for (long b = 108100; b < 125100; b += 17) {
bool f = true;
for (unsigned short d = 2; f && d * d <= b; d++) {
if (b % d == 0) {
f = false;
}
}
h += !f;
}
return h;
}
int main() {
printf("%hu\n", count());
}

12
23.hs
View File

@ -4,7 +4,6 @@ import Data.Char (ord)
import Data.Sequence (Seq, fromList, index) import Data.Sequence (Seq, fromList, index)
import Data.Vector.Unboxed (Vector, (!), (//)) import Data.Vector.Unboxed (Vector, (!), (//))
import qualified Data.Vector.Unboxed as V (replicate) import qualified Data.Vector.Unboxed as V (replicate)
import Debug.Trace
type Registers = Vector Int type Registers = Vector Int
type Instruction = State -> State type Instruction = State -> State
@ -29,10 +28,10 @@ getValue r v = case v of
c -> r ! (getIndex c) c -> r ! (getIndex c)
app :: Function -> Value -> Value -> Instruction app :: Function -> Value -> Value -> Instruction
app (Function op fn) index value state@(State reg pos cnt) = app (Function op fn) x y (State reg pos cnt) =
let i = getIndex index let i = getIndex x
in State { in State {
registers = reg // [(i, reg ! i `fn` getValue reg value)], registers = reg // [(i, reg ! i `fn` getValue reg y)],
position = pos + 1, position = pos + 1,
countMul = cnt + fromEnum (op == Mul) countMul = cnt + fromEnum (op == Mul)
} }
@ -56,12 +55,11 @@ parseLine str =
Nothing -> Register $ head s Nothing -> Register $ head s
runInstructions :: Seq Instruction -> State -> Int runInstructions :: Seq Instruction -> State -> Int
runInstructions instructions state@(State reg pos cnt) = runInstructions instructions state@(State _ pos cnt) =
if pos >= length instructions then cnt else if pos >= length instructions then cnt else
let !nextState = instructions `index` pos $ state in runInstructions instructions nextState let !nextState = instructions `index` pos $ state in runInstructions instructions nextState
main :: IO () main :: IO ()
main = do main = do
instructions <- fromList . map parseLine . lines <$> readFile "23.txt" instructions <- fromList . map parseLine . lines <$> readFile "23.txt"
print $ runInstructions instructions (State (V.replicate 8 0) 0 0) print $ runInstructions instructions (State (V.replicate 8 0) 0 0)
print $ runInstructions instructions (State (V.replicate 8 0 // [(0, 1)]) 0 0)

View File

@ -31,6 +31,7 @@ These are the runtimes of only one trial but the variances are fairly small and
| 20 | 0.168 | | 20 | 0.168 |
| 21 | 4.013 | | 21 | 4.013 |
| 22 | 12.867 | 7.880 | 22 | 12.867 | 7.880
| 23 | 0.274 |
Problems that should be optimized further: 15 Problems that should be optimized further: 15